blob: 9b314c278520777a61950565e267d83376856cae [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')
Kailash8ae83ac2019-02-11 12:23:52 -080093 if 'onos' not in podName:
94 os.system('/usr/bin/kubectl logs -n %s -f %s > %s/%s.log 2>&1 &' %
Gilles Depatie0bf31352019-02-04 13:48:41 -050095 (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName))
Kailash8ae83ac2019-02-11 12:23:52 -080096 else:
97 os.system('/usr/bin/kubectl logs -n %s -f %s onos > %s/%s.log 2>&1 &' %
Gilles Depatie0bf31352019-02-04 13:48:41 -050098 (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName))
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040099
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400100
Gilles Depatie0bf31352019-02-04 13:48:41 -0500101def get_all_running_pods():
102 allRunningPods = []
103 proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'pods', '--all-namespaces'],
104 stdout=subprocess.PIPE,
105 stderr=subprocess.PIPE)
106 proc2 = subprocess.Popen(['grep', '-v', 'NAMESPACE'], stdin=proc1.stdout,
107 stdout=subprocess.PIPE,
108 stderr=subprocess.PIPE)
109 proc1.stdout.close()
110 out, err = proc2.communicate()
111 print(out)
112 if out:
113 for line in out.split('\n'):
114 items = line.split()
115 if len(items) > 2:
116 nsName = dict()
117 nsName['NS'] = items[0]
118 nsName['Name'] = items[1]
119 allRunningPods.append(nsName)
120 return allRunningPods
Gilles Depatiec68b3ad2018-08-21 16:29:03 -0400121
Gilles Depatie0bf31352019-02-04 13:48:41 -0500122
123def voltha_initialize(root_dir, voltha_dir, log_dir):
124 voltha = VolthaMngr()
125 voltha.v_set_log_dirs(root_dir, voltha_dir, log_dir)
126 voltha.stop_all_pods()
127 voltha.reset_kube_adm()
128 voltha.start_all_pods()
129 voltha.alter_onos_net_cfg()
130 voltha.collect_pod_logs()