admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ''' |
| 3 | Created on 31-May-2013 |
| 4 | |
| 5 | @author: Anil Kumar (anilkumar.s@paxterrasolutions.com) |
| 6 | |
| 7 | |
| 8 | TestON is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | TestON is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | |
| 22 | ZookeeperCliDriver is the basic driver which will handle the Zookeeper functions |
| 23 | ''' |
| 24 | |
| 25 | import pexpect |
| 26 | import struct |
| 27 | import fcntl |
| 28 | import os |
| 29 | import signal |
| 30 | import re |
| 31 | import sys |
| 32 | import core.teston |
| 33 | import time |
| 34 | |
| 35 | sys.path.append("../") |
| 36 | from drivers.common.clidriver import CLI |
| 37 | |
| 38 | class ZookeeperCliDriver(CLI): |
| 39 | ''' |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 40 | ZookeeperCliDriver is the basic driver which will handle the Zookeeper's functions |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 41 | ''' |
| 42 | def __init__(self): |
| 43 | super(CLI, self).__init__() |
| 44 | self.handle = self |
| 45 | self.wrapped = sys.modules[__name__] |
| 46 | |
admin | 680b78c | 2014-08-08 11:46:45 -0700 | [diff] [blame] | 47 | def kill(self): |
| 48 | import re |
| 49 | try: |
| 50 | self.handle.sendline("ps -ef |grep 'zookeeper.log.dir' |awk 'NR==1 {print $2}' |xargs sudo kill -9") |
| 51 | self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT]) |
| 52 | self.handle.sendline("ps -ef |grep 'zookeeper.log.dir' |wc -l") |
| 53 | self.handle.expect(["wc -l",pexpect.EOF,pexpect.TIMEOUT]) |
| 54 | response = self.handle.after |
| 55 | if re.search("1",response): |
| 56 | return "Zookeeper Killed!" |
| 57 | else: |
| 58 | return "ERROR!!! ZOOKEEPER MAY NOT HAVE BEEN KILLED PROPERLY!!!" |
| 59 | except pexpect.EOF: |
| 60 | main.log.error(self.name + ": EOF exception found") |
| 61 | main.log.error(self.hane + ": " + self.handle.before) |
| 62 | main.cleanup() |
| 63 | main.exit() |
| 64 | except: |
| 65 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 66 | main.log.error( traceback.print_exc() ) |
| 67 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 68 | main.cleanup() |
| 69 | main.exit() |
| 70 | |
| 71 | |
| 72 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 73 | def connect(self, **connectargs): |
| 74 | # Here the main is the TestON instance after creating all the log handles. |
| 75 | self.port = None |
| 76 | for key in connectargs: |
| 77 | vars(self)[key] = connectargs[key] |
Jon Hall | 4a2b048 | 2014-04-18 16:29:26 -0700 | [diff] [blame] | 78 | self.home = "~/ONOS" |
admin | 803f05d | 2014-07-18 10:44:38 -0700 | [diff] [blame] | 79 | self.zkhome = "~/zookeeper-3.4.6" |
| 80 | self.clustername = "sanity-rc-onos" |
Jon Hall | 4a2b048 | 2014-04-18 16:29:26 -0700 | [diff] [blame] | 81 | #self.home = "~/zookeeper-3.4.5" |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 82 | for key in self.options: |
| 83 | if key == "home": |
| 84 | self.home = self.options['home'] |
| 85 | break |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 86 | |
| 87 | self.name = self.options['name'] |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 88 | self.handle = super(ZookeeperCliDriver, self).connect(user_name = self.user_name, ip_address = self.ip_address,port = self.port, pwd = self.pwd, home = self.home) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 89 | |
| 90 | self.ssh_handle = self.handle |
| 91 | if self.handle : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 92 | return main.TRUE |
| 93 | else : |
| 94 | main.log.error("Connection failed to the host "+self.user_name+"@"+self.ip_address) |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 95 | main.log.error(self.name + ": Failed to connect to Zookeeper") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 96 | return main.FALSE |
| 97 | |
| 98 | |
| 99 | def start(self): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 100 | ''' |
| 101 | This Function will start the Zookeeper |
| 102 | ''' |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 103 | main.log.info(self.name + ": Starting Zookeeper" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 104 | self.handle.sendline("") |
| 105 | self.handle.expect("\$") |
Jon Hall | 4a2b048 | 2014-04-18 16:29:26 -0700 | [diff] [blame] | 106 | self.handle.sendline("cd "+self.home) |
| 107 | self.handle.sendline("./onos.sh zk start") |
| 108 | self.handle.expect("zk start") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 109 | self.handle.expect("\$") |
| 110 | response = self.handle.before + self.handle.after |
| 111 | if re.search("STARTED", response): |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 112 | main.log.info(self.name + ": Zookeeper Started ") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 113 | return main.TRUE |
| 114 | elif re.search("running", response): |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 115 | main.log.warn(self.name +": zookeeper ... already running") |
| 116 | return main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 117 | else: |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 118 | main.log.error(self.name + ": Failed to start Zookeeper"+ response) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 119 | return main.FALSE |
| 120 | |
| 121 | def status(self): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 122 | ''' |
| 123 | This Function will return the Status of the Zookeeper |
| 124 | ''' |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 125 | self.execute(cmd="\n",prompt="\$",timeout=10) |
Jon Hall | 4a2b048 | 2014-04-18 16:29:26 -0700 | [diff] [blame] | 126 | self.handle.sendline("cd "+self.home) |
| 127 | response = self.execute(cmd="./onos.sh zk status ",prompt="JMX",timeout=10) |
admin | 680b78c | 2014-08-08 11:46:45 -0700 | [diff] [blame] | 128 | response=self.handle.after |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 129 | self.execute(cmd="\n",prompt="\$",timeout=10) |
admin | 680b78c | 2014-08-08 11:46:45 -0700 | [diff] [blame] | 130 | return self.handle.before + self.handle.after |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 131 | |
| 132 | def stop(self): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 133 | ''' |
| 134 | This Function will stop the Zookeeper if it is Running |
| 135 | ''' |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 136 | self.execute(cmd="\n",prompt="\$",timeout=10) |
Jon Hall | bd795bf | 2014-06-18 09:46:32 -0700 | [diff] [blame] | 137 | time.sleep(1) |
Jon Hall | 4a2b048 | 2014-04-18 16:29:26 -0700 | [diff] [blame] | 138 | self.handle.sendline("cd "+self.home) |
| 139 | response = self.execute(cmd="./onos.sh zk stop ",prompt="$",timeout=10) |
| 140 | if re.search("stopping",response): |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 141 | main.log.info(self.name + ": Zookeeper Stopped") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 142 | return main.TRUE |
| 143 | else: |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 144 | main.log.warn(self.name + ": No zookeeper to stop") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 145 | return main.FALSE |
| 146 | |
| 147 | def disconnect(self): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 148 | ''' |
| 149 | Called at the end of the test to disconnect the ZK handle |
| 150 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 151 | response = '' |
| 152 | if self.handle: |
| 153 | self.handle.sendline("exit") |
| 154 | self.handle.expect("closed") |
| 155 | else : |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 156 | main.log.error(self.name + ": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 157 | response = main.FALSE |
| 158 | return response |
admin | 6903e46 | 2014-07-22 15:30:54 -0700 | [diff] [blame] | 159 | |
| 160 | #********************************************************************************************** |
| 161 | #********************************************************************************************** |
| 162 | # findMaster is used to determine the master controller of a switch. |
| 163 | # it uses the switchList which is a json dict, and finds the first controller of |
| 164 | # each switch |
| 165 | #********************************************************************************************** |
| 166 | #********************************************************************************************** |
| 167 | |
| 168 | |
admin | 5f900c0 | 2014-07-22 15:33:50 -0700 | [diff] [blame] | 169 | def findMaster(self, switchDPID, switchList): |
admin | 803f05d | 2014-07-18 10:44:38 -0700 | [diff] [blame] | 170 | import json |
admin | 6903e46 | 2014-07-22 15:30:54 -0700 | [diff] [blame] | 171 | decoded = json.loads(switchList) |
admin | 680b78c | 2014-08-08 11:46:45 -0700 | [diff] [blame] | 172 | if switchList=="": |
| 173 | return "NO CONTROLLERS FOUND" |
admin | 803f05d | 2014-07-18 10:44:38 -0700 | [diff] [blame] | 174 | for k in decoded.iteritems(): |
| 175 | k2 = json.dumps(k) |
| 176 | if re.search(switchDPID,k2): |
| 177 | k3 = k2.split(',') |
| 178 | k4 = k3[1].split() |
| 179 | k5 = k4[1].split('"') |
admin | 803f05d | 2014-07-18 10:44:38 -0700 | [diff] [blame] | 180 | return k5[1] |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 181 | return "NO CONTROLLERS FOUND" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 182 | |
| 183 | def isup(self): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 184 | ''' |
| 185 | Calls the zookeeper status and returns TRUE if it has an assigned Mode to it. |
| 186 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 187 | self.execute(cmd="\n",prompt="\$",timeout=10) |
Jon Hall | e80ef8c | 2014-04-29 15:29:13 -0700 | [diff] [blame] | 188 | response = self.execute(cmd=self.home + "/onos.sh zk status ",prompt="Mode",timeout=10) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 189 | pattern = '(.*)Mode(.*)' |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 190 | if re.search(pattern, response): |
| 191 | main.log.info(self.name + ": Zookeeper is up.") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 192 | return main.TRUE |
| 193 | else: |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 194 | main.log.info(self.name + ": Zookeeper is down.") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 195 | return main.FALSE |
| 196 | |
| 197 | |