def update_ziu(trigger): if in_relation_hook(): ziu_stage = relation_get("ziu") log("ZIU: stage from relation {}".format(ziu_stage)) else: ziu_stage = config.get("ziu") log("ZIU: stage from config {}".format(ziu_stage)) if ziu_stage is not None: ziu_stage = int(ziu_stage) config_set("ziu", ziu_stage) if ziu_stage > int(config.get("ziu_done", -1)): log("ZIU: run stage {}, trigger {}".format(ziu_stage, trigger)) stages[ziu_stage](ziu_stage, trigger) # This code is on controller only if not is_leader(): return ziu_stage = config.get("ziu") if ziu_stage is None: return ziu_stage = int(ziu_stage) if not check_ziu_stage_done(ziu_stage): return # move to next stage ziu_stage += 1 signal_ziu("ziu", ziu_stage) # run next stage on self to avoid waiting for update_status log("ZIU: run stage {}, trigger {}".format(ziu_stage, trigger)) # last stage must be called immediately to provide ziu=max_stage to all # all other stages can call stage handler immediately to not wait for update_status max_stage = max(stages.keys()) if ziu_stage != max_stage: stages[ziu_stage](ziu_stage, trigger)
def start(): # Tell the benchmark-gui charm the action_id via the benchmark # relation. Benchmark-gui will pass the action_id to all # collector charms in the environment (via the collector relation), # triggering profile data collection on each. charm_dir = os.environ.get('CHARM_DIR') action_uuid = os.environ.get('JUJU_ACTION_UUID') if in_relation_hook() and charm_dir and action_uuid: """ If the cabs-collector charm is installed, take a snapshot of the current profile data. """ # Do profile data collection immediately on this unit if os.path.exists(COLLECT_PROFILE_DATA): subprocess.check_output([COLLECT_PROFILE_DATA]) with open(os.path.join(charm_dir, 'metadata.yaml'), 'r') as f: metadata = yaml.safe_load(f.read()) for relation in metadata.get('provides', {}): if metadata['provides'][relation]['interface'] == 'benchmark': for rid in relation_ids(relation): relation_set( relation_id=rid, relation_settings={'action_id': action_uuid}) return Benchmark.set_data( {'meta.start': time.strftime('%Y-%m-%dT%H:%M:%SZ')})
def __init__(self, benchmarks=None): if in_relation_hook(): """ When we're inside a hook relation (not an action context), we want to notify whatever's listening on the other end of the wire, i.e., the benchmark-gui, of what benchmark's we're advertising. """ if benchmarks is not None: for rid in sorted(relation_ids('benchmark')): relation_set( relation_id=rid, relation_settings={'benchmarks': ",".join(benchmarks)}) # Check the relation data config = {} for key in self.required_keys: val = relation_get(key) if val is not None: config[key] = val else: # We don't have all of the required keys config = {} break if len(config): with open('/etc/benchmark.conf', 'w') as f: for key, val in config.items(): f.write("%s=%s\n" % (key, val))
def __init__(self, benchmarks=None): if in_relation_hook(): """ When we're inside a hook relation (not an action context), we want to notify whatever's listening on the other end of the wire, i.e., the benchmark-gui, of what benchmark's we're advertising. """ if benchmarks is not None: for rid in sorted(relation_ids('benchmark')): relation_set(relation_id=rid, relation_settings={ 'benchmarks': ",".join(benchmarks) }) # Check the relation data config = {} for key in self.required_keys: val = relation_get(key) if val is not None: config[key] = val else: # We don't have all of the required keys config = {} break if len(config): with open('/etc/benchmark.conf', 'w') as f: for key, val in config.items(): f.write("%s=%s\n" % (key, val))
def logfiles(self): fromConfig = json.loads(str(config("logfiles"))) if in_relation_hook(): # Can't guarantee the data will be there try: fromRelation = json.loads(str(relation_get("logfiles"))) except ValueError: fromRelation = {} else: fromRelation = {} # Local config overrides relation config return dict(fromRelation.items() + fromConfig.items())
def update_ziu(trigger): if in_relation_hook(): ziu_stage = relation_get("ziu") log("ZIU: stage from relation {}".format(ziu_stage)) else: ziu_stage = config.get("ziu") log("ZIU: stage from config {}".format(ziu_stage)) if ziu_stage is None: return ziu_stage = int(ziu_stage) config_set("ziu", ziu_stage) if ziu_stage > int(config.get("ziu_done", -1)): log("ZIU: run stage {}, trigger {}".format(ziu_stage, trigger)) stages[ziu_stage](ziu_stage, trigger)
def update_nrpe_config(nagios): unit_data = unitdata.kv() nagios_hostname = unit_data.get('nagios.hostname', None) nagios_host_context = unit_data.get('nagios.host_context', None) # require the nrpe-external-master relation to provide the host context if in_relation_hook() and relation_id().\ startswith('nrpe-external-master:'): rel = relation_get() if 'nagios_host_context' in rel: nagios_host_context = rel['nagios_host_context'] unit_data.set('nagios.host_context', nagios_host_context) # We have to strip the nagios host context from the nagios hostname # since the nagios.add_check will put it back again... nagios_hostname = rel['nagios_hostname'] if nagios_hostname.startswith(nagios_host_context + '-'): nagios_hostname = nagios_hostname[len(nagios_host_context + '-'):] unit_data.set('nagios.hostname', nagios_hostname) if not nagios_hostname or not nagios_host_context: return # The above boilerplate is needed until this issue is fixed: # # https://github.com/cmars/nrpe-external-master-interface/issues/6 status_set('maintenance', 'Updating Nagios configs') creds = Credentials() check = [ '/usr/lib/nagios/plugins/check_http', '-H', 'localhost', '-p', '8080', '-u', urlparse(Api().url).path, '-a', "{}:{}".format(creds.username(), creds.token()), ] nagios.add_check(check, name="check_jenkins_http", description="Verify Jenkins HTTP is up.", context=nagios_host_context, unit=nagios_hostname) status_set('active', 'Ready')
def logfiles(self): if config('logfiles'): from_config = json.loads(str(config('logfiles'))) else: from_config = {} if in_relation_hook(): try: from_relation = json.loads(str(relation_get('logfiles'))) except ValueError: from_relation = {} else: from_relation = {} # Local config overrides relation config return dict( [x for x in from_relation.items()] + \ [x for x in from_config.items()] )
def __init__(self, benchmarks=None): if in_relation_hook(): if benchmarks is not None: for rid in sorted(relation_ids('benchmark')): relation_set( relation_id=rid, relation_settings={'benchmarks': ",".join(benchmarks)}) # Check the relation data config = {} for key in self.required_keys: val = relation_get(key) if val is not None: config[key] = val else: # We don't have all of the required keys config = {} break if len(config): with open(self.BENCHMARK_CONF, 'w') as f: for key, val in iter(config.items()): f.write("%s=%s\n" % (key, val))
def __init__(self, benchmarks=None): if in_relation_hook(): if benchmarks is not None: for rid in sorted(relation_ids('benchmark')): relation_set(relation_id=rid, relation_settings={ 'benchmarks': ",".join(benchmarks) }) # Check the relation data config = {} for key in self.required_keys: val = relation_get(key) if val is not None: config[key] = val else: # We don't have all of the required keys config = {} break if len(config): with open('/etc/benchmark.conf', 'w') as f: for key, val in iter(config.items()): f.write("%s=%s\n" % (key, val))
def benchmark(): if not hookenv.in_relation_hook(): return set_action_id(hookenv.relation_get('action_id')) benchmarks = hookenv.relation_get('benchmarks') if benchmarks: hookenv.log('benchmarks received: %s' % benchmarks) service = hookenv.remote_unit().split('/')[0] payload = {'benchmarks': [b for b in benchmarks.split(',')]} requests.post( 'http://localhost:9000/api/services/{}'.format(service), data=json.dumps(payload), headers={ 'content-type': 'application/json' } ) graphite_url = 'http://%s:9001' % hookenv.unit_get('public-address') hookenv.relation_set(hostname=hookenv.unit_private_ip(), port=2003, graphite_port=9001, graphite_endpoint=graphite_url, api_port=9000)
def test_checks_that_is_not_running_in_relation_hook(self, os_): os_.environ = { 'bar': 'foo', } self.assertFalse(hookenv.in_relation_hook())
def test_checks_that_is_running_in_relation_hook(self, os_): os_.environ = { 'JUJU_RELATION': 'foo', } self.assertTrue(hookenv.in_relation_hook())
def emitter_rel(): if hookenv.in_relation_hook(): hookenv.relation_set(hostname=hookenv.unit_private_ip(), port=2003, api_port=9000)