def test_hostname_truncated_no_hyphen(self): hostname = "a" * 62 hostname = hostname + "-" + "a" res = utils.sanitize_hostname(hostname) # we trim to 63 and then trim the trailing dash self.assertEqual(62, len(res)) self.assertFalse(res.endswith("-"), "The hostname ends with a -")
def test_hostname_truncated_no_hyphen(self): hostname = "a" * 62 hostname = hostname + '-' + 'a' res = utils.sanitize_hostname(hostname) # we trim to 63 and then trim the trailing dash self.assertEqual(62, len(res)) self.assertFalse(res.endswith('-'), 'The hostname ends with a -')
def _copy_instance(self, context, instance_uuid, new_name, launch=False, new_user_data=None, security_groups=None): # (dscannell): Basically we want to copy all of the information from # instance with id=instance_uuid into a new instance. This is because we # are basically "cloning" the vm as far as all the properties are # concerned. instance_ref = self.db.instance_get_by_uuid(context, instance_uuid) image_ref = instance_ref.get('image_ref', '') if image_ref == '': image_ref = instance_ref.get('image_id', '') if launch: metadata = {'launched_from':'%s' % (instance_ref['uuid'])} else: metadata = {'blessed_from':'%s' % (instance_ref['uuid'])} instance = { 'reservation_id': utils.generate_uid('r'), 'image_ref': image_ref, 'vm_state': vm_states.BUILDING, 'state_description': 'halted', 'user_id': context.user_id, 'project_id': context.project_id, 'launch_time': '', 'instance_type_id': instance_ref['instance_type_id'], 'memory_mb': instance_ref['memory_mb'], 'vcpus': instance_ref['vcpus'], 'root_gb': instance_ref['root_gb'], 'ephemeral_gb': instance_ref['ephemeral_gb'], 'display_name': new_name, 'hostname': utils.sanitize_hostname(new_name), 'display_description': instance_ref['display_description'], 'user_data': new_user_data or '', 'key_name': instance_ref.get('key_name', ''), 'key_data': instance_ref.get('key_data', ''), 'locked': False, 'metadata': metadata, 'availability_zone': instance_ref['availability_zone'], 'os_type': instance_ref['os_type'], 'host': None, } new_instance_ref = self.db.instance_create(context, instance) # (dscannell) We need to reload the instance_ref in order for it to be associated with # the database session of lazy-loading. new_instance_ref = self.db.instance_get(context, new_instance_ref.id) elevated = context.elevated() if security_groups == None: security_groups = self.db.security_group_get_by_instance(context, instance_ref['id']) for security_group in security_groups: self.db.instance_add_security_group(elevated, new_instance_ref['uuid'], security_group['id']) return new_instance_ref
def _populate_instance_names(self, instance, num_instances): """Populate instance display_name and hostname.""" display_name = instance.get('display_name') hostname = instance.get('hostname') if display_name is None: display_name = self._default_display_name(instance['uuid']) instance['display_name'] = display_name if hostname is None and num_instances == 1: # NOTE(russellb) In the multi-instance case, we're going to # overwrite the display_name using the # multi_instance_display_name_template. We need the default # display_name set so that it can be used in the template, though. # Only set the hostname here if we're only creating one instance. # Otherwise, it will be built after the template based # display_name. hostname = display_name instance['hostname'] = utils.sanitize_hostname(hostname)
def _apply_instance_name_template(self, context, instance, index): params = { 'uuid': instance['uuid'], 'name': instance['display_name'], 'count': index + 1, } try: new_name = (CONF.multi_instance_display_name_template % params) except (KeyError, TypeError): LOG.exception(_('Failed to set instance name using ' 'multi_instance_display_name_template.')) new_name = instance['display_name'] updates = {'display_name': new_name} if not instance.get('hostname'): updates['hostname'] = utils.sanitize_hostname(new_name) instance = self.db.instance_update(context, instance['uuid'], updates) return instance
def test_hostname_empty_has_default(self): hostname = u"\u7684" defaultname = "Server-1" self.assertEqual(defaultname, utils.sanitize_hostname(hostname, defaultname))
def test_hostname_has_default(self): hostname = u"\u7684hello" defaultname = "Server-1" self.assertEqual("hello", utils.sanitize_hostname(hostname, defaultname))
def test_hostname_sanitize_characters(self): hostname = "(#@&$!(@*--#&91)(__=+--test-host.example!!.com-0+" self.assertEqual("91----test-host.example.com-0", utils.sanitize_hostname(hostname))
def test_hostname_sanitize_periods(self): hostname = "....test.example.com..." self.assertEqual("test.example.com", utils.sanitize_hostname(hostname))
def test_hostname_empty_no_default(self): hostname = u"\u7684" self.assertEqual("", utils.sanitize_hostname(hostname))
def test_hostname_unicode_sanitization(self): hostname = u"\u7684.test.example.com" self.assertEqual("test.example.com", utils.sanitize_hostname(hostname))
def _copy_instance(self, context, instance, new_name, launch=False, new_user_data=None, security_groups=None, key_name=None, launch_index=0, availability_zone=None): # (OmgLag): Basically we want to copy all of the information from # instance with provided instance into a new instance. This is because # we are basically "cloning" the vm as far as all the properties are # concerned. image_ref = instance.get('image_ref', '') if image_ref == '': image_ref = instance.get('image_id', '') system_metadata = {} for data in instance.get('system_metadata', []): # (dscannell) Do not copy over the system metadata that we setup # on an instance. This is important when doing clone-of-clones. if data['key'] not in ['blessed_from', 'launched_from']: system_metadata[data['key']] = data['value'] metadata = {} # We need to record the launched_from / blessed_from in both the # metadata and system_metadata. It needs to be in the metadata so # that we can we can query the database to support list-blessed # and list-launched operations. It needs to be in the system # metadata so that the manager can access it. if launch: metadata['launched_from'] = '%s' % (instance['uuid']) system_metadata['launched_from'] = '%s' % (instance['uuid']) else: metadata['blessed_from'] = '%s' % (instance['uuid']) system_metadata['blessed_from'] = '%s' % (instance['uuid']) if key_name is None: key_name = instance.get('key_name', '') key_data = instance.get('key_data', '') else: key_pair = self.db.key_pair_get(context, context.user_id, key_name) key_data = key_pair['public_key'] if availability_zone is None: availability_zone = instance['availability_zone'] instance_params = { 'reservation_id': utils.generate_uid('r'), 'image_ref': image_ref, 'ramdisk_id': instance.get('ramdisk_id', ''), 'kernel_id': instance.get('kernel_id', ''), 'vm_state': vm_states.BUILDING, 'user_id': context.user_id, 'project_id': context.project_id, 'launched_at': None, 'instance_type_id': instance['instance_type_id'], 'memory_mb': instance['memory_mb'], 'vcpus': instance['vcpus'], 'root_gb': instance['root_gb'], 'ephemeral_gb': instance['ephemeral_gb'], 'display_name': new_name, 'hostname': utils.sanitize_hostname(new_name), 'display_description': instance['display_description'], 'user_data': new_user_data or '', 'key_name': key_name, 'key_data': key_data, 'locked': False, 'metadata': metadata, 'availability_zone': availability_zone, 'os_type': instance['os_type'], 'host': None, 'system_metadata': system_metadata, 'launch_index': launch_index, 'root_device_name': instance['root_device_name'], 'power_state': power_state.NOSTATE, 'vm_mode': instance['vm_mode'], 'architecture': instance['architecture'], 'access_ip_v4': instance['access_ip_v4'], 'access_ip_v6': instance['access_ip_v6'], 'config_drive': instance['config_drive'], 'default_ephemeral_device': instance['default_ephemeral_device'], 'default_swap_device': instance['default_swap_device'], 'auto_disk_config': instance['auto_disk_config'], # Set disable_terminate on bless so terminate in nova-api barfs on a # blessed instance. 'disable_terminate': not launch, } new_instance = instance_obj.Instance() new_instance.update(instance_params) if security_groups != None: self.sg_api.populate_security_groups(new_instance, security_groups) new_instance.create(context) nw_info = instance['info_cache'].get('network_info') self.db.instance_info_cache_update(context, new_instance['uuid'], {'network_info': nw_info}) # (dscannell) We need to reload the instance reference in order for it to be associated with # the database session of lazy-loading. new_instance = self.db.instance_get(context, new_instance.id) elevated = context.elevated() # Create a copy of all the block device mappings block_device_mappings =\ self.db.block_device_mapping_get_all_by_instance(context, instance['uuid']) block_device_mappings =\ self._parse_block_device_mapping(block_device_mappings) for bdev in block_device_mappings: bdev['instance_uuid'] = new_instance['uuid'] self.db.block_device_mapping_create(elevated, bdev, legacy=False) return new_instance
def test_hostname_too_long(self): hostname = "a" * 64 self.assertEqual(63, len(utils.sanitize_hostname(hostname)))
def test_hostname_with_space(self): hostname = " a b c " self.assertEqual("a-b-c", utils.sanitize_hostname(hostname))
def test_hostname_empty_minus_period(self): hostname = "---..." self.assertEqual("", utils.sanitize_hostname(hostname))
def _copy_instance(self, context, instance_uuid, new_name, launch=False, new_user_data=None, security_groups=None, key_name=None, launch_index=0, availability_zone=None): # (dscannell): Basically we want to copy all of the information from # instance with id=instance_uuid into a new instance. This is because we # are basically "cloning" the vm as far as all the properties are # concerned. instance_ref = self.db.instance_get_by_uuid(context, instance_uuid) image_ref = instance_ref.get('image_ref', '') if image_ref == '': image_ref = instance_ref.get('image_id', '') system_metadata = {} for data in instance_ref.get('system_metadata', []): # (dscannell) Do not copy over the system metadata that we setup # on an instance. This is important when doing clone-of-clones. if data['key'] not in ['blessed_from', 'launched_from']: system_metadata[data['key']] = data['value'] metadata = {} # We need to record the launched_from / blessed_from in both the # metadata and system_metadata. It needs to be in the metadata so # that we can we can query the database to support list-blessed # and list-launched operations. It needs to be in the system # metadata so that the manager can access it. if launch: metadata['launched_from'] = '%s' % (instance_ref['uuid']) system_metadata['launched_from'] = '%s' % (instance_ref['uuid']) else: metadata['blessed_from'] = '%s' % (instance_ref['uuid']) system_metadata['blessed_from'] = '%s' % (instance_ref['uuid']) if key_name is None: key_name = instance_ref.get('key_name', '') key_data = instance_ref.get('key_data', '') else: key_pair = self.db.key_pair_get(context, context.user_id, key_name) key_data = key_pair['public_key'] if availability_zone is None: availability_zone = instance_ref['availability_zone'] instance = { 'reservation_id': utils.generate_uid('r'), 'image_ref': image_ref, 'ramdisk_id': instance_ref.get('ramdisk_id', ''), 'kernel_id': instance_ref.get('kernel_id', ''), 'vm_state': vm_states.BUILDING, 'state_description': 'halted', 'user_id': context.user_id, 'project_id': context.project_id, 'launch_time': '', 'instance_type_id': instance_ref['instance_type_id'], 'memory_mb': instance_ref['memory_mb'], 'vcpus': instance_ref['vcpus'], 'root_gb': instance_ref['root_gb'], 'ephemeral_gb': instance_ref['ephemeral_gb'], 'display_name': new_name, 'hostname': utils.sanitize_hostname(new_name), 'display_description': instance_ref['display_description'], 'user_data': new_user_data or '', 'key_name': key_name, 'key_data': key_data, 'locked': False, 'metadata': metadata, 'availability_zone': availability_zone, 'os_type': instance_ref['os_type'], 'host': None, 'system_metadata': system_metadata, 'launch_index': launch_index, 'root_device_name': instance_ref['root_device_name'], 'power_state': power_state.NOSTATE, # Set disable_terminate on bless so terminate in nova-api barfs on a # blessed instance. 'disable_terminate': not launch, } new_instance_ref = self.db.instance_create(context, instance) nw_info = instance_ref['info_cache'].get('network_info') self.db.instance_info_cache_update(context, new_instance_ref['uuid'], {'network_info': nw_info}) # (dscannell) We need to reload the instance_ref in order for it to be associated with # the database session of lazy-loading. new_instance_ref = self.db.instance_get(context, new_instance_ref.id) elevated = context.elevated() if security_groups == None: security_groups = self.db.security_group_get_by_instance(context, instance_ref['uuid']) for security_group in security_groups: self.db.instance_add_security_group(elevated, new_instance_ref['uuid'], security_group['id']) # Create a copy of all the block device mappings block_device_mappings = self.db.block_device_mapping_get_all_by_instance(context, instance_ref['uuid']) for mapping in block_device_mappings: values = { 'instance_uuid': new_instance_ref['uuid'], 'device_name': mapping['device_name'], 'delete_on_termination': mapping.get('delete_on_termination', True), 'source_type': mapping.get('source_type'), 'destination_type': mapping.get('destination_type'), 'guest_format': mapping.get('guest_format'), 'device_type': mapping.get('device_type'), 'disk_bus': mapping.get('disk_bus'), 'boot_index': mapping.get('boot_index'), 'image_id': mapping.get('image_id'), # The snapshot id / volume id will be re-written once the bless / launch completes. # For now we just copy over the data from the source instance. 'snapshot_id': mapping.get('snapshot_id', None), 'volume_id': mapping.get('volume_id', None), 'volume_size': mapping.get('volume_size', None), 'no_device': mapping.get('no_device', None), 'connection_info': mapping.get('connection_info', None) } self.db.block_device_mapping_create(elevated, values, legacy=False) return new_instance_ref
def test_hostname_sanitize_dashes(self): hostname = "----test.example.com---" self.assertEqual("test.example.com", utils.sanitize_hostname(hostname))
def test_hostname_translate(self): hostname = "<}\x1fh\x10e\x08l\x02l\x05o\x12!{>" self.assertEqual("hello", utils.sanitize_hostname(hostname))
def _copy_instance(self, context, instance_uuid, new_name, launch=False, new_user_data=None, security_groups=None): # (dscannell): Basically we want to copy all of the information from # instance with id=instance_uuid into a new instance. This is because we # are basically "cloning" the vm as far as all the properties are # concerned. instance_ref = self.db.instance_get_by_uuid(context, instance_uuid) image_ref = instance_ref.get('image_ref', '') if image_ref == '': image_ref = instance_ref.get('image_id', '') if launch: metadata = {'launched_from': '%s' % (instance_ref['uuid'])} else: metadata = {'blessed_from': '%s' % (instance_ref['uuid'])} instance = { 'reservation_id': utils.generate_uid('r'), 'image_ref': image_ref, 'vm_state': vm_states.BUILDING, 'state_description': 'halted', 'user_id': context.user_id, 'project_id': context.project_id, 'launch_time': '', 'instance_type_id': instance_ref['instance_type_id'], 'memory_mb': instance_ref['memory_mb'], 'vcpus': instance_ref['vcpus'], 'root_gb': instance_ref['root_gb'], 'ephemeral_gb': instance_ref['ephemeral_gb'], 'display_name': new_name, 'hostname': utils.sanitize_hostname(new_name), 'display_description': instance_ref['display_description'], 'user_data': new_user_data or '', 'key_name': instance_ref.get('key_name', ''), 'key_data': instance_ref.get('key_data', ''), 'locked': False, 'metadata': metadata, 'availability_zone': instance_ref['availability_zone'], 'os_type': instance_ref['os_type'], 'host': None, } new_instance_ref = self.db.instance_create(context, instance) # (dscannell) We need to reload the instance_ref in order for it to be associated with # the database session of lazy-loading. new_instance_ref = self.db.instance_get(context, new_instance_ref.id) elevated = context.elevated() if security_groups == None: security_groups = self.db.security_group_get_by_instance( context, instance_ref['id']) for security_group in security_groups: self.db.instance_add_security_group(elevated, new_instance_ref['uuid'], security_group['id']) return new_instance_ref
def test_hostname_empty_has_default_too_long(self): hostname = u"\u7684" defaultname = "a" * 64 self.assertEqual("a" * 63, utils.sanitize_hostname(hostname, defaultname))