blob: ec63b99db38e3fe4780e19dd78f5801e933ce4d8 [file] [log] [blame]
Sean Dague70112362012-04-03 13:48:49 -04001# 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
Sean Dague70112362012-04-03 13:48:49 -040017import shlex
Matthew Treinish8d6836b2012-12-10 10:07:56 -050018import subprocess
Sean Dague70112362012-04-03 13:48:49 -040019
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 = 'sudo ' + command
36 command = "ssh %s %s@%s %s" % (get_ssh_options(keypath), user,
37 node, command)
38 popenargs = shlex.split(command)
39 process = subprocess.Popen(popenargs, stdout=subprocess.PIPE)
40 output, unused_err = process.communicate()
41 retcode = process.poll()
42 if retcode and check:
43 raise Exception("%s: ssh failed with retcode: %s" % (node, retcode))
44 return output
45
46
47def execute_on_all(keypath, user, nodes, command):
48 for node in nodes:
49 ssh(keypath, user, node, command)
50
51
52def enum(*sequential, **named):
Sean Daguef237ccb2013-01-04 15:19:14 -050053 """Create auto-incremented enumerated types."""
Sean Dague70112362012-04-03 13:48:49 -040054 enums = dict(zip(sequential, range(len(sequential))), **named)
55 return type('Enum', (), enums)