#!/usr/bin/env python from builtins import object import logging import argparse import apache_conf_parser import subprocess QDA_QPKG_NAME = 'CloudConnector3' FORMAT = "%(lineno)s %(funcName)-10s - %(message)s" logging.basicConfig(format=FORMAT, level=logging.INFO) class Ports(object): def __init__(self): self.http = subprocess.run(['/sbin/getcfg', 'System', 'Web Access Port', '-d', '8080'], check=False, encoding='utf-8', stdout=subprocess.PIPE).stdout.strip() self.https = subprocess.run(['/sbin/getcfg', 'Stunnel', 'Port', '-d', '443'], check=False, encoding='utf-8', stdout=subprocess.PIPE).stdout.strip() def get(self): return { 'http': self.http, 'https': self.https, } class ProxyOp(object): def __init__(self, **kwargs): self.type = kwargs['type'] self.path = { 'ssl': '/etc/apache-sys-proxy-ssl.conf', 'nossl': '/etc/apache-sys-proxy.conf', 'ssl_tplt': '/etc/default_config/apache-sys-proxy-ssl.conf.tplt', 'nossl_tplt': '/etc/default_config/apache-sys-proxy.conf.tplt', }[self.type] ports = Ports().get() self.virtualhost = { 'ssl': 'VirtualHost _default_:{https}'.format(**ports), 'nossl': 'VirtualHost _default_:{http}'.format(**ports), 'ssl_tplt': 'VirtualHost _default_:@SYS_HTTPS_PORT@', 'nossl_tplt': 'VirtualHost _default_:@SYS_HTTP_PORT@', }[self.type] self.qda_tag = "### CloudConnector3 section" self.qda_proxy = 'Include /mnt/ext/opt/CloudConnector3/conf/apache-fastcgi.conf' self.vhost_default = apache_conf_parser.ApacheConfParser(self.path) def find_qda_proxy(self): for node in self.vhost_default.nodes: if hasattr(node, 'content') and \ node.content == self.virtualhost: for subnode in node.body.nodes: if hasattr(subnode, 'content') and \ subnode.content == 'IfModule proxy_module' and \ subnode.body.pprint().startswith(self.qda_tag): return node, subnode return node, None return None, None def add_qvs(self): vf, im = self.find_qda_proxy() logging.debug('Add qvs proxy to {}'.format(self.path)) if vf: if im: logging.debug('Clear old proxy configure') vf.body.nodes.remove(im) newim = apache_conf_parser.ComplexDirective() newim.add_line('') newim.add_line(self.qda_tag) newim.add_line(self.qda_proxy) newim.add_line('') vf.body.nodes.insert(0, newim) self.update_proxy() logging.debug('Added') def del_qvs(self): vf, im = self.find_qda_proxy() logging.debug('Delete qvs proxy from {}'.format(self.path)) if vf: if im: vf.body.nodes.remove(im) self.update_proxy() logging.debug('Deleted') else: logging.debug('Nothing has changed') def update_proxy(self): with open(self.path, 'w') as f: f.write(self.vhost_default.__str__()) class Command(object): def __init__(self, **kwargs): self.type = kwargs['type'] self.ssl = kwargs['ssl'] self.tplt = kwargs['tplt'] logging.debug(kwargs) def start(self): try: ssl = { (True, True): 'ssl_tplt', (True, False): 'ssl', (False, True): 'nossl_tplt', (False, False): 'nossl', }[self.ssl, self.tplt] op = ProxyOp(type=ssl) func = { 'add': op.add_qvs, 'del': op.del_qvs, }[self.type] func() except: pass if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('type', choices=['add', 'del']) args = parser.parse_args() ret = Command(type=args.type, ssl=False, tplt=False).start() ret = Command(type=args.type, ssl=False, tplt=True).start() ret = Command(type=args.type, ssl=True, tplt=False).start() ret = Command(type=args.type, ssl=True, tplt=True).start()