From 18f6ac940ae844ee672e026a39fb5f776f240eff Mon Sep 17 00:00:00 2001 From: Tony Mack Date: Tue, 18 Mar 2008 21:10:43 +0000 Subject: [PATCH] - Added Node, Site, Slice, Person, and PLC classes useful for defining a default instantiation of these objects. PLCs have there own separate config/api object. - Added Remote class which is inherited by any object that may have a remote home other than the machine we are running on. --- qaapi/qa/Nodes.py | 56 ++++++++++++++++++++++++++++++++++++++++ qaapi/qa/PLCs.py | 34 ++++++++++++++++++++++++ qaapi/qa/Persons.py | 23 +++++++++++++++++ qaapi/qa/Remote.py | 63 +++++++++++++++++++++++++++++++++++++++++++++ qaapi/qa/Sites.py | 26 +++++++++++++++++++ qaapi/qa/Slices.py | 24 +++++++++++++++++ qaapi/qa/Table.py | 7 +++++ 7 files changed, 233 insertions(+) create mode 100644 qaapi/qa/Nodes.py create mode 100644 qaapi/qa/PLCs.py create mode 100644 qaapi/qa/Persons.py create mode 100644 qaapi/qa/Remote.py create mode 100644 qaapi/qa/Sites.py create mode 100644 qaapi/qa/Slices.py create mode 100644 qaapi/qa/Table.py diff --git a/qaapi/qa/Nodes.py b/qaapi/qa/Nodes.py new file mode 100644 index 0000000..95a4dcf --- /dev/null +++ b/qaapi/qa/Nodes.py @@ -0,0 +1,56 @@ +import utils +import os +from Remote import Remote +from Table import Table + + +class Node(dict, Remote): + + fields = { + 'plc': None, + 'hostname': None, # Node Hostname + 'host': 'localhost', # host where node lives + 'vserver': None, # vserver where this node lives + 'type': 'virtual', # type of node + 'nodenetworks': [], # node networks + 'homedir': '/var/VirtualMachines/', + 'rootkey': '/home/tmack/.ssh/plc-root' # path to root ssh key + } + + def __init__(self, config, fields = {}): + + # XX Filter out fields not specified in fields + dict.__init__(self, self.fields) + + # Merge defined fields with defaults + self.update(fields) + self.config = config + + def create_boot_image(self): + + command = "" + if self['host'] and self['host'] not in ['localhost']: + command += "ssh -i %s root@%s " % (self['rootkey'], self['host']) + + def create_disk_image(self, size = '10G'): + diskimg_path = self['homedir'] + os.sep + self['hostname'] + os.sep + \ + 'hda.img' + command = "" + command += " qemu-img create -f qcow2 %(diskimg_path)s %(size)s " % locals() + + #utils.header(command) + (status, output) = self.commands(command, True) + + def boot(self): + pass + + def scp(self): + pass + +class Nodes(list, Table): + + def __init__(self, config, nodes): + nodelist = [Node(config, node) for node in nodes] + list.__init__(self, nodelist) + self.config = config + diff --git a/qaapi/qa/PLCs.py b/qaapi/qa/PLCs.py new file mode 100644 index 0000000..e16aaa7 --- /dev/null +++ b/qaapi/qa/PLCs.py @@ -0,0 +1,34 @@ +import os +import copy +from Remote import Remote +from Table import Table + +class PLC(dict, Remote): + fields = { + 'name': 'TestPLC', # PLC Name + 'host': 'localhost', # Node Hostname + 'ip': '127.0.0.1', # IP + 'chroot': None, + 'vserver': None, # Vserver where this PLC lives + 'rootkey': '/home/tmack/.ssh/plc-root', # Root Key + 'api_path': '/PLCAPI/', # PLCAPI path + 'port': '443' # PLCAPI port + + } + + def __init__(self, config, fields = {}): + # XX Filter out fields not specified in fields + dict.__init__(self, self.fields) + + # Merge defined fields with defaults + self.update(fields) + + # init config + self.config = config + self.config.update_api(self) + +class PLCs(list, Table): + + def __init__(self, config, plcs): + plclist = [PLC(config, plc) for plc in plcs] + list.__init__(self, plclist) diff --git a/qaapi/qa/Persons.py b/qaapi/qa/Persons.py new file mode 100644 index 0000000..8012a1e --- /dev/null +++ b/qaapi/qa/Persons.py @@ -0,0 +1,23 @@ +import os +from Table import Table + +class Person(dict): + + fields = { + 'plc': None, + 'first_name': None, + 'last_name': None, + 'password': None, + 'email': None, + } + + def __init__(self, fields = {}): + + dict.__init__(self, self.fields) + + self.update(fields) + +class Persons(list, Table): + def __init__(self, persons): + personlist = [Person(person) for person in persons] + list.__init__(self, personlist) diff --git a/qaapi/qa/Remote.py b/qaapi/qa/Remote.py new file mode 100644 index 0000000..6463945 --- /dev/null +++ b/qaapi/qa/Remote.py @@ -0,0 +1,63 @@ +import utils + +class Remote: + + def get_remote_command(self, command): + if 'chroot' in self and self['chroot']: + command = " chroot %s %s" % (self['chroot'], command) + if 'vserver' in self and self['vserver']: + command = " vserver %s exec '%s' " % (self['vserver'], command) + if 'host' in self and self['host'] not in ['localhost']: + options = "" + if 'rootkey' in self and self['rootkey']: + options = "-i %s " % self['rootkey'] + command = "ssh %s root@%s \"%s\" " % (options, self['host'], command) + + return command + + def popen(self, command, fatal = True): + command = self.get_remote_command(command) + #utils.header(command) + return utils.popen(command, fatal) + + def commands(self, command, fatal = True): + command = self.get_remote_command(command) + #utils.header(command) + return utils.commands(command, fatal) + + def scp(self, src, dest): + options = "" + if 'rootkey' in self and self['rootkey'] is not None: + options += " -i %s " % (self['rootkey']) + path = "" + if 'chroot' in self and self['chroot'] is not None: + path += "/plc/root/" + if 'vserver' in self and self['vserver'] is not None: + path += '/vservers/%s/' % self['vserver'] + + src_cmd = "" + src_parts = src.split(":") + dest_cmd = "" + dest_parts = dest.split(":") + command = "scp " + if len(src_parts) == 1: + src_cmd = src + elif src_parts[0].find('localhost') != -1: + src_cmd = src_parts[1] + else: + host, file = src_parts[0], src_parts[1] + src_cmd = 'root@%(host)s:%(path)s%(file)s ' % locals() + + if len(dest_parts) == 1: + dest_cmd = dest + elif dest_parts[0].find('localhost') != -1: + dest_cmd = dest_parts[1] + else: + host, file = dest_parts[0], dest_parts[1] + dest_cmd = 'root@%(host)s:%(path)s%(file)s' % locals() + + print dest_parts + command = 'scp %(options)s %(src_cmd)s %(dest_cmd)s' % locals() + utils.header(command) + return utils.commands(command) + diff --git a/qaapi/qa/Sites.py b/qaapi/qa/Sites.py new file mode 100644 index 0000000..406895b --- /dev/null +++ b/qaapi/qa/Sites.py @@ -0,0 +1,26 @@ +import os +from Table import Table + +class Site(dict): + + fields = { + 'plc': 'TestPLC', + 'name': None, + 'login_base': None, + 'enabled': True, + 'abbreviated_name': None, + 'max_slices': 100, + 'is_public': True, + 'url': None + } + + def __init__(self, fields = {}): + + dict.__init__(self, self.fields) + + self.update(fields) + +class Sites(list, Table): + def __init__(self, sites): + sitelist = [Site(site) for site in sites] + list.__init__(self, sitelist) diff --git a/qaapi/qa/Slices.py b/qaapi/qa/Slices.py new file mode 100644 index 0000000..60b78ec --- /dev/null +++ b/qaapi/qa/Slices.py @@ -0,0 +1,24 @@ +import os +from Table import Table + +class Slice(dict): + + fields = { + 'plc': None, + 'name': None, + 'instantiation': 'plc-instantiated', + 'max_nodes': 1000, + 'description': 'blank', + 'url': None + } + + def __init__(self, fields = {}): + + dict.__init__(self, self.fields) + + self.update(fields) + +class Slices(list, Table): + def __init__(self, slices): + slicelist = [Slice(slice) for slice in slices] + list.__init__(self, slicelist) diff --git a/qaapi/qa/Table.py b/qaapi/qa/Table.py new file mode 100644 index 0000000..d2d90a7 --- /dev/null +++ b/qaapi/qa/Table.py @@ -0,0 +1,7 @@ + +class Table: + def dict(self, key_field): + """ + Return ourself as a dict keyed on key_fields + """ + return dict([(obj[key_field], obj) for obj in self]) -- 2.43.0