From 6fc93984d8a5ce597faf5194e4a0f785999792a7 Mon Sep 17 00:00:00 2001 From: Ken Date: Sat, 4 Sep 2021 14:54:51 -0400 Subject: [PATCH] Remote service control --- scripts/evio-control.py | 68 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100755 scripts/evio-control.py diff --git a/scripts/evio-control.py b/scripts/evio-control.py new file mode 100755 index 0000000..dd93c95 --- /dev/null +++ b/scripts/evio-control.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 + +import requests +import os +import subprocess +import logging +import logging.handlers as lh +import time +try: + import simplejson as json +except ImportError: + import json + +config_file="/etc/opt/evio/config.json" +nid_file="/var/opt/evio/nid" +log_file="/var/log/evio/evio-control.log" +server_addr="x.x.x.x:5802" +logger=None + +def runcmd(cmd): + """ Run a shell command. if fails, raise an exception. """ + if cmd[0] is None: + raise ValueError("No executable specified to run") + p = subprocess.run(cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, check=False) + return p + +def setup_logger(): + global logger + if os.path.isfile(log_file): + os.remove(log_file) + logger = logging.getLogger() + logger.setLevel(logging.DEBUG) + handler = lh.RotatingFileHandler(filename=log_file, maxBytes=10000,backupCount=2) + formatter = logging.Formatter( + "[%(asctime)s.%(msecs)03d] %(levelname)s:%(message)s", datefmt="%Y%m%d %H:%M:%S") + logging.Formatter.converter = time.localtime + handler.setFormatter(formatter) + logger.addHandler(handler) + +def node_id(): + with open(config_file) as f: + config = json.load(f) + if "NodeId" in config["CFx"]: + return config["CFx"]["NodeId"] + with open(nid_file) as f: + return f.readline().strip('\n') + + +def main(): + setup_logger() + nid = node_id() + url=f'http://{server_addr}/eviocontrol/?nodeid={nid}' + resp=requests.get(url).json() + logger.info(f"Server response: {resp}") + if resp and nid == resp[0]["EvioControl"]["NodeId"]: + ctrl = resp[0]["EvioControl"]["Control"] + if ctrl not in ("stop", "start", "status"): + logger.info(f"Invalid evio control {ctrl} received from {url}") + return + cp = runcmd(["systemctl", ctrl, "openvswitch-switch"]) + logger.info(cp) + cp = runcmd(["systemctl", ctrl, "evio"]) + logger.info(cp) + logger.info("***************************************") + +if __name__ == "__main__": + main() \ No newline at end of file