示例#1
0
    def create_vm(self, name, memory=locals.MEMORY,
                  template=locals.TEMPLATE_NAME):
        """Creates a VM from given parameters and returns its hostname."""

        show('VM creation:')
        show.tab()

        # Set VM's parameters as defined in locals.py
        pars = params.VM(name=name,
                         memory=memory,
                         cluster=self.api.clusters.get(self.cluster),
                         template=util.get_latest_template(self.api, template))

        # locals.HOST can be used to enforce usage of a particular host
        if locals.HOST is not None:
            pars.set_placement_policy(params.VmPlacementPolicy(
                                         host=self.api.hosts.get(locals.HOST),
                                         affinity='pinned'))

        # Check whether the template exist, if so, create the VM
        if util.get_latest_template(self.api, template) is None:
            raise ValueError('Template does not exist.')
        vm = self.api.vms.add(pars)
        show('VM was created from Template successfully')

        # Set corret permissions so that VM can be seen in WebAdmin
        admin_vm_manager_perm = params.Permission(
                                    role=self.api.roles.get('UserVmManager'),
                                    user=self.api.users.get('admin'))

        vm.permissions.add(admin_vm_manager_perm)
        show('Permissions for admin to see VM set')

        # VM automatically shuts down after creation
        show('Waiting for VM to reach Down status')
        while self.api.vms.get(name).status.state != 'down':
            sleep(1)

        return self.load_vm(name)
示例#2
0
    def check_arguments(self, name, template, connect):

        if connect:
            show('Checking whether given VM exists')
            self.get_vm(name)
        else:
            show('Checking whether given template exists')
            if util.get_latest_template(self.api, template) is None:
                raise ValueError('Template %s does not exist' % template)

            show('Checking whether given VM name is not used')
            if self.get_vm(name):
                raise ValueError('Given VM name %s is already used' % name)
示例#3
0
文件: backend.py 项目: tbabej/labtool
    def check_arguments(self, name, template, connect):

        if connect:
            show('Checking whether given VM exists')
            self.get_vm(name)
        else:
            show('Checking whether given template exists')
            if util.get_latest_template(self.api, template) is None:
                raise ValueError('Template %s does not exist' % template)

            show('Checking whether given VM name is not used')
            if self.get_vm(name):
                raise ValueError('Given VM name %s is already used' % name)
示例#4
0
文件: backend.py 项目: tbabej/labtool
    def create_vm(self, name, memory=locals.MEMORY,
                  template=locals.TEMPLATE_NAME):
        """Creates a VM from given parameters and returns its hostname."""

        show('VM creation:')
        show.tab()
        show('Name: %s' % name)
        show('Template: %s' % template)
        show('Memory: %s' % memory)

        tmpl = util.get_latest_template(self.api, template)
        if not tmpl:
            raise ValueError('Template does not exist: %s' % template)

        # Set VM's parameters as defined in locals.py
        pars = params.VM(name=name,
                         memory=memory,
                         cluster=self.api.clusters.get(self.cluster),
                         template=tmpl)

        # locals.HOST can be used to enforce usage of a particular host
        if locals.HOST:
            pars.set_placement_policy(params.VmPlacementPolicy(
                                         host=self.api.hosts.get(locals.HOST),
                                         affinity='pinned'))


        vm = self.api.vms.add(pars)
        show('VM was created from Template successfully')

        # Set corret permissions so that VM can be seen in WebAdmin
        if not self.kerberos:
            admin_vm_manager_perm = params.Permission(
                                        role=self.api.roles.get('UserVmManager'),
                                        user=self.api.users.get('admin'))

            vm.permissions.add(admin_vm_manager_perm)
            show('Permissions for admin to see VM set')

        # VM automatically shuts down after creation
        show('Waiting for VM to reach Down status')
        while self.get_vm_state(name, vm) != 'down':
            vm = self.get_vm(name)
            sleep(15)
        show.untab()
        return vm