Sean Dague | 7011236 | 2012-04-03 13:48:49 -0400 | [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 | |
Sean Dague | 7011236 | 2012-04-03 13:48:49 -0400 | [diff] [blame] | 17 | import shlex |
Matthew Treinish | 8d6836b | 2012-12-10 10:07:56 -0500 | [diff] [blame] | 18 | import subprocess |
Sean Dague | 7011236 | 2012-04-03 13:48:49 -0400 | [diff] [blame] | 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): |
| 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 | |
| 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): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame^] | 53 | """Create auto-incremented enumerated types.""" |
Sean Dague | 7011236 | 2012-04-03 13:48:49 -0400 | [diff] [blame] | 54 | enums = dict(zip(sequential, range(len(sequential))), **named) |
| 55 | return type('Enum', (), enums) |