blob: aac6c263a0e87624cb0a0e7b9b47aab206d42d0c [file] [log] [blame]
David Kranz6308ec22012-02-22 09:36:48 -05001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2011 Quanta Research Cambridge, Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import subprocess
18import shlex
19
20SSH_OPTIONS = (" -q" +
21 " -o UserKnownHostsFile=/dev/null" +
22 " -o StrictHostKeyChecking=no -i ")
23
24
25def get_ssh_options(keypath):
26 return SSH_OPTIONS + keypath
27
28
29def scp(keypath, args):
30 options = get_ssh_options(keypath)
31 return subprocess.check_call(shlex.split("scp" + options + args))
32
33
34def ssh(keypath, user, node, command, check=True):
35 command = "ssh %s %s@%s %s" % (get_ssh_options(keypath), user,
36 node, command)
37 popenargs = shlex.split(command)
38 process = subprocess.Popen(popenargs, stdout=subprocess.PIPE)
39 output, unused_err = process.communicate()
40 retcode = process.poll()
41 if retcode and check:
42 raise Exception("%s: ssh failed with retcode: %s" % (node, retcode))
43 return output
44
45
46def execute_on_all(keypath, user, nodes, command):
47 for node in nodes:
48 ssh(keypath, user, node, command)
49
50
51def enum(*sequential, **named):
52 """Create auto-incremented enumerated types"""
53 enums = dict(zip(sequential, range(len(sequential))), **named)
54 return type('Enum', (), enums)