From 81558e5f78d6dda575553ff28bab6e1aef1bd4fb Mon Sep 17 00:00:00 2001 From: Liam Young Date: Tue, 16 Jun 2015 14:34:36 +0000 Subject: [PATCH 1/3] Add support for related charm to request odl commands to run --- hooks/odl_controller_hooks.py | 26 +++++++++++++++++--------- hooks/odl_controller_utils.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/hooks/odl_controller_hooks.py b/hooks/odl_controller_hooks.py index f3de4d7..f14b877 100755 --- a/hooks/odl_controller_hooks.py +++ b/hooks/odl_controller_hooks.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +import json import os import re import shutil @@ -12,22 +13,25 @@ from charmhelpers.core.hookenv import ( UnregisteredHookError, config, log, - relation_set + relation_get, + relation_ids, + relation_set, + related_units, ) from charmhelpers.core.host import ( adduser, mkdir, - restart_on_change, service_restart, + restart_on_change, service_start ) from charmhelpers.fetch import apt_install, install_remote -from odl_controller_utils import write_mvn_config +from odl_controller_utils import write_mvn_config, process_odl_cmds -PACKAGES = [ "default-jre-headless", "python-jinja2" ] +PACKAGES = ["default-jre-headless", "python-jinja2"] hooks = Hooks() config = config() @@ -41,6 +45,15 @@ def config_changed(): 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') + if odl_cmds_json: + odl_cmds = json.loads(odl_cmds_json) + process_odl_cmds(odl_cmds) + @hooks.hook() def install(): # install dependencies @@ -61,11 +74,6 @@ def install(): # install features write_mvn_config() service_start("odl-controller") - check_call(["/opt/opendaylight-karaf/bin/client", "-r", "61", - "feature:install", "odl-base-all", "odl-aaa-authn", - "odl-restconf", "odl-nsf-all", "odl-adsal-northbound", - "odl-mdsal-apidocs", "odl-ovsdb-openstack", - "odl-ovsdb-northbound", "odl-dlux-core"]) def main(): try: diff --git a/hooks/odl_controller_utils.py b/hooks/odl_controller_utils.py index 8f3eefa..07f3f45 100644 --- a/hooks/odl_controller_utils.py +++ b/hooks/odl_controller_utils.py @@ -1,3 +1,5 @@ +import subprocess +import json from os import environ import urlparse @@ -36,3 +38,35 @@ 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) + output = subprocess.check_output(run_cmd) + return output + +def installed_features(): + installed = [] + out = run_odl(['feature:list']) + for line in out.split('\n'): + columns = line.split('|') + if len(columns) > 2: + install_flag = columns[2].replace(" ", "") + if install_flag == 'x': + 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] + return whitelist + +def process_odl_cmds(odl_cmds): + features = filter_installed(cmd_dics.get('feature:install', [])) + if features: + run_odl(['feature:install'] + features) + logging = cmd_dics.get('log:set') + if logging: + for log_level in logging.keys(): + for target in logging[log_level]: + run_odl(['log:set', log_level, target]) From 66c6a24f2ecc6cfd4c24670a7d6a4a450ab98672 Mon Sep 17 00:00:00 2001 From: Liam Young Date: Tue, 16 Jun 2015 14:38:51 +0000 Subject: [PATCH 2/3] flake8 fixes --- hooks/odl_controller_hooks.py | 17 +++++++++++++---- hooks/odl_controller_utils.py | 21 ++++++++++++++------- 2 files changed, 27 insertions(+), 11 deletions(-) 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]: From a542f9e3808da0e2f82bd370f6249b128e1412e8 Mon Sep 17 00:00:00 2001 From: Liam Young Date: Tue, 16 Jun 2015 16:34:29 +0000 Subject: [PATCH 3/3] Switch to double quotes for consistency --- hooks/odl_controller_hooks.py | 4 ++-- hooks/odl_controller_utils.py | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/hooks/odl_controller_hooks.py b/hooks/odl_controller_hooks.py index 88aa638..b3340ea 100755 --- a/hooks/odl_controller_hooks.py +++ b/hooks/odl_controller_hooks.py @@ -49,10 +49,10 @@ def controller_api_joined(): @hooks.hook("controller-api-relation-changed") def controller_api_changed(): - for rid in relation_ids('controller-api'): + 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') + attribute="odl-cmds") if odl_cmds_json: odl_cmds = json.loads(odl_cmds_json) process_odl_cmds(odl_cmds) diff --git a/hooks/odl_controller_utils.py b/hooks/odl_controller_utils.py index 3808cf0..8af163c 100644 --- a/hooks/odl_controller_utils.py +++ b/hooks/odl_controller_utils.py @@ -42,7 +42,7 @@ def write_mvn_config(): "opendaylight", "opendaylight", 0400) -def run_odl(cmds, host='localhost', port=8101, retries=20): +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) @@ -52,12 +52,12 @@ def run_odl(cmds, host='localhost', port=8101, retries=20): def installed_features(): installed = [] - out = run_odl(['feature:list']) - for line in out.split('\n'): - columns = line.split('|') + out = run_odl(["feature:list"]) + for line in out.split("\n"): + columns = line.split("|") if len(columns) > 2: install_flag = columns[2].replace(" ", "") - if install_flag == 'x': + if install_flag == "x": installed.append(columns[0].replace(" ", "")) return installed @@ -69,11 +69,11 @@ def filter_installed(features): def process_odl_cmds(odl_cmds): - features = filter_installed(odl_cmds.get('feature:install', [])) + features = filter_installed(odl_cmds.get("feature:install", [])) if features: - run_odl(['feature:install'] + features) - logging = odl_cmds.get('log:set') + run_odl(["feature:install"] + features) + logging = odl_cmds.get("log:set") if logging: for log_level in logging.keys(): for target in logging[log_level]: - run_odl(['log:set', log_level, target]) + run_odl(["log:set", log_level, target])