diff options
Diffstat (limited to 'bin/emq-cli')
| -rwxr-xr-x | bin/emq-cli | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/bin/emq-cli b/bin/emq-cli new file mode 100755 index 0000000..389bdbd --- /dev/null +++ b/bin/emq-cli | |||
| @@ -0,0 +1,120 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | # -*- mode: python -*- | ||
| 3 | # | ||
| 4 | # This file is part of eventmq. | ||
| 5 | # | ||
| 6 | # eventmq is free software: you can redistribute it and/or modify | ||
| 7 | # it under the terms of the GNU General Public License as published by | ||
| 8 | # the Free Software Foundation, either version 3 of the License, or | ||
| 9 | # (at your option) any later version. | ||
| 10 | # | ||
| 11 | # eventmq is distributed in the hope that it will be useful, | ||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | # GNU General Public License for more details. | ||
| 15 | # | ||
| 16 | # You should have received a copy of the GNU General Public License | ||
| 17 | # along with eventmq. If not, see <http://www.gnu.org/licenses/>. | ||
| 18 | from __future__ import print_function | ||
| 19 | |||
| 20 | import argparse | ||
| 21 | import cmd | ||
| 22 | import sys | ||
| 23 | |||
| 24 | from eventmq import exceptions | ||
| 25 | from eventmq.client.messages import send_emqp_message | ||
| 26 | from eventmq.constants import PROTOCOL_VERSION | ||
| 27 | from eventmq.sender import Sender | ||
| 28 | from eventmq.utils.messages import generate_msgid | ||
| 29 | |||
| 30 | class Shell(cmd.Cmd): | ||
| 31 | """ | ||
| 32 | Interactive EventMQ Shell | ||
| 33 | """ | ||
| 34 | def __init__(self, addr=None): | ||
| 35 | cmd.Cmd.__init__(self) | ||
| 36 | |||
| 37 | self.prompt = "> " | ||
| 38 | |||
| 39 | self.addr = addr | ||
| 40 | if self.addr: | ||
| 41 | self.do_connect(addr) | ||
| 42 | |||
| 43 | self.socket = Sender() | ||
| 44 | |||
| 45 | def do_connect(self, line): | ||
| 46 | """ | ||
| 47 | Connect to a service | ||
| 48 | """ | ||
| 49 | if not line: | ||
| 50 | sys.stderr.write('Error: No address provided\n') | ||
| 51 | return | ||
| 52 | |||
| 53 | try: | ||
| 54 | self.socket.connect(line) | ||
| 55 | except exceptions.EventMQError as e: | ||
| 56 | if "status=201" in e.message: | ||
| 57 | sys.stderr.write('Error: Already connected to {} Disconnect ' | ||
| 58 | 'first\n'.format(self.addr)) | ||
| 59 | return | ||
| 60 | raise | ||
| 61 | |||
| 62 | self.addr = line | ||
| 63 | print("Connecting to {}...".format(line)) | ||
| 64 | self.send_message('HELLO') | ||
| 65 | |||
| 66 | def do_disconnect(self, line): | ||
| 67 | """ | ||
| 68 | Disconnect from a service | ||
| 69 | """ | ||
| 70 | print('Closing connection to {}'.format(self.addr)) | ||
| 71 | self.socket.rebuild() | ||
| 72 | self.addr = None | ||
| 73 | |||
| 74 | def do_status(self, line): | ||
| 75 | """ | ||
| 76 | Request the status of the connected component | ||
| 77 | """ | ||
| 78 | raise NotImplementedError('TODO: STATUS command') | ||
| 79 | self.send_message(('STATUS',)) | ||
| 80 | print(self.socket.recv_multipart()) | ||
| 81 | |||
| 82 | def do_shutdown(self, line): | ||
| 83 | """ | ||
| 84 | Request the connected component shutdown | ||
| 85 | """ | ||
| 86 | self.send_message('DISCONNECT') | ||
| 87 | import time; time.sleep(1) | ||
| 88 | print(self.socket.recv_multipart()) | ||
| 89 | |||
| 90 | def do_send_cmd(self, line): | ||
| 91 | """ | ||
| 92 | Send a raw command to the connected host. | ||
| 93 | """ | ||
| 94 | |||
| 95 | def send_message(self, command, message=()): | ||
| 96 | if not self.addr: | ||
| 97 | sys.stderr.write('Error: Not connected\n') | ||
| 98 | return | ||
| 99 | |||
| 100 | message = (command, generate_msgid('admin:')) + message | ||
| 101 | |||
| 102 | self.socket.send_multipart(message, PROTOCOL_VERSION) | ||
| 103 | |||
| 104 | def do_quit(self, line): | ||
| 105 | return True | ||
| 106 | |||
| 107 | def do_EOF(self, line): | ||
| 108 | return self.do_quit(line) | ||
| 109 | |||
| 110 | if __name__ == '__main__': | ||
| 111 | parser = argparse.ArgumentParser(description='Utility for interacting with ' | ||
| 112 | 'an EventMQ cluster') | ||
| 113 | |||
| 114 | parser.add_argument('--addr', '-a', type=str, nargs='?', | ||
| 115 | help='specify address to connect to') | ||
| 116 | |||
| 117 | args = parser.parse_args() | ||
| 118 | |||
| 119 | app = Shell(addr=args.addr) | ||
| 120 | app.cmdloop() | ||