def build_image(): openstack_client = init() flavor_name = cfg.CONF.flavor_name image_name = cfg.CONF.image_name if nova.is_flavor_exists(openstack_client.nova, flavor_name): LOG.info('Using existing flavor: %s', flavor_name) else: openstack_client.nova.flavors.create(name=flavor_name, ram=512, vcpus=1, disk=3) LOG.info('Created flavor %s', flavor_name) 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: template = utils.read_file(template_filename) 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_params = { 'stack_name': 'shaker_%s' % uuid.uuid4(), 'parameters': {'external_net': external_net, 'flavor': flavor_name}, 'template': template, } stack = openstack_client.heat.stacks.create(**stack_params)['stack'] LOG.debug('New stack: %s', stack) heat.wait_stack_completion(openstack_client.heat, stack['id']) 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.debug('Clearing up') openstack_client.heat.stacks.delete(stack['id']) LOG.info('Created image: %s', image_name)
def connect_to_openstack(self, openstack_params, flavor_name, image_name, external_net, dns_nameservers): LOG.debug('Connecting to OpenStack') self.openstack_client = openstack.OpenStackClient(openstack_params) self.flavor_name = flavor_name self.image_name = image_name self.external_net = (external_net or neutron.choose_external_net( self.openstack_client.neutron)) self.stack_name = 'shaker_%s' % utils.random_string() self.dns_nameservers = dns_nameservers
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)
def connect_to_openstack(self, openstack_params, flavor_name, image_name, external_net, dns_nameservers): LOG.debug('Connecting to OpenStack') self.openstack_client = openstack.OpenStackClient(openstack_params) self.flavor_name = flavor_name self.image_name = image_name self.stack_name = 'shaker_%s' % utils.random_string() self.dns_nameservers = dns_nameservers # intiailizing self.external_net last so that other attributes don't # remain uninitialized in case user forgets to create external network self.external_net = (external_net or neutron.choose_external_net( self.openstack_client.neutron))
def connect_to_openstack(self, os_username, os_password, os_tenant_name, os_auth_url, os_region_name, external_net, flavor_name, image_name): LOG.debug('Connecting to OpenStack') self.openstack_client = openstack.OpenStackClient( username=os_username, password=os_password, tenant_name=os_tenant_name, auth_url=os_auth_url, region_name=os_region_name) self.flavor_name = flavor_name self.image_name = image_name self.external_net = (external_net or neutron.choose_external_net( self.openstack_client.neutron)) self.stack_name = 'shaker_%s' % utils.random_string()
def connect_to_openstack(self, os_username, os_password, os_tenant_name, os_auth_url, os_region_name, external_net, flavor_name, image_name, os_cacert, os_insecure): LOG.debug('Connecting to OpenStack') self.openstack_client = openstack.OpenStackClient( username=os_username, password=os_password, tenant_name=os_tenant_name, auth_url=os_auth_url, region_name=os_region_name, cacert=os_cacert, insecure=os_insecure) self.flavor_name = flavor_name self.image_name = image_name self.external_net = (external_net or neutron.choose_external_net( self.openstack_client.neutron)) self.stack_name = 'shaker_%s' % utils.random_string()
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)
def build_image(): openstack_client = init() flavor_name = cfg.CONF.flavor_name image_name = cfg.CONF.image_name if nova.is_flavor_exists(openstack_client.nova, flavor_name): LOG.info('Using existing flavor: %s', flavor_name) else: openstack_client.nova.flavors.create(name=flavor_name, ram=1024, vcpus=1, disk=3) LOG.info('Created flavor %s', flavor_name) 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: template = utils.read_file(template_filename) 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_params = { 'stack_name': 'shaker_%s' % uuid.uuid4(), 'parameters': { 'external_net': external_net, 'flavor': flavor_name }, 'template': template, } stack = openstack_client.heat.stacks.create(**stack_params)['stack'] LOG.debug('New stack: %s', stack) heat.wait_stack_completion(openstack_client.heat, stack['id']) 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.debug('Clearing up') openstack_client.heat.stacks.delete(stack['id']) LOG.info('Created image: %s', image_name)