~abentley/workspace-runner/enhanced-artifacts

« back to all changes in this revision

Viewing changes to workspace_runner/__init__.py

  • Committer: Aaron Bentley
  • Date: 2015-06-18 21:14:57 UTC
  • Revision ID: aaron.bentley@canonical.com-20150618211457-f73y7vzi2yictrw5
Initial test of workspace_run.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from argparse import ArgumentParser
 
2
import subprocess
 
3
import sys
 
4
 
 
5
from yaml import safe_load
 
6
 
 
7
__metaclass__ = type
 
8
 
 
9
 
 
10
def parse_args(argv=None):
 
11
    parser = ArgumentParser()
 
12
    parser.add_argument('config', help='Config file to use.')
 
13
    parser.add_argument('host', help='Machine to run the command on.')
 
14
    return parser.parse_args(argv)
 
15
 
 
16
 
 
17
class Runner:
 
18
 
 
19
    def __init__(self, host):
 
20
        self.host = host
 
21
 
 
22
 
 
23
class SSHRunner(Runner):
 
24
    """Class to run commands via SSH."""
 
25
 
 
26
    def run(self, args, out_file):
 
27
        subprocess.check_call(['ssh', self.host] + args, stdout=out_file)
 
28
 
 
29
 
 
30
def workspace_run(argv=None, runner_factory=SSHRunner):
 
31
    args = parse_args(argv)
 
32
    with open(args.config) as config_file:
 
33
        config = safe_load(config_file)
 
34
        runner = runner_factory(args.host)
 
35
        runner.run(config['command'], sys.stdout)