blob: b7fe4bac1c2b8bc3d48d5d41bc8422d8ba379523 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
2'''
3Created 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
22ZookeeperCliDriver is the basic driver which will handle the Zookeeper functions
23'''
24
25import pexpect
26import struct
27import fcntl
28import os
29import signal
30import re
31import sys
32import core.teston
33import time
34
35sys.path.append("../")
36from drivers.common.clidriver import CLI
37
38class ZookeeperCliDriver(CLI):
39 '''
adminaeedddd2013-08-02 15:14:15 -070040 ZookeeperCliDriver is the basic driver which will handle the Zookeeper's functions
adminbae64d82013-08-01 10:50:15 -070041 '''
42 def __init__(self):
43 super(CLI, self).__init__()
44 self.handle = self
45 self.wrapped = sys.modules[__name__]
46
47 def connect(self, **connectargs):
48 # Here the main is the TestON instance after creating all the log handles.
49 self.port = None
50 for key in connectargs:
51 vars(self)[key] = connectargs[key]
Jon Hallf89c8552014-04-02 13:14:06 -070052 self.home = "~/zookeeper-3.4.5"
53 for key in self.options:
54 if key == "home":
55 self.home = self.options['home']
56 break
adminbae64d82013-08-01 10:50:15 -070057
58 self.name = self.options['name']
Jon Hallf89c8552014-04-02 13:14:06 -070059 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)
adminbae64d82013-08-01 10:50:15 -070060
61 self.ssh_handle = self.handle
62 if self.handle :
adminbae64d82013-08-01 10:50:15 -070063 return main.TRUE
64 else :
65 main.log.error("Connection failed to the host "+self.user_name+"@"+self.ip_address)
Jon Hallf89c8552014-04-02 13:14:06 -070066 main.log.error(self.name + ": Failed to connect to Zookeeper")
adminbae64d82013-08-01 10:50:15 -070067 return main.FALSE
68
69
70 def start(self):
adminaeedddd2013-08-02 15:14:15 -070071 '''
72 This Function will start the Zookeeper
73 '''
Jon Hallf89c8552014-04-02 13:14:06 -070074 main.log.info(self.name + ": Starting Zookeeper" )
adminbae64d82013-08-01 10:50:15 -070075 self.handle.sendline("")
76 self.handle.expect("\$")
Jon Hallf89c8552014-04-02 13:14:06 -070077 self.handle.sendline(self.home + "/bin/zkServer.sh start")
adminbae64d82013-08-01 10:50:15 -070078 self.handle.expect("zkServer.sh start")
79 self.handle.expect("\$")
80 response = self.handle.before + self.handle.after
81 if re.search("STARTED", response):
Jon Hallf89c8552014-04-02 13:14:06 -070082 main.log.info(self.name + ": Zookeeper Started ")
adminbae64d82013-08-01 10:50:15 -070083 return main.TRUE
84 elif re.search("running", response):
Jon Hallf89c8552014-04-02 13:14:06 -070085 main.log.warn(self.name +": zookeeper ... already running")
86 return main.TRUE
adminbae64d82013-08-01 10:50:15 -070087 else:
Jon Hallf89c8552014-04-02 13:14:06 -070088 main.log.error(self.name + ": Failed to start Zookeeper"+ response)
adminbae64d82013-08-01 10:50:15 -070089 return main.FALSE
90
91 def status(self):
adminaeedddd2013-08-02 15:14:15 -070092 '''
93 This Function will return the Status of the Zookeeper
94 '''
adminbae64d82013-08-01 10:50:15 -070095 time.sleep(5)
Jon Hallf89c8552014-04-02 13:14:06 -070096 self.execute(cmd="\n",prompt="\$",timeout=10)
97 response = self.execute(cmd=self.home + "/bin/zkServer.sh status ",prompt="JMX",timeout=10)
98
99 self.execute(cmd="\n",prompt="\$",timeout=10)
adminbae64d82013-08-01 10:50:15 -0700100 return response
101
102 def stop(self):
adminaeedddd2013-08-02 15:14:15 -0700103 '''
104 This Function will stop the Zookeeper if it is Running
105 '''
Jon Hallf89c8552014-04-02 13:14:06 -0700106 self.execute(cmd="\n",prompt="\$",timeout=10)
adminbae64d82013-08-01 10:50:15 -0700107 time.sleep(5)
Jon Hallf89c8552014-04-02 13:14:06 -0700108 response = self.execute(cmd=self.home + "/bin/zkServer.sh stop ",prompt="STOPPED",timeout=10)
109 self.execute(cmd="\n",prompt="\$",timeout=10)
adminbae64d82013-08-01 10:50:15 -0700110 if re.search("STOPPED",response):
Jon Hallf89c8552014-04-02 13:14:06 -0700111 main.log.info(self.name + ": Zookeeper Stopped")
adminbae64d82013-08-01 10:50:15 -0700112 return main.TRUE
113 else:
Jon Hallf89c8552014-04-02 13:14:06 -0700114 main.log.warn(self.name + ": No zookeeper to stop")
adminbae64d82013-08-01 10:50:15 -0700115 return main.FALSE
116
117 def disconnect(self):
adminaeedddd2013-08-02 15:14:15 -0700118 '''
119 Called at the end of the test to disconnect the ZK handle
120 '''
adminbae64d82013-08-01 10:50:15 -0700121 response = ''
122 if self.handle:
123 self.handle.sendline("exit")
124 self.handle.expect("closed")
125 else :
Jon Hallf89c8552014-04-02 13:14:06 -0700126 main.log.error(self.name + ": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700127 response = main.FALSE
128 return response
129
130 def isup(self):
adminaeedddd2013-08-02 15:14:15 -0700131 '''
132 Calls the zookeeper status and returns TRUE if it has an assigned Mode to it.
133 '''
adminbae64d82013-08-01 10:50:15 -0700134 self.execute(cmd="\n",prompt="\$",timeout=10)
Jon Hallf89c8552014-04-02 13:14:06 -0700135 response = self.execute(cmd=self.home + "/bin/zkServer.sh status ",prompt="Mode",timeout=10)
adminbae64d82013-08-01 10:50:15 -0700136 pattern = '(.*)Mode(.*)'
Jon Hallf89c8552014-04-02 13:14:06 -0700137 if re.search(pattern, response):
138 main.log.info(self.name + ": Zookeeper is up.")
adminbae64d82013-08-01 10:50:15 -0700139 return main.TRUE
140 else:
Jon Hallf89c8552014-04-02 13:14:06 -0700141 main.log.info(self.name + ": Zookeeper is down.")
adminbae64d82013-08-01 10:50:15 -0700142 return main.FALSE
143
144