Merge "[RECV-94] Separate docker/robot invoking"
[validation.git] / bluval / blucon.py
1 #!/usr/bin/python3
2 ##############################################################################
3 # Copyright (c) 2019 AT&T Intellectual Property.                             #
4 # Copyright (c) 2019 Nokia.                                                  #
5 #                                                                            #
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may    #
7 # not use this file except in compliance with the License.                   #
8 #                                                                            #
9 # You may obtain a copy of the License at                                    #
10 #       http://www.apache.org/licenses/LICENSE-2.0                           #
11 #                                                                            #
12 # Unless required by applicable law or agreed to in writing, software        #
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT  #
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.           #
15 # See the License for the specific language governing permissions and        #
16 # limitations under the License.                                             #
17 ##############################################################################
18 """This module parses yaml file, reads layers runs container for each layer.
19 """
20
21 import subprocess
22 import sys
23 import traceback
24 from pathlib import Path
25
26 import click
27 import yaml
28
29 from bluutil import BluvalError
30 from bluutil import ShowStopperError
31
32 def invoke_docker(bluprint, layer):
33     """Start docker container for given layer
34     """
35     cmd = ("docker run"
36            " -v $HOME/.ssh:/root/.ssh"
37            " -v $HOME/.kube/config:/root/.kube/config"
38            " -v $VALIDATION_HOME/tests/variables.yaml:"
39            "/opt/akraino/validation/tests/variables.yaml"
40            " -v $AKRAINO_HOME/results:/opt/akraino/results"
41            " akraino/validation:{0}-latest"
42            " bin/sh -c"
43            " 'cd /opt/akraino/validation "
44            "&& python bluval/bluval.py -l {0} {1}'").format(layer, bluprint)
45     args = [cmd]
46     try:
47         print('Invoking {}'.format(args))
48         subprocess.call(args, shell=True)
49     except OSError:
50         #print('Error while executing {}'.format(args))
51         raise BluvalError(OSError)
52
53
54 def invoke_dockers(yaml_loc, layer, blueprint_name):
55     """Parses yaml file and starts docker container for one/all layers
56     """
57     with open(str(yaml_loc)) as yaml_file:
58         yamldoc = yaml.safe_load(yaml_file)
59     blueprint = yamldoc['blueprint']
60     if layer is None or layer == "all":
61         for each_layer in blueprint['layers']:
62             invoke_docker(blueprint_name, each_layer)
63     else:
64         invoke_docker(blueprint_name, layer)
65
66
67 @click.command()
68 @click.argument('blueprint')
69 @click.option('--layer', '-l')
70 def main(blueprint, layer):
71     """Takes blueprint name and optional layer. Validates inputs and derives
72     yaml location from blueprint name. Invokes validate on blue print.
73     """
74     mypath = Path(__file__).absolute()
75     yaml_loc = mypath.parents[0].joinpath('bluval-{}.yaml'.format(blueprint))
76     if layer is not None:
77         layer = layer.lower()
78     try:
79         invoke_dockers(yaml_loc, layer, blueprint)
80     except ShowStopperError as err:
81         print('ShowStopperError:', err)
82     except BluvalError as err:
83         print('Unexpected BluvalError', err)
84         raise
85     except:
86         print("Exception in user code:")
87         print("-"*60)
88         traceback.print_exc(file=sys.stdout)
89         print("-"*60)
90         raise
91
92
93 if __name__ == "__main__":
94     # pylint: disable=no-value-for-parameter
95     main()