Slaves config file is now a jinja2 template

Change-Id: I593ea08f87997dbd99934ad3b34c38c34fe1b824
diff --git a/common/utils/dockerhelpers.py b/common/utils/dockerhelpers.py
index af23dbc..9708e9e 100644
--- a/common/utils/dockerhelpers.py
+++ b/common/utils/dockerhelpers.py
@@ -210,7 +210,7 @@
                         data = self.client.inspect_image(i)
                     data[i] = data
                 except errors.NotFound:
-                    log.info('No data for container {}'.format(i))
+                    log.debug('No data for container {}'.format(i))
 
             status = get_status(event)
             if status in handlers:
diff --git a/podder/handlers.py b/podder/handlers.py
index 6b261a6..40d53a8 100644
--- a/podder/handlers.py
+++ b/podder/handlers.py
@@ -17,6 +17,7 @@
     get_all_running_containers, inspect_container, remove_container
 
 from structlog import get_logger
+import yaml
 
 log = get_logger()
 
@@ -48,7 +49,6 @@
     # Assuming only one network exists....
     return create_networking_config(network[0], { l : l for l in links})
 
-
 def process_value(value):
     if value is None:
         return None
@@ -68,7 +68,6 @@
 def construct_container_spec(config):
     container_spec = {}
     container_spec['image'] = get_entry('image', config, mandatory=True)
-    #TODO need to rewrite command to connect to right service instance
     container_spec['command'] = get_entry('command', config, mandatory=True)
     container_spec['environment'] = get_entry('environment', config, noneval={})
     container_spec['ports'] = get_entry('ports', config)
@@ -86,7 +85,11 @@
                     log.info('Removing container {}'.format(container['Names']))
                     remove_container(container['Id'])
 
-def start_slaves(service, instance_name, instance_id, data, config):
+def start_slaves(service, instance_name, instance_id, data, conf):
+    network = obtain_network_name(data)
+    # still assuming a single network
+    config = yaml.load(conf.render(data=data, network=network[0]))
+
     if service not in config['services']:
         log.debug('Unknown service {}'.format(service))
         return
@@ -94,7 +97,7 @@
         if slave not in config['slaves']:
             log.debug('Unknown slave service {}'.format(slave))
             continue
-        network = obtain_network_name(data)
+
         netcfg = create_network_config(network, get_entry('links', config['slaves'][slave]))
         container_spec = construct_container_spec(config['slaves'][slave])
         container_spec['networking_config'] = netcfg
@@ -107,11 +110,14 @@
         container_spec['environment']['PODDER_MASTER'] = instance_name
 
         container = create_container(container_spec)
+        log.info('Starting slaves for {}'.format(instance_name))
         start_container(container)
 
 
-def stop_slaves(service, instance_name, instance_id, data, config):
+def stop_slaves(service, instance_name, instance_id, data, conf):
     log.info('Stopping slaves for {}'.format(instance_name))
+    network = obtain_network_name(data)
+    config = yaml.load(conf.render())
     if service in config['services']:
         service_shutdown(service, instance_name, config)
     else:
diff --git a/podder/main.py b/podder/main.py
index 3f74446..d02fc63 100755
--- a/podder/main.py
+++ b/podder/main.py
@@ -25,7 +25,7 @@
 from common.utils.nethelpers import get_my_primary_local_ipv4
 
 defs = dict(
-    slaves=os.environ.get('SLAVES', './slaves.yml'),
+    slaves=os.environ.get('SLAVES', './slaves.yml.j2'),
     config=os.environ.get('CONFIG', './podder.yml'),
     consul=os.environ.get('CONSUL', 'localhost:8500'),
     external_host_address=os.environ.get('EXTERNAL_HOST_ADDRESS',
@@ -110,15 +110,20 @@
 
     return args
 
-def load_config(config):
-    path = config
+def load_file(file):
+    path = file
     if path.startswith('.'):
         dir = os.path.dirname(os.path.abspath(__file__))
         path = os.path.join(dir, path)
     path = os.path.abspath(path)
     with open(path) as fd:
-        config = yaml.load(fd)
-    return config
+        contents = fd.read()
+    return contents
+
+def load_config(config):
+    contents = load_file(config)
+    return yaml.load(contents)
+
 
 banner = r'''
  _____
@@ -139,7 +144,7 @@
     def __init__(self):
         self.args = args = parse_args()
         self.config = load_config(args.config)
-        self.slave_config = load_config(args.slaves)
+        self.slave_config = load_file(args.slaves)
 
         verbosity_adjust = (args.verbose or 0) - (args.quiet or 0)
         self.log = setup_logging(self.config.get('logging', {}),
diff --git a/podder/podder.py b/podder/podder.py
index daa9e91..7cd166d 100644
--- a/podder/podder.py
+++ b/podder/podder.py
@@ -16,6 +16,7 @@
 
 from handlers import handler_start, handler_stop
 from structlog import get_logger
+from jinja2 import Template
 
 from common.utils.dockerhelpers import EventProcessor
 
@@ -27,7 +28,7 @@
         self.log.info('Initializing Podder')
         self.running = False
         self.events = EventProcessor()
-        self.handlers = { 'podder_config' : slave_config }
+        self.handlers = { 'podder_config' : Template(slave_config) }
 
     def run(self):
         if self.running:
diff --git a/podder/slaves.yml b/podder/slaves.yml.j2
similarity index 65%
rename from podder/slaves.yml
rename to podder/slaves.yml.j2
index 2adc46b..668d7d6 100644
--- a/podder/slaves.yml
+++ b/podder/slaves.yml.j2
@@ -1,3 +1,11 @@
+{% macro address(data, network) -%}
+    {% if data is defined and network is defined -%}
+    {{ data['NetworkSettings']['Networks'][network]['IPAddress'] }}:{{ data['NetworkSettings']['Ports']['50555/tcp'][0]['HostPort'] }}
+    {%- else -%}
+    None:None
+    {%- endif %}
+{%- endmacro %}
+
 services:
   cord/voltha:
     slaves: ["chameleon", "ofagent"]
@@ -7,12 +15,12 @@
   chameleon:
     image: cord/chameleon
     command: [
-      "/chameleon/main.py",
+      "/chameleon/chameleon/main.py",
       "-v",
       "--consul=consul:8500",
       "--fluentd=fluentd:24224",
       "--rest-port=8881",
-      "--grpc-endpoint=@voltha-grpc",
+      "--grpc-endpoint={{ address(data, network) }}",
       "--instance-id-is-container-name",
       "-v"
     ]
@@ -32,12 +40,12 @@
   ofagent:
     image: cord/ofagent
     command: [
-      "/ofagent/main.py",
+      "/ofagent/ofagent/main.py",
       "-v",
       "--consul=${DOCKER_HOST_IP}:8500",
       "--fluentd=fluentd:24224",
       "--controller=${DOCKER_HOST_IP}:6633",
-      "--grpc-endpoint=@voltha-grpc",
+      "--grpc-endpoint={{ address(data, network) }}",
       "--instance-id-is-container-name",
       "-v"
     ]