diff --git a/hooks/odl_controller_hooks.py b/hooks/odl_controller_hooks.py index f14b877..88aa638 100755 --- a/hooks/odl_controller_hooks.py +++ b/hooks/odl_controller_hooks.py @@ -22,7 +22,6 @@ from charmhelpers.core.hookenv import ( from charmhelpers.core.host import ( adduser, mkdir, - service_restart, restart_on_change, service_start ) @@ -36,24 +35,29 @@ PACKAGES = ["default-jre-headless", "python-jinja2"] hooks = Hooks() config = config() + @hooks.hook("config-changed") @restart_on_change({"/home/opendaylight/.m2/settings.xml": ["odl-controller"]}) def config_changed(): write_mvn_config() + @hooks.hook("controller-api-relation-joined") def controller_api_joined(): relation_set(port=8080, username="admin", password="admin") + @hooks.hook("controller-api-relation-changed") def controller_api_changed(): for rid in relation_ids('controller-api'): for unit in related_units(rid): - odl_cmds_json = relation_get(rid=rid, unit=unit, attribute='odl-cmds') + odl_cmds_json = relation_get(rid=rid, unit=unit, + attribute='odl-cmds') if odl_cmds_json: odl_cmds = json.loads(odl_cmds_json) process_odl_cmds(odl_cmds) + @hooks.hook() def install(): # install dependencies @@ -67,24 +71,29 @@ def install(): os.symlink(name, "/opt/opendaylight-karaf") shutil.copy("files/odl-controller.conf", "/etc/init") adduser("opendaylight", system_user=True) - mkdir("/home/opendaylight", owner="opendaylight", group="opendaylight", perms=0755) + mkdir("/home/opendaylight", owner="opendaylight", group="opendaylight", + perms=0755) check_call(["chown", "-R", "opendaylight:opendaylight", "/opt/" + name]) - mkdir("/var/log/opendaylight", owner="opendaylight", group="opendaylight", perms=0755) + mkdir("/var/log/opendaylight", owner="opendaylight", group="opendaylight", + perms=0755) # install features write_mvn_config() service_start("odl-controller") + def main(): try: hooks.execute(sys.argv) except UnregisteredHookError as e: log("Unknown hook {} - skipping.".format(e)) + @hooks.hook("ovsdb-manager-relation-joined") def ovsdb_manager_joined(): relation_set(port=6640, protocol="tcp") + @hooks.hook("upgrade-charm") def upgrade_charm(): pass diff --git a/hooks/odl_controller_utils.py b/hooks/odl_controller_utils.py index 07f3f45..3808cf0 100644 --- a/hooks/odl_controller_utils.py +++ b/hooks/odl_controller_utils.py @@ -1,16 +1,17 @@ import subprocess -import json from os import environ import urlparse from charmhelpers.core.templating import render + def mvn_ctx(): ctx = {} ctx.update(mvn_proxy_ctx("http")) ctx.update(mvn_proxy_ctx("https")) return ctx + def mvn_proxy_ctx(protocol): ctx = {} key = protocol + "_proxy" @@ -34,16 +35,20 @@ def mvn_proxy_ctx(protocol): ctx[protocol + "_noproxy"] = no_proxy return ctx + def write_mvn_config(): ctx = mvn_ctx() render("settings.xml", "/home/opendaylight/.m2/settings.xml", ctx, "opendaylight", "opendaylight", 0400) + def run_odl(cmds, host='localhost', port=8101, retries=20): - run_cmd = ["/opt/opendaylight-karaf/bin/client", "-r", str(retries), "-h", host, "-a", str(port)] - run_cmd.extend(cmds) + run_cmd = ["/opt/opendaylight-karaf/bin/client", "-r", str(retries), + "-h", host, "-a", str(port)] + run_cmd.extend(cmds) output = subprocess.check_output(run_cmd) - return output + return output + def installed_features(): installed = [] @@ -56,16 +61,18 @@ def installed_features(): installed.append(columns[0].replace(" ", "")) return installed + def filter_installed(features): installed = installed_features() - whitelist = [ feature for feature in features if feature not in installed] + whitelist = [feature for feature in features if feature not in installed] return whitelist + def process_odl_cmds(odl_cmds): - features = filter_installed(cmd_dics.get('feature:install', [])) + features = filter_installed(odl_cmds.get('feature:install', [])) if features: run_odl(['feature:install'] + features) - logging = cmd_dics.get('log:set') + logging = odl_cmds.get('log:set') if logging: for log_level in logging.keys(): for target in logging[log_level]: