From a357e129439bdeb8fa6302d658ea407f5247fee8 Mon Sep 17 00:00:00 2001 From: Ian Southam Date: Tue, 29 Jul 2014 17:04:31 +0200 Subject: [PATCH] Add the Python bits --- .../debian/config/opt/cloud/bin/merge.py | 112 ++++++++++++++++++ .../config/opt/cloud/bin/merge_cline.py | 18 +++ .../debian/config/opt/cloud/bin/test.sh | 6 + .../config/opt/cloud/bin/update_config.py | 12 +- 4 files changed, 144 insertions(+), 4 deletions(-) create mode 100755 systemvm/patches/debian/config/opt/cloud/bin/merge.py create mode 100755 systemvm/patches/debian/config/opt/cloud/bin/merge_cline.py create mode 100755 systemvm/patches/debian/config/opt/cloud/bin/test.sh diff --git a/systemvm/patches/debian/config/opt/cloud/bin/merge.py b/systemvm/patches/debian/config/opt/cloud/bin/merge.py new file mode 100755 index 00000000000..62c1018f918 --- /dev/null +++ b/systemvm/patches/debian/config/opt/cloud/bin/merge.py @@ -0,0 +1,112 @@ +#!/usr/bin/python + +import json +import os +import logging +import cs_ip + +from pprint import pprint + +class updateDataBag: + + qFile = {} + fpath = '' + bdata = { } + DPATH = "/var/chef/data_bags/vr" + + def __init__(self,qFile): + self.qFile = qFile + self.process() + + def save(self, dbag): + try: + handle = open(self.fpath, 'w') + except IOError: + logging.error("Could not write data bag %s", self.qFile.type) + else: + logging.debug("Writing data bag type %s", self.qFile.type) + jsono = json.dumps(dbag, indent=4, sort_keys=True) + handle.write(jsono) + + def load(self, key): + data = self.bdata + if not os.path.exists(self.DPATH): + os.makedirs(self.DPATH) + self.fpath = self.DPATH + '/' + key + '.json' + try: + handle = open(self.fpath) + except IOError: + logging.debug("Creating data bag type %s for key %s", self.qFile.type, key) + data.update( { "id": key } ) + else: + logging.debug("Loading data bag type %s for key %s", self.qFile.type, key) + data = json.load(handle) + handle.close() + return data + + def process(self): + dbag = self.load( self.qFile.type ) + logging.info("Command of type %s received", self.qFile.type) + if self.qFile.type == 'ips': + dbag = self.processIP(dbag) + if self.qFile.type == 'cl': + dbag = self.processCL(dbag) + self.save(dbag) + + def processIP(self, dbag): + for ip in self.qFile.data: + dbag = cs_ip.merge(dbag, ip) + return dbag + + def processCL(self, dbag): + # Convert the ip stuff to an ip object and pass that into cs_ip_merge + # "eth0ip": "192.168.56.32", + # "eth0mask": "255.255.255.0", + dbag['id'] = self.qFile.type + self.processCLItem('0', dbag) + self.processCLItem('1', dbag) + return dbag + + def processCLItem(self, num, dbag): + key = 'eth' + num + 'ip' + dp = {} + if(key in self.qFile.data['cmdline']): + dp['publicIp'] = self.qFile.data['cmdline'][key] + dp['vlanNetmask'] = self.qFile.data['cmdline']['eth' + num + 'mask'] + dp['sourceNat'] = False + dp['add'] = True + dp['oneToOneNat'] = False + dp['vlanGateway'] = ?? + dp['nicDevId'] = num + return + +class loadQueueFile: + + fileName = '' + dpath = "/etc/cloudstack" + data = {} + type = 'ips' + + def load(self): + fn = self.dpath + '/' + self.fileName + try: + handle = open(fn) + except IOError: + logging.error("Could not open %s", fn) + else: + self.data = json.load(handle) + handle.close() + proc = updateDataBag(self) + + def setFile(self, name): + self.fileName = name + + def setType(self, name): + self.type = name + + def getData(self): + return self.data + + def setPath(self, path): + self.dpath = path + diff --git a/systemvm/patches/debian/config/opt/cloud/bin/merge_cline.py b/systemvm/patches/debian/config/opt/cloud/bin/merge_cline.py new file mode 100755 index 00000000000..eb9f6e5fc79 --- /dev/null +++ b/systemvm/patches/debian/config/opt/cloud/bin/merge_cline.py @@ -0,0 +1,18 @@ +#!/usr/bin/python + +import sys +from merge import loadQueueFile +import logging + +logging.basicConfig(filename='/var/log/cloud.log',level=logging.DEBUG, format='%(asctime)s %(message)s') + +# first commandline argument should be the file to process +if ( len(sys.argv) != 2 ): + print "Invalid usage" + sys.exit(1) + +qf = loadQueueFile() +qf.setType("cl") +qf.setFile("cmdline.json") +qf.setPath("/var/chef/data_bags/vr") +qf.load() diff --git a/systemvm/patches/debian/config/opt/cloud/bin/test.sh b/systemvm/patches/debian/config/opt/cloud/bin/test.sh new file mode 100755 index 00000000000..3099459cf63 --- /dev/null +++ b/systemvm/patches/debian/config/opt/cloud/bin/test.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +/opt/cloud/bin/update_config.py ips0001.json +/opt/cloud/bin/update_config.py ips0002.json +/opt/cloud/bin/update_config.py ips0003.json + diff --git a/systemvm/patches/debian/config/opt/cloud/bin/update_config.py b/systemvm/patches/debian/config/opt/cloud/bin/update_config.py index ed3d3fbd598..1ab3f7d7ef9 100644 --- a/systemvm/patches/debian/config/opt/cloud/bin/update_config.py +++ b/systemvm/patches/debian/config/opt/cloud/bin/update_config.py @@ -1,13 +1,17 @@ #!/usr/bin/python -import syslog import sys +from merge import loadQueueFile +import logging + +logging.basicConfig(filename='/var/log/cloud.log',level=logging.DEBUG, format='%(asctime)s %(message)s') # first commandline argument should be the file to process if ( len(sys.argv) != 2 ): print "Invalid usage" sys.exit(1) -json_file = sys.argv[1] - -syslog.syslog(sys.argv[0] + " called for file " + json_file) +qf = loadQueueFile() +qf.setType("ips") +qf.setFile(sys.argv[1]) +qf.load()