Esempio n. 1
0
    def _deploy_from_hot(self, specification, server_endpoint, base_dir=None):
        accommodation = normalize_accommodation(
            specification.get('accommodation')
            or specification.get('vm_accommodation'))

        agents = generate_agents(self._get_compute_nodes(accommodation),
                                 accommodation, self.stack_name)

        # render template by jinja
        vars_values = {
            'agents': agents,
            'unique': self.stack_name,
        }
        heat_template = utils.read_file(specification['template'],
                                        base_dir=base_dir)
        compiled_template = jinja2.Template(heat_template)
        rendered_template = compiled_template.render(vars_values)
        LOG.debug('Rendered template: %s', rendered_template)

        # create stack by Heat
        try:
            merged_parameters = {
                'server_endpoint': server_endpoint,
                'external_net': self.external_net,
                'image': self.image_name,
                'flavor': self.flavor_name,
                'dns_nameservers': self.dns_nameservers,
            }
        except AttributeError as e:
            LOG.error(
                'Failed to gather required parameters to create '
                'heat stack: %s', e)
            exit(1)
        merged_parameters.update(specification.get('template_parameters', {}))

        self.has_stack = True
        stack_id = heat.create_stack(self.openstack_client.heat,
                                     self.stack_name, rendered_template,
                                     merged_parameters)

        # get info about deployed objects
        outputs = heat.get_stack_outputs(self.openstack_client.heat, stack_id)
        override = self._get_override(specification.get('override'))

        agents = filter_agents(agents, outputs, override)

        if (not self.privileged_mode) and accommodation.get('density', 1) == 1:
            get_host_fn = functools.partial(nova.get_server_host_id,
                                            self.openstack_client.nova)
            agents = distribute_agents(agents, get_host_fn)

        return agents
Esempio n. 2
0
def build_image_with_heat(openstack_client, image_name, flavor_name,
                          dns_nameservers):
    template = None
    template_filename = cfg.CONF.image_builder_template
    try:
        am = lambda f: config.IMAGE_BUILDER_TEMPLATES + '%s.yaml' % f
        template = utils.read_file(template_filename, alias_mapper=am)
    except IOError:
        LOG.error(
            'Error reading template file: %s. '
            'Please verify correctness of --image-builder-template '
            'parameter', template_filename)
        exit(1)
    external_net = (cfg.CONF.external_net
                    or neutron.choose_external_net(openstack_client.neutron))
    stack_name = 'shaker_%s' % uuid.uuid4()
    stack_parameters = {
        'external_net': external_net,
        'flavor': flavor_name,
        'dns_nameservers': dns_nameservers
    }
    stack_id = None
    try:
        stack_id = heat.create_stack(openstack_client.heat, stack_name,
                                     template, stack_parameters)

        outputs = heat.get_stack_outputs(openstack_client.heat, stack_id)
        LOG.debug('Stack outputs: %s', outputs)

        LOG.debug('Waiting for server to shutdown')
        server_id = outputs['server_info'].get('id')
        nova.wait_server_shutdown(openstack_client.nova, server_id)

        LOG.debug('Making snapshot')
        openstack_client.nova.servers.create_image(server_id, image_name)

        LOG.debug('Waiting for server to snapshot')
        nova.wait_server_snapshot(openstack_client.nova, server_id)

        LOG.info('Created image: %s', image_name)
    except BaseException as e:
        if isinstance(e, KeyboardInterrupt):
            LOG.info('Caught SIGINT. Terminating')
        else:
            error_msg = 'Error while building the image: %s' % e
            LOG.error(error_msg)
            LOG.exception(e)
    finally:
        if stack_id and cfg.CONF.cleanup_on_error:
            LOG.debug('Cleaning up the stack: %s', stack_id)
            openstack_client.heat.stacks.delete(stack_id)
Esempio n. 3
0
    def _deploy_from_hot(self, specification, server_endpoint, base_dir=None):
        accommodation = normalize_accommodation(
            specification.get('accommodation') or
            specification.get('vm_accommodation'))

        agents = generate_agents(self._get_compute_nodes(accommodation),
                                 accommodation, self.stack_name)

        # render template by jinja
        vars_values = {
            'agents': agents,
            'unique': self.stack_name,
        }
        heat_template = utils.read_file(specification['template'],
                                        base_dir=base_dir)
        compiled_template = jinja2.Template(heat_template)
        rendered_template = compiled_template.render(vars_values)
        LOG.debug('Rendered template: %s', rendered_template)

        # create stack by Heat
        merged_parameters = {
            'server_endpoint': server_endpoint,
            'external_net': self.external_net,
            'image': self.image_name,
            'flavor': self.flavor_name,
            'dns_nameservers': self.dns_nameservers,
        }
        merged_parameters.update(specification.get('template_parameters', {}))

        self.has_stack = True
        stack_id = heat.create_stack(
            self.openstack_client.heat, self.stack_name, rendered_template,
            merged_parameters)

        # get info about deployed objects
        outputs = heat.get_stack_outputs(self.openstack_client.heat, stack_id)
        override = self._get_override(specification.get('override'))

        agents = filter_agents(agents, outputs, override)

        if (not self.privileged_mode) and accommodation.get('density', 1) == 1:
            get_host_fn = functools.partial(nova.get_server_host_id,
                                            self.openstack_client.nova)
            agents = distribute_agents(agents, get_host_fn)

        return agents
Esempio n. 4
0
    def _deploy_support_stacks(self, support_templates, base_dir):
        for stack in support_templates:
            try:
                support_name = stack['name']
                support_template = utils.read_file(stack['template'],
                                                   base_dir=base_dir)

                support_env_file = stack.get('env_file', None)
                if support_env_file is not None:
                    support_env_file = self._render_env_template(
                        support_env_file, base_dir)

                # user should set default values in supoort template
                # or provide a heat environment file to update
                # parameters for support templates
                support_template_params = {}

                support_id = heat.create_stack(self.openstack_client.heat,
                                               support_name, support_template,
                                               support_template_params,
                                               support_env_file)

                # track support stacks for cleanup
                current_stack = self.TrackStack(name=support_name,
                                                id=support_id)
                self.support_stacks.append(current_stack)
                LOG.debug('Tracking support stacks: %s', self.support_stacks)

            except heat.exc.Conflict as err:
                # continue even if support stack already exists. This
                # allows re-use of existing support stacks if multiple
                # runs reference the same support stack.
                LOG.info('Ignoring stack exists errors: %s', err)
                # clear the exception so polling heat later doesn't
                # continue to show the exception in the logs
                if sys.version_info < (3, 0):
                    sys.exc_clear()

            except heat.exc.StackFailure as err:
                self.stackid = err.args[0]
                raise
Esempio n. 5
0
    def _deploy_from_hot(self, specification, base_dir=None):
        agents = generate_agents(
            nova.get_available_compute_nodes(self.openstack_client.nova),
            specification.get('accommodation') or
            specification.get('vm_accommodation'),
            self.stack_name)

        # render template by jinja
        vars_values = {
            'agents': agents,
            'unique': self.stack_name,
        }
        heat_template = utils.read_file(specification['template'],
                                        base_dir=base_dir)
        compiled_template = jinja2.Template(heat_template)
        rendered_template = compiled_template.render(vars_values)
        LOG.debug('Rendered template: %s', rendered_template)

        # create stack by Heat
        merged_parameters = {
            'server_endpoint': self.server_endpoint,
            'external_net': self.external_net,
            'image': self.image_name,
            'flavor': self.flavor_name,
        }
        merged_parameters.update(specification.get('template_parameters', {}))

        stack_id = heat.create_stack(
            self.openstack_client.heat, self.stack_name, rendered_template,
            merged_parameters)
        self.stack_created = True

        # get info about deployed objects
        outputs = heat.get_stack_outputs(self.openstack_client.heat, stack_id)
        override = self._get_override(specification.get('override'))

        return filter_agents(agents, outputs, override)
Esempio n. 6
0
    def _deploy_from_hot(self, specification, base_dir=None):
        agents = generate_agents(
            nova.get_available_compute_nodes(self.openstack_client.nova),
            specification.get('accommodation')
            or specification.get('vm_accommodation'), self.stack_name)

        # render template by jinja
        vars_values = {
            'agents': agents,
            'unique': self.stack_name,
        }
        heat_template = utils.read_file(specification['template'],
                                        base_dir=base_dir)
        compiled_template = jinja2.Template(heat_template)
        rendered_template = compiled_template.render(vars_values)
        LOG.debug('Rendered template: %s', rendered_template)

        # create stack by Heat
        merged_parameters = {
            'server_endpoint': self.server_endpoint,
            'external_net': self.external_net,
            'image': self.image_name,
            'flavor': self.flavor_name,
        }
        merged_parameters.update(specification.get('template_parameters', {}))

        stack_id = heat.create_stack(self.openstack_client.heat,
                                     self.stack_name, rendered_template,
                                     merged_parameters)
        self.stack_created = True

        # get info about deployed objects
        outputs = heat.get_stack_outputs(self.openstack_client.heat, stack_id)
        override = self._get_override(specification.get('override'))

        return filter_agents(agents, outputs, override)
Esempio n. 7
0
def build_image():
    openstack_client = init()
    flavor_name = cfg.CONF.flavor_name
    image_name = cfg.CONF.image_name
    dns_nameservers = cfg.CONF.dns_nameservers

    if nova.does_flavor_exist(openstack_client.nova, flavor_name):
        LOG.info('Using existing flavor: %s', flavor_name)
    else:
        try:
            nova.create_flavor(openstack_client.nova, name=flavor_name,
                               ram=cfg.CONF.flavor_ram,
                               vcpus=cfg.CONF.flavor_vcpus,
                               disk=cfg.CONF.flavor_disk)
            LOG.info('Created flavor %s', flavor_name)
        except nova.ForbiddenException:
            LOG.error('User does not have permissions to create the flavor. '
                      'Specify user with admin privileges or specify existing '
                      'flavor via --flavor-name parameter.')
            exit(1)

    if glance.get_image(openstack_client.glance, image_name):
        LOG.info('Using existing image: %s', image_name)
    else:
        template = None
        template_filename = cfg.CONF.image_builder_template
        try:
            am = lambda f: config.IMAGE_BUILDER_TEMPLATES + '%s.yaml' % f
            template = utils.read_file(template_filename, alias_mapper=am)
        except IOError:
            LOG.error('Error reading template file: %s. '
                      'Please verify correctness of --image-builder-template '
                      'parameter', template_filename)
            exit(1)

        external_net = (cfg.CONF.external_net or
                        neutron.choose_external_net(openstack_client.neutron))
        stack_name = 'shaker_%s' % uuid.uuid4()
        stack_parameters = {'external_net': external_net,
                            'flavor': flavor_name,
                            'dns_nameservers': dns_nameservers}

        stack_id = None

        try:
            stack_id = heat.create_stack(openstack_client.heat, stack_name,
                                         template, stack_parameters)

            outputs = heat.get_stack_outputs(openstack_client.heat, stack_id)
            LOG.debug('Stack outputs: %s', outputs)

            LOG.debug('Waiting for server to shutdown')
            server_id = outputs['server_info'].get('id')
            nova.wait_server_shutdown(openstack_client.nova, server_id)

            LOG.debug('Making snapshot')
            openstack_client.nova.servers.create_image(
                server_id, image_name)

            LOG.debug('Waiting for server to snapshot')
            nova.wait_server_snapshot(openstack_client.nova, server_id)

            LOG.info('Created image: %s', image_name)
        except BaseException as e:
            if isinstance(e, KeyboardInterrupt):
                LOG.info('Caught SIGINT. Terminating')
            else:
                error_msg = 'Error while building the image: %s' % e
                LOG.error(error_msg)
                LOG.exception(e)
        finally:
            if stack_id and cfg.CONF.cleanup_on_error:
                LOG.debug('Cleaning up the stack: %s', stack_id)
                openstack_client.heat.stacks.delete(stack_id)