def vm_sub(): vm = VMware() folder_path = vm.connect.value("vm.folder_path") name_list = str2list(vm.connect.value("vm.name")) is_serial = vm.connect.value("vm.is_serial") ser_list = str2list(vm.connect.value("vm.serial")) is_eth0 = vm.connect.value("vm.is_eth0") eth0_list = str2list(vm.connect.value("vm.eth0")) is_eth1 = vm.connect.value("vm.is_eth1") eth1_list = str2list(vm.connect.value("vm.eth1")) if len(name_list) != len(ser_list): raise ValueError, '''The length of vm.name "%s" and vm.serial "%s" is not the same''' % (str(name_list), str(ser_list)) for name in name_list: vmx = (folder_path + '/' + name + '/' + name + '.vmx').replace('//', '/') if is_serial != "false": ser = ser_list[name_list.index(name)] vm.sub_vm(vmx, sernum=ser) if is_eth0 != "false": if is_eth0 == "fix": eth0_list = [eth0_list[0]] * len(name_list) eth0 = eth0_list[name_list.index(name)] vm.sub_vm(vmx, eth0net=eth0) if is_eth1 != "false": if is_eth1 == "fix": eth1_list = [eth1_list[0]] * len(name_list) eth1 = eth1_list[name_list.index(name)] vm.sub_vm(vmx, eth1net=eth1)
def vm_sub(): vm = VMware() folder_path = vm.connect.value("vm.folder_path") name_list = str2list(vm.connect.value("vm.name")) is_serial = vm.connect.value("vm.is_serial") ser_list = str2list(vm.connect.value("vm.serial")) is_eth0 = vm.connect.value("vm.is_eth0") eth0_list = str2list(vm.connect.value("vm.eth0")) is_eth1 = vm.connect.value("vm.is_eth1") eth1_list = str2list(vm.connect.value("vm.eth1")) if len(name_list) != len(ser_list): raise ValueError, '''The length of vm.name "%s" and vm.serial "%s" is not the same''' % ( str(name_list), str(ser_list)) for name in name_list: vmx = (folder_path + '/' + name + '/' + name + '.vmx').replace( '//', '/') if is_serial != "false": ser = ser_list[name_list.index(name)] vm.sub_vm(vmx, sernum=ser) if is_eth0 != "false": if is_eth0 == "fix": eth0_list = [eth0_list[0]] * len(name_list) eth0 = eth0_list[name_list.index(name)] vm.sub_vm(vmx, eth0net=eth0) if is_eth1 != "false": if is_eth1 == "fix": eth1_list = [eth1_list[0]] * len(name_list) eth1 = eth1_list[name_list.index(name)] vm.sub_vm(vmx, eth1net=eth1)
def vm_poweroff(): vm = VMware() is_all = vm.connect.value("vm.is_all") is_id = vm.connect.value("vm.is_id") name_list = str2list(vm.connect.value("vm.name")) id_list = str2list(vm.connect.value("vm.id")) if is_all == "true": vm.power_off_vm_all() else: if is_id == "false": for vmname in name_list: vm.power_off_vm_via_vmname(vmname) elif is_id == "true": for vmid in id_list: vm.power_off_vm_via_vmid(vmid)
def vm_vlandel(): #Need power off the related vms firstly vm = VMware() vlan_list = str2list(vm.connect.value("vm.vlan")) vswitch = vm.connect.value("vm.vswitch") for vlan in vlan_list: vm.del_vswitch_portgroup(vswitch, vlan)
def vm_vlanadd(): vm = VMware() vlan_list = str2list(vm.connect.value("vm.vlan")) vswitch = vm.connect.value("vm.vswitch") int_reg = re.compile('\d+') for vlan in vlan_list: vm.add_vswitch_portgroup(vswitch, vlan) vlan_id = int(int_reg.search(vlan).group()) vm.bind_portgroup_vlan(vlan, vlan_id)
def vm_copy(): vm = VMware() folder_path = vm.connect.value("vm.folder_path") src = vm.connect.value("vm.src") dst_list = str2list(vm.connect.value("vm.dst")) is_poweron = vm.connect.value("vm.is_poweron") for dst in dst_list: vm.copy_vm(folder_path, src, dst) vm.reg_vm(folder_path, dst + '/' + dst + '.vmx') if is_poweron == "true": vm.power_on_vm_via_vmname(dst)
def vm_copy(): vm = VMware() folder_path = vm.connect.value("vm.folder_path") src = vm.connect.value("vm.src") dst_list = str2list(vm.connect.value("vm.dst")) is_poweron = vm.connect.value("vm.is_poweron") for dst in dst_list: vm.copy_vm(folder_path, src, dst) vm.reg_vm(folder_path , dst + '/' + dst + '.vmx') if is_poweron == "true": vm.power_on_vm_via_vmname(dst)
def vm_del(): vm = VMware() folder_path = vm.connect.value("vm.folder_path") name_list = str2list(vm.connect.value("vm.name")) is_all = vm.connect.value("vm.is_all") if is_all == "true": vm.power_off_vm_all() vm.unreg_vm_all(folder_path) vm.del_vm_all(folder_path) else: for name in name_list: vm.power_off_vm_via_vmname(name) #disname will transfer to regname in back vm.unreg_vm(folder_path, name) vm.del_vm(folder_path, name)
def telnet_exec(): connect = Expect() ser_list = str2list(connect.value("vm.serial")) user = connect.value("vm.user") passwd = connect.value("vm.passwd") prompt = connect.value("vm.prompt") os.system('''echo ''>%s''' % connect.log_file) for ser in ser_list: connect = Expect() connect.port = ser connect.user = user connect.passwd = passwd connect.prompt = prompt connect.telnet_login() connect.basic_exec() connect.basic_logout()