def create_charm_class(servicename, service_dict, servicemeta): # some attempts to guess at subordinate status from bundle format, # to avoid having to include it in metadata: # This doesn't work because bundles with no machines might # just use the juju default: is_subordinate = 'to' not # in service_dict.keys() is_subordinate = service_dict['num_units'] == 0 charm = Charm(charm_name=servicename, display_name=servicemeta.get('display-name', servicename), constraints=servicemeta.get('constraints', {}), depends=servicemeta.get('depends', []), conflicts=servicemeta.get('conflicts', []), allowed_assignment_types=servicemeta.get( 'allowed_assignment_types', list(AssignmentType)), num_units=service_dict.get('num_units', 1), allow_multi_units=servicemeta.get('allow_multi_units', True), subordinate=is_subordinate, required=servicemeta.get('required', True)) # Make sure to map any strings to an assignment type enum if any(isinstance(atype, str) for atype in charm.allowed_assignment_types): charm.allowed_assignment_types = label_to_atype( charm.allowed_assignment_types) return charm
def create_service(servicename, service_dict, servicemeta, relations): # a little cleaning to normalize a dict from the charmstore v4 api: service_dict = {k.lower(): v for k, v in service_dict.items()} if 'num_units' not in service_dict: service_dict['num_units'] = service_dict.get('numunits', 0) # some attempts to guess at subordinate status from bundle format, # to avoid having to include it in metadata: # This doesn't work because bundles with no machines might # just use the juju default: # is_subordinate = 'to' not in service_dict.keys() is_subordinate = service_dict.get('num_units', 0) == 0 if 'charm' not in service_dict: m = "Service '{}' has no 'charm' key.".format(servicename) if 'branch' in service_dict: m += " 'branch' key is not supported." raise BundleFormatError(m) charm_name = service_dict['charm'].split('/')[-1] charm_name = '-'.join(charm_name.split('-')[:-1]) myrelations = [] for src, dst in relations: if src.startswith(servicename) or dst.startswith(servicename): myrelations.append((src, dst)) service = Service(service_name=servicename, charm_source=service_dict['charm'], summary_future=None, constraints=servicemeta.get('constraints', {}), depends=servicemeta.get('depends', []), conflicts=servicemeta.get('conflicts', []), allowed_assignment_types=servicemeta.get( 'allowed_assignment_types', list(AssignmentType)), num_units=service_dict.get('num_units', 1), options=service_dict.get('options', {}), allow_multi_units=servicemeta.get('allow_multi_units', True), subordinate=is_subordinate, required=servicemeta.get('required', True), relations=myrelations, placement_spec=service_dict.get('to', None), expose=service_dict.get('expose', False)) # Make sure to map any strings to an assignment type enum if any(isinstance(atype, str) for atype in service.allowed_assignment_types): service.allowed_assignment_types = label_to_atype( service.allowed_assignment_types) return service
def add_bundle_assignments(self, new_as): for sname, tostrs in new_as.items(): service = next((s for s in self.bundle.services if s.service_name == sname), None) if service is None: continue for tostr in tostrs: parts = tostr.split(":") if len(parts) > 1: atype, mid = parts atype = label_to_atype([atype])[0] else: atype, mid = AssignmentType.DEFAULT, parts[0] machine = next((m for m in self.machines() if m.instance_id == mid), None) if machine: self.assign(machine, service, atype)
def create_charm_class(servicename, service_dict, servicemeta): # some attempts to guess at subordinate status from bundle format, # to avoid having to include it in metadata: # This doesn't work because bundles with no machines might # just use the juju default: # is_subordinate = 'to' not in service_dict.keys() is_subordinate = service_dict['num_units'] == 0 charm_name = service_dict['charm'].split('/')[-1] charm_name = '-'.join(charm_name.split('-')[:-1]) entity = CharmStoreAPI().lookup_charm(charm_name) display_name = "{} ({})".format(servicename, entity['Meta']['charm-metadata']['Name']) summary = entity['Meta']['charm-metadata']['Summary'] charm = Charm(charm_name=servicename, display_name=servicemeta.get('display-name', display_name), summary=servicemeta.get('summary', summary), constraints=servicemeta.get('constraints', {}), depends=servicemeta.get('depends', []), conflicts=servicemeta.get('conflicts', []), allowed_assignment_types=servicemeta.get( 'allowed_assignment_types', list(AssignmentType)), num_units=service_dict.get('num_units', 1), allow_multi_units=servicemeta.get('allow_multi_units', True), subordinate=is_subordinate, required=servicemeta.get('required', True)) # Make sure to map any strings to an assignment type enum if any(isinstance(atype, str) for atype in charm.allowed_assignment_types): charm.allowed_assignment_types = label_to_atype( charm.allowed_assignment_types) return charm