blob: 1978f82501dbeb310b88f3da6f740d288e99f897 [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
23import time
24import subprocess
Gilles Depatie84cb1e72018-10-26 12:41:33 -040025import testCaseUtils
Gilles Depatie1be639b2018-12-06 10:51:08 -050026import logging
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040027
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 Depatie84cb1e72018-10-26 12:41:33 -040034 self.dirs = {}
35 self.dirs ['root'] = None
36 self.dirs ['voltha'] = None
37 self.dirs ['log'] = None
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040038
Gilles Depatie84cb1e72018-10-26 12:41:33 -040039 def vSetLogDirs(self, rootDir, volthaDir, logDir):
40 testCaseUtils.configDirs(self, logDir, rootDir, volthaDir)
41
42 def startAllPods(self):
43 proc1 = subprocess.Popen([testCaseUtils.getDir(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)
48 proc1.stdout.close
49
50 def stopAllPods(self):
51 proc1 = subprocess.Popen([testCaseUtils.getDir(self, 'root') + '/build.sh', 'stop'],
52 stdout=subprocess.PIPE,
53 stderr=subprocess.PIPE)
54 output = proc1.communicate()[0]
55 print(output)
56 proc1.stdout.close
57
58 def resetKubeAdm(self):
59 proc1 = subprocess.Popen([testCaseUtils.getDir(self, 'root') + '/build.sh', 'clear'],
60 stdout=subprocess.PIPE,
61 stderr=subprocess.PIPE)
62 output = proc1.communicate()[0]
63 print(output)
64 proc1.stdout.close
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 """
72 def alterOnosNetCfg(self):
Gilles Depatie1be639b2018-12-06 10:51:08 -050073 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.getDir(self, 'voltha'))
Gilles Depatie84cb1e72018-10-26 12:41:33 -040077 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
Gilles Depatie1be639b2018-12-06 10:51:08 -050078 'http://localhost:30120/onos/v1/network/configuration/devices/ -d @%s/tests/atests/build/devices_json'
79 % testCaseUtils.getDir(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.getDir(self, 'voltha'))
Gilles Depatie84cb1e72018-10-26 12:41:33 -040083 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
Gilles Depatie1be639b2018-12-06 10:51:08 -050084 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/sadis_json'
85 % testCaseUtils.getDir(self, 'voltha'))
86
Gilles Depatie84cb1e72018-10-26 12:41:33 -040087 def getAllRunningPods(self):
88 allRunningPods = []
89 proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'pods', '--all-namespaces'],
90 stdout=subprocess.PIPE,
91 stderr=subprocess.PIPE)
92 proc2 = subprocess.Popen(['grep', '-v', 'NAMESPACE'], stdin=proc1.stdout,
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040093 stdout=subprocess.PIPE,
94 stderr=subprocess.PIPE)
95 proc1.stdout.close
96 out, err = proc2.communicate()
Gilles Depatie1be639b2018-12-06 10:51:08 -050097 print(out)
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040098 if out:
99 for line in out.split('\n'):
100 items = line.split()
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400101 nsName = {}
102 if len(items) > 2:
103 nsName = {}
104 nsName['NS'] = items[0]
105 nsName['Name'] = items[1]
106 allRunningPods.append(nsName)
107 return allRunningPods
108
109 def collectPodLogs(self):
Gilles Depatie1be639b2018-12-06 10:51:08 -0500110 logging.info('Collect logs from all Pods')
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400111 allRunningPods = self.getAllRunningPods()
112 for nsName in allRunningPods:
113 Namespace = nsName.get('NS')
114 podName = nsName.get('Name')
115 os.system('/usr/bin/kubectl logs -n %s -f %s > %s/%s.log 2>&1 &' %
116 (Namespace, podName, testCaseUtils.getDir(self, 'log'), podName))
Gilles Depatiec68b3ad2018-08-21 16:29:03 -0400117
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400118
Gilles Depatiec68b3ad2018-08-21 16:29:03 -0400119def voltha_Initialize(rootDir, volthaDir, logDir):
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400120 voltha = VolthaMngr()
121 voltha.vSetLogDirs(rootDir, volthaDir, logDir)
122 voltha.stopAllPods()
123 voltha.resetKubeAdm()
124 voltha.startAllPods()
125 voltha.alterOnosNetCfg()
126 voltha.collectPodLogs()
Gilles Depatiec68b3ad2018-08-21 16:29:03 -0400127