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 | ''' |
| 40 | ZookeeperCliDriver is the basic driver which will handle the Zookeeper's functions |
| 41 | ''' |
| 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] |
| 52 | |
| 53 | self.name = self.options['name'] |
| 54 | self.handle = super(ZookeeperCliDriver, self).connect(user_name = self.user_name, ip_address = self.ip_address,port = self.port, pwd = self.pwd) |
| 55 | |
| 56 | self.ssh_handle = self.handle |
| 57 | if self.handle : |
| 58 | self.start() |
| 59 | return main.TRUE |
| 60 | else : |
| 61 | main.log.error("Connection failed to the host "+self.user_name+"@"+self.ip_address) |
| 62 | main.log.error("Failed to connect to the Onos system") |
| 63 | return main.FALSE |
| 64 | |
| 65 | |
| 66 | def start(self): |
| 67 | ''' This Function will start the Zookeeper''' |
| 68 | main.log.info( "Starting Zookeeper" ) |
| 69 | self.handle.sendline("") |
| 70 | self.handle.expect("\$") |
| 71 | self.handle.sendline("~/zookeeper-3.4.5/bin/zkServer.sh start") |
| 72 | self.handle.expect("zkServer.sh start") |
| 73 | self.handle.expect("\$") |
| 74 | response = self.handle.before + self.handle.after |
| 75 | if re.search("STARTED", response): |
| 76 | main.log.info("Zookeeper Started ") |
| 77 | return main.TRUE |
| 78 | elif re.search("running", response): |
| 79 | main.log.warn("zookeeper ... already running") |
| 80 | else: |
| 81 | main.log.error("Failed to start Zookeeper"+ response) |
| 82 | return main.FALSE |
| 83 | |
| 84 | def status(self): |
| 85 | '''This Function will return the Status of the Zookeeper ''' |
| 86 | time.sleep(5) |
| 87 | self.execute(cmd="\r",prompt="\$",timeout=10) |
| 88 | response = self.execute(cmd="~/zookeeper-3.4.5/bin/zkServer.sh status ",prompt="JMX",timeout=10) |
| 89 | |
| 90 | self.execute(cmd="\r",prompt="\$",timeout=10) |
| 91 | return response |
| 92 | |
| 93 | def stop(self): |
| 94 | '''This Function will stop the Zookeeper if it is Running''' |
| 95 | self.execute(cmd="\r",prompt="\$",timeout=10) |
| 96 | time.sleep(5) |
| 97 | response = self.execute(cmd="~/zookeeper-3.4.5/bin/zkServer.sh stop ",prompt="STOPPED",timeout=10) |
| 98 | self.execute(cmd="\r",prompt="\$",timeout=10) |
| 99 | if re.search("STOPPED",response): |
| 100 | main.log.info("Zookeeper Stopped") |
| 101 | return main.TRUE |
| 102 | else: |
| 103 | main.log.warn("No zookeeper to stop") |
| 104 | return main.FALSE |
| 105 | |
| 106 | def disconnect(self): |
| 107 | |
| 108 | response = '' |
| 109 | if self.handle: |
| 110 | self.handle.sendline("exit") |
| 111 | self.handle.expect("closed") |
| 112 | else : |
| 113 | main.log.error("Connection failed to the host") |
| 114 | response = main.FALSE |
| 115 | return response |
| 116 | |
| 117 | def isup(self): |
| 118 | self.execute(cmd="\n",prompt="\$",timeout=10) |
| 119 | response = self.execute(cmd="~/zookeeper-3.4.5/bin/zkServer.sh status ",prompt="Mode",timeout=10) |
| 120 | pattern = '(.*)Mode(.*)' |
| 121 | if re.search(pattern, response): |
| 122 | return main.TRUE |
| 123 | else: |
| 124 | return main.FALSE |
| 125 | |
| 126 | |