2020-08-05 17:01:47 -04:00
|
|
|
#!/usr/bin/env python3
|
2013-08-06 14:38:43 -04:00
|
|
|
|
|
|
|
from nilmdb.client.httpclient import HTTPClient, ClientError, ServerError
|
|
|
|
from nilmdb.utils.printf import *
|
|
|
|
import nilmrun
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Run a command on the NilmRun server"""
|
|
|
|
def_url = os.environ.get("NILMRUN_URL", "http://localhost/nilmrun/")
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description = 'Run a command on the NilmRun server',
|
2020-08-05 16:59:56 -04:00
|
|
|
formatter_class = argparse.ArgumentDefaultsHelpFormatter)
|
|
|
|
parser.add_argument("-v", "--version", action="version",
|
|
|
|
version=nilmrun.__version__)
|
2013-08-06 14:38:43 -04:00
|
|
|
group = parser.add_argument_group("Standard options")
|
|
|
|
group.add_argument('-u', '--url',
|
|
|
|
help = 'NilmRun server URL', default = def_url)
|
|
|
|
group.add_argument('-n', '--noverify', action="store_true",
|
|
|
|
help = 'Disable SSL certificate verification')
|
|
|
|
group = parser.add_argument_group("Program")
|
|
|
|
group.add_argument('-d', '--detach', action="store_true",
|
|
|
|
help = 'Run process and return immediately without '
|
|
|
|
'printing its output')
|
|
|
|
group.add_argument('cmd', help="Command to run")
|
|
|
|
group.add_argument('arg', nargs=argparse.REMAINDER,
|
|
|
|
help="Arguments for command")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2020-08-05 16:59:56 -04:00
|
|
|
client = HTTPClient(baseurl=args.url, verify_ssl=not args.noverify,
|
|
|
|
post_json=True)
|
2013-08-06 14:38:43 -04:00
|
|
|
|
|
|
|
# Run command
|
|
|
|
pid = client.post("run/command", { "argv": [ args.cmd ] + args.arg })
|
|
|
|
|
|
|
|
# If we're detaching, just print the PID
|
|
|
|
if args.detach:
|
2020-08-03 16:54:33 -04:00
|
|
|
print(pid)
|
2013-08-06 14:38:43 -04:00
|
|
|
raise SystemExit(0)
|
|
|
|
|
|
|
|
# Otherwise, watch the log output, and kill the process when it's done
|
|
|
|
# or when this script terminates.
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
s = client.get("process/status", { "pid": pid, "clear": 1 })
|
|
|
|
sys.stdout.write(s['log'])
|
|
|
|
sys.stdout.flush()
|
|
|
|
if not s['alive']:
|
|
|
|
break
|
|
|
|
time.sleep(1)
|
|
|
|
finally:
|
|
|
|
s = client.post("process/remove", { "pid": pid })
|
|
|
|
|
|
|
|
raise SystemExit(s['exitcode'])
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|