def test_default(self): result = utils.random_name() self.assertIsNotNone(result) self.assertEqual(8, len(result)) result1 = utils.random_name() self.assertIsNotNone(result1) self.assertEqual(8, len(result1)) self.assertNotEqual(result, result1)
def test_with_length(self): result = utils.random_name(12) self.assertIsNotNone(result) self.assertEqual(12, len(result)) result1 = utils.random_name(12) self.assertIsNotNone(result1) self.assertEqual(12, len(result1)) self.assertNotEqual(result, result1)
def __init__(self, name, profile_id, cluster_id, context=None, **kwargs): self.id = kwargs.get('id', None) if name: self.name = name else: self.name = 'node-' + utils.random_name(8) self.physical_id = kwargs.get('physical_id', '') self.profile_id = profile_id self.user = kwargs.get('user', '') self.project = kwargs.get('project', '') self.domain = kwargs.get('domain', '') self.cluster_id = cluster_id self.index = kwargs.get('index', -1) self.role = kwargs.get('role', '') self.init_time = kwargs.get('init_time', None) self.created_time = kwargs.get('created_time', None) self.updated_time = kwargs.get('updated_time', None) self.deleted_time = kwargs.get('deleted_time', None) self.status = kwargs.get('status', self.INIT) self.status_reason = kwargs.get('status_reason', 'Initializing') self.data = kwargs.get('data', {}) self.metadata = kwargs.get('metadata', {}) self.rt = {} if context is not None: if self.user == '': self.user = context.user if self.project == '': self.project = context.project if self.domain == '': self.domain = context.domain self._load_runtime_data(context)
def do_create(self, obj): """Create a container instance using the given profile. :param obj: The node object for this container. :returns: ID of the container instance or ``None`` if driver fails. :raises: `EResourceCreation` """ name = self.properties[self.NAME] if name is None: name = '-'.join([obj.name, utils.random_name()]) params = { 'image': self.properties[self.IMAGE], 'name': name, 'command': self.properties[self.COMMAND], } try: ctx = context.get_service_context(project=obj.project, user=obj.user) dockerclient = self.docker(obj) db_api.node_add_dependents(ctx, self.host.id, obj.id) container = dockerclient.container_create(**params) dockerclient.start(container['Id']) except exc.InternalError as ex: raise exc.EResourceCreation(type='container', message=str(ex)) self.container_id = container['Id'][:36] return self.container_id
def do_create(self, obj): """Create a heat stack using the given node object. :param obj: The node object to operate on. :returns: The UUID of the heat stack created. """ kwargs = { 'stack_name': obj.name + '-' + utils.random_name(8), 'template': self.properties[self.TEMPLATE], 'template_url': self.properties[self.TEMPLATE_URL], 'timeout_mins': self.properties[self.TIMEOUT], 'disable_rollback': self.properties[self.DISABLE_ROLLBACK], 'parameters': self.properties[self.PARAMETERS], 'files': self.properties[self.FILES], 'environment': self.properties[self.ENVIRONMENT], } try: stack = self.orchestration(obj).stack_create(**kwargs) # Timeout = None means we will use the 'default_action_timeout' # It can be overridden by the TIMEOUT profile propertie timeout = None if self.properties[self.TIMEOUT]: timeout = self.properties[self.TIMEOUT] * 60 self.orchestration(obj).wait_for_stack(stack.id, 'CREATE_COMPLETE', timeout=timeout) return stack.id except exc.InternalError as ex: raise exc.EResourceCreation(type='stack', message=ex.message)
def __init__(self, name, profile_id, cluster_id=None, context=None, **kwargs): self.id = kwargs.get('id', None) if name: self.name = name else: self.name = 'node-' + utils.random_name(8) # This is a safe guard to ensure that we have orphan node's cluster # correctly set to an empty string if cluster_id is None: cluster_id = '' self.physical_id = kwargs.get('physical_id', None) self.profile_id = profile_id self.user = kwargs.get('user', '') self.project = kwargs.get('project', '') self.domain = kwargs.get('domain', '') self.cluster_id = cluster_id self.index = kwargs.get('index', -1) self.role = kwargs.get('role', '') self.init_at = kwargs.get('init_at', None) self.created_at = kwargs.get('created_at', None) self.updated_at = kwargs.get('updated_at', None) self.status = kwargs.get('status', consts.NS_INIT) self.status_reason = kwargs.get('status_reason', 'Initializing') self.data = kwargs.get('data', {}) self.metadata = kwargs.get('metadata', {}) self.dependents = kwargs.get('dependents', {}) self.tainted = False self.rt = {} if context is not None: if self.user == '': self.user = context.user_id if self.project == '': self.project = context.project_id if self.domain == '': self.domain = context.domain_id self._load_runtime_data(context)
def __init__(self, name, profile_id, cluster_id=None, context=None, **kwargs): self.id = kwargs.get('id', None) if name: self.name = name else: self.name = 'node-' + utils.random_name(8) # This is a safe guard to ensure that we have orphan node's cluster # correctly set to an empty string if cluster_id is None: cluster_id = '' self.physical_id = kwargs.get('physical_id', None) self.profile_id = profile_id self.user = kwargs.get('user', '') self.project = kwargs.get('project', '') self.domain = kwargs.get('domain', '') self.cluster_id = cluster_id self.index = kwargs.get('index', -1) self.role = kwargs.get('role', '') self.init_at = kwargs.get('init_at', None) self.created_at = kwargs.get('created_at', None) self.updated_at = kwargs.get('updated_at', None) self.status = kwargs.get('status', self.INIT) self.status_reason = kwargs.get('status_reason', 'Initializing') self.data = kwargs.get('data', {}) self.metadata = kwargs.get('metadata', {}) self.rt = {} if context is not None: if self.user == '': self.user = context.user if self.project == '': self.project = context.project if self.domain == '': self.domain = context.domain self._load_runtime_data(context)
def do_create(self, obj): '''Create a stack using the given profile.''' kwargs = { 'stack_name': obj.name + '-' + utils.random_name(8), 'template': self.properties[self.TEMPLATE], 'timeout_mins': self.properties[self.TIMEOUT], 'disable_rollback': self.properties[self.DISABLE_ROLLBACK], 'parameters': self.properties[self.PARAMETERS], 'files': self.properties[self.FILES], 'environment': self.properties[self.ENVIRONMENT], } LOG.info('Creating stack: %s' % kwargs) stack = self.heat(obj).stack_create(**kwargs) self.stack_id = stack.id # Wait for action to complete/fail while not self._check_action_complete(obj, 'CREATE'): scheduler.sleep(1) return stack.id
def do_validate(self, obj): """Validate the stack template used by a node. :param obj: Node object to operate. :returns: True if validation succeeds. :raises: `InvalidSpec` exception is raised if template is invalid. """ kwargs = { 'stack_name': utils.random_name(), 'template': self.properties[self.TEMPLATE], 'template_url': self.properties[self.TEMPLATE_URL], 'parameters': self.properties[self.PARAMETERS], 'files': self.properties[self.FILES], 'environment': self.properties[self.ENVIRONMENT], 'preview': True, } try: self.orchestration(obj).stack_create(**kwargs) except exc.InternalError as ex: msg = _('Failed in validating template: %s') % six.text_type(ex) raise exc.InvalidSpec(message=msg) return True
def do_create(self, obj): '''Create a server using the given profile.''' kwargs = {} for key in self.KEYS: # context is treated as connection parameters if key == self.CONTEXT: continue if self.properties[key] is not None: kwargs[key] = self.properties[key] name_or_id = self.properties[self.IMAGE] if name_or_id is not None: image = self.nova(obj).image_get_by_name(name_or_id) # wait for new version of openstacksdk to fix this kwargs.pop(self.IMAGE) kwargs['imageRef'] = image.id flavor_id = self.properties[self.FLAVOR] flavor = self.nova(obj).flavor_find(flavor_id, False) # wait for new verson of openstacksdk to fix this kwargs.pop(self.FLAVOR) kwargs['flavorRef'] = flavor.id if obj.name is not None: kwargs[self.NAME] = obj.name + '-' + utils.random_name(8) metadata = self.properties[self.METADATA] or {} if obj.cluster_id is not None: metadata['cluster'] = obj.cluster_id kwargs['metadata'] = metadata scheduler_hint = self.properties[self.SCHEDULER_HINTS] if scheduler_hint is not None: kwargs['scheduler_hints'] = scheduler_hint user_data = self.properties[self.USER_DATA] if user_data is not None: ud = encodeutils.safe_encode(user_data) kwargs['user_data'] = encodeutils.safe_decode(base64.b64encode(ud)) networks = self.properties[self.NETWORKS] if networks is not None: for network in networks: net_name_id = network.get(self.NETWORK) if net_name_id: res = self.neutron(obj).network_get(net_name_id) network['uuid'] = res.id del network[self.NETWORK] if network['port'] is None: del network['port'] if network['fixed-ip'] is None: del network['fixed-ip'] kwargs['networks'] = networks LOG.info('Creating server: %s' % kwargs) try: server = self.nova(obj).server_create(**kwargs) self.nova(obj).wait_for_server(server) except Exception as ex: LOG.exception(_('Failed in creating server: %s'), six.text_type(ex)) return False self.server_id = server.id return server.id
def attach(self, cluster, enabled=True): """Routine to be invoked when policy is to be attached to a cluster. :para cluster: The cluster to which the policy is being attached to. :param enabled: The attached cluster policy is enabled or disabled. :returns: When the operation was successful, returns a tuple (True, message); otherwise, return a tuple (False, error). """ res, data = super(AffinityPolicy, self).attach(cluster) if res is False: return False, data data = {'inherited_group': False} nc = self.nova(cluster.user, cluster.project) group = self.properties.get(self.SERVER_GROUP) # guess servergroup name group_name = group.get(self.GROUP_NAME, None) if group_name is None: profile = cluster.rt['profile'] if 'scheduler_hints' in profile.spec: hints = profile.spec['scheduler_hints'] group_name = hints.get('group', None) if group_name: try: server_group = nc.server_group_find(group_name, True) except exc.InternalError as ex: msg = _("Failed in retrieving servergroup '%s'.") % group_name LOG.exception('%(msg)s: %(ex)s', {'msg': msg, 'ex': ex}) return False, msg if server_group: # Check if the policies match policies = group.get(self.GROUP_POLICIES) if policies and policies != server_group.policies[0]: msg = _( "Policies specified (%(specified)s) doesn't match " "that of the existing servergroup (%(existing)s).") % { 'specified': policies, 'existing': server_group.policies[0] } return False, msg data['servergroup_id'] = server_group.id data['inherited_group'] = True if not data['inherited_group']: # create a random name if necessary if not group_name: group_name = 'server_group_%s' % utils.random_name() try: server_group = nc.server_group_create( name=group_name, policies=[group.get(self.GROUP_POLICIES)]) except Exception as ex: msg = _('Failed in creating servergroup.') LOG.exception('%(msg)s: %(ex)s', {'msg': msg, 'ex': ex}) return False, msg data['servergroup_id'] = server_group.id policy_data = self._build_policy_data(data) return True, policy_data
def test_with_bad_length(self): result = utils.random_name(0) self.assertEqual('', result) result = utils.random_name(-9) self.assertEqual('', result)
def attach(self, cluster): """Routine to be invoked when policy is to be attached to a cluster. :para cluster: The target cluster to attach to; :returns: When the operation was successful, returns a tuple (True, message); otherwise, return a tuple (False, error). """ res, data = super(AffinityPolicy, self).attach(cluster) if res is False: return False, data data = {'inherited_group': False} nc = self.nova(cluster) group = self.properties.get(self.SERVER_GROUP) # guess servergroup name group_name = group.get(self.GROUP_NAME, None) if group_name is None: profile = cluster.rt['profile'] if 'scheduler_hints' in profile.spec: hints = profile.spec['scheduler_hints'] group_name = hints.get('group', None) if group_name: try: server_group = nc.find_server_group(group_name, True) except exception.InternalError as ex: msg = _("Failed in retrieving servergroup '%s'." ) % group_name LOG.exception(_LE('%(msg)s: %(ex)s') % { 'msg': msg, 'ex': six.text_type(ex)}) return False, msg if server_group: # Check if the policies match policies = group.get(self.GROUP_POLICIES) if policies and policies != server_group.policies[0]: msg = _("Policies specified (%(specified)s) doesn't match " "that of the existing servergroup (%(existing)s)." ) % {'specified': policies, 'existing': server_group.policies[0]} return False, msg data['servergroup_id'] = server_group.id data['inherited_group'] = True if not data['inherited_group']: # create a random name if necessary if not group_name: group_name = 'server_group_%s' % utils.random_name() try: server_group = nc.create_server_group( name=group_name, policies=[group.get(self.GROUP_POLICIES)]) except Exception as ex: msg = _('Failed in creating servergroup.') LOG.exception(_LE('%(msg)s: %(ex)s') % { 'msg': msg, 'ex': six.text_type(ex)}) return False, msg data['servergroup_id'] = server_group.id policy_data = self._build_policy_data(data) return True, policy_data
def do_create(self, obj): """Create a server using the given profile.""" kwargs = {} for key in self.KEYS: # context is treated as connection parameters if key == self.CONTEXT: continue if self.properties[key] is not None: kwargs[key] = self.properties[key] name_or_id = self.properties[self.IMAGE] if name_or_id is not None: image = self.nova(obj).image_get_by_name(name_or_id) # wait for new version of openstacksdk to fix this kwargs.pop(self.IMAGE) kwargs["imageRef"] = image.id flavor_id = self.properties[self.FLAVOR] flavor = self.nova(obj).flavor_find(flavor_id, False) # wait for new verson of openstacksdk to fix this kwargs.pop(self.FLAVOR) kwargs["flavorRef"] = flavor.id if obj.name is not None: kwargs[self.NAME] = obj.name + "-" + utils.random_name(8) metadata = self.properties[self.METADATA] or {} if obj.cluster_id is not None: metadata["cluster"] = obj.cluster_id kwargs["metadata"] = metadata scheduler_hint = self.properties[self.SCHEDULER_HINTS] if scheduler_hint is not None: kwargs["scheduler_hints"] = scheduler_hint user_data = self.properties[self.USER_DATA] if user_data is not None: ud = encodeutils.safe_encode(user_data) kwargs["user_data"] = encodeutils.safe_decode(base64.b64encode(ud)) networks = self.properties[self.NETWORKS] if networks is not None: for network in networks: net_name_id = network.get(self.NETWORK) if net_name_id: res = self.neutron(obj).network_get(net_name_id) network["uuid"] = res.id del network[self.NETWORK] if network["port"] is None: del network["port"] if network["fixed-ip"] is None: del network["fixed-ip"] kwargs["networks"] = networks if "placement" in obj.data: kwargs["availability_zone"] = obj.data["placement"]["zone"] LOG.info("Creating server: %s" % kwargs) try: server = self.nova(obj).server_create(**kwargs) self.nova(obj).wait_for_server(server) except Exception as ex: LOG.exception(_("Failed in creating server: %s"), six.text_type(ex)) return False self.server_id = server.id return server.id