Пример #1
0
    def make_snapshot(self, name):
        show("Deleting all previous snapshots")
        show.tab()

        self.shutdown(name)

        for snap in self.get_vm(name).snapshots.list():
            if snap.get_description() != 'Active VM':
                show("Deleting snapshot: %s" % snap.get_description())
                snap.delete()

        while len(self.get_vm(name).snapshots.list()) > 1:
            show("Waiting for the deletion to complete.")
            sleep(15)

        show.untab()

        try:
            snapshot = params.Snapshot(description=locals.SNAPSHOT_NAME,
                                       vm=self.get_vm(name))
            self.get_vm(name).snapshots.add(snapshot)
            show("Creating a Snapshot")
            show('Waiting for Snapshot creation to finish')
            while self.get_vm(name).status.state == 'image_locked':
                sleep(15)
        except Exception as e:
            show('Failed to Create a Snapshot:\n%s' % str(e))

        if self.get_snapshot(name, locals.SNAPSHOT_NAME):
            show("Snapshot created: %s" % locals.SNAPSHOT_NAME)

        show.untab()
Пример #2
0
    def build(self, build_args):
        show('Building:')
        show.tab()

        if build_args[0] == 'patch':
            show('Syncing the patches:')
            # TODO: make this generic
            #util.run(['rsync', '-rc', '--delete', '/home/tbabej/Work/patches',
            #          '%s:dev/' % self.ip])

            for patch_id in build_args[1:]:
                show('Applying patch {patch}'.format(patch=patch_id))
                self.cmd("bash labtool/ipa-fun-apply-patch.sh"
                         " {patch} {log}".format(patch=patch_id,
                                                 **self.locals))

        elif build_args[0] == 'branch':
            show('Checking out to {branch}'.format(branch=build_args[1]))
            self.cmd("bash labtool/ipa-fun-checkout.sh"
                     " {branch} {log}".format(branch=build_args[1],
                                              **self.locals))
        else:
            raise RuntimeError("Unknown action: %s" % build_args[0])

        show('Installing build dependencies')
        self.install_build_dependencies()

        show('Applying build workarounds')
        self.apply_build_workarounds()

        show('Building sources')
        self.cmd("bash labtool/ipa-fun-build.sh"
                 " {log}".format(**self.locals))

        show.untab()
Пример #3
0
    def make_snapshot(self, name):
        show("Deleting all previous snapshots")
        show.tab()

        self.shutdown(name)

        for snap in self.api.vms.get(name).snapshots.list():
            if snap.get_description() != 'Active VM':
                show("Deleting snapshot: %s" % snap.get_description())
                snap.delete()

        while len(self.api.vms.get(name).snapshots.list()) > 1:
            show("Waiting for the deletion to complete.")
            sleep(5)

        show.untab()

        try:
            snapshot = params.Snapshot(description=locals.SNAPSHOT_NAME,
                                       vm=self.api.vms.get(name))
            self.api.vms.get(name).snapshots.add(snapshot)
            show("Creating a Snapshot")
            show('Waiting for Snapshot creation to finish')
            while self.api.vms.get(name).status.state == 'image_locked':
                sleep(5)
        except Exception as e:
            show('Failed to Create a Snapshot:\n%s' % str(e))

        if self.get_snapshot(name, locals.SNAPSHOT_NAME):
            show("Snapshot created: %s" % locals.SNAPSHOT_NAME)

        sleep(15)
        show.untab()
Пример #4
0
    def load_vm(self, name, vm=None, start=True, interactive=True, update=True):
        if not vm:
            vm = self.get_vm(name)
        if start:
            vm = self.start(name, vm)

        # Obtain the IP address. It can take a while for the guest agent
        # to start, so we wait 2 minutes here before giving up.
        show('Waiting to obtain IP address')
        show('Press CTRL+C to interrupt and enter manually.')
        if start:
            max_counter = 120
        else:
            max_counter = 2
        counter = 0
        ip = self.get_ip(vm)
        try:
            while ip is None:
                vm = self.get_vm(name)
                ip = self.get_ip(vm)
                counter = counter + 1
                if counter > max_counter:
                    break
                sleep(15)
        except KeyboardInterrupt:
            counter = 100000

        if counter <= max_counter:
            fqdn = vm.get_guest_info().fqdn
            show("IP address of the VM is %s" % ip)
            show("FQDN of the VM is %s" % fqdn)
        else:
            if interactive:
                notify('Enter the IP manually.')
                fqdn = ''
                last_ip_segment = ''

                while not (len(last_ip_segment) > 0 and len(last_ip_segment) < 4):
                    last_ip_segment = raw_input("IP address could not be "
                    "determined. Enter the VM number (no leading zeros):")
                    ip = locals.IP_BASE + last_ip_segment
            else:
                ip = ''
                fqdn = ''

        # Set the VM's description so that it can be identified in WebAdmin
        if fqdn and update:
            vm.set_description(fqdn)
            vm.update()
            show("Description set to %s" % fqdn)

        # Necessary because of RHEV bug
        show("Pinging the VM")
        output, errors, rc = util.run(['ping', '-c', '3', ip])

        show.untab()

        return VM(name=name, backend=self, hostname=fqdn,
                  domain=locals.DOMAIN, ip=ip)
Пример #5
0
    def load_vm(self, name, vm=None):
        if not vm:
            vm = self.get_vm(name)
        vm = self.start(name, vm)

        # Obtain the IP address. It can take a while for the guest agent
        # to start, so we wait 2 minutes here before giving up.
        show('Waiting to obtain IP address')
        show('Press CTRL+C to interrupt and enter manually.')
        counter = 0
        ip = self.get_ip(vm)
        try:
            while ip is None:
                vm = self.get_vm(name)
                ip = self.get_ip(vm)
                counter = counter + 1
                if counter > 40:
                    break
                sleep(15)
        except KeyboardInterrupt:
            counter = 100000

        if counter <= 40:
            fqdn = vm.get_guest_info().fqdn
            show("IP address of the VM is %s" % ip)
            show("FQDN of the VM is %s" % fqdn)
        else:
            notify('Enter the IP manually.')
            fqdn = ''
            last_ip_segment = ''

            while not (len(last_ip_segment) > 0 and len(last_ip_segment) < 4):
                last_ip_segment = raw_input(
                    "IP address could not be "
                    "determined. Enter the VM number (no leading zeros):")
                ip = locals.IP_BASE + last_ip_segment

        # Set the VM's description so that it can be identified in WebAdmin
        if fqdn:
            vm.set_description(fqdn)
            vm.update()
            show("Description set to %s" % fqdn)

        # Necessary because of RHEV bug
        show("Pinging the VM")
        output, errors, rc = util.run(['ping', '-c', '3', ip])

        show.untab()

        return VM(name=name,
                  backend=self,
                  hostname=fqdn,
                  domain=locals.DOMAIN,
                  ip=ip)
Пример #6
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()
        show('Name: %s' % name)
        show('Template: %s' % template)
        show('Memory: %s' % memory)

        tmpl = self.api.templates.get(template)
        if not tmpl:
            raise ValueError('Template does not exist: %s' % template)

        # # 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: %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
Пример #7
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()
        show('Name: %s' % name)
        show('Template: %s' % template)
        show('Memory: %s' % memory)

        tmpl = self.api.templates.get(template)
        if not tmpl:
            raise ValueError('Template does not exist: %s' % template)

        # # 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: %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(2)
        show.untab()
        return vm
Пример #8
0
    def load_vm(self, name):
        self.start(name)

        # Obtain the IP address. It can take a while for the guest agent
        # to start, so we wait 2 minutes here before giving up.
        show('Waiting to obtain IP address')
        show('Press CTRL+C to interrupt and enter manually.')
        counter = 0
        try:
            while self.get_ip(name) is None:
                counter = counter + 1
                if counter > 120:
                    break
                sleep(1)
        except KeyboardInterrupt:
            counter = 100000

        if counter <= 120:
            ip = self.get_ip(name)
            last_ip_segment = ip.split('.')[-1]
            show("IP address of the VM is %s" % ip)
        else:
            notify('Enter the IP manually.')

            last_ip_segment = ''

            while not (len(last_ip_segment) > 0 and len(last_ip_segment) < 4):
                last_ip_segment = raw_input("IP address could not be "
                "determined. Enter the VM number (no leading zeros):")
                ip = locals.IP_BASE + last_ip_segment

        # Update the description
        hostname = util.normalize_hostname(ip)

        # Set the VM's description so that it can be identified in WebAdmin
        vm = self.api.vms.get(name)
        vm.set_description(hostname)
        vm.update()

        show("Description set to %s" % hostname)

        # Necessary because of RHEV bug
        show("Pinging the VM")
        output, errors, rc = util.run(['ping', '-c', '3', ip])

        show.untab()

        return VM(name=name, backend=self, hostname=hostname,
                  domain=locals.DOMAIN, ip=ip)
Пример #9
0
    def revert_to_snapshot(self, name):
        show.tab()

        self.stop(name)

        show('Restoring the snapshot: %s' % locals.SNAPSHOT_NAME)
        snapshot = self.get_snapshot(name, locals.SNAPSHOT_NAME)
        if not snapshot:
            raise ValueError("Snapshot %s does not exist"
                             % locals.SNAPSHOT_NAME)

        snapshot.restore()

        # VM automatically shuts down after creation
        show('Waiting for VM to reach Down status')
        while self.get_vm(name).status.state != 'down':
            sleep(1)
        show.untab()
        return self.load_vm(name)
Пример #10
0
    def revert_to_snapshot(self, name):
        show.tab()

        self.stop(name)

        show('Restoring the snapshot: %s' % locals.SNAPSHOT_NAME)
        snapshot = self.get_snapshot(name, locals.SNAPSHOT_NAME)
        if not snapshot:
            raise ValueError("Snapshot %s does not exist"
                             % locals.SNAPSHOT_NAME)

        snapshot.restore()

        # VM automatically shuts down after creation
        show('Waiting for VM to reach Down status')
        while self.get_vm(name).status.state != 'down':
            sleep(15)
        show.untab()
        return self.load_vm(name)
Пример #11
0
    def run_tests(self):
        show('Testing:')
        show.tab()

        # run the test suite
        show('Configuring VM for tests')
        self.cmd("bash labtool/ipa-fun-setup-tests.sh"
                 " {log}".format(**self.locals))

        show('Running whole test suite')
        ret = self.cmd("bash labtool/ipa-fun-run-tests.sh"
                       " {log}".format(**self.locals),
           allow_failure=True)

        if ret == 0:
            show('PASSED. See {log} for the logs.'.format(**self.locals))
        else:
            show('FAILED. See {log} for the logs.'.format(**self.locals))

        show.untab()
Пример #12
0
    def revert_to_snapshot(self, name):
        show.tab()

        if len(self.get_domain(name).listAllSnapshots()) != 1:
            raise RuntimeError("Incorrect number of snapshots for %s" % name)

        show('Correct number of snapshots for %s' % name)

        snapshot = self.get_domain(name).listAllSnapshots()[0].getName()

        stdout, stderr, rc = util.run([
            'virsh', 'snapshot-revert', '--domain', name, '--snapshotname',
            snapshot, '--force'
        ])

        if rc != 0:
            raise RuntimeError("Could not revert to snapshot for %s" % name)

        show('Revert successful')
        show.untab()
Пример #13
0
    def run_tests(self):
        show('Testing:')
        show.tab()

        # run the test suite
        show('Configuring VM for tests')
        self.cmd("bash labtool/ipa-fun-setup-tests.sh"
                 " {log}".format(**self.locals))

        show('Running whole test suite')
        ret = self.cmd("bash labtool/ipa-fun-run-tests.sh"
                       " {log}".format(**self.locals),
                       allow_failure=True)

        if ret == 0:
            show('PASSED. See {log} for the logs.'.format(**self.locals))
        else:
            show('FAILED. See {log} for the logs.'.format(**self.locals))

        show.untab()
Пример #14
0
    def remove_vm(self, name):
        show('Removing the VM:')
        show.tab()

        vm = self.api.vms.get(name)
        if vm is None:
            show('Could not obtain VM. Probably does not exist.')
            return

        try:
            vm.stop()
            show('Waiting for VM to reach Down status')
        except Exception:
            show('Vm is not running.')
            pass

        while self.api.vms.get(name).status.state != 'down':
            sleep(1)

        vm.delete()
        show('{name} was removed.'.format(name=name))
        show.untab()
Пример #15
0
    def reboot(self, name):
        show('Rebooting the VM:')
        show.tab()

        vm = self.api.vms.get(name)
        vm.shutdown()

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

        if self.api.vms.get(name).status.state != 'up':
            show('Starting VM')
            vm.start()
            show('Waiting for VM to reach Up status')
            while self.api.vms.get(name).status.state != 'up':
                sleep(1)

        show('Waiting for all the services to start')
        sleep(60)

        show.untab()
Пример #16
0
    def reboot(self, name):
        show('Rebooting the VM:')
        show.tab()

        vm = self.get_vm(name)
        vm.shutdown()

        show('Waiting for VM to reach Down status')
        while self.get_vm_state(name, vm) != 'down':
            vm = self.get_vm(name)
            sleep(15)

        if self.get_vm_state(name, vm) != 'up':
            show('Starting VM')
            vm.start()
            show('Waiting for VM to reach Up status')
            while self.get_vm_state(name) != 'up':
                sleep(15)

        show('Waiting for all the services to start')
        sleep(60)

        show.untab()
Пример #17
0
    def reboot(self, name):
        show('Rebooting the VM:')
        show.tab()

        vm = self.get_vm(name)
        vm.shutdown()

        show('Waiting for VM to reach Down status')
        while self.get_vm_state(name, vm) != 'down':
            vm = self.get_vm(name)
            sleep(15)

        if self.get_vm_state(name, vm) != 'up':
            show('Starting VM')
            vm.start()
            show('Waiting for VM to reach Up status')
            while self.get_vm_state(name) != 'up':
                sleep(15)

        show('Waiting for all the services to start')
        sleep(60)

        show.untab()
Пример #18
0
    def remove_vm(self, name):
        show('Removing the VM:')
        show.tab()

        try:
            vm = self.get_vm(name)
        except ValueError:
            show('Could not obtain VM. Probably does not exist.')
            return

        try:
            vm.stop()
            show('Waiting for VM to reach Down status')
        except Exception:
            show('Vm is not running.')
            pass

        while self.get_vm_state(name) != 'down':
            sleep(10)

        vm.delete()
        show('{name} was removed.'.format(name=name))
        show.untab()
Пример #19
0
    def revert_to_snapshot(self, name):
        show.tab()

        if len(self.get_domain(name).listAllSnapshots()) != 1:
            raise RuntimeError("Incorrect number of snapshots for %s" % name)

        show('Correct number of snapshots for %s' % name)

        snapshot = self.get_domain(name).listAllSnapshots()[0].getName()

        stdout, stderr, rc = util.run(['virsh',
                                       'snapshot-revert',
                                       '--domain',
                                       name,
                                       '--snapshotname',
                                       snapshot,
                                       '--force'
                                     ])

        if rc != 0:
            raise RuntimeError("Could not revert to snapshot for %s" % name)

        show('Revert successful')
        show.untab()
Пример #20
0
        try:
            vm.start()
            break
        except Exception, e:
            # vm.start() can fail if disks are not in state 'down'
            show("Skipping error %s" % str(e))
            time.sleep(5)
            pass

    vm.connect()

    # Install selected packages from ipa-devel repo
    if args.ipadevel:
        vm.install_devel_packages(packages=args.ipadevel)

    show.untab()

    if args.build:
        vm.build(args.build)

    # Setup a new hostname
    vm.set_hostname(trust=args.trust)

    if args.install:
        show('Preparing:')

        # Installs FreeIPA packages either from local source or from repository
        vm.install_packages(args.install)

        if args.install[0] == 'ipa':
            vm.prepare_install(args.firewall, args.selinux, args.trust)
Пример #21
0
        try:
            vm.start()
            break
        except Exception, e:
            # vm.start() can fail if disks are not in state 'down'
            show("Skipping error %s" % str(e))
            time.sleep(5)
            pass

    vm.connect()

    # Install selected packages from ipa-devel repo
    if args.ipadevel:
        vm.install_devel_packages(packages=args.ipadevel)

    show.untab()

    if args.build:
        vm.build(args.build)

    # Setup a new hostname
    vm.set_hostname(trust=args.trust)

    if args.install:
        show('Preparing:')

        # Installs FreeIPA packages either from local source or from repository
        vm.install_packages(args.install)

        if args.install[0] == 'ipa':
            vm.prepare_install(args.firewall, args.selinux, args.trust)