Beispiel #1
0
 def get_template_id_by_name(self, template_name):
     templates_pool = oca.VmTemplatePool(self.client)
     templates_pool.info()
     for template in templates_pool:
         import pdb
         pdb.set_trace()
         print(template)
Beispiel #2
0
    def test_update(self):
        oca.VmTemplate.allocate(
            self.c, '<VMTEMPLATE><NAME>inttest_update01</NAME>\
            <TEMPLATE/></VMTEMPLATE>')

        tp = oca.VmTemplatePool(self.c)
        tp.info()
        templ = tp.get_by_name('inttest_update01')
        templ.update('MEMORY=1024 CPU=2')
 def new_vminstance(self, name, description, start, image_id, flavor_id, net_list, cloud_config=None, disk_list=None,
                    availability_zone_index=None, availability_zone_list=None):
     """Adds a VM instance to VIM
     Params:
         start: indicates if VM must start or boot in pause mode. Ignored
         image_id,flavor_id: image and flavor uuid
         net_list: list of interfaces, each one is a dictionary with:
             name:
             net_id: network uuid to connect
             vpci: virtual vcpi to assign
             model: interface model, virtio, e2000, ...
             mac_address:
             use: 'data', 'bridge',  'mgmt'
             type: 'virtual', 'PF', 'VF', 'VFnotShared'
             vim_id: filled/added by this function
             #TODO ip, security groups
     Returns the instance identifier
     """
     self.logger.debug(
         "new_vminstance input: image='{}' flavor='{}' nics='{}'".format(image_id, flavor_id, str(net_list)))
     try:
         client = oca.Client(self.user + ':' + self.passwd, self.url)
         listaTemplate = oca.VmTemplatePool(client)
         listaTemplate.info()
         for template in listaTemplate:
             if str(template.id) == str(flavor_id):
                 cpu = ' CPU = "{}"'.format(template.template.cpu)
                 memory = ' MEMORY = "{}"'.format(template.template.memory)
                 context = ' CONTEXT = [NETWORK = "YES",SSH_PUBLIC_KEY = "$USER[SSH_PUBLIC_KEY]" ]'
                 graphics = ' GRAPHICS = [ LISTEN = "0.0.0.0", TYPE = "VNC" ]'
                 disk = ' DISK = [ IMAGE_ID = {}]'.format(image_id)
                 sched_requeriments = ' SCHED_REQUIREMENTS = "CLUSTER_ID={}"'.format(self.config["cluster"]["id"])
                 template_updated = cpu + memory + context + graphics + disk + sched_requeriments
                 networkListVim = oca.VirtualNetworkPool(client)
                 networkListVim.info()
                 network = ""
                 for net in net_list:
                     network_found = False
                     for network_existingInVim in networkListVim:
                         if str(net["net_id"]) == str(network_existingInVim.id):
                             net["vim_id"] = network_existingInVim["id"]
                             network = 'NIC = [NETWORK = "{}",NETWORK_UNAME = "{}" ]'.format(
                                 network_existingInVim.name, network_existingInVim.uname)
                             network_found = True
                             break
                     if not network_found:
                         raise vimconn.vimconnNotFoundException("Network {} not found".format(net["net_id"]))
                     template_updated += network
                 oca.VmTemplate.update(template, template_updated)
                 self.logger.info(
                     "Instanciating in OpenNebula a new VM name:{} id:{}".format(template.name, template.id))
                 vminstance_id = template.instantiate(name=name)
                 return str(vminstance_id), None
         raise vimconn.vimconnNotFoundException("Flavor {} not found".format(flavor_id))
     except Exception as e:
         self.logger.error("Create new vm instance error: " + str(e))
         raise vimconn.vimconnException(e)
 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')
Beispiel #5
0
 def _get_template_pool(self):
     try:
         template_pool = oca.VmTemplatePool(self.oneadmin_client)
         template_pool.info()
         return template_pool
     except ConnectionRefusedError:
         logger.info("""Could not connect to host: {host} via protocol
              {protocol}""".format(host=settings.OPENNEBULA_DOMAIN,
                                   protocol=settings.OPENNEBULA_PROTOCOL))
         raise ConnectionRefusedError
     except:
         raise ConnectionRefusedError
Beispiel #6
0
def create(client, name, memory, cpu, vcpu, number, template_id):
    extra_template = """
    MEMORY = {}
    CPU = {}
    VCPU = {}
    """.format(memory, cpu, vcpu)
    tp = oca.VmTemplatePool(client)
    tp.info()
    template = tp.get_by_id(template_id)
    for i in range(number):
        print("creating node '{}'".format(name))
        template.instantiate(name=name, extra_template=extra_template)
 def get_flavor(self, flavor_id):  # Esta correcto
     """Obtain flavor details from the  VIM"""
     try:
         client = oca.Client(self.user + ':' + self.passwd, self.url)
         listaTemplate = oca.VmTemplatePool(client)
         listaTemplate.info()
         for template in listaTemplate:
             if str(template.id) == str(flavor_id):
                 return {'id': template.id, 'name': template.name}
         raise vimconn.vimconnNotFoundException("Flavor {} not found".format(flavor_id))
     except Exception as e:
         self.logger.error("get flavor " + str(flavor_id) + " error: " + str(e))
         raise vimconn.vimconnException(e)
Beispiel #8
0
def create(client):
    tp = oca.VmTemplatePool(client)
    tp.info()
    template = tp.get_by_id(DEFAULT_TEMPLATE_ID)
    print(f"creating node '{NODENAME}'")

    extra_template = f"""
    MEMORY = {MEM}
    CPU = {CPU}
    VCPU = {CPU}
    """

    template.instantiate(name=NODENAME, extra_template=extra_template)
 def delete_flavor(self, flavor_id):
     """ Deletes a tenant flavor from VIM
         Returns the old flavor_id
     """
     try:
         client = oca.Client(self.user + ':' + self.passwd, self.url)
         listaTemplate = oca.VmTemplatePool(client)
         listaTemplate.info()
         self.logger.info("Deleting VIM flavor DELETE {}".format(self.url))
         for template in listaTemplate:
             if str(template.id) == str(flavor_id):
                 template.delete()
                 return template.id
         raise vimconn.vimconnNotFoundException("Flavor {} not found".format(flavor_id))
     except Exception as e:
         self.logger.error("Delete flavor " + str(flavor_id) + " error: " + str(e))
         raise vimconn.vimconnException(e)
Beispiel #10
0
def get_template(module, client, predicate):
    pool = oca.VmTemplatePool(client)
    # Filter -2 means fetch all templates user can Use
    pool.info(filter=-2)
    found = 0
    found_template = None
    template_name = ''

    for template in pool:
        if predicate(template):
            found = found + 1
            found_template = template
            template_name = template.name

    if found == 0:
        return None
    elif found > 1:
        module.fail_json(msg='There are more templates with name: ' + template_name)
    return found_template
Beispiel #11
0
 def test_delete(self):
     tp = oca.VmTemplatePool(self.c)
     tp.info()
     for tpl in tp:
         if tpl.name.startswith('inttest'):
             tpl.delete()
Beispiel #12
0
 def test_info(self):
     self.client.call = Mock(return_value=self.xml)
     pool = oca.VmTemplatePool(self.client)
     pool.info()
     assert len(pool) == 2
Beispiel #13
0
 def templates_pool(self):
     if self._templates_pool is None:
         self._templates_pool = oca.VmTemplatePool(self.client)
     self._templates_pool.info()
     return self._templates_pool
Beispiel #14
0
 def getVmTemplateByName(self, templateName):
     pool = oca.VmTemplatePool(self.client)
     pool.info()
     return pool.get_by_name(templateName)
Beispiel #15
0
 def getListOfVmTemplates(self):
     pool = oca.VmTemplatePool(self.client)
     pool.info()
     return pool