Revision f6d9f4c3 lib/ssh.py
b/lib/ssh.py | ||
---|---|---|
33 | 33 |
from ganeti import ssconf |
34 | 34 |
|
35 | 35 |
|
36 |
KNOWN_HOSTS_OPTS = [ |
|
37 |
"-oGlobalKnownHostsFile=%s" % constants.SSH_KNOWN_HOSTS_FILE, |
|
38 |
"-oUserKnownHostsFile=/dev/null", |
|
39 |
] |
|
40 |
|
|
41 |
# Note: BATCH_MODE conflicts with ASK_KEY |
|
42 |
BATCH_MODE_OPTS = [ |
|
43 |
"-oBatchMode=yes", |
|
44 |
"-oEscapeChar=none", |
|
45 |
"-oStrictHostKeyChecking=yes", |
|
46 |
] |
|
47 |
|
|
48 |
ASK_KEY_OPTS = [ |
|
49 |
"-oEscapeChar=none", |
|
50 |
"-oHashKnownHosts=no", |
|
51 |
"-oStrictHostKeyChecking=ask", |
|
52 |
] |
|
53 |
|
|
54 |
|
|
55 | 36 |
def GetUserFiles(user, mkdir=False): |
56 | 37 |
"""Return the paths of a user's ssh files. |
57 | 38 |
|
... | ... | |
97 | 78 |
else: |
98 | 79 |
self.sstore = sstore |
99 | 80 |
|
100 |
def _GetHostKeyAliasOption(self): |
|
101 |
return "-oHostKeyAlias=%s" % self.sstore.GetClusterName() |
|
81 |
def _BuildSshOptions(self, batch, ask_key, use_cluster_key): |
|
82 |
options = [ |
|
83 |
"-oEscapeChar=none", |
|
84 |
"-oHashKnownHosts=no", |
|
85 |
"-oGlobalKnownHostsFile=%s" % constants.SSH_KNOWN_HOSTS_FILE, |
|
86 |
"-oUserKnownHostsFile=/dev/null", |
|
87 |
] |
|
88 |
|
|
89 |
if use_cluster_key: |
|
90 |
options.append("-oHostKeyAlias=%s" % self.sstore.GetClusterName()) |
|
91 |
|
|
92 |
# Note: ask_key conflicts with batch mode |
|
93 |
if batch: |
|
94 |
if ask_key: |
|
95 |
raise errors.ProgrammerError("SSH call requested conflicting options") |
|
96 |
|
|
97 |
options.extend([ |
|
98 |
"-oBatchMode=yes", |
|
99 |
"-oStrictHostKeyChecking=yes", |
|
100 |
]) |
|
101 |
|
|
102 |
elif ask_key: |
|
103 |
options.extend([ |
|
104 |
"-oStrictHostKeyChecking=ask", |
|
105 |
]) |
|
106 |
|
|
107 |
return options |
|
102 | 108 |
|
103 | 109 |
def BuildCmd(self, hostname, user, command, batch=True, ask_key=False, |
104 | 110 |
tty=False, use_cluster_key=True): |
... | ... | |
118 | 124 |
|
119 | 125 |
""" |
120 | 126 |
argv = [constants.SSH, "-q"] |
121 |
argv.extend(KNOWN_HOSTS_OPTS) |
|
122 |
if use_cluster_key: |
|
123 |
argv.append(self._GetHostKeyAliasOption()) |
|
124 |
if batch: |
|
125 |
# if we are in batch mode, we can't ask the key |
|
126 |
if ask_key: |
|
127 |
raise errors.ProgrammerError("SSH call requested conflicting options") |
|
128 |
argv.extend(BATCH_MODE_OPTS) |
|
129 |
elif ask_key: |
|
130 |
argv.extend(ASK_KEY_OPTS) |
|
127 |
argv.extend(self._BuildSshOptions(batch, ask_key, use_cluster_key)) |
|
131 | 128 |
if tty: |
132 | 129 |
argv.append("-t") |
133 | 130 |
argv.extend(["%s@%s" % (user, hostname), command]) |
... | ... | |
168 | 165 |
return False |
169 | 166 |
|
170 | 167 |
command = [constants.SCP, "-q", "-p"] |
171 |
command.extend(KNOWN_HOSTS_OPTS) |
|
172 |
command.extend(BATCH_MODE_OPTS) |
|
173 |
command.append(self._GetHostKeyAliasOption()) |
|
168 |
command.extend(self._BuildSshOptions(True, False, True)) |
|
174 | 169 |
command.append(filename) |
175 | 170 |
command.append("%s:%s" % (node, filename)) |
176 | 171 |
|
Also available in: Unified diff