1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
__author__ = "Konstantin Osipov <kostja.osipov@gmail.com>"
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
import re
import sys
import yaml
from tarantool_connection import TarantoolConnection, TarantoolPool, TarantoolAsyncConnection
ADMIN_SEPARATOR = '\n'
def get_handshake(sock, length=128, max_try=100):
"""
Correct way to get tarantool handshake
"""
result = ""
i = 0
while len(result) != length and i < max_try:
result = "%s%s" % (result, sock.recv(length-len(result)))
# max_try counter for tarantool/gh-1362
i += 1
return result
class AdminPool(TarantoolPool):
def _new_connection(self):
s = super(AdminPool, self)._new_connection()
handshake = get_handshake(s)
if handshake and not re.search(r'^Tarantool.*console.*', str(handshake)):
# tarantool/gh-1163
# 1. raise only if handshake is not full
# 2. be silent on crashes or if it's server.stop() operation
print 'Handshake error {\n', handshake, '\n}'
raise RuntimeError('Broken tarantool console handshake')
return s
class ExecMixIn(object):
def cmd(self, socket, cmd, silent):
socket.sendall(cmd)
bufsiz = 4096
res = ""
while True:
buf = socket.recv(bufsiz)
if not buf:
break
res = res + buf
if (res.rfind("\n...\n") >= 0 or res.rfind("\r\n...\r\n") >= 0):
break
try:
yaml.load(res)
finally:
if not silent:
sys.stdout.write(res.replace("\r\n", "\n"))
return res
class AdminConnection(TarantoolConnection, ExecMixIn):
def execute_no_reconnect(self, command, silent):
if not command:
return
if not silent:
sys.stdout.write(command + ADMIN_SEPARATOR)
cmd = command.replace('\n', ' ') + ADMIN_SEPARATOR
return self.cmd(self.socket, cmd, silent)
def connect(self):
super(AdminConnection, self).connect()
handshake = get_handshake(self.socket)
if not re.search(r'^Tarantool.*console.*', str(handshake)):
raise RuntimeError('Broken tarantool console handshake')
class AdminAsyncConnection(TarantoolAsyncConnection, ExecMixIn):
pool = AdminPool
def execute_no_reconnect(self, command, silent):
if not command:
return
if not silent:
sys.stdout.write(command + ADMIN_SEPARATOR)
cmd = command.replace('\n', ' ') + ADMIN_SEPARATOR
result = None
with self.connections.get() as sock:
result = self.cmd(sock, cmd, silent)
return result
def execute(self, command, silent=True):
if not self.is_connected:
self.connect()
try:
return self.execute_no_reconnect(command, silent)
except Exception, e:
return None
|