def try_run(client, cmd):
     logger.info("running '%s'", cmd)
     _, stdout, stderr = client.exec_command(cmd)
     exit_status = stdout.channel.recv_exit_status()
     if exit_status:
         raise Exception(
             "Failed to delete image build user on image: %s.  "
             "Exit code: %s." % (stderr.read(), exit_status))
     logger.debug("Stdout: %s", stdout.read())
Beispiel #2
0
def run_all(client, config):
    """
    Test blueprint.
    """
    tests_passed = False
    try:
        setup(client, config)
        verify(client, config)
        tests_passed = True
    finally:
        teardown(client, config)
    if tests_passed:
        logger.info("All tests passed!")
Beispiel #3
0
def verify(client, config):
    """
    Verify that the instances are behaving as expected.
    """
    logger.debug("Running verify on: %s", config)
    config_obj = BlueprintTestConfiguration(config)
    state = get_state(config_obj)
    blueprint_tester = get_blueprint_tester(
        client, config_obj.get_config_dir(),
        config_obj.get_verify_fixture_type(),
        config_obj.get_verify_fixture_options())
    setup_info = SetupInfo(state["setup_info"]["deployment_info"],
                           state["setup_info"]["blueprint_vars"])
    network = client.network.get(state["network_name"])
    service = client.service.get(network, state["service_name"])
    blueprint_tester.verify(network, service, setup_info)
    logger.info("Verify successful!")
    return (service, state["ssh_username"], private_key_path(config_obj))
Beispiel #4
0
def call_with_retries(function, retry_count, retry_delay):
    """
    Calls the given function with retries.  Also handles logging on each retry.
    """
    logger.debug("Calling function: %s with retry count: %s, retry_delay: %s",
                 function, retry_count, retry_delay)
    retry = 0
    while True:
        logger.info("Attempt number: %s", retry)
        retry = retry + 1
        try:
            return function()
        # pylint: disable=broad-except
        except Exception as verify_exception:
            logger.info("Verify exception: %s", verify_exception)
            time.sleep(float(retry_delay))
            if retry > int(retry_count):
                logger.info("Exceeded max retries!  Reraising last exception")
                raise
    def _save(self, mock=False):
        """
        Save: Saves the image after it is configured and checked (must run after configure/check)

        This is internal and not exposed on the command line because you should only save an image
        as part of a full build.  All the other steps here are for debugging and development.
        """
        # 1. Load existing configuration
        state = self._load_state()
        if not self._load_state():
            raise DisallowedOperationException(
                "Called save but found no state at %s.  Call deploy first." %
                (self.state_file))
        logger.debug("Loaded state: %s", state)

        network = self.client.network.get(state["network"])
        if not network:
            raise DisallowedOperationException("Network %s not found!" %
                                               state["network"])
        service = self.client.service.get(network, state["service"])
        if not service:
            raise DisallowedOperationException("Service %s not found!" %
                                               state["service"])
        instances = self.client.service.get_instances(service)
        if not instances:
            raise DisallowedOperationException(
                "No running instances in service %s!" % (state["service"]))
        if len(instances) > 1:
            raise DisallowedOperationException(
                "More than one running instance in service %s!" %
                (state["service"]))
        public_ip = instances[0].public_ip

        # 2. Remove test keys
        if not mock:
            ssh = paramiko.SSHClient()
            ssh_key = paramiko.RSAKey(file_obj=open(state["ssh_private_key"]))
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(hostname=public_ip,
                        username=state["ssh_username"],
                        pkey=ssh_key)

            # This removes all permissions from the temporary user's home directory and sets the
            # account to expire immediately.  We unfortunately can't completely delete this
            # temporary account because we are currently logged in.
            def try_run(client, cmd):
                logger.info("running '%s'", cmd)
                _, stdout, stderr = client.exec_command(cmd)
                exit_status = stdout.channel.recv_exit_status()
                if exit_status:
                    raise Exception(
                        "Failed to delete image build user on image: %s.  "
                        "Exit code: %s." % (stderr.read(), exit_status))
                logger.debug("Stdout: %s", stdout.read())

            try_run(ssh, 'cd /tmp')
            try_run(ssh, 'sudo chmod -R 000 /home/%s/' % state["ssh_username"])
            try_run(ssh,
                    'sudo usermod --expiredate 1 %s' % state["ssh_username"])
            logger.info("Deleted test user: %s", state["ssh_username"])
            ssh.close()

        # 3. Save the image with the correct name
        logger.debug("Saving service %s with name: %s", service.name,
                     self.config.get_image_name())
        image = self.client.image.create(self.config.get_image_name(), service)
        logger.info("Image saved with name: %s", self.config.get_image_name())

        return image