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 |
| 23 | import time |
| 24 | import subprocess |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 25 | import testCaseUtils |
| 26 | import urlparse |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 27 | |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 28 | class VolthaMngr(object): |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 29 | |
| 30 | """ |
| 31 | This class implements voltha startup/shutdown callable helper functions |
| 32 | """ |
| 33 | def __init__(self): |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 34 | self.dirs = {} |
| 35 | self.dirs ['root'] = None |
| 36 | self.dirs ['voltha'] = None |
| 37 | self.dirs ['log'] = None |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 38 | |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 39 | 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 Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 44 | stdout=subprocess.PIPE, |
| 45 | stderr=subprocess.PIPE) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 46 | 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 Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 90 | stdout=subprocess.PIPE, |
| 91 | stderr=subprocess.PIPE) |
| 92 | proc1.stdout.close |
| 93 | out, err = proc2.communicate() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 94 | print (out) |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 95 | if out: |
| 96 | for line in out.split('\n'): |
| 97 | items = line.split() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 98 | 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 Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 114 | |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 115 | |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 116 | def voltha_Initialize(rootDir, volthaDir, logDir): |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 117 | voltha = VolthaMngr() |
| 118 | voltha.vSetLogDirs(rootDir, volthaDir, logDir) |
| 119 | voltha.stopAllPods() |
| 120 | voltha.resetKubeAdm() |
| 121 | voltha.startAllPods() |
| 122 | voltha.alterOnosNetCfg() |
| 123 | voltha.collectPodLogs() |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 124 | |