def iplist(client): vp = oca.VirtualMachinePool(client) vp.info() for vm in vp: name = vm.name ip = list(v.ip for v in vm.template.nics)[0] yield name, ip
def tearDown(self): print("teardown") vmp = oca.VirtualMachinePool(self.c) vmp.info() for vm in vmp: if vm.name.startswith('inttest_vm_'): vm.delete()
def _get_vm_by_ip(self, ip): # type: (str) -> oca.VirtualMachine vm_pool = oca.VirtualMachinePool(self.client) vm_pool.info() for vm in vm_pool: if ip in (n.ip for n in vm.template.nics): return vm raise OpenNebulaException('VM {} not found'.format(ip))
def stopVmById(self, vmId): pool = oca.VirtualMachinePool(self.client) pool.info() vm = pool.get_by_id(vmId) vm.shutdown() while vm.str_state != 'DONE': vm.info() print("%s:%s:%s" % (vm.id, vm.str_state, vm.str_lcm_state)) return vm
def test_instantiate(self): templ = oca.VmTemplate.allocate(self.c, '<VMTEMPLATE><NAME>inttest_instantiate_me01</NAME><MEMORY>1234</MEMORY><CPU>2</CPU></VMTEMPLATE>') tp = oca.VmTemplatePool(self.c) tp.info() templ = tp.get_by_name('inttest_instantiate_me01') templ.instantiate('inttest_vm_instantiate_me01') vmpool = oca.VirtualMachinePool(self.c) vmpool.info() vm = vmpool.get_by_name('inttest_vm_instantiate_me01') self.assertEqual(vm.name, 'inttest_vm_instantiate_me01')
def get_all_vms_by_attributes(client, attributes_dict, labels_list): pool = oca.VirtualMachinePool(client) # Retrieves information for all or part of the vms pool # -4: Vms belonging to the user's primary group # -3: Vms belonging to the user # -2: All vms user can Use # -1: Vms belonging to the user and any of his groups - default # >= 0: UID User's vms pool.info(filter=-2) vm_list = [] name = '' if attributes_dict: name = attributes_dict.pop('NAME', '') if name != '': base_name = name[:len(name) - name.count('#')] # Check does the name have indexed format with_hash = name.endswith('#') for vm in pool: if vm.name.startswith(base_name): if with_hash and vm.name[len(base_name):].isdigit(): # If the name has indexed format and after base_name it has only digits it'll be matched vm_list.append(vm) elif not with_hash and vm.name == name: # If the name is not indexed it has to be same vm_list.append(vm) pool = vm_list import copy vm_list = copy.copy(pool) for vm in pool: vm_labels_list, vm_attributes_dict = get_vm_labels_and_attributes_dict( client, vm.id) if attributes_dict and len(attributes_dict) > 0: for key, val in attributes_dict.items(): if key in vm_attributes_dict: if val and vm_attributes_dict[key] != val and vm in vm_list: vm_list.remove(vm) break else: if vm in vm_list: vm_list.remove(vm) break if labels_list and len(labels_list) > 0: for label in labels_list: if label not in vm_labels_list and vm in vm_list: vm_list.remove(vm) break return vm_list
def refresh_vms_status(self, vm_list): """Refreshes the status of the virtual machines""" vm_dict = {} try: client = oca.Client(self.user + ':' + self.passwd, self.url) vm_pool = oca.VirtualMachinePool(client) vm_pool.info() for vm_id in vm_list: vm = {"interfaces": []} vm_exist = False vm_element = None for i in vm_pool: if str(i.id) == str(vm_id): vm_exist = True vm_element = i break if not vm_exist: self.logger.info("The vm " + str(vm_id) + " does not exist.") vm['status'] = "DELETED" vm['error_msg'] = ("The vm " + str(vm_id) + " does not exist.") continue vm_element.info() vm["vim_info"] = None VMstatus = vm_element.str_lcm_state if VMstatus == "RUNNING": vm['status'] = "ACTIVE" elif "FAILURE" in VMstatus: vm['status'] = "ERROR" vm['error_msg'] = "VM failure" else: vm['status'] = "BUILD" try: for red in vm_element.template.nics: interface = {'vim_info': None, "mac_address": str(red.mac), "vim_net_id": str(red.network_id), "vim_interface_id": str(red.network_id)} # maybe it should be 2 different keys for ip_address if an interface has ipv4 and ipv6 if hasattr(red, 'ip'): interface["ip_address"] = str(red.ip) if hasattr(red, 'ip6_global'): interface["ip_address"] = str(red.ip6_global) vm["interfaces"].append(interface) except Exception as e: self.logger.error("Error getting vm interface_information " + type(e).__name__ + ":" + str(e)) vm["status"] = "VIM_ERROR" vm["error_msg"] = "Error getting vm interface_information " + type(e).__name__ + ":" + str(e) vm_dict[vm_id] = vm return vm_dict except Exception as e: self.logger.error(e) for k in vm_dict: vm_dict[k]["status"] = "VIM_ERROR" vm_dict[k]["error_msg"] = str(e) return vm_dict
def get_vm_by_id(client, vm_id): pool = oca.VirtualMachinePool(client) # Retrieves information for all or part of the vms pool # -4: Vms belonging to the user's primary group # -3: Vms belonging to the user # -2: All vms user can Use # -1: Vms belonging to the user and any of his groups - default # >= 0: UID User's vms pool.info(filter=-2) for vm in pool: if str(vm.id) == str(vm_id): return vm return None
def get_vm_by_id(client, vm_id): pool = oca.VirtualMachinePool(client) # Retrieves information for all or part of the vms pool # -4: Vms belonging to the user's primary group # -3: Vms belonging to the user # -2: All vms user can Use # -1: Vms belonging to the user and any of his groups - default # >= 0: UID User's vms pool.info(filter=-2, range_start=int(vm_id), range_end=int(vm_id)) if len(pool) == 1: return pool[0] else: return None
def destroy(client): vmp = oca.VirtualMachinePool(client=client) vmp.info() destroyed = [] for i in vmp: if i.name == NODENAME: answer = input(f"{NODENAME} exists, going to destroy. is that OK? (Y/n)") if answer.lower().strip() == 'y': print("destroying node '{}'".format(i.name)) i.delete() destroyed.append([v.ip for v in i.template.nics][0]) else: exit(1) return destroyed
def _get_vm_pool(self): try: vm_pool = oca.VirtualMachinePool(self.client) vm_pool.info() return vm_pool except AttributeError: logger.info('Could not connect via client, using oneadmin instead') try: vm_pool = oca.VirtualMachinePool(self.oneadmin_client) vm_pool.info(filter=-2) return vm_pool except: raise ConnectionRefusedError except ConnectionRefusedError: logger.info( 'Could not connect to host: {host} via protocol {protocol}'. format(host=settings.OPENNEBULA_DOMAIN, protocol=settings.OPENNEBULA_PROTOCOL)) raise ConnectionRefusedError # For now we'll just handle all other errors as connection errors except: raise ConnectionRefusedError
def stopVmByIp(self, vmIp): pool = oca.VirtualMachinePool(self.client) pool.info() vms = filter( lambda x:x.template.nics[0].ip == vmIp, list(pool) ) if not vms: return None else: vm = vms[0] vm.shutdown() while vm.str_state != 'DONE': vm.info() print("%s:%s:%s" % (vm.id, vm.str_state, vm.str_lcm_state)) return vm
def __init__(self, secret=None, address=None, proxy=None): if (secret[0] == '/'): secretfile = secret if os.path.isfile(secretfile): try: f = open(secretfile, 'r') secret = f.readlines()[0].strip('\n') f.close() except (IOError, OSError) as e: raise exception.SecretFileError(secretfile, e) else: e = 'secret file does not exist' raise exception.SecretFileError(secretfile, e) self._oca = oca.Client(secret=secret, address=address, proxy=proxy) self._vm_pool = oca.VirtualMachinePool(self._oca) self._image_pool = oca.ImagePool(self._oca)
def active_instance_count(): client = get_client() if not client: logging.error('Cannot connect to OpenNebula RPC endpoint') return None, None vmpool = oca.VirtualMachinePool(client) vmpool.info() n_active_instances = 0 for vm in vmpool: vm.info() if vm.state == 3: n_active_instances += 1 return n_active_instances
def delete_vminstance(self, vm_id, created_items=None): """Removes a VM instance from VIM, returns the deleted vm_id""" try: client = oca.Client(self.user + ':' + self.passwd, self.url) vm_pool = oca.VirtualMachinePool(client) vm_pool.info() vm_exist = False for i in vm_pool: if str(i.id) == str(vm_id): vm_exist = True break if not vm_exist: self.logger.info("The vm " + str(vm_id) + " does not exist or is already deleted") raise vimconn.vimconnNotFoundException("The vm {} does not exist or is already deleted".format(vm_id)) params = '<?xml version="1.0"?> \ <methodCall>\ <methodName>one.vm.recover</methodName>\ <params>\ <param>\ <value><string>{}:{}</string></value>\ </param>\ <param>\ <value><int>{}</int></value>\ </param>\ <param>\ <value><int>{}</int></value>\ </param>\ </params>\ </methodCall>'.format(self.user, self.passwd, str(vm_id), str(3)) r = requests.post(self.url, params) obj = untangle.parse(str(r.content)) response_success = obj.methodResponse.params.param.value.array.data.value[0].boolean.cdata.encode('utf-8') response = obj.methodResponse.params.param.value.array.data.value[1].i4.cdata.encode('utf-8') # response can be the resource ID on success or the error string on failure. response_error_code = obj.methodResponse.params.param.value.array.data.value[2].i4.cdata.encode('utf-8') if response_success.lower() == "true": return response else: raise vimconn.vimconnException("vm {} cannot be deleted with error_code {}: {}".format(vm_id, response_error_code, response)) except Exception as e: self.logger.error("Delete vm instance " + str(vm_id) + " error: " + str(e)) raise vimconn.vimconnException(e)
def wait_for_active(client, destroyed=None): if not destroyed: destroyed = [] vp = oca.VirtualMachinePool(client) while True: print(f"waiting for new {NODENAME} to become active") sleep(1) vp.info() for vm in vp: if vm.name == NODENAME: ip = [v.ip for v in vm.template.nics][0] if vm.state == vm.ACTIVE: # the destroyed machines stay active for a while if ip in destroyed: continue print(f"name: {vm.name} state: {vm.str_state} ip: {ip}") return ip
def clean_opennebula(hours, network, user, pwd, endpoint): now = datetime.datetime.utcnow() delta = datetime.timedelta(hours=hours) client = oca.Client('{user}:{pwd}'.format(user=user, pwd=pwd), 'https://{host}:2633/RPC2'.format(host=endpoint)) vms = oca.VirtualMachinePool(client) vms.info() vns = oca.VirtualNetworkPool(client) vns.info() net = vns.get_by_name(network) net.info() to_delete = [] print("{uid:<6} {name:10} {started}".format(uid="#", name="Name", started="Started")) for vm in vms: text = "{uid:<6} {name:10} {started!s:10}".format( uid=vm.id, name=vm.name, started=datetime.datetime.fromtimestamp(vm.stime)) print(text) if now - datetime.datetime.fromtimestamp(vm.stime) > delta: to_delete.append(vm) for vm in to_delete: # Release cluster IP on hold, if any if getattr(vm.user_template, 'reserved_ips', None): ips = vm.user_template.reserved_ips.split(',') for ip in ips: text = "{} IP on hold - released".format(ip) print(text) client.call('vn.release', net.id, 'LEASES=[IP={}]'.format(ip)) # Delete VM text = "{uid:<6} {name:10} {started!s:10} - deleted".format( uid=vm.id, name=vm.name, started=datetime.datetime.fromtimestamp(vm.stime)) print(text) vm.delete()
def destroy(client): vmp = oca.VirtualMachinePool(client=client) vmp.info() for i in vmp: print("destroying node '{}'".format(i.name)) i.delete()
def getListOfVMs(self): pool = oca.VirtualMachinePool(self.client) pool.info() return pool
def getVmByName(self, vmName): pool = oca.VirtualMachinePool(self.client) pool.info() return pool.get_by_name(vmName)
def ext_pillar(minion_id, pillar, _arg_): ''' Finds OpenNebula VMs which belong to bio class and return their data as dict ''' endpoint = __opts__['opennebula.endpoint'] secret = __opts__['opennebula.secret'] log.debug('endpoint: {0}, secret: {1}'.format(endpoint, secret)) client = oca.Client(secret, endpoint) vm_pool = oca.VirtualMachinePool(client) vm_pool.info(-2) data = {} minion_id_split = minion_id.split('-', 2) user_token = minion_id_split[0] vm_id = minion_id_split[1] # set bio_type, bio_image, username and password for vm in vm_pool: if vm['ID'] == vm_id and vm[ 'TEMPLATE/CONTEXT/USER_TOKEN'] == user_token: if vm['GNAME'] not in TYPE_MAP: return {} data['username'] = vm['UNAME'] random.seed(user_token) data['password'] = ''.join( random.choice(string.ascii_letters + string.digits) for _ in range(10)) data['bio_type'] = TYPE_MAP[vm['GNAME']] bio_image_element = vm['TEMPLATE'].find('CONTEXT/BIO_IMAGE') if bio_image_element is None or isinstance(bio_image_element, (int, long)): continue data['bio_image'] = bio_image_element.text # set nfs ip vm_pool.info(-2) for vm in vm_pool: if vm['GNAME'] not in TYPE_MAP or TYPE_MAP[vm['GNAME']] != 'nfs': continue data['nfs_ip'] = vm.xml.findtext('TEMPLATE/NIC[last()]/IP') break if not data.has_key('bio_type') or data['bio_type'] != 'nfs': return data # set ip addresses and types for all users (only for nfs server) user_data = {} vm_pool.info(-2) for vm in vm_pool: if vm['GNAME'] not in TYPE_MAP or TYPE_MAP[vm['GNAME']] == 'nfs': continue username = vm['UNAME'] ips = [] for nic in vm.xml.findall('TEMPLATE/NIC'): ips.append(nic.find('IP').text) if user_data.has_key(username): user_data[username]['ip_addresses'].extend(ips) else: user_data[username] = {} user_data[username]['ip_addresses'] = ips user_data[username]['bio_type'] = TYPE_MAP[vm['GNAME']] data['user_data'] = user_data return data
def test_wrong_get_by_name(self): self.client.call = Mock(return_value=self.xml) pool = oca.VirtualMachinePool(self.client) pool.info() pool.get_by_name('wrong-vm-name')
def test_wrong_get_by_id(self): self.client.call = Mock(return_value=self.xml) pool = oca.VirtualMachinePool(self.client) pool.info() pool.get_by_id(1010011010)
def test_get_by_name(self): self.client.call = Mock(return_value=self.xml) pool = oca.VirtualMachinePool(self.client) pool.info() assert pool.get_by_name('vm-in').name == 'vm-in'
def test_get_by_id(self): self.client.call = Mock(return_value=self.xml) pool = oca.VirtualMachinePool(self.client) pool.info() assert pool.get_by_id(8).id == 8
def test_iterate_before_info(self): pool = oca.VirtualMachinePool(self.client) assert list(pool) == []
def test_vm_pool_info(self): self.client.call = Mock(return_value=self.xml) pool = oca.VirtualMachinePool(self.client) pool.info() assert len(list(pool)) == 3
def vm_pool(self): if self._vm_pool is None: self._vm_pool = oca.VirtualMachinePool(self.client) # Update information to retrieve latest data on each call self._vm_pool.info(filter=self._filter) return self._vm_pool
def __init__(self, secret=None, address=None, proxy=None): self._oca = oca.Client(secret=secret, address=address, proxy=proxy) self._vm_pool = oca.VirtualMachinePool(self._oca)
def test_deploy(self): vms = oca.VirtualMachinePool(self.c) vms.info() for vm in vms: if vm.name.startswith('inttest_vm_'): vm.deploy()