Guest User

Untitled

a guest
May 28th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. import socket, argparse
  2. import select, sys, os, tty, atexit, termios, signal, shutil
  3. import fcntl, termios, struct
  4.  
  5. hasVarlink = False
  6. try:
  7. from varlink import (Client, VarlinkError)
  8. hasVarlink = True
  9. except ImportError:
  10. pass
  11.  
  12.  
  13. sock = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
  14. address = "unix:/run/io.projectatomic.podman"
  15.  
  16. # stdin 1
  17. # stdout 2
  18. # stderr 3
  19.  
  20.  
  21. parser = argparse.ArgumentParser()
  22. parser.add_argument('container', help='container name or id')
  23. args = parser.parse_args()
  24. socket_path = ""
  25. control_socket_path = ""
  26.  
  27. if hasVarlink:
  28. with Client(address=address) as client:
  29. podman = client.open('io.projectatomic.podman')
  30. try:
  31. response = podman.GetAttachSockets(args.container)
  32. except VarlinkError as e:
  33. sys.exit(e.error())
  34.  
  35. socket_path = response["sockets"]["io_socket"]
  36. control_socket_path = response["sockets"]["control_socket"]
  37. else:
  38. sockets_dir = os.path.join("/var/run/libpod/socket/", args.container)
  39. socket_path = os.path.join(sockets_dir, "attach")
  40. control_socket_path = os.path.join(sockets_dir, "ctl")
  41.  
  42.  
  43. def reset_fd(my_fd, old_settings):
  44. termios.tcsetattr(my_fd, termios.TCSADRAIN, old_settings)
  45. os.close(my_fd)
  46.  
  47.  
  48. def error_print(*args, **kwargs):
  49. print(*args, file=sys.stderr, **kwargs)
  50.  
  51. def terminal_size():
  52. h, w, hp, wp = struct.unpack('HHHH',
  53. fcntl.ioctl(1, termios.TIOCGWINSZ,
  54. struct.pack('HHHH', 0, 0, 0, 0)))
  55. return w, h
  56.  
  57. def resize_handler(signum, frame):
  58. control_socket = open(control_socket_path, 'w')
  59. # "%d %d %d\n", 1, size.Height, size.Width)
  60. w, h = terminal_size()
  61. #term_size = shutil.get_terminal_size()
  62. print(w,h)
  63. control_socket.write("{} {} {}\n".format(1, h, w))
  64. control_socket.close()
  65. #control_socket.flush()
  66.  
  67.  
  68. signal.signal(signal.SIGWINCH, resize_handler)
  69.  
  70. sock.connect(socket_path)
  71.  
  72. stdin = os.open("/dev/stdin", os.O_RDWR)
  73. old_settings = termios.tcgetattr(stdin)
  74. atexit.register(reset_fd, stdin, old_settings)
  75. tty.setraw(stdin)
  76.  
  77. inputs = [sock, stdin]
  78. outputs = []
  79.  
  80. while inputs:
  81. try:
  82. readable, writable, exceptional = select.select(inputs, outputs, inputs)
  83. for r in readable:
  84. if isinstance(r, socket.socket):
  85. data = r.recv(1024)
  86. os.write(2, data[1:])
  87.  
  88. if isinstance(r, int):
  89. data = os.read(stdin, 1024)
  90. if 29 in data: # Bail on ctrl-[
  91. sys.exit(0)
  92. sock.send(data)
  93. except BrokenPipeError:
  94. error_print("socket quit on container end")
  95. sys.exit(1)
Add Comment
Please, Sign In to add comment