summaryrefslogtreecommitdiffstats
path: root/tests/topotests/lib/micronet_cli.py
blob: ef804f6dc261233fd9f6c2393cec3f5dc14a0b00 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# -*- coding: utf-8 eval: (blacken-mode 1) -*-
#
# July 24 2021, Christian Hopps <chopps@labn.net>
#
# Copyright (c) 2021, LabN Consulting, L.L.C.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; see the file COPYING; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
import argparse
import logging
import os
import pty
import re
import readline
import select
import socket
import subprocess
import sys
import tempfile
import termios
import tty


ENDMARKER = b"\x00END\x00"


def lineiter(sock):
    s = ""
    while True:
        sb = sock.recv(256)
        if not sb:
            return

        s += sb.decode("utf-8")
        i = s.find("\n")
        if i != -1:
            yield s[:i]
            s = s[i + 1 :]


def spawn(unet, host, cmd):
    if sys.stdin.isatty():
        old_tty = termios.tcgetattr(sys.stdin)
        tty.setraw(sys.stdin.fileno())
    try:
        master_fd, slave_fd = pty.openpty()

        # use os.setsid() make it run in a new process group, or bash job
        # control will not be enabled
        p = unet.hosts[host].popen(
            cmd,
            preexec_fn=os.setsid,
            stdin=slave_fd,
            stdout=slave_fd,
            stderr=slave_fd,
            universal_newlines=True,
        )

        while p.poll() is None:
            r, w, e = select.select([sys.stdin, master_fd], [], [], 0.25)
            if sys.stdin in r:
                d = os.read(sys.stdin.fileno(), 10240)
                os.write(master_fd, d)
            elif master_fd in r:
                o = os.read(master_fd, 10240)
                if o:
                    os.write(sys.stdout.fileno(), o)
    finally:
        # restore tty settings back
        if sys.stdin.isatty():
            termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_tty)


def doline(unet, line, writef):
    def host_cmd_split(unet, cmd):
        csplit = cmd.split()
        for i, e in enumerate(csplit):
            if e not in unet.hosts:
                break
        hosts = csplit[:i]
        if not hosts:
            hosts = sorted(unet.hosts.keys())
        cmd = " ".join(csplit[i:])
        return hosts, cmd

    line = line.strip()
    m = re.match(r"^(\S+)(?:\s+(.*))?$", line)
    if not m:
        return True

    cmd = m.group(1)
    oargs = m.group(2) if m.group(2) else ""
    if cmd == "q" or cmd == "quit":
        return False
    if cmd == "hosts":
        writef("%% hosts: %s\n" % " ".join(sorted(unet.hosts.keys())))
    elif cmd in ["term", "vtysh", "xterm"]:
        args = oargs.split()
        if not args or (len(args) == 1 and args[0] == "*"):
            args = sorted(unet.hosts.keys())
        hosts = [unet.hosts[x] for x in args if x in unet.hosts]
        for host in hosts:
            if cmd == "t" or cmd == "term":
                host.run_in_window("bash", title="sh-%s" % host)
            elif cmd == "v" or cmd == "vtysh":
                host.run_in_window("vtysh", title="vt-%s" % host)
            elif cmd == "x" or cmd == "xterm":
                host.run_in_window("bash", title="sh-%s" % host, forcex=True)
    elif cmd == "sh":
        hosts, cmd = host_cmd_split(unet, oargs)
        for host in hosts:
            if sys.stdin.isatty():
                spawn(unet, host, cmd)
            else:
                if len(hosts) > 1:
                    writef("------ Host: %s ------\n" % host)
                output = unet.hosts[host].cmd_legacy(cmd)
                writef(output)
                if len(hosts) > 1:
                    writef("------- End: %s ------\n" % host)
        writef("\n")
    elif cmd == "h" or cmd == "help":
        writef(
            """
Commands:
  help                       :: this help
  sh [hosts] <shell-command> :: execute <shell-command> on <host>
  term [hosts]               :: open shell terminals for hosts
  vtysh [hosts]              :: open vtysh terminals for hosts
  [hosts] <vtysh-command>    :: execute vtysh-command on hosts\n\n"""
        )
    else:
        hosts, cmd = host_cmd_split(unet, line)
        for host in hosts:
            if len(hosts) > 1:
                writef("------ Host: %s ------\n" % host)
            output = unet.hosts[host].cmd_legacy('vtysh -c "{}"'.format(cmd))
            writef(output)
            if len(hosts) > 1:
                writef("------- End: %s ------\n" % host)
        writef("\n")
    return True


def cli_server_setup(unet):
    sockdir = tempfile.mkdtemp("-sockdir", "pyt")
    sockpath = os.path.join(sockdir, "cli-server.sock")
    try:
        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        sock.settimeout(10)
        sock.bind(sockpath)
        sock.listen(1)
        return sock, sockdir, sockpath
    except Exception:
        unet.cmd_status("rm -rf " + sockdir)
        raise


def cli_server(unet, server_sock):
    sock, addr = server_sock.accept()

    # Go into full non-blocking mode now
    sock.settimeout(None)

    for line in lineiter(sock):
        line = line.strip()

        def writef(x):
            xb = x.encode("utf-8")
            sock.send(xb)

        if not doline(unet, line, writef):
            return
        sock.send(ENDMARKER)


def cli_client(sockpath, prompt="unet> "):
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    sock.settimeout(10)
    sock.connect(sockpath)

    # Go into full non-blocking mode now
    sock.settimeout(None)

    print("\n--- Micronet CLI Starting ---\n\n")
    while True:
        if sys.version_info[0] == 2:
            line = raw_input(prompt)  # pylint: disable=E0602
        else:
            line = input(prompt)
        if line is None:
            return

        # Need to put \n back
        line += "\n"

        # Send the CLI command
        sock.send(line.encode("utf-8"))

        def bendswith(b, sentinel):
            slen = len(sentinel)
            return len(b) >= slen and b[-slen:] == sentinel

        # Collect the output
        rb = b""
        while not bendswith(rb, ENDMARKER):
            lb = sock.recv(4096)
            if not lb:
                return
            rb += lb

        # Remove the marker
        rb = rb[: -len(ENDMARKER)]

        # Write the output
        sys.stdout.write(rb.decode("utf-8"))


def local_cli(unet, outf, prompt="unet> "):
    print("\n--- Micronet CLI Starting ---\n\n")
    while True:
        if sys.version_info[0] == 2:
            line = raw_input(prompt)  # pylint: disable=E0602
        else:
            line = input(prompt)
        if line is None:
            return
        if not doline(unet, line, outf.write):
            return


def cli(
    unet,
    histfile=None,
    sockpath=None,
    force_window=False,
    title=None,
    prompt=None,
    background=True,
):
    logger = logging.getLogger("cli-client")

    if prompt is None:
        prompt = "unet> "

    if force_window or not sys.stdin.isatty():
        # Run CLI in another window b/c we have no tty.
        sock, sockdir, sockpath = cli_server_setup(unet)

        python_path = unet.get_exec_path(["python3", "python"])
        us = os.path.realpath(__file__)
        cmd = "{} {}".format(python_path, us)
        if histfile:
            cmd += " --histfile=" + histfile
        if title:
            cmd += " --prompt={}".format(title)
        cmd += " " + sockpath

        try:
            unet.run_in_window(cmd, new_window=True, title=title, background=background)
            return cli_server(unet, sock)
        finally:
            unet.cmd_status("rm -rf " + sockdir)

    if not unet:
        logger.debug("client-cli using sockpath %s", sockpath)

    try:
        if histfile is None:
            histfile = os.path.expanduser("~/.micronet-history.txt")
            if not os.path.exists(histfile):
                if unet:
                    unet.cmd("touch " + histfile)
                else:
                    subprocess.run("touch " + histfile)
        if histfile:
            readline.read_history_file(histfile)
    except Exception:
        pass

    try:
        if sockpath:
            cli_client(sockpath, prompt=prompt)
        else:
            local_cli(unet, sys.stdout, prompt=prompt)
    except EOFError:
        pass
    except Exception as ex:
        logger.critical("cli: got exception: %s", ex, exc_info=True)
        raise
    finally:
        readline.write_history_file(histfile)


if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG, filename="/tmp/topotests/cli-client.log")
    logger = logging.getLogger("cli-client")
    logger.info("Start logging cli-client")

    parser = argparse.ArgumentParser()
    parser.add_argument("--histfile", help="file to user for history")
    parser.add_argument("--prompt-text", help="prompt string to use")
    parser.add_argument("socket", help="path to pair of sockets to communicate over")
    args = parser.parse_args()

    prompt = "{}> ".format(args.prompt_text) if args.prompt_text else "unet> "
    cli(None, args.histfile, args.socket, prompt=prompt)