David Kranz | 6308ec2 | 2012-02-22 09:36:48 -0500 | [diff] [blame] | 1 | # 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 |
|
| 17 | import subprocess
|
| 18 | import shlex
|
| 19 |
|
| 20 | SSH_OPTIONS = (" -q" +
|
| 21 | " -o UserKnownHostsFile=/dev/null" +
|
| 22 | " -o StrictHostKeyChecking=no -i ")
|
| 23 |
|
| 24 |
|
| 25 | def get_ssh_options(keypath):
|
| 26 | return SSH_OPTIONS + keypath
|
| 27 |
|
| 28 |
|
| 29 | def scp(keypath, args):
|
| 30 | options = get_ssh_options(keypath)
|
| 31 | return subprocess.check_call(shlex.split("scp" + options + args))
|
| 32 |
|
| 33 |
|
| 34 | def ssh(keypath, user, node, command, check=True):
|
David Kranz | 30fe84a | 2012-03-20 16:25:47 -0400 | [diff] [blame] | 35 | command = 'sudo ' + command
|
David Kranz | 6308ec2 | 2012-02-22 09:36:48 -0500 | [diff] [blame] | 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 |
|
| 47 | def execute_on_all(keypath, user, nodes, command):
|
| 48 | for node in nodes:
|
| 49 | ssh(keypath, user, node, command)
|
| 50 |
|
| 51 |
|
| 52 | def enum(*sequential, **named):
|
| 53 | """Create auto-incremented enumerated types"""
|
| 54 | enums = dict(zip(sequential, range(len(sequential))), **named)
|
| 55 | return type('Enum', (), enums)
|