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 | ea42371 | 2019-04-12 16:39:12 -0400 | [diff] [blame] | 28 | |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 29 | class VolthaMngr(object): |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 30 | |
| 31 | """ |
| 32 | This class implements voltha startup/shutdown callable helper functions |
| 33 | """ |
Gilles Depatie | ea42371 | 2019-04-12 16:39:12 -0400 | [diff] [blame] | 34 | |
| 35 | DEFAULT_ADAPTER = 'ponsim' |
| 36 | |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 37 | def __init__(self): |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 38 | self.dirs = dict() |
| 39 | self.dirs['root'] = None |
| 40 | self.dirs['voltha'] = None |
| 41 | self.dirs['log'] = None |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 42 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 43 | def v_set_log_dirs(self, root_dir, voltha_dir, log_dir): |
| 44 | testCaseUtils.config_dirs(self, log_dir, root_dir, voltha_dir) |
Gilles Depatie | ea42371 | 2019-04-12 16:39:12 -0400 | [diff] [blame] | 45 | |
| 46 | def start_all_pods(self, adapter=DEFAULT_ADAPTER): |
| 47 | proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'start', adapter], |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 48 | stdout=subprocess.PIPE, |
| 49 | stderr=subprocess.PIPE) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 50 | output = proc1.communicate()[0] |
| 51 | print(output) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 52 | proc1.stdout.close() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 53 | |
Gilles Depatie | ea42371 | 2019-04-12 16:39:12 -0400 | [diff] [blame] | 54 | def stop_all_pods(self, adapter=DEFAULT_ADAPTER): |
| 55 | proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'stop', adapter], |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 56 | stdout=subprocess.PIPE, |
| 57 | stderr=subprocess.PIPE) |
| 58 | output = proc1.communicate()[0] |
| 59 | print(output) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 60 | proc1.stdout.close() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 61 | |
Gilles Depatie | ea42371 | 2019-04-12 16:39:12 -0400 | [diff] [blame] | 62 | def reset_kube_adm(self, adapter=DEFAULT_ADAPTER): |
| 63 | proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'clear', adapter], |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 64 | stdout=subprocess.PIPE, |
| 65 | stderr=subprocess.PIPE) |
| 66 | output = proc1.communicate()[0] |
| 67 | print(output) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 68 | proc1.stdout.close() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 69 | |
| 70 | """ |
| 71 | Because we are not deploying SEBA with XOS and NEM, and that a standalone Voltha |
| 72 | deployment is not common, in order to get flows to work, we need to alter Onos |
| 73 | NetCfg in two fashion. |
| 74 | One is to add to device list and the other is to add the missing Sadis section |
| 75 | """ |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 76 | def alter_onos_net_cfg(self): |
| 77 | logging.info('Altering the Onos NetCfg to suit Voltha\'s needs') |
| 78 | logging.debug('curl --user karaf:karaf -X POST -H "Content-Type: application/json" ' |
| 79 | 'http://localhost:30120/onos/v1/network/configuration/devices/ -d @%s/tests/atests/build/devices_json' |
| 80 | % testCaseUtils.get_dir(self, 'voltha')) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 81 | 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] | 82 | 'http://localhost:30120/onos/v1/network/configuration/devices/ -d @%s/tests/atests/build/devices_json' |
| 83 | % testCaseUtils.get_dir(self, 'voltha')) |
| 84 | logging.debug('curl --user karaf:karaf -X POST -H "Content-Type: application/json" ' |
| 85 | 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/sadis_json' |
| 86 | % testCaseUtils.get_dir(self, 'voltha')) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 87 | 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] | 88 | 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/sadis_json' |
| 89 | % testCaseUtils.get_dir(self, 'voltha')) |
| 90 | |
| 91 | def collect_pod_logs(self): |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 92 | logging.info('Collect logs from all Pods') |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 93 | allRunningPods = get_all_running_pods() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 94 | for nsName in allRunningPods: |
| 95 | Namespace = nsName.get('NS') |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 96 | podName = nsName.get('Name') |
Gilles Depatie | 82dd202 | 2019-02-19 14:31:33 -0500 | [diff] [blame] | 97 | if 'onos' in podName: |
Kailash | 8ae83ac | 2019-02-11 12:23:52 -0800 | [diff] [blame] | 98 | 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] | 99 | (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName)) |
Gilles Depatie | 82dd202 | 2019-02-19 14:31:33 -0500 | [diff] [blame] | 100 | elif 'calico-node' in podName: |
| 101 | os.system('/usr/bin/kubectl logs -n %s -f %s calico-node > %s/%s.log 2>&1 &' % |
| 102 | (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName)) |
| 103 | else: |
| 104 | os.system('/usr/bin/kubectl logs -n %s -f %s > %s/%s.log 2>&1 &' % |
| 105 | (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName)) |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 106 | |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 107 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 108 | def get_all_running_pods(): |
| 109 | allRunningPods = [] |
| 110 | proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'pods', '--all-namespaces'], |
| 111 | stdout=subprocess.PIPE, |
| 112 | stderr=subprocess.PIPE) |
| 113 | proc2 = subprocess.Popen(['grep', '-v', 'NAMESPACE'], stdin=proc1.stdout, |
| 114 | stdout=subprocess.PIPE, |
| 115 | stderr=subprocess.PIPE) |
| 116 | proc1.stdout.close() |
| 117 | out, err = proc2.communicate() |
| 118 | print(out) |
| 119 | if out: |
| 120 | for line in out.split('\n'): |
| 121 | items = line.split() |
| 122 | if len(items) > 2: |
| 123 | nsName = dict() |
| 124 | nsName['NS'] = items[0] |
| 125 | nsName['Name'] = items[1] |
| 126 | allRunningPods.append(nsName) |
| 127 | return allRunningPods |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 128 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 129 | |
Gilles Depatie | ea42371 | 2019-04-12 16:39:12 -0400 | [diff] [blame] | 130 | def voltha_initialize(root_dir, voltha_dir, log_dir, adapter): |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 131 | voltha = VolthaMngr() |
| 132 | voltha.v_set_log_dirs(root_dir, voltha_dir, log_dir) |
Gilles Depatie | ea42371 | 2019-04-12 16:39:12 -0400 | [diff] [blame] | 133 | voltha.stop_all_pods(adapter) |
| 134 | voltha.reset_kube_adm(adapter) |
| 135 | voltha.start_all_pods(adapter) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 136 | voltha.alter_onos_net_cfg() |
| 137 | voltha.collect_pod_logs() |