我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用charmhelpers.core.hookenv.service_name()。
def update_prometheus_targets(target): services = target.services() related_targets = [] for service in services: targets = [] for unit in service['hosts']: hookenv.log('{} has a unit {}:{}'.format( service['service_name'], unit['hostname'], unit['port'])) targets.append('{hostname}:{port}'.format(**unit)) related_targets.append({'job_name': service['service_name'], 'targets': targets}) unitdata.kv().set('target_jobs', related_targets) set_state('prometheus.do-check-reconfig')
def _get_address_override(endpoint_type=PUBLIC): """Returns any address overrides that the user has defined based on the endpoint type. Note: this function allows for the service name to be inserted into the address if the user specifies {service_name}.somehost.org. :param endpoint_type: the type of endpoint to retrieve the override value for. :returns: any endpoint address or hostname that the user has overridden or None if an override is not present. """ override_key = ADDRESS_MAP[endpoint_type]['override'] addr_override = hookenv.config(override_key) if not addr_override: return None else: return addr_override.format(service_name=hookenv.service_name())
def storage_ceph_connected(ceph): ceph.create_pool(hookenv.service_name())
def configure_ceph(ceph): with charm.provide_charm_instance() as charm_class: # TODO(jamespage): refactor to avoid massaging helper ceph_helper.KEYRING = charm_class.ceph_keyring host.mkdir(os.path.dirname(charm_class.ceph_keyring)) ceph_helper.ensure_ceph_keyring(service=hookenv.service_name(), key=ceph.key(), user=charm_class.gnocchi_user, group=charm_class.gnocchi_group)
def storage_ceph_disconnected(): with charm.provide_charm_instance() as charm_class: # TODO(jamespage): refactor to avoid massaging helper ceph_helper.KEYRING = charm_class.ceph_keyring ceph_helper.delete_keyring(hookenv.service_name())
def write_prometheus_config_yml(): config = hookenv.config() target_jobs = unitdata.kv().get('target_jobs', []) scrape_jobs = unitdata.kv().get('scrape_jobs', []) # transform eg. 'h1:p1 , h2:p2' (string), to ['h1:p1', 'h2:p2'] (list) static_targets = None if config.get('static-targets'): static_targets = [x.strip() for x in config.get('static-targets', '').split(',')] default_monitor_name = '{}-monitor'.format(hookenv.service_name()) options = { 'scrape_interval': config['scrape-interval'], 'evaluation_interval': config['evaluation-interval'], 'static_targets': static_targets, 'private_address': hookenv.unit_get('private-address'), 'monitor_name': config.get('monitor_name', default_monitor_name), 'jobs': target_jobs, 'scrape_jobs': scrape_jobs, } # custom-rules content must be passed verbatim with e.g. # juju set prometheus custom-rules @my.rules if config.get('custom-rules'): custom_rules = config['custom-rules'] with open(CUSTOM_RULES_PATH, 'w') as fh: fh.write(custom_rules) options['custom_rules_file'] = CUSTOM_RULES_PATH render(source=PROMETHEUS_YML_TMPL, target=PROMETHEUS_YML, context=options ) validate_config() set_state('prometheus.do-restart') remove_state('prometheus.do-reconfig-yml')
def update_prometheus_alertmanager(alertmanager): services = alertmanager.services() if not (data_changed('alertmanager-service.related_services', services)): return # XXX: as of prometheus 0.17, it can only point to a single alertmanager, # last one from below loop will be used. for service in services: for unit in service['hosts']: hookenv.log('{} has a unit {}:{}'.format( service['service_name'], unit['hostname'], unit['port'])) runtime_args('-alertmanager.url', 'http://{hostname}:{port}'.format(**unit)) set_state('prometheus.do-check-reconfig')
def domain_name(self): """Domain name for the running application :returns: string: containing the current domain name for the application """ return hookenv.config('domain-name') or hookenv.service_name()
def configure_domain_name(domain): keystone_ldap.render_config(domain.trigger_restart) domain.domain_name(hookenv.config('domain-name') or hookenv.service_name()) reactive.set_state('domain-name-configured')
def ssl_ca_file(self): return '/var/lib/charm/{}/rabbit-client-ca.pem'.format( hookenv.service_name())
def application_name(self): """Return the name of the deployed charm""" return hookenv.service_name()
def rabbit_client_cert_dir(self): return '/var/lib/charm/{}'.format(hookenv.service_name())
def test_gets_service_name(self, _unit): _unit.return_value = 'mysql/3' self.assertEqual(hookenv.service_name(), 'mysql')
def __init__(self, port_map=None, service_name=None, charm_instance=None): """ Note passing port_map and service_name is deprecated, but supported for backwards compatibility. The port_map and service_name can be obtained from the self.charm_instance weak reference. :param port_map: Map containing service names and the ports used e.g. port_map = { 'svc1': { 'admin': 9001, 'public': 9001, 'int': 9001, }, 'svc2': { 'admin': 9002, 'public': 9002, 'int': 9002, }, } :param service_name: Name of service being deployed :param charm_instance: a charm instance that will be passed to the base constructor """ super(APIConfigurationAdapter, self).__init__( charm_instance=charm_instance) if port_map is not None: hookenv.log( "DEPRECATION: should not use port_map parameter in " "APIConfigurationAdapter.__init__()", level=hookenv.WARNING) self.port_map = port_map elif self.charm_instance is not None: self.port_map = self.charm_instance.api_ports else: self.port_map = None if service_name is not None: hookenv.log( "DEPRECATION: should not use service_name parameter in " "APIConfigurationAdapter.__init__()", level=hookenv.WARNING) self.service_name = service_name elif self.charm_instance is not None: self.service_name = self.charm_instance.name else: self.service_name = None self.__network_addresses = None