silverlining / silverlining / commands /

"""Run a remote command"""
import os
import re
from cStringIO import StringIO
import zipfile
from cmdutils import CommandError
from import ssh
from silversupport.appdata import normalize_location

_tmp_re = re.compile(r'tmp="(.*)"')

def command_run(config):
    if not hasattr(config.args, 'unknown_args'):
        raise CommandError("You may not place any arguments before 'run'")
    args = config.args
    out = StringIO()
    zip = zipfile.ZipFile(out, 'w')
    translated_args = []
    any_translated = False
    for arg in args.unknown_args:
        if '=' in arg:
            option, filename = arg.split('=', 1)
            option = None
            filename = arg
        if os.path.exists(filename):
            if os.path.isdir(filename):
                for dirpath, dirnames, filenames in os.walk(filename):
                    relpath = dirpath.replace('\\', '/')
                    relpath = relpath[len(filename):].lstrip('/')
                    for fn in filenames:
                            os.path.join(dirpath, fn),
                            os.path.join(os.path.basename(filename), relpath, fn))
                ## FIXME: this basename might be unique
                zip.write(filename, os.path.basename(filename))
            translated_filename = os.path.join('$TMP', os.path.basename(filename))
            any_translated = True
            if option:
                translated_args.append(option + translated_filename)
    zip_content = out.getvalue()
    if args.user not in ['root', 'www-mgr', 'www-data']:
        raise CommandError(
            "Unknown --user=%s" % args.user)
    if any_translated:
        stdout, stderr, returncode = ssh(
            args.user, config.node_hostname, '/usr/local/share/silverlining/mgr-scripts/',
            stdin=zip_content, capture_stdout=True, capture_stderr=True)
        match =
        if not match:
            raise CommandError(
                "Got bad output from\n%s" % stdout)
        tmp_location =
        tmp_location = 'NONE'
    stdout, stderr, returncode = ssh(
        args.user, config.node_hostname,
         args.location, tmp_location, args.script] + translated_args)
    return returncode
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.