blob: 20b45bca8f3bfae0c95191fcfeb1bbc2ebf9277d [file] [log] [blame]
Gilles Depatiec68b3ad2018-08-21 16:29:03 -04001#
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"""
18vOLT-HA Start/Stop module
19"""
20
21
22import os
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040023import subprocess
Gilles Depatie84cb1e72018-10-26 12:41:33 -040024import testCaseUtils
Gilles Depatie1be639b2018-12-06 10:51:08 -050025import logging
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040026
Gilles Depatie0bf31352019-02-04 13:48:41 -050027
Gilles Depatie84cb1e72018-10-26 12:41:33 -040028class VolthaMngr(object):
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040029
30 """
31 This class implements voltha startup/shutdown callable helper functions
32 """
33 def __init__(self):
Gilles Depatie0bf31352019-02-04 13:48:41 -050034 self.dirs = dict()
35 self.dirs['root'] = None
36 self.dirs['voltha'] = None
37 self.dirs['log'] = None
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040038
Gilles Depatie0bf31352019-02-04 13:48:41 -050039 def v_set_log_dirs(self, root_dir, voltha_dir, log_dir):
40 testCaseUtils.config_dirs(self, log_dir, root_dir, voltha_dir)
Gilles Depatie84cb1e72018-10-26 12:41:33 -040041
Gilles Depatie0bf31352019-02-04 13:48:41 -050042 def start_all_pods(self):
43 proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'start'],
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040044 stdout=subprocess.PIPE,
45 stderr=subprocess.PIPE)
Gilles Depatie84cb1e72018-10-26 12:41:33 -040046 output = proc1.communicate()[0]
47 print(output)
Gilles Depatie0bf31352019-02-04 13:48:41 -050048 proc1.stdout.close()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040049
Gilles Depatie0bf31352019-02-04 13:48:41 -050050 def stop_all_pods(self):
51 proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'stop'],
Gilles Depatie84cb1e72018-10-26 12:41:33 -040052 stdout=subprocess.PIPE,
53 stderr=subprocess.PIPE)
54 output = proc1.communicate()[0]
55 print(output)
Gilles Depatie0bf31352019-02-04 13:48:41 -050056 proc1.stdout.close()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040057
Gilles Depatie0bf31352019-02-04 13:48:41 -050058 def reset_kube_adm(self):
59 proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'clear'],
Gilles Depatie84cb1e72018-10-26 12:41:33 -040060 stdout=subprocess.PIPE,
61 stderr=subprocess.PIPE)
62 output = proc1.communicate()[0]
63 print(output)
Gilles Depatie0bf31352019-02-04 13:48:41 -050064 proc1.stdout.close()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040065
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 Depatie0bf31352019-02-04 13:48:41 -050072 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 Depatie84cb1e72018-10-26 12:41:33 -040077 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
Gilles Depatie0bf31352019-02-04 13:48:41 -050078 '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 Depatie84cb1e72018-10-26 12:41:33 -040083 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
Gilles Depatie0bf31352019-02-04 13:48:41 -050084 '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 Depatie1be639b2018-12-06 10:51:08 -050088 logging.info('Collect logs from all Pods')
Gilles Depatie0bf31352019-02-04 13:48:41 -050089 allRunningPods = get_all_running_pods()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040090 for nsName in allRunningPods:
91 Namespace = nsName.get('NS')
Gilles Depatie0bf31352019-02-04 13:48:41 -050092 podName = nsName.get('Name')
Gilles Depatie82dd2022019-02-19 14:31:33 -050093 if 'onos' in podName:
Kailash8ae83ac2019-02-11 12:23:52 -080094 os.system('/usr/bin/kubectl logs -n %s -f %s onos > %s/%s.log 2>&1 &' %
Gilles Depatie0bf31352019-02-04 13:48:41 -050095 (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName))
Gilles Depatie82dd2022019-02-19 14:31:33 -050096 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 Depatiec68b3ad2018-08-21 16:29:03 -0400102
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400103
Gilles Depatie0bf31352019-02-04 13:48:41 -0500104def 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 Depatiec68b3ad2018-08-21 16:29:03 -0400124
Gilles Depatie0bf31352019-02-04 13:48:41 -0500125
126def 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()