From ac6bdf870d0c4727128ce38cf818ee93ce510ce8 Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Tue, 18 Feb 2014 19:00:53 +0000 Subject: Split out 'util' and 'config' modules from test_trove_upgrades --- util.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 util.py (limited to 'util.py') diff --git a/util.py b/util.py new file mode 100644 index 0000000..8974674 --- /dev/null +++ b/util.py @@ -0,0 +1,90 @@ +# Copyright (C) 2014 Codethink Limited +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +'''Baserock system-test utility functions.''' + +import cliapp +import contextlib +import sys +import urlparse + +import config + + +def run_morph(args, **kwargs): + '''Run Morph on the current machine.''' + morph_command = config.MORPH_COMMAND + if isinstance(morph_command, str): + morph_command = morph_command.split(' ') + cmd = morph_command + args + if config.VERBOSE: + print ' '.join(cmd) + if 'stdout' not in kwargs: + kwargs['stdout'] = sys.stdout + if 'stderr' not in kwargs: + kwargs['stderr'] = sys.stdout + return cliapp.runcmd(cmd, **kwargs) + + +def run_git(args, **kwargs): + return cliapp.runcmd(['git'] + args, **kwargs) + + +def remote_runcmd(url, command, **kwargs): + ''' + Execute a command on machine 'url'. + + Command must be a list of arguments, not a single string. + + FIXME: perhaps this functionality should be merged into cliapp.ssh_runcmd() + so that we can use that instead. + ''' + if config.VERBOSE: + print "%s: %s" % (url, ' '.join(command)) + url = urlparse.urlsplit(url) + if url[0] in ['ssh', 'kvm+ssh']: + ssh_host = url[1] + + ssh_cmd = ['ssh'] + + # The identity of the newly-created test machine will never be in + # '~/.ssh/known_hosts'; this switch avoids seeing the 'do you want to + # connect' prompt that SSH would normally present in this situation. + ssh_cmd.extend(['-o', 'StrictHostKeyChecking=no']) + + return cliapp.runcmd(ssh_cmd + [ssh_host, ' '.join(command)], **kwargs) + else: + raise NotImplementedError("Remote machine must be an ssh:// URL") + + +def read_file(file_path): + with open(file_path, 'r') as f: + return f.read() + + +def write_file(file_path, text): + with open(file_path, 'w') as f: + f.write(text) + + +@contextlib.contextmanager +def set_directory(path): + '''Context manager to set current working directory of a script.''' + old_path = os.getcwd() + os.chdir(path) + try: + yield + finally: + os.chdir(old_path) -- cgit v1.2.1