Add support for related charm to request odl commands to run

This commit is contained in:
Liam Young 2015-06-16 14:34:36 +00:00
parent f58967feb2
commit 81558e5f78
2 changed files with 51 additions and 9 deletions

View File

@ -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:

View File

@ -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])