def delete_vm(vm: data.VM) -> None: """ Delete the specific VM. :param vm: A VM instance. :return: None. """ server = vm.server if vm.config.is_double: server.A_cpu_rest += vm.config.cpu // 2 server.B_cpu_rest += vm.config.cpu // 2 server.A_memory_rest += vm.config.memory // 2 server.B_memory_rest += vm.config.memory // 2 server.AB_vm.remove(vm) elif vm.node == 'A': server.A_cpu_rest += vm.config.cpu server.A_memory_rest += vm.config.memory server.A_vm.remove(vm) else: server.B_cpu_rest += vm.config.cpu server.B_memory_rest += vm.config.memory server.B_vm.remove(vm) vm.server = None vm.node = None
def deploy_vm(vm: data.VM, server: data.Server, server_node: str = None) -> None: """ Deploy the specific VM on the specific Server. :param vm: A VM instance. :param server: A Server instance. :param server_node: The value must be 'A' or 'B'. If the VM is deployed on both nodes 'AB', the value will be ignored. :return: None. """ if not vm.config.is_double and server_node not in ['A', 'B']: raise ValueError('You must specify the server node.') if not has_capacity(server=server, server_node=server_node, vm=vm): raise ValueError('The Server has no capacity for the VM.') if vm.config.is_double: server.A_cpu_rest -= vm.config.cpu // 2 server.B_cpu_rest -= vm.config.cpu // 2 server.A_memory_rest -= vm.config.memory // 2 server.B_memory_rest -= vm.config.memory // 2 server.AB_vm.append(vm) vm.server = server deploy = data.Deploy(vm=vm, to_server=server) data.Operation_List[-1].deploy.append(deploy) elif server_node == 'A': server.A_cpu_rest -= vm.config.cpu server.A_memory_rest -= vm.config.memory server.A_vm.append(vm) vm.server = server vm.node = 'A' deploy = data.Deploy(vm=vm, to_server=server, to_node='A') data.Operation_List[-1].deploy.append(deploy) else: server.B_cpu_rest -= vm.config.cpu server.B_memory_rest -= vm.config.memory server.B_vm.append(vm) vm.server = server vm.node = 'B' deploy = data.Deploy(vm=vm, to_server=server, to_node='B') data.Operation_List[-1].deploy.append(deploy)
def tcpdump_vm(args): try: vm = VM.get(VM.id == args['id']) except Exception: print >> sys.stderr, 'Error: get vm from db failed' return try: host = HostDevice.get(HostDevice.ip == vm.launch_server) except Exception: print >> sys.stderr, \ 'Error: get launch_server %s from db failed' % vm.launch_server return try: vif = VInterface.get(VInterface.devicetype == VIF_DEVICE_TYPE_VM, VInterface.deviceid == args['id'], VInterface.ifindex == args['if_index']) except Exception: print >> sys.stderr, 'Error: get vif if_index=%s from db failed' % \ args['if_index'] return cmd = 'sshpass -p %s ssh %s %s@%s "ovs-vsctl --bare -- --columns=name ' \ 'find interface \'external_ids:attached-mac=\\\"%s\\\"\'"' % \ (host.user_passwd, SSH_OPTION_STRING, host.user_name, host.ip, vif.mac) rc, output = commands.getstatusoutput(cmd) if rc: print >> sys.stderr, \ 'Error: cmd [%s] failed, rc=%d out=%s' % \ (cmd.replace(host.user_passwd, '*'), rc, output) return if_name = output tcpdump_cmds = [ 'tcpdump', '-i', if_name, '-l', '-nn', '-e', '-v', '-c', args['count'], args['filter'] ] print ' '.join(tcpdump_cmds) for line in call_system_streaming([ '/usr/bin/timeout', args['timeout'], '/usr/bin/sshpass', '-p', host.user_passwd, 'ssh', ] + SSH_OPTION_LIST + ['%s@%s' % (host.user_name, host.ip)] + tcpdump_cmds, ignore_err=False): print line, # do not print duplicate newline
def tcpdump_vm(args): try: vm = VM.get(VM.id == args['id']) except Exception: print >> sys.stderr, 'Error: get vm from db failed' return try: host = HostDevice.get(HostDevice.ip == vm.launch_server) except Exception: print >> sys.stderr, \ 'Error: get launch_server %s from db failed' % vm.launch_server return try: vif = VInterface.get( VInterface.devicetype == VIF_DEVICE_TYPE_VM, VInterface.deviceid == args['id'], VInterface.ifindex == args['if_index']) except Exception: print >> sys.stderr, 'Error: get vif if_index=%s from db failed' % \ args['if_index'] return cmd = 'sshpass -p %s ssh %s %s@%s "ovs-vsctl --bare -- --columns=name ' \ 'find interface \'external_ids:attached-mac=\\\"%s\\\"\'"' % \ (host.user_passwd, SSH_OPTION_STRING, host.user_name, host.ip, vif.mac) rc, output = commands.getstatusoutput(cmd) if rc: print >> sys.stderr, \ 'Error: cmd [%s] failed, rc=%d out=%s' % \ (cmd.replace(host.user_passwd, '*'), rc, output) return if_name = output tcpdump_cmds = [ 'tcpdump', '-i', if_name, '-l', '-nn', '-e', '-v', '-c', args['count'], args['filter']] print ' '.join(tcpdump_cmds) for line in call_system_streaming( ['/usr/bin/timeout', args['timeout'], '/usr/bin/sshpass', '-p', host.user_passwd, 'ssh', ] + SSH_OPTION_LIST + ['%s@%s' % (host.user_name, host.ip)] + tcpdump_cmds, ignore_err=False): print line, # do not print duplicate newline
def find_vif_by_id(vifid): try: vif = VInterface.get(VInterface.id == vifid) except Exception: return (None, None, None, 'Can not find vif id=%d in db' % vifid) try: if vif.devicetype == VIF_DEVICE_TYPE_VM: ins = VM.get(VM.id == vif.deviceid) host = HostDevice.get(HostDevice.ip == ins.launch_server) elif vif.deviceid == VIF_DEVICE_TYPE_VGATEWAY: ins = VGateway.get(VGateway.id == vif.deviceid) host = HostDevice.get(HostDevice.ip == ins.gw_launch_server) else: return (None, None, vif, 'Can not find device/host of %r' % vif.mac) except Exception: return (None, None, vif, 'Can not find device/host of %r' % vif.mac) return (host, ins, vif, '')
def vif_conifg(args): assert isinstance(args, dict) assert args['devicetype'] in VINTERFACE_DEVICETYPE.keys() devicetype = VINTERFACE_DEVICETYPE[args['devicetype']] try: vif = VInterface.get((VInterface.devicetype == devicetype) & (VInterface.deviceid == args['deviceid']) & (VInterface.ifindex == args['ifindex'])) except Exception: print >>sys.stderr, 'Error: vif not found' return -1 try: if args['devicetype'] == 'VM': vdevice = VM.get(VM.id == args['deviceid']) else: vdevice = VGateway.get(VGateway.id == args['deviceid']) except Exception: print >>sys.stderr, 'Error: VM/VGATEWAY %s not found' % \ args['deviceid'] return -1 launch_server = vdevice.launch_server if args['devicetype'] == 'VM' else \ vdevice.gw_launch_server try: host = HostDevice.get(HostDevice.ip == launch_server) except Exception: print >>sys.stderr, 'Error: Host device %s not found' % launch_server return -1 cmd = 'sshpass -p %s ssh -o ConnectTimeout=9 %s@%s ' % (host.user_passwd, host.user_name, launch_server) if 'vlantag' in args: if vif.state != VINTERFACE_STATE_ATTACH: print >>sys.stderr, 'Error: vif is detached' return -1 if vif.iftype != VINTERFACE_TYPE_WAN: print >>sys.stderr, 'Error: vif is not WAN' return -1 if args['devicetype'] == 'VM': cmd += 'sh /usr/local/livecloud/pyagexec/script/vport.sh UPDATE '\ 'vlantag %s %s' % (vif.mac, args['vlantag']) else: cmd += 'sh /usr/local/livegate/script/router.sh update '\ 'vlantag %s %s' % (vif.mac, args['vlantag']) rc, output = commands.getstatusoutput(cmd) if rc: print >>sys.stderr, 'Error: "%s" failed' % cmd print >>sys.stderr, 'Error: %s' % output vif_syslog( viftype=vif.iftype, devicetype=args['devicetype'], deviceid=vif.deviceid, ifindex=vif.ifindex, vifid=vif.id, loginfo='vlantag config failed', level=VINTERFACE_CONFIG_VLANTAG_LEVEL) return -1 ips = IP.select().where(IP.vifid == vif.id) for ip in ips: ip.vlantag = args['vlantag'] ip.save() old_vlantag = vif.vlantag vif.vlantag = args['vlantag'] vif.save() vif_syslog( viftype=vif.iftype, devicetype=args['devicetype'], deviceid=vif.deviceid, ifindex=vif.ifindex, vifid=vif.id, loginfo='vlantag config successful on launch_server %s,' ' from %d to %s' % (launch_server, old_vlantag, args['vlantag']), level=VINTERFACE_CONFIG_VLANTAG_LEVEL) print "SUCCESS" return if 'broadc_bandw' in args or 'broadc_ceil_bandw' in args: if vif.state != VINTERFACE_STATE_ATTACH: print >>sys.stderr, 'Error: vif is detached' return -1 if args['devicetype'] != 'VM' and vif.iftype != VINTERFACE_TYPE_WAN: print >>sys.stderr, 'Error: vif is not VGATEWAY WAN or VM DATA' return -1 if 'broadc_bandw' not in args: print >>sys.stderr, 'Error: broadc_bandw is also required '\ 'for broadc_ceil_bandw' return -1 if 'broadc_ceil_bandw' not in args: print >>sys.stderr, 'Error: broadc_ceil_bandw is also '\ 'required for broadc_bandw' return -1 if int(args['broadc_bandw']) > int(args['broadc_ceil_bandw']): print >>sys.stderr, 'Error: broadc_bandw cannot be larger '\ 'than broadc_ceil_bandw' return -1 if int(args['broadc_ceil_bandw']) > 0: if args['devicetype'] == 'VM': cmd += 'sh /usr/local/livecloud/pyagexec/script/vport.sh ADD-BROADCAST-QOS '\ '%s %s %s' % (vif.mac, args['broadc_bandw'], args['broadc_ceil_bandw']) else: cmd += 'sh /usr/local/livegate/script/router.sh add broadcast-qos '\ '%s %s %s %s' % (vif.deviceid, vif.ifindex, args['broadc_bandw'], args['broadc_ceil_bandw']) else: if args['devicetype'] == 'VM': cmd += 'sh /usr/local/livecloud/pyagexec/script/vport.sh DEL-BROADCAST-QOS '\ '%s' % vif.mac else: cmd += 'sh /usr/local/livegate/script/router.sh delete broadcast-qos '\ '%s %s' % (vif.deviceid, vif.ifindex) rc, output = commands.getstatusoutput(cmd) if rc: print >>sys.stderr, 'Error: "%s" failed' % cmd print >>sys.stderr, 'Error: %s' % output vif_syslog( viftype=vif.iftype, devicetype=args['devicetype'], deviceid=vif.deviceid, ifindex=vif.ifindex, vifid=vif.id, loginfo='broadcast bandwidth config failed', level=VINTERFACE_CONFIG_BROADC_BANDW_LEVEL) return -1 vif.broadc_bandw = args['broadc_bandw'] vif.broadc_ceil_bandw = args['broadc_ceil_bandw'] vif.save() vif_syslog( viftype=vif.iftype, devicetype=args['devicetype'], deviceid=vif.deviceid, ifindex=vif.ifindex, vifid=vif.id, loginfo='broadcast bandwidth config successful on ' 'launch_server %s' % launch_server, level=VINTERFACE_CONFIG_BROADC_BANDW_LEVEL) print "SUCCESS" return print "ERROR: No config parameter is given"