blob: 553f7c04a8211697aef73addc6a7b6dfb34a0bb4 [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
25import paramiko
26import spur
27
28class volthaMngr(object):
29
30 """
31 This class implements voltha startup/shutdown callable helper functions
32 """
33 def __init__(self):
34 self.__rootDir = None
35 self.__volthaDir = None
36 self.__logDir = None
37 self.__rootSsh = None
38
39 def configDir(self, rootDir, volthaDir, logDir):
40 self.__rootDir = rootDir
41 self.__volthaDir = volthaDir
42 self.__logDir = logDir
43
44 os.chdir(volthaDir)
45
46 def openRootSsh(self):
47 shell = spur.SshShell(hostname='localhost', username='root',
48 password='root',
49 missing_host_key=spur.ssh.MissingHostKey.accept)
50 return shell
51
52 def getAllRunningContainers(self):
53 allContainers = []
54 proc1 = subprocess.Popen(['docker', 'ps', '-a'],
55 stdout=subprocess.PIPE,
56 stderr=subprocess.PIPE)
57 proc2 = subprocess.Popen(['grep', '-v', 'CONT'], stdin=proc1.stdout,
58 stdout=subprocess.PIPE,
59 stderr=subprocess.PIPE)
60 proc1.stdout.close
61 out, err = proc2.communicate()
62 if out:
63 for line in out.split('\n'):
64 items = line.split()
65 if len(items):
66 allContainers.append(items)
67 return allContainers
68
69 def stopPonsim(self):
70 command = "for pid in $(ps -ef | grep ponsim | grep -v grep | " \
71 "awk '{print $2}'); do echo $pid; done"
72 client = paramiko.SSHClient()
73 client.load_system_host_keys()
74 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
75 client.connect('localhost', username='root', password='root')
76 transport = client.get_transport()
77 channel = transport.open_session()
78
79 channel.exec_command(command)
80 procIds = channel.recv(4096).replace('\n', ' ')
81 channel = transport.open_session()
82 channel.exec_command('sudo kill -9 %s' % procIds)
83
84 def removeExistingContainers(self):
85 allContainers = self.getAllRunningContainers()
86 for container in allContainers:
87 procID = container[0]
88 os.system('docker rm -f %s > /dev/null 2>&1' % procID)
89
90 def startVolthaContainers(self):
91 print('Start VOLTHA containers')
92 # Bring up all the containers required for VOLTHA (total 15)
93 os.system(
94 'docker-compose -f compose/docker-compose-system-test.yml '
95 'up -d > %s/start_voltha_containers.log 2>&1' %
96 self.__logDir)
97
98 def collectAllLogs(self):
99 print('Collect all VOLTHA container logs')
100 allContainers = self.getAllRunningContainers()
101 for container in allContainers:
102 containerName = container[-1]
103 os.system('docker logs --since 0m -f %s > %s/%s.log 2>&1 &' %
104 (containerName, self.__logDir, containerName))
105
106 def enableBridge(self):
107 self.__rootSsh = self.openRootSsh()
108 result = self.__rootSsh.run([self.__rootDir + '/enable_bridge.sh'])
109 print(result.output)
110
111 def startPonsim(self, onusAmount=1):
112 command = 'source env.sh ; ./ponsim/main.py -v'
113 if onusAmount > 1:
114 command += ' -o %s' % onusAmount
115 ponsimLog = open('%s/ponsim.log' % self.__logDir, 'w')
116 process = self.__rootSsh.spawn(['bash', '-c', command],
117 cwd=self.__volthaDir, store_pid=True,
118 stdout=ponsimLog)
119 return process.pid
120
121
122def voltha_Initialize(rootDir, volthaDir, logDir):
123
124 voltha = volthaMngr()
125 voltha.configDir(rootDir, volthaDir, logDir)
126 voltha.stopPonsim()
127 voltha.removeExistingContainers()
128 voltha.startVolthaContainers()
129 voltha.collectAllLogs()
130 voltha.enableBridge()
131 voltha.startPonsim(3)
132