Commit 094b00ba authored by Sigmund Augdal's avatar Sigmund Augdal

Cleaner usage of daemonize

parent ede88c92
......@@ -14,10 +14,12 @@ CONF_FILE = "/etc/haproxy/haproxy.cfg"
class Generator(object):
def __init__(self, cert, key, cacert):
self.etcd_key = key
self.etcd_cert = cert
self.etcd_cacert = cacert
def __init__(self, cert, key, cacert, logfile=None):
self.etcd_client = etcd.Etcd(ssl_key=key, ssl_cert=cert, verify=cacert)
if logfile:
handler = logging.handlers.RotatingFileHandler(logfile, maxBytes=10*1024**3,
backupCount=5)
logging.getLogger("").addHandler(handler)
def output(self, line):
self.output_file.write(line)
......@@ -103,12 +105,7 @@ class Generator(object):
subprocess.call("ipset restore", stdin=self.output_file, shell=True)
return index
def main(self, logfile=None):
if logfile:
handler = logging.handlers.RotatingFileHandler(logfile, maxBytes=10*1024**3,
backupCount=5)
logging.getLogger("").addHandler(handler)
self.etcd_client = etcd.Etcd(ssl_key=self.etcd_key, ssl_cert=self.etcd_cert, verify=self.etcd_cacert)
def main(self):
index = self.generate_all()
sys.exit(0)
while True:
......@@ -118,6 +115,11 @@ class Generator(object):
index = self.generate_all()
def daemon_main(args):
generator = Generator(args.cert, args.key, args.cacert, args.logfeil)
generator.main()
def parse_args():
parser = argparse.ArgumentParser(description="Configure ipset based on data from etcd")
parser.add_argument('-d', '--daemonize', default=False, action='store_true', help="Run as daemon")
......@@ -125,6 +127,8 @@ def parse_args():
parser.add_argument('--cert', default="client.crt", help="client certificate to use")
parser.add_argument('--key', default="client.key", help="private key to use for client certificate")
parser.add_argument('--cacert', default="etcd_ca.crt", help="ca certificate to use")
parser.add_argument('--logfile', default='/var/log/nova_sg_configurator.log', help="logfile to use")
return parser.parse_args()
if __name__ == '__main__':
......@@ -132,9 +136,7 @@ if __name__ == '__main__':
logging.getLogger("requests").setLevel(logging.WARNING)
args = parse_args()
if args.daemonize:
logfile = '/var/log/nova_sg_configurator.log'
generator = Generator(args.cert, args.key, args.cacert)
daemon = Daemonize(app="nova_sg_configurator", pid=args.pidfile, action=lambda: generator.main(logfile))
daemon = Daemonize(app="nova_sg_configurator", pid=args.pidfile, action=lambda: daemon_main(args))
daemon.start()
else:
Generator(args.cert, args.key, args.cacert).main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment