我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用charmhelpers.core.hookenv.leader_set()。
def init_rndckey(self): """Create a RNDC key if needed Return the rndc key from the leader DB or if one is not present generate a new one. :returns: str: rndc key """ secret = DesignateBindCharm.get_rndc_secret() hookenv.log('Retrieving secret', level=hookenv.DEBUG) if not secret: hookenv.log('Secret not found in leader db', level=hookenv.DEBUG) if hookenv.is_leader(): hookenv.log('Creating new secret as leader', level=hookenv.DEBUG) secret = self.generate_rndc_key() hookenv.leader_set({LEADERDB_SECRET_KEY: secret}) return secret
def _save_state(self): self.msg('Publishing state'.format(self._name())) if hookenv.is_leader(): # sort_keys to ensure stability. raw = json.dumps(self.grants, sort_keys=True) hookenv.leader_set({self.key: raw}) local_unit = hookenv.local_unit() if self.relid is None: # No peers relation yet. Fallback to local state. self.msg('No peer relation. Saving local state') self._save_local_state(self.requests[local_unit]) else: # sort_keys to ensure stability. raw = json.dumps(self.requests[local_unit], sort_keys=True) hookenv.relation_set(self.relid, relation_settings={self.key: raw})
def db_sync(self): """Perform a database sync using the command defined in the self.sync_cmd attribute. The services defined in self.services are restarted after the database sync. """ if not self.db_sync_done() and hookenv.is_leader(): subprocess.check_call(self.sync_cmd) hookenv.leader_set({'db-sync-done': True}) # Restart services immediately after db sync as # render_domain_config needs a working system self.restart_all()
def db_sync(self): """Perform a database sync using the command defined in the self.sync_cmd attribute. The services defined in self.services are restarted after the database sync. """ if not self.db_sync_done() and hookenv.is_leader(): subprocess.check_call(['mistral-db-manage', '--config-file', '/etc/mistral/mistral.conf', 'upgrade', 'head']) subprocess.check_call(['mistral-db-manage', '--config-file', '/etc/mistral/mistral.conf', 'stamp', 'head']) subprocess.check_call(['mistral-db-manage', '--config-file', '/etc/mistral/mistral.conf', 'populate']) hookenv.leader_set({'db-sync-done': True}) # Restart services immediately after db sync as # render_domain_config needs a working system self.restart_all()
def check_zone_ids(cls, nova_domain_name, neutron_domain_name): zone_org_ids = { 'nova-domain-id': cls.get_domain_id(nova_domain_name), 'neutron-domain-id': cls.get_domain_id(neutron_domain_name), } yield zone_ids = { 'nova-domain-id': cls.get_domain_id(nova_domain_name), 'neutron-domain-id': cls.get_domain_id(neutron_domain_name), } if zone_org_ids != zone_ids: # Update leader-db to trigger peers to rerender configs # as sink files will need updating with new domain ids # Use host ID and current time UUID to help with debugging hookenv.leader_set({'domain-init-done': uuid.uuid1()})
def create_initial_servers_and_domains(cls): """Create the nameserver entry and domains based on the charm user supplied config NOTE(AJK): This only wants to be done ONCE and by the leader, so we use leader settings to store that we've done it, after it's successfully completed. @returns None """ KEY = 'create_initial_servers_and_domains' if hookenv.is_leader() and not hookenv.leader_get(KEY): nova_domain_name = hookenv.config('nova-domain') neutron_domain_name = hookenv.config('neutron-domain') with cls.check_zone_ids(nova_domain_name, neutron_domain_name): if hookenv.config('nameservers'): for ns in hookenv.config('nameservers').split(): cls.create_server(ns) else: hookenv.log('No nameserver specified, skipping creation of' 'nova and neutron domains', level=hookenv.WARNING) return if nova_domain_name: cls.create_domain( nova_domain_name, hookenv.config('nova-domain-email')) if neutron_domain_name: cls.create_domain( neutron_domain_name, hookenv.config('neutron-domain-email')) # if this fails, we weren't the leader any more; another unit may # attempt to do this too. hookenv.leader_set({KEY: 'done'})
def set_sync_info(self, sync_time, sync_file): """Update leader DB with sync information :param sync_time: str Time sync was created in epoch seconds :param sync_file: str Local file containing zone information :returns: None """ sync_info = { LEADERDB_SYNC_SRC_KEY: 'http://{}:80/zone-syncs/{}'.format( hookenv.unit_private_ip(), sync_file), LEADERDB_SYNC_TIME_KEY: sync_time, } hookenv.leader_set(sync_info)
def __setitem__(self, key, value): if not hookenv.is_leader(): raise TypeError('Not the leader. Cannot change leader settings.') if value is not None and not isinstance(value, six.string_types): # We don't do implicit casting. This would cause simple # types like integers to be read back as strings in subsequent # hooks, and mutable types would require a lot of wrapping # to ensure leader-set gets called when they are mutated. raise ValueError('Only string values allowed') hookenv.leader_set({key: value})
def server_connected(peer) -> None: """ The peer.available state is set when there are one or more peer units that have joined. :return: """ update_status() bricks = check_for_new_devices() if bricks.is_ok(): log('Reporting my bricks {} to the leader'.format(bricks.value)) peer.set_bricks(bricks=bricks.value) if not is_leader(): log('Reporting my public address {} to the leader'.format( unit_public_ip())) peer.set_address(address_type='public', address=unit_public_ip()) return # I am the leader log('Leader probing peers') probed_units = [] try: p = hookenv.leader_get('probed-units') if p: probed_units = json.loads(p) except json.decoder.JSONDecodeError as e: log("json decoder failed for {}: {}".format(e.doc, e.msg)) log("probed_units: {}".format(probed_units)) peer_info = peer.get_peer_info() for unit in peer_info: if unit in probed_units: continue address = peer_info[unit]['address'] log('probing host {} at {}'.format(unit, address)) status_set('maintenance', 'Probing peer {}'.format(unit)) try: peer_probe(address) probed_units.append(unit) except (GlusterCmdException, GlusterCmdOutputParseError): log('Error probing host {}: {}'.format(unit, address), ERROR) continue log('successfully probed {}: {}'.format(unit, address), DEBUG) settings = {'probed-units': json.dumps(probed_units)} hookenv.leader_set(settings) status_set('maintenance', '')