def main(args): # Manually trigger any register atstart events to ensure all endpoints # are correctly setup, Bug #1916008. ch_core.hookenv._run_atstart() action_name = os.path.basename(args[0]) try: action = ACTIONS[action_name] except KeyError: return 'Action {} undefined'.format(action_name) else: try: action(args) except Exception as e: ch_core.hookenv.log('action "{}" failed: "{}" "{}"'.format( action_name, str(e), traceback.format_exc()), level=ch_core.hookenv.ERROR) ch_core.hookenv.action_fail(str(e)) else: # we were successful, so commit changes from the action ch_core.unitdata.kv().flush() # try running handlers based on new state try: reactive.main() except Exception: exc = traceback.format_exc() ch_core.hookenv.log(exc, ch_core.hookenv.ERROR) ch_core.hookenv.action_fail(exc.splitlines()[-1]) ch_core.hookenv._run_atexit()
def test_main(self, hook_name, log, discover, dispatch, _KV): hook_name.return_value = "hook_name" reactive.main() log.assert_called_once_with("Reactive main running for hook hook_name", level=reactive.hookenv.INFO) discover.assert_called_once_with() dispatch.assert_called_once_with() _KV.flush.assert_called_once_with() _KV.flush.reset_mock() discover.side_effect = SystemExit self.assertRaises(SystemExit, reactive.main) _KV.flush.assert_called_once_with()
def test_main(self, hook_name, log, discover, dispatch, _KV): hook_name.return_value = 'hook_name' reactive.main() log.assert_called_once_with('Reactive main running for hook hook_name', level=reactive.hookenv.INFO) discover.assert_called_once_with() dispatch.assert_called_once_with() _KV.flush.assert_called_once_with() _KV.flush.reset_mock() discover.side_effect = SystemExit self.assertRaises(SystemExit, reactive.main) _KV.flush.assert_called_once_with()
def test_main(self, rel_factory, hook_name, log, _run_atstart, discover, dispatch, _KV): hook_name.return_value = 'hook_name' reactive.main() _run_atstart.assert_called_once_with() log.assert_called_once_with('Reactive main running for hook hook_name', level=reactive.hookenv.INFO) discover.assert_called_once_with() dispatch.assert_called_once_with(restricted=False) _KV.flush.assert_called_once_with() _KV.flush.reset_mock() discover.side_effect = SystemExit(1) self.assertRaises(SystemExit, reactive.main) assert not _KV.flush.called
def test_main(self, hook_name, log, _run_atstart, discover, dispatch, _KV): hook_name.return_value = 'meter-status-changed' reactive.main() _run_atstart.assert_not_called() log.any_call( 'Reactive restricted main running for hook meter-status-changed', level=reactive.hookenv.INFO) log.any_call('Restricted mode.', level=reactive.hookenv.INFO) discover.assert_called_once_with() dispatch.assert_called_once_with(restricted=True) _KV.flush.assert_called_once_with() _KV.flush.reset_mock() discover.side_effect = SystemExit(1) self.assertRaises(SystemExit, reactive.main) assert not _KV.flush.called
def refresh_pools(args): """Refresh list of pools from Ceph. This is done by updating data on relations to ceph-mons which lead to them updating the relation data they have with us as a response. Due to how the reactive framework handles publishing of relation data we must do this by setting a flag and runnnig the reactive handlers, emulating a full hook execution. """ if not reactive.is_flag_set('leadership.is_leader'): ch_core.hookenv.action_fail('run action on the leader unit') return # set and flush flag to disk reactive.set_flag('refresh.pools') ch_core.unitdata._KV.flush() # run reactive handlers to deal with flag return reactive.main()
if os.path.isfile('/etc/apt/sources.list.d/nodesource.list'): os.remove('/etc/apt/sources.list.d/nodesource.list') url = node_version_map[config['node-version']]['remote'] hookenv.status_set('maintenance', 'Using Node.js Repo: {}'.format(url)) try: curl_cmd = ['curl', '-sl', url] bash_cmd = ['bash', '-e'] pipe1 = Popen(curl_cmd, stdout=PIPE) pipe2 = Popen(bash_cmd, stdin=pipe1.stdout, stdout=PIPE) pipe1.stdout.close() output = pipe2.communicate()[0] hookenv.log('Added nodesource archive, output: {}'.format(output), 'debug') except: hookenv.log('Problem installing: {}'.format(output), 'debug') sys.exit(1) apt_purge(['nodejs']) apt_install(['nodejs']) hookenv.status_set('maintenance', 'Installing Node.js completed.') hookenv.status_set('active', 'Node.js is ready!') set_state('nodejs.available') if __name__ == "__main__": main()
'prometheus.available') def not_configured(): if not is_flag_set('config.set.ga-view-id'): blocked('ga-view-id must be set') elif not is_flag_set('config.set.ga-creds'): blocked('ga-creds must be set') else: blocked('Waiting for push-gateway relation') @hook('upgrade-charm') def upgrade(): set_flag('charmbuild.upgrade') clear_flag('charmbuild.installed') @when_not('charmbuild.installed') def install(): # this part lifted from haproxy charm hooks.py src = os.path.join(os.environ["CHARM_DIR"], "scripts") dst = '/srv/charmbuild-kpi-import/parts/' maint('Copying scripts from %s to %s' % (src, dst)) host.mkdir(dst, perms=0o755) for fname in glob.glob(os.path.join(src, "*")): host.rsync(fname, os.path.join(dst, os.path.basename(fname))) set_flag('charmbuild.installed') if __name__ == '__main__': main()
#!/usr/bin/env python3 import sys sys.path.append('lib') import charm.openstack.tempest as tempest import charms.reactive as reactive if __name__ == '__main__': # Cloud may have different artifacts (flavors, images etc) since last run # so rerun handlers file to regenerate config. reactive.main() charm = tempest.get_charm() charm.run_test('smoke')
status_set('maintenance', 'installing mongodb') m.install() set_state('mongodb.installed') @when('mongodb.installed') @when_not('mongodb.ready') def configure(): m = mongodb.mongodb(config().get('version')) m.configure(config()) service_restart('mongodb') set_state('mongodb.ready') @when('config.changed') @when_not('config.changed.version') def check_config(): remove_state('mongodb.ready') @hook('update-status') def update_status(): if mongodb.installed(): status_set('active', 'mongodb {}'.format(mongodb.version())) else: status_set('blocked', 'unable to install mongodb') if __name__ == '__main__': main() # pragma: no cover
#!/usr/bin/env python3 import sys sys.path.append('lib') import charm.openstack.tempest as tempest import charms.reactive as reactive if __name__ == '__main__': # Cloud may have different artifacts (flavors, images etc) since last run # so rerun handlers file to regenerate config. reactive.main() tempest.run_test('smoke')
def reactive_action(state): reactive.set_state(state) reactive.main()