Convention over configuration
[validation.git] / bluval / blucon.py
index 5d7406b..cea16de 100644 (file)
@@ -29,22 +29,44 @@ import yaml
 from bluutil import BluvalError
 from bluutil import ShowStopperError
 
+_OPTIONAL_ALSO = False
+_SUBNET = ""
+
+def get_volumes(layer):
+    """Create a list with volumes to mount in the container for given layer
+    """
+    mypath = Path(__file__).absolute()
+    volume_yaml = yaml.safe_load(mypath.parents[0].joinpath("volumes.yaml").open())
+
+    if layer not in volume_yaml['layers']:
+        return ''
+    if volume_yaml['layers'][layer] is None:
+        return ''
+
+    volume_list = ''
+    for vol in volume_yaml['layers'][layer]:
+        if volume_yaml['volumes'][vol]['local'] == '':
+            continue
+        volume_list = (volume_list + ' -v ' +
+                       volume_yaml['volumes'][vol]['local'] + ':' +
+                       volume_yaml['volumes'][vol]['target'])
+    return volume_list
+
+
 def invoke_docker(bluprint, layer):
     """Start docker container for given layer
     """
-    cmd = ("docker run"
-           " -v $HOME/.ssh:/root/.ssh"
-           " -v $HOME/.kube/config:/root/.kube/config"
-           " -v $VALIDATION_HOME/tests/variables.yaml:"
-           "/opt/akraino/validation/tests/variables.yaml"
-           " -v $AKRAINO_HOME/results:/opt/akraino/results"
+    volume_list = get_volumes('common') + get_volumes(layer)
+    cmd = ("docker run --rm" + volume_list + _SUBNET +
            " akraino/validation:{0}-latest"
-           " bin/sh -c"
+           " /bin/sh -c"
            " 'cd /opt/akraino/validation "
-           "&& python bluval/bluval.py -l {0} {1}'").format(layer, bluprint)
+           "&& python bluval/bluval.py -l {0} {1} {2}'"
+           .format(layer, ("-o" if _OPTIONAL_ALSO else ""), bluprint))
+
     args = [cmd]
     try:
-        print('Invoking {}'.format(args))
+        print('\nInvoking {}'.format(args))
         subprocess.call(args, shell=True)
     except OSError:
         #print('Error while executing {}'.format(args))
@@ -67,14 +89,24 @@ def invoke_dockers(yaml_loc, layer, blueprint_name):
 @click.command()
 @click.argument('blueprint')
 @click.option('--layer', '-l')
-def main(blueprint, layer):
+@click.option('--network', '-n')
+@click.option('--optional_also', '-o', is_flag=True)
+def main(blueprint, layer, network, optional_also):
     """Takes blueprint name and optional layer. Validates inputs and derives
     yaml location from blueprint name. Invokes validate on blue print.
     """
+    global _OPTIONAL_ALSO  # pylint: disable=global-statement
+    global _SUBNET # pylint: disable=global-statement
     mypath = Path(__file__).absolute()
     yaml_loc = mypath.parents[0].joinpath('bluval-{}.yaml'.format(blueprint))
     if layer is not None:
         layer = layer.lower()
+    if optional_also:
+        _OPTIONAL_ALSO = True
+        print("_OPTIONAL_ALSO {}".format(_OPTIONAL_ALSO))
+    if network is not None:
+        _SUBNET = " --net=" + network
+        print("Using", _SUBNET)
     try:
         invoke_dockers(yaml_loc, layer, blueprint)
     except ShowStopperError as err: