def get_next_dynic(argv=[]): """Get the next available dynamic nic on this host""" cmd = ["ifconfig", "-a"] f_cmd_output = ( utils.subprocess_popen(cmd, stdout=subprocess.PIPE).communicate()[0]) eths = [lines.split(' ')[0] for lines in f_cmd_output.splitlines() if "eth" in lines] #print eths for eth in eths: cmd = ["ethtool", "-i", eth] f_cmd_output = ( utils.subprocess_popen(cmd, stdout=subprocess.PIPE).communicate()[0]) bdf = [lines.split(' ')[1] for lines in f_cmd_output.splitlines() if "bus-info" in lines] #print bdf cmd = ["lspci", "-n", "-s", bdf[0]] f_cmd_output = (utils.subprocess_popen(cmd, stdout=subprocess.PIPE). communicate()[0]) deviceid = [(lines.split(':')[3]).split(' ')[0] for lines in f_cmd_output.splitlines()] #print deviceid if deviceid[0] == "0044": cmd = ["/sbin/ip", "link", "show", eth] f_cmd_output = ( utils.subprocess_popen(cmd, stdout=subprocess.PIPE). communicate()[0]) used = [lines for lines in f_cmd_output.splitlines() if "UP" in lines] if not used: break return eth
def test_KillFilter(self): p = utils.subprocess_popen(["/bin/sleep", "5"]) f = filters.KillFilter("root", "/bin/sleep", "-9", "-HUP") f2 = filters.KillFilter("root", "/usr/bin/sleep", "-9", "-HUP") usercmd = ['kill', '-ALRM', p.pid] # Incorrect signal should fail self.assertFalse(f.match(usercmd) or f2.match(usercmd)) usercmd = ['kill', p.pid] # Providing no signal should fail self.assertFalse(f.match(usercmd) or f2.match(usercmd)) # Providing matching signal should be allowed usercmd = ['kill', '-9', p.pid] self.assertTrue(f.match(usercmd) or f2.match(usercmd)) f = filters.KillFilter("root", "/bin/sleep") f2 = filters.KillFilter("root", "/usr/bin/sleep") usercmd = ['kill', os.getpid()] # Our own PID does not match /bin/sleep, so it should fail self.assertFalse(f.match(usercmd) or f2.match(usercmd)) usercmd = ['kill', 999999] # Nonexistant PID should fail self.assertFalse(f.match(usercmd) or f2.match(usercmd)) usercmd = ['kill', p.pid] # Providing no signal should work self.assertTrue(f.match(usercmd) or f2.match(usercmd))
def execute(cmd, root_helper=None, process_input=None, addl_env=None, check_exit_code=True, return_stderr=False): if root_helper: cmd = shlex.split(root_helper) + cmd cmd = map(str, cmd) LOG.debug(_("Running command: %s"), cmd) env = os.environ.copy() if addl_env: env.update(addl_env) obj = utils.subprocess_popen(cmd, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) _stdout, _stderr = (process_input and obj.communicate(process_input) or obj.communicate()) obj.stdin.close() m = _("\nCommand: %(cmd)s\nExit code: %(code)s\nStdout: %(stdout)r\n" "Stderr: %(stderr)r") % {'cmd': cmd, 'code': obj.returncode, 'stdout': _stdout, 'stderr': _stderr} LOG.debug(m) if obj.returncode and check_exit_code: raise RuntimeError(m) return return_stderr and (_stdout, _stderr) or _stdout
def get_next_dynic(argv=[]): """Get the next available dynamic nic on this host""" cmd = ["ifconfig", "-a"] f_cmd_output = utils.subprocess_popen(cmd, stdout=subprocess.PIPE).communicate()[0] eths = [lines.split(" ")[0] for lines in f_cmd_output.splitlines() if "eth" in lines] # print eths for eth in eths: cmd = ["ethtool", "-i", eth] f_cmd_output = utils.subprocess_popen(cmd, stdout=subprocess.PIPE).communicate()[0] bdf = [lines.split(" ")[1] for lines in f_cmd_output.splitlines() if "bus-info" in lines] # print bdf cmd = ["lspci", "-n", "-s", bdf[0]] f_cmd_output = utils.subprocess_popen(cmd, stdout=subprocess.PIPE).communicate()[0] deviceid = [(lines.split(":")[3]).split(" ")[0] for lines in f_cmd_output.splitlines()] # print deviceid if deviceid[0] == "0044": cmd = ["/sbin/ip", "link", "show", eth] f_cmd_output = utils.subprocess_popen(cmd, stdout=subprocess.PIPE).communicate()[0] used = [lines for lines in f_cmd_output.splitlines() if "UP" in lines] if not used: break return eth
def connect_vm(tenant_id, vm_image_id, service_instance_id, *args): """ Starts a VMs and is connected to southbound network """ l2db.initialize() client = Client(HOST, PORT, USE_SSL, format='json', tenant=tenant_id) print (_("Connecting %(vm_image_id)s to Service " "%(service_instance_id)s") % locals()) service_logic = servlogcs.ServicesLogistics() service_nets = sdb.get_service_bindings(service_instance_id) client.create_port(service_nets.mngnet_id) client.create_port(service_nets.nbnet_id) sb_port_id = client.create_port(service_nets.sbnet_id) LOG.debug(_("Operation 'create_port' executed.")) new_port_id = sb_port_id[servconts.PORT][servconts.ID] try: create_vm_args = [] create_vm_args.append(servconts.CREATE_VM_CMD) create_vm_args.append(vm_image_id) print _("Creating VM with image: %s") % (vm_image_id) process = utils.subprocess_popen(create_vm_args, stdout=subprocess.PIPE) result = process.stdout.readlines() tokens = re.search("i-[a-f0-9]*", str(result[1])) vm_name = tokens.group(0) print _("Image: %s instantiated successfully") % (vm_name) except Exception as exc: print exc service_logic.image_status(vm_name) print _("Completed") print _("Attaching Ports To VM Service interfaces") south_net = service_nets.sbnet_id attachment = client.show_port_attachment(south_net, new_port_id) attachment = attachment[servconts.ATTACHMENT][servconts.ID][:36] LOG.debug(_("Plugging virtual interface: %(attachment)s of VM " "%(vm_name)s into port: %(new_port_id)s on network: " "%(sbnet_id)s"), {'attachment': attachment, 'vm_name': vm_name, 'new_port_id': new_port_id, 'sbnet_id': service_nets.sbnet_id}) attach_data = {servconts.ATTACHMENT: {servconts.ID: '%s' % attachment}} client.attach_resource(service_nets.sbnet_id, new_port_id, attach_data) print _("Connect VM Ended")
def image_shutdown_verification(self, image_name): """ Verifies that the VM has been properly shutdown """ try: service_args = [] service_args.append(servconts.DESCRIBE_VM_CMD) service_args.append(image_name) counter = 0 flag = False while not flag and counter <= 5: counter = counter + 1 time.sleep(2.5) process = utils.subprocess_popen(service_args, stdout=subprocess.PIPE) result = process.stdout.readlines() if not result: flag = True except Exception, exc: print exc
def connect_vm(tenant_id, vm_image_id, service_instance_id, *args): """ Starts a VMs and is connected to southbound network """ l2db.initialize() client = Client(HOST, PORT, USE_SSL, format='json', tenant=tenant_id) print("Connecting %s to Service %s " % (vm_image_id, service_instance_id)) service_logic = servlogcs.ServicesLogistics() service_nets = sdb.get_service_bindings(service_instance_id) client.create_port(service_nets.mngnet_id) client.create_port(service_nets.nbnet_id) sb_port_id = client.create_port(service_nets.sbnet_id) LOG.debug("Operation 'create_port' executed.") new_port_id = sb_port_id[servconts.PORT][servconts.ID] try: create_vm_args = [] create_vm_args.append(servconts.CREATE_VM_CMD) create_vm_args.append(vm_image_id) print("Creating VM with image: %s" % (vm_image_id)) process = utils.subprocess_popen(create_vm_args, stdout=subprocess.PIPE) result = process.stdout.readlines() tokens = re.search("i-[a-f0-9]*", str(result[1])) vm_name = tokens.group(0) print("Image: %s instantiated successfully" % (vm_name)) except Exception as exc: print exc service_logic.image_status(vm_name) print "Completed" print "Attaching Ports To VM Service interfaces" south_net = service_nets.sbnet_id attachment = client.show_port_attachment(south_net, new_port_id) attachment = attachment[servconts.ATTACHMENT][servconts.ID][:36] LOG.debug(("Plugging virtual interface: %s of VM %s " "into port: %s on network: %s") % (attachment, vm_name, new_port_id, service_nets.sbnet_id)) attach_data = {servconts.ATTACHMENT: {servconts.ID: '%s' % attachment}} client.attach_resource(service_nets.sbnet_id, new_port_id, attach_data) print("Connect VM Ended")
def image_status(self, image_name): """ Checks the status of the image """ try: service_args = [] service_args.append(servconts.DESCRIBE_VM_CMD) service_args.append(image_name) counter = 0 flag = False while not flag and counter <= 10: counter = counter + 1 time.sleep(2.5) process = utils.subprocess_popen(service_args, stdout=subprocess.PIPE) result = process.stdout.readlines() if result: tokens = re.search("running", str(result[1])) if tokens: service_status = tokens.group(0) if service_status == "running": flag = True except Exception as exc: print exc
def insert_inpath_service(tenant_id, service_image_id, management_net_name, northbound_net_name, southbound_net_name, *args): """Inserting a network service between two networks""" print ("Creating Network for Services and Servers") service_logic = servlogcs.ServicesLogistics() net_list = {} multiport_net_list = [] networks_name_list = [management_net_name, northbound_net_name, southbound_net_name] client = Client(HOST, PORT, USE_SSL, format='json', tenant=tenant_id) for net in networks_name_list: data = {servconts.NETWORK: {servconts.NAME: net}} net_list[net] = client.create_network(data) net_list[net][servconts.PORTS] = [] LOG.debug("Network %s Created with ID: %s " % ( net, net_list[net][servconts.NETWORK][servconts.ID])) print "Completed" print ("Creating Ports on Services and Server Networks") LOG.debug("Operation 'create_port' executed.") if not service_logic.verify_plugin(const.UCS_PLUGIN): for net in networks_name_list: net_list[net][servconts.PORTS].append (client.create_port (net_list[net][servconts.NETWORK][servconts.ID])) LOG.debug("Operation 'create_port' executed.") else: for net in networks_name_list: nets = net_list[net][servconts.NETWORK][servconts.ID] multiport_net_list.append(nets) data = create_multiport(tenant_id, multiport_net_list) net_idx = 0 for net in networks_name_list: port_id = data[servconts.PORTS][net_idx][servconts.ID] net_list[net][servconts.PORTS].append(port_id) LOG.debug("Port UUID: %s on network: %s" % (data[servconts.PORTS][net_idx][servconts.ID], net)) net_idx = net_idx + 1 print "Completed" try: create_vm_args = [] create_vm_args.append(servconts.CREATE_VM_CMD) create_vm_args.append(service_image_id) print ("Creating VM with image: %s" % (service_image_id)) process = utils.subprocess_popen(create_vm_args, stdout=subprocess.PIPE) result = process.stdout.readlines() tokens = re.search("i-[a-f0-9]*", str(result[1])) service_vm_name = tokens.group(0) print ("Image: %s instantiated successfully" % (service_vm_name)) except Exception as exc: print exc service_logic.image_status(service_vm_name) print "Completed" print "Attaching Ports To VM Service interfaces" try: idx = 0 for net in networks_name_list: network_id = net_list[net][servconts.NETWORK][servconts.ID] port_id = net_list[net][servconts.PORTS][idx] attachment = client.show_port_attachment(network_id, port_id) attachment = attachment[servconts.ATTACHMENT][servconts.ID][:36] LOG.debug(("Plugging virtual interface: %s of VM %s" "into port: %s on network: %s") % (attachment, service_vm_name, port_id, net)) attach_data = {servconts.ATTACHMENT: {servconts.ID: '%s' % attachment}} client.attach_resource(network_id, port_id, attach_data) except Exception as exc: print exc print "Completed" try: LOG.debug("Registering Service in DB") l2db.initialize() for uuid_net in db.network_id(networks_name_list[0]): mngnet_id = str(uuid_net.uuid) for uuid_net in db.network_id(networks_name_list[1]): nbnet_id = str(uuid_net.uuid) for uuid_net in db.network_id(networks_name_list[2]): sbnet_id = str(uuid_net.uuid) sdb.add_services_binding(service_vm_name, mngnet_id, nbnet_id, sbnet_id) except Exception as exc: print exc
def insert_inpath_service(tenant_id, service_image_id, management_net_name, northbound_net_name, southbound_net_name, *args): """Inserting a network service between two networks""" print("Creating Network for Services and Servers") service_logic = servlogcs.ServicesLogistics() net_list = {} multiport_net_list = [] networks_name_list = [ management_net_name, northbound_net_name, southbound_net_name ] client = Client(HOST, PORT, USE_SSL, format='json', tenant=tenant_id) for net in networks_name_list: data = {servconts.NETWORK: {servconts.NAME: net}} net_list[net] = client.create_network(data) net_list[net][servconts.PORTS] = [] LOG.debug("Network %s Created with ID: %s " % (net, net_list[net][servconts.NETWORK][servconts.ID])) print "Completed" print("Creating Ports on Services and Server Networks") LOG.debug("Operation 'create_port' executed.") if not service_logic.verify_plugin(const.UCS_PLUGIN): for net in networks_name_list: net_list[net][servconts.PORTS].append (client.create_port( net_list[net][servconts.NETWORK][servconts.ID])) LOG.debug("Operation 'create_port' executed.") else: for net in networks_name_list: nets = net_list[net][servconts.NETWORK][servconts.ID] multiport_net_list.append(nets) data = create_multiport(tenant_id, multiport_net_list) net_idx = 0 for net in networks_name_list: port_id = data[servconts.PORTS][net_idx][servconts.ID] net_list[net][servconts.PORTS].append(port_id) LOG.debug("Port UUID: %s on network: %s" % (data[servconts.PORTS][net_idx][servconts.ID], net)) net_idx = net_idx + 1 print "Completed" try: create_vm_args = [] create_vm_args.append(servconts.CREATE_VM_CMD) create_vm_args.append(service_image_id) print("Creating VM with image: %s" % (service_image_id)) process = utils.subprocess_popen(create_vm_args, stdout=subprocess.PIPE) result = process.stdout.readlines() tokens = re.search("i-[a-f0-9]*", str(result[1])) service_vm_name = tokens.group(0) print("Image: %s instantiated successfully" % (service_vm_name)) except Exception as exc: print exc service_logic.image_status(service_vm_name) print "Completed" print "Attaching Ports To VM Service interfaces" try: idx = 0 for net in networks_name_list: network_id = net_list[net][servconts.NETWORK][servconts.ID] port_id = net_list[net][servconts.PORTS][idx] attachment = client.show_port_attachment(network_id, port_id) attachment = attachment[servconts.ATTACHMENT][servconts.ID][:36] LOG.debug(("Plugging virtual interface: %s of VM %s" "into port: %s on network: %s") % (attachment, service_vm_name, port_id, net)) attach_data = { servconts.ATTACHMENT: { servconts.ID: '%s' % attachment } } client.attach_resource(network_id, port_id, attach_data) except Exception as exc: print exc print "Completed" try: LOG.debug("Registering Service in DB") l2db.initialize() for uuid_net in db.network_id(networks_name_list[0]): mngnet_id = str(uuid_net.uuid) for uuid_net in db.network_id(networks_name_list[1]): nbnet_id = str(uuid_net.uuid) for uuid_net in db.network_id(networks_name_list[2]): sbnet_id = str(uuid_net.uuid) sdb.add_services_binding(service_vm_name, mngnet_id, nbnet_id, sbnet_id) except Exception as exc: print exc