-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorch_magi_orch.py
executable file
·95 lines (75 loc) · 3.15 KB
/
orch_magi_orch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
import logging
import argparse
import yaml
import json
import tempfile
import subprocess
from sys import stdin
from os.path import isfile
log = logging.getLogger(__name__)
def handleLoggingArgs(args):
logLevels = {
u'none': 100,
u'all': 0,
u'debug': logging.DEBUG,
u'info': logging.INFO,
u'warning': logging.WARNING,
u'error': logging.ERROR,
u'critical': logging.CRITICAL
}
log_format = u'%(asctime)s %(name)-12s %(levelname)-8s %(message)s'
log_datefmt = u'%m-%d %H:%M:%S'
logging.basicConfig(format=log_format, datefmt=log_datefmt,
level=logLevels[args.loglevel])
log.debug('Logging set to {}'.format(args.loglevel))
return logLevels[args.loglevel]
def addLoggingArgs(ap):
ap.add_argument("-l", "--loglevel", dest="loglevel",
help="The level at which to log. Must be one of "
"none, debug, info, warning, error, or critical. Default is none. ("
"This is mostly used for debugging.)",
default='none', choices=['none', u'all', u'debug', u'info', u'warning',
u'error', u'critical'])
def execute_cmd(cmd, ev):
proc = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, universal_newlines=True)
for stdout_line in iter(proc.stdout.readline, ""):
yield stdout_line
proc.stdout.close()
ev = proc.wait()
if __name__ == '__main__':
def_conf = '/var/log/magi/config/experiment.conf'
def_path = '/share/magi/current/magi_orchestrator.py'
ap = argparse.ArgumentParser(description='Given an AAL on stdin, run the Magi Orchestrator with that AAL.')
addLoggingArgs(ap)
ap.add_argument('-c', '--config', type=str, default=def_conf,
help='Full path to default Magi config file. Default is {}.'.format(def_conf))
ap.add_argument('-p', '--path', type=str, default=def_path,
help='Full path to magi orchestrator script. Default is {}.'.format(def_path))
args = ap.parse_args()
handleLoggingArgs(args)
if not isfile(args.config):
log.critical('Config file does not exist: {}'.format(args.config))
exit(1)
log.info('Waiting for AAL...')
line = stdin.readline()
log.debug('read line from socket: {}'.format(line))
aal_json = json.loads(line)
aal = yaml.safe_load(line)
log.debug('Got AAL:\n{}'.format(aal))
exit_code = 0
try:
with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmpfile:
log.debug('AAL read. Saving to tmp file: {}'.format(tmpfile.name))
yaml.dump(aal, stream=tmpfile)
tmpfile.flush()
log.info('Spawning Magi Orchestrator...')
cmd = '{} --config={} -b localhost -f {} -o /dev/null --nocolor'.format(args.path, args.config, tmpfile.name)
log.info('running: {}'.format(cmd))
for l in execute_cmd(cmd, exit_code):
print(l)
except Exception as e:
log.critical('Error: {}'.format(e))
exit_code = 1
log.debug('Exiting with code: {}'.format(exit_code))
exit(exit_code)