PageRenderTime 193ms CodeModel.GetById 90ms app.highlight 8ms RepoModel.GetById 93ms app.codeStats 0ms

/silverlining/commands/run.py

https://bitbucket.org/ianb/silverlining/
Python | 69 lines | 65 code | 3 blank | 1 comment | 10 complexity | cceba43499125423b88fd21d79412419 MD5 | raw file
 1"""Run a remote command"""
 2import os
 3import re
 4from cStringIO import StringIO
 5import zipfile
 6from cmdutils import CommandError
 7from silversupport.shell import ssh
 8from silversupport.appdata import normalize_location
 9
10_tmp_re = re.compile(r'tmp="(.*)"')
11
12
13def command_run(config):
14    if not hasattr(config.args, 'unknown_args'):
15        raise CommandError("You may not place any arguments before 'run'")
16    args = config.args
17    out = StringIO()
18    zip = zipfile.ZipFile(out, 'w')
19    translated_args = []
20    any_translated = False
21    for arg in args.unknown_args:
22        if '=' in arg:
23            option, filename = arg.split('=', 1)
24        else:
25            option = None
26            filename = arg
27        if os.path.exists(filename):
28            if os.path.isdir(filename):
29                for dirpath, dirnames, filenames in os.walk(filename):
30                    relpath = dirpath.replace('\\', '/')
31                    relpath = relpath[len(filename):].lstrip('/')
32                    for fn in filenames:
33                        zip.write(
34                            os.path.join(dirpath, fn),
35                            os.path.join(os.path.basename(filename), relpath, fn))
36            else:
37                ## FIXME: this basename might be unique
38                zip.write(filename, os.path.basename(filename))
39            translated_filename = os.path.join('$TMP', os.path.basename(filename))
40            any_translated = True
41            if option:
42                translated_args.append(option + translated_filename)
43            else:
44                translated_args.append(translated_filename)
45        else:
46            translated_args.append(arg)
47    zip.close()
48    zip_content = out.getvalue()
49    if args.user not in ['root', 'www-mgr', 'www-data']:
50        raise CommandError(
51            "Unknown --user=%s" % args.user)
52    if any_translated:
53        stdout, stderr, returncode = ssh(
54            args.user, config.node_hostname, '/usr/local/share/silverlining/mgr-scripts/save-tmp-file.py',
55            stdin=zip_content, capture_stdout=True, capture_stderr=True)
56        match = _tmp_re.search(stdout)
57        if not match:
58            raise CommandError(
59                "Got bad output from save-tmp-file.py:\n%s" % stdout)
60        tmp_location = match.group(1)
61    else:
62        tmp_location = 'NONE'
63    ssh_args = ['-t'] if args.interactive else []
64    stdout, stderr, returncode = ssh(
65        args.user, config.node_hostname,
66        ['/usr/local/share/silverlining/mgr-scripts/run-command.py',
67         args.location, tmp_location, args.script] + translated_args,
68        ssh_args=ssh_args)
69    return returncode