def db_initiation(cls): LOG.info("--- Initiating SONA DB ---") init_sql = [ 'CREATE TABLE ' + cls.NODE_INFO_TBL + '(nodename text primary key, ip_addr, username)', 'CREATE TABLE ' + cls.STATUS_TBL + '(nodename text primary key, ping, app, cpu, memory, disk, time)', 'CREATE TABLE ' + cls.RESOURCE_TBL + '(nodename text primary key, cpu real, memory real, disk real)', 'CREATE TABLE ' + cls.REGI_SYS_TBL + '(url text primary key, auth)', 'CREATE TABLE ' + cls.EVENT_TBL + '(nodename, item, grade, desc, time, PRIMARY KEY (nodename, item))' ] for sql in init_sql: sql_rt = cls.sql_execute(sql) if "already exist" in sql_rt: table_name = sql_rt.split()[1] LOG.info( "\'%s\' table already exist. Delete all tuple of this table...", table_name) sql = 'DELETE FROM ' + table_name sql_rt = cls.sql_execute(sql) if sql_rt != 'SUCCESS': LOG.info("DB %s table initiation fail\n%s", table_name, sql_rt) sys.exit(1) elif sql_rt != 'SUCCESS': LOG.info("DB initiation fail\n%s", sql_rt) sys.exit(1) LOG.info('Insert nodes information ...') for node in CONF.watchdog()['check_system']: if str(node).lower() == 'onos': cls.sql_insert_nodes(CONF.onos()['list'], str(CONF.onos()['account']).split(':')[0]) elif str(node).lower() == 'xos': cls.sql_insert_nodes(CONF.xos()['list'], str(CONF.xos()['account']).split(':')[0]) elif str(node).lower() == 'swarm': cls.sql_insert_nodes( CONF.swarm()['list'], str(CONF.swarm()['account']).split(':')[0]) elif str(node).lower() == 'openstack': cls.sql_insert_nodes( CONF.openstack()['list'], str(CONF.openstack()['account']).split(':')[0])
def get_onos_ip(self): return str(list(CONF.onos()['list']).pop()).split(':')[-1]
def create_instance(server_options, client_options): server_instance = client_instance = None image_name = CONF.openstack()['image'] flavor_name = CONF.openstack()['flavor'] securitygroups = CONF.openstack()['security_groups'] keypair = CONF.openstack()['keypair_name'] # TODO add exception for connection nova_credentials = client.Client(CONF.openstack()['version'], CONF.openstack()['username'], CONF.openstack()['api_key'], CONF.openstack()['project_id'], CONF.openstack()['auth_url']) image = nova_credentials.images.find(name=image_name) flavor = nova_credentials.flavors.find(name=flavor_name) hypervisors = nova_credentials.hypervisors.list() onos_ip = CONF.onos()['list'].pop().split(':')[-1] dpid2ip = {c[2]: c[3] for c in [(" ".join(l.split()).split(" ")) for l in SshCommand.onos_ssh_exec(onos_ip, 'openstack-nodes | grep COMPUTE').splitlines()]} def get_zone(dpid): if dpid: for h in hypervisors: if h.host_ip == dpid2ip[dpid]: return 'nova:' + h.service['host'] else: return "nova" # TODO: when no network_id, choice a network excepted external netwrok # network_id = server_options['network_id'] if server_options['network_id'] \ # else random.choice(nova.networks.list()).id # network_list = nova_credentials.networks.list() # target_network.append(str(network_list[-1]).split(':')[1][:-1].strip()) # Create server VM info vm_name = 'tperf_server_vm_' + str(random.randrange(10000, 20000)) LOG.info('[server] - vmname = %s', vm_name) LOG.info(' | image = %s', image) LOG.info(' | flavor = %s', flavor) LOG.info(' | availability_zone = %s', get_zone(server_options['vm_location'])) LOG.info(' | nics = %s', server_options['network_id']) LOG.info(' | security_groups = %s', securitygroups) LOG.info(' | key_pair = %s', keypair) nova_credentials.servers.create(name=vm_name, image=image, flavor=flavor, availability_zone=get_zone(server_options['vm_location']), nics=[{'net-id': server_options['network_id']}], security_groups=securitygroups, key_name=keypair) for i in range(20): time.sleep(1) server_instance = nova_credentials.servers.list(search_opts={'name': vm_name})[0] # server_instance = nova_credentials.servers.list(search_opts={'name': 'tperf_server_vm_17693'})[0] if server_instance.__dict__['addresses']: LOG.info("[Server VM created and ACTIVE] - %s", server_instance) break # Create client VM info vm_name = 'tperf_client_vm_' + str(random.randrange(10000, 20000)) LOG.info('[client] - vmname = %s', vm_name) LOG.info(' | image = %s', image) LOG.info(' | flavor = %s', flavor) LOG.info(' | availability_zone = %s', get_zone(client_options['vm_location'])) LOG.info(' | nics = %s', client_options['network_id']) LOG.info(' | security_groups = %s', securitygroups) LOG.info(' | key_pair = %s', keypair) nova_credentials.servers.create(name=vm_name, image=image, flavor=flavor, availability_zone=get_zone(client_options['vm_location']), nics=[{'net-id': client_options['network_id']}], security_groups=securitygroups, key_name=keypair) client_floatingip = get_floatingip(nova_credentials) # client_floatingip = '172.27.0.179' for i in range(20): time.sleep(1) client_instance = nova_credentials.servers.list(search_opts={'name': vm_name})[0] # client_instance = nova_credentials.servers.list(search_opts={'name': 'tperf_client_vm_15442'})[0] if client_instance.__dict__['addresses']: LOG.info("[Client VM created and ACTIVE] - %s", client_instance) nova_credentials.servers.add_floating_ip(client_instance, client_floatingip.ip) LOG.info("[Floating_IP Assignment] to Client ---") break return server_instance, client_instance, client_floatingip