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 |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 26 | import logging |
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): |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 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.getDir(self, 'voltha')) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 77 | os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" ' |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 78 | '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 Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 83 | os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" ' |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 84 | 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/sadis_json' |
| 85 | % testCaseUtils.getDir(self, 'voltha')) |
| 86 | |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 87 | 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 Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 93 | stdout=subprocess.PIPE, |
| 94 | stderr=subprocess.PIPE) |
| 95 | proc1.stdout.close |
| 96 | out, err = proc2.communicate() |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 97 | print(out) |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 98 | if out: |
| 99 | for line in out.split('\n'): |
| 100 | items = line.split() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 101 | 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 Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 110 | logging.info('Collect logs from all Pods') |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 111 | 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 Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 117 | |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 118 | |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 119 | def voltha_Initialize(rootDir, volthaDir, logDir): |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 120 | voltha = VolthaMngr() |
| 121 | voltha.vSetLogDirs(rootDir, volthaDir, logDir) |
| 122 | voltha.stopAllPods() |
| 123 | voltha.resetKubeAdm() |
| 124 | voltha.startAllPods() |
| 125 | voltha.alterOnosNetCfg() |
| 126 | voltha.collectPodLogs() |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 127 | |