Ejemplo n.º 1
0
    def start(self):
        self._rnd_sleep()
        self._reserve_ips()
        log_dict(self.env, "Cluster settings:")

        LOG.debug("Running vagrant up...")
        try:
            with log_timing_ctx("vagrant up --no-provision"):
                retry(self.vagrant.up,
                      tries=3,
                      interval=15,
                      provider="opennebula",
                      no_provision=True)
            self.created_at = datetime.utcnow()
            self._log_vm_ips()
        except subprocess.CalledProcessError:
            raise VmCreateError('Failed to create VMs in OpenNebula')
        finally:
            self._print_vagrant_log()

        LOG.debug("Running vagrant provision...")
        try:
            with log_timing_ctx("vagrant provision"):
                self.vagrant.provision()
        except subprocess.CalledProcessError:
            raise VmProvisionError('Failed Ansible provision')
        finally:
            self._print_vagrant_log()

        self._save_reserved_ips()
Ejemplo n.º 2
0
    def get_ssh(self, host):
        def is_alive(conn):
            transport = conn.get_transport()
            if transport and transport.is_active():
                try:
                    transport.send_ignore()  # healthcheck
                    return True
                except EOFError:
                    pass  # dead
            return False

        host = self._provider.vm_names[host]
        if host in self._ssh_connections:
            cached = self._ssh_connections[host]
            if is_alive(cached):
                return cached

        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        with log_timing_ctx("ssh.connect({})".format(host)):
            ssh.connect(self._provider.get_hostname(host),
                        port=self._provider.get_host_ssh_port(host),
                        username=self._provider.ssh_user,
                        key_filename=self._provider.ssh_key)

        self._ssh_connections[host] = ssh
        return ssh
Ejemplo n.º 3
0
    def create(self):
        """
        Create a pipeline. Underneath creates a KD cluster and destroys it
        automatically if any error occurs during that process
        """
        # Do not create cluster if wasn't requested.
        if not self.build_cluster:
            LOG.info('BUILD_CLUSTER flag not passed. Pipeline create '
                     'call skipped.')
            return

        if self.infra_provider.any_vm_exists:
            raise ClusterAlreadyCreated(
                "Cluster is already up. Either perform \"vagrant destroy\" "
                "(or provider alternative) if you want to run tests on new "
                "cluster, or make sure you do not pass BUILD_CLUSTER env "
                "variable if you want run tests on the existing one.")

        try:
            log_begin("Provision", self.name)
            self.infra_provider.start()
            log_end("Provision", self.name)

            log_begin("Pipeline post create hook", self.name)
            with log_timing_ctx("'{}' post_create_hook".format(self.name)):
                self.post_create_hook()
            log_end("Pipeline post create hook", self.name)
        except:
            self.destroy()
            raise
Ejemplo n.º 4
0
        def reserve_ips(ips, net):
            """
            Tries to reserve a random free IP. Retries if any OpenNebula
            related problem occurs. If ips set is empty or became empty
            during the while loop consider there is not enough IPs

            :return: reserved IP
            """
            while ips:
                ip = ips.pop()
                LOG.debug("Trying to hold IP: {}".format(ip))
                try:
                    with log_timing_ctx("net.hold({})".format(ip)):
                        net.hold(ip)
                    return ip
                except (OpenNebulaException, ProtocolError) as e:
                    # It's not possible to distinguish if that was an
                    # arbitrary API error or the IP was concurrently
                    # reserved. We'll consider it's always the latter case
                    LOG.debug("Trouble while holding IP {}:\n{}\nTrying the "
                              "next available IP.".format(ip, repr(e)))

            raise NotEnoughFreeIPs(
                'The number of free IPs became less than requested during '
                'reservation')
Ejemplo n.º 5
0
 def start(self):
     log_dict(self.vagrant.env, "Cluster settings:")
     LOG.debug("Running vagrant provision")
     try:
         with log_timing_ctx("vagrant up (with provision)"):
             self.vagrant.up()
         self.created_at = datetime.utcnow()
     except subprocess.CalledProcessError:
         raise VmCreateError('Failed either to create or provision VMs')
     finally:
         self._print_vagrant_log()
Ejemplo n.º 6
0
 def wrapper(*args, **kwargs):
     if setup:
         try:
             with log_timing_ctx("'{}' setup".format(get_func_fqn(f))):
                 setup(*args, **kwargs)
         except Exception as e:
             msg = "=== Error in test linked setup: ===\n{}"
             LOG.error(msg.format(repr(e)))
             raise
     try:
         f(*args, **kwargs)
     finally:
         if teardown:
             try:
                 with log_timing_ctx("'{}' teardown".format(
                         get_func_fqn(f))):
                     teardown(*args, **kwargs)
             except Exception as e:
                 msg = "=== Error in test linked teardown: ===\n{}"
                 LOG.error(msg.format(repr(e)))
                 raise
Ejemplo n.º 7
0
    def run_test(self, test):
        """
        Runs a given test executing set_up/tear_down before/after a test

        :param test: a callable which represents a test and accepts one a
            KDIntegrationAPI object as a first argument
        """
        test_fqn = get_func_fqn(test)
        log_begin(test_fqn, self.name)

        with log_timing_ctx("Pipeline {} set_up".format(self.name)):
            self.set_up()
        try:
            with log_timing_ctx("Test {}".format(test_fqn)):
                test(self.cluster)
        except Exception:
            trace = format_exception(sys.exc_info())
            LOG.error("Test {} FAILED:\n{}".format(test_fqn, trace))
            raise
        finally:
            with log_timing_ctx("Pipeline {} tear_down".format(self.name)):
                self.tear_down()
            log_end(test_fqn, self.name)
Ejemplo n.º 8
0
 def free_reserved_ips(self):
     """
     Tries to release all IPs reserved within this class object instance
     """
     LOG.debug("Starting release of IP addresses: {}".format(self.reserved))
     for net_name, ip_set in self.reserved.items():
         net = self.pool.get_by_name(net_name)
         for ip in ip_set:
             LOG.debug("Trying to release IP: {}".format(ip))
             try:
                 with log_timing_ctx("net.release({})".format(ip)):
                     net.release(ip)
             except (OpenNebulaException, ProtocolError) as e:
                 LOG.debug("Trouble while releasing IP {}:\n{}\nTrying the "
                           "next one".format(ip, repr(e)))
     LOG.debug("Done release of IP addresses.")