Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2018 the original author or authors. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | """ |
| 18 | vOLT-HA Start/Stop module |
| 19 | """ |
| 20 | |
| 21 | |
| 22 | import os |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 23 | import subprocess |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 24 | import testCaseUtils |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 25 | import logging |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 26 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 27 | |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 28 | class VolthaMngr(object): |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 29 | |
| 30 | """ |
| 31 | This class implements voltha startup/shutdown callable helper functions |
| 32 | """ |
| 33 | def __init__(self): |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 34 | self.dirs = dict() |
| 35 | self.dirs['root'] = None |
| 36 | self.dirs['voltha'] = None |
| 37 | self.dirs['log'] = None |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 38 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 39 | def v_set_log_dirs(self, root_dir, voltha_dir, log_dir): |
| 40 | testCaseUtils.config_dirs(self, log_dir, root_dir, voltha_dir) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 41 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 42 | def start_all_pods(self): |
| 43 | proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'start'], |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 44 | stdout=subprocess.PIPE, |
| 45 | stderr=subprocess.PIPE) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 46 | output = proc1.communicate()[0] |
| 47 | print(output) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 48 | proc1.stdout.close() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 49 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 50 | def stop_all_pods(self): |
| 51 | proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'stop'], |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 52 | stdout=subprocess.PIPE, |
| 53 | stderr=subprocess.PIPE) |
| 54 | output = proc1.communicate()[0] |
| 55 | print(output) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 56 | proc1.stdout.close() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 57 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 58 | def reset_kube_adm(self): |
| 59 | proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'clear'], |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 60 | stdout=subprocess.PIPE, |
| 61 | stderr=subprocess.PIPE) |
| 62 | output = proc1.communicate()[0] |
| 63 | print(output) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 64 | proc1.stdout.close() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 65 | |
| 66 | """ |
| 67 | Because we are not deploying SEBA with XOS and NEM, and that a standalone Voltha |
| 68 | deployment is not common, in order to get flows to work, we need to alter Onos |
| 69 | NetCfg in two fashion. |
| 70 | One is to add to device list and the other is to add the missing Sadis section |
| 71 | """ |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 72 | def alter_onos_net_cfg(self): |
| 73 | logging.info('Altering the Onos NetCfg to suit Voltha\'s needs') |
| 74 | logging.debug('curl --user karaf:karaf -X POST -H "Content-Type: application/json" ' |
| 75 | 'http://localhost:30120/onos/v1/network/configuration/devices/ -d @%s/tests/atests/build/devices_json' |
| 76 | % testCaseUtils.get_dir(self, 'voltha')) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 77 | os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" ' |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 78 | 'http://localhost:30120/onos/v1/network/configuration/devices/ -d @%s/tests/atests/build/devices_json' |
| 79 | % testCaseUtils.get_dir(self, 'voltha')) |
| 80 | logging.debug('curl --user karaf:karaf -X POST -H "Content-Type: application/json" ' |
| 81 | 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/sadis_json' |
| 82 | % testCaseUtils.get_dir(self, 'voltha')) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 83 | os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" ' |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 84 | 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/sadis_json' |
| 85 | % testCaseUtils.get_dir(self, 'voltha')) |
| 86 | |
| 87 | def collect_pod_logs(self): |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 88 | logging.info('Collect logs from all Pods') |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 89 | allRunningPods = get_all_running_pods() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 90 | for nsName in allRunningPods: |
| 91 | Namespace = nsName.get('NS') |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 92 | podName = nsName.get('Name') |
Gilles Depatie | 82dd202 | 2019-02-19 14:31:33 -0500 | [diff] [blame] | 93 | if 'onos' in podName: |
Kailash | 8ae83ac | 2019-02-11 12:23:52 -0800 | [diff] [blame] | 94 | os.system('/usr/bin/kubectl logs -n %s -f %s onos > %s/%s.log 2>&1 &' % |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 95 | (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName)) |
Gilles Depatie | 82dd202 | 2019-02-19 14:31:33 -0500 | [diff] [blame] | 96 | elif 'calico-node' in podName: |
| 97 | os.system('/usr/bin/kubectl logs -n %s -f %s calico-node > %s/%s.log 2>&1 &' % |
| 98 | (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName)) |
| 99 | else: |
| 100 | os.system('/usr/bin/kubectl logs -n %s -f %s > %s/%s.log 2>&1 &' % |
| 101 | (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName)) |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 102 | |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 103 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 104 | def get_all_running_pods(): |
| 105 | allRunningPods = [] |
| 106 | proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'pods', '--all-namespaces'], |
| 107 | stdout=subprocess.PIPE, |
| 108 | stderr=subprocess.PIPE) |
| 109 | proc2 = subprocess.Popen(['grep', '-v', 'NAMESPACE'], stdin=proc1.stdout, |
| 110 | stdout=subprocess.PIPE, |
| 111 | stderr=subprocess.PIPE) |
| 112 | proc1.stdout.close() |
| 113 | out, err = proc2.communicate() |
| 114 | print(out) |
| 115 | if out: |
| 116 | for line in out.split('\n'): |
| 117 | items = line.split() |
| 118 | if len(items) > 2: |
| 119 | nsName = dict() |
| 120 | nsName['NS'] = items[0] |
| 121 | nsName['Name'] = items[1] |
| 122 | allRunningPods.append(nsName) |
| 123 | return allRunningPods |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 124 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 125 | |
| 126 | def voltha_initialize(root_dir, voltha_dir, log_dir): |
| 127 | voltha = VolthaMngr() |
| 128 | voltha.v_set_log_dirs(root_dir, voltha_dir, log_dir) |
| 129 | voltha.stop_all_pods() |
| 130 | voltha.reset_kube_adm() |
| 131 | voltha.start_all_pods() |
| 132 | voltha.alter_onos_net_cfg() |
| 133 | voltha.collect_pod_logs() |