blob: 5257dfa2dee164de0087c28bb7fcd48acee34db1 [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
26import urlparse
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):
73 print ('Altering the Onos NetCfg to suit Voltha\'s needs')
74 time.sleep(30)
75 onosIp = testCaseUtils.extractIpAddr("onos-ui")
76 netloc = onosIp.rstrip() + ":8181"
77 devUrl = urlparse.urlunparse(('http', netloc, '/onos/v1/network/configuration/devices/', '', '', ''))
78 sadisUrl = urlparse.urlunparse(('http', netloc, '/onos/v1/network/configuration/apps/', '', '', ''))
79 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
80 '%s -d @%s/tests/atests/build/devices_json' % (devUrl, testCaseUtils.getDir(self, 'voltha')))
81 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
82 '%s -d @%s/tests/atests/build/sadis_json' % (sadisUrl, testCaseUtils.getDir(self, 'voltha')))
83
84 def getAllRunningPods(self):
85 allRunningPods = []
86 proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'pods', '--all-namespaces'],
87 stdout=subprocess.PIPE,
88 stderr=subprocess.PIPE)
89 proc2 = subprocess.Popen(['grep', '-v', 'NAMESPACE'], stdin=proc1.stdout,
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040090 stdout=subprocess.PIPE,
91 stderr=subprocess.PIPE)
92 proc1.stdout.close
93 out, err = proc2.communicate()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040094 print (out)
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040095 if out:
96 for line in out.split('\n'):
97 items = line.split()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040098 nsName = {}
99 if len(items) > 2:
100 nsName = {}
101 nsName['NS'] = items[0]
102 nsName['Name'] = items[1]
103 allRunningPods.append(nsName)
104 return allRunningPods
105
106 def collectPodLogs(self):
107 print('Collect logs from all Pods')
108 allRunningPods = self.getAllRunningPods()
109 for nsName in allRunningPods:
110 Namespace = nsName.get('NS')
111 podName = nsName.get('Name')
112 os.system('/usr/bin/kubectl logs -n %s -f %s > %s/%s.log 2>&1 &' %
113 (Namespace, podName, testCaseUtils.getDir(self, 'log'), podName))
Gilles Depatiec68b3ad2018-08-21 16:29:03 -0400114
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400115
Gilles Depatiec68b3ad2018-08-21 16:29:03 -0400116def voltha_Initialize(rootDir, volthaDir, logDir):
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400117 voltha = VolthaMngr()
118 voltha.vSetLogDirs(rootDir, volthaDir, logDir)
119 voltha.stopAllPods()
120 voltha.resetKubeAdm()
121 voltha.startAllPods()
122 voltha.alterOnosNetCfg()
123 voltha.collectPodLogs()
Gilles Depatiec68b3ad2018-08-21 16:29:03 -0400124