|
| 1 | +import tarfile |
1 | 2 | import time |
| 3 | +from tempfile import TemporaryFile |
2 | 4 |
|
3 | 5 | from kubernetes import config |
4 | 6 | from kubernetes.client import Configuration |
|
74 | 76 | commands = [ |
75 | 77 | "echo test1", |
76 | 78 | "echo \"This message goes to stderr\" >&2", |
| 79 | + "ls -l /etc", |
77 | 80 | ] |
78 | 81 | while resp.is_open(): |
79 | 82 | resp.update(timeout=1) |
|
95 | 98 | user = resp.readline_stdout(timeout=3) |
96 | 99 | print("Server user is: %s" % user) |
97 | 100 | resp.close() |
| 101 | + |
| 102 | +# Copying file client -> pod |
| 103 | +print('copying client -> pod') |
| 104 | +exec_command = ['tar', 'xvf', '-', '-C', '/'] |
| 105 | +resp = stream(api.connect_get_namespaced_pod_exec, name, 'default', |
| 106 | + command=exec_command, |
| 107 | + stderr=True, stdin=True, |
| 108 | + stdout=True, tty=False, |
| 109 | + binary=True, |
| 110 | + _preload_content=False) |
| 111 | + |
| 112 | +source_file = '/tmp/dash' |
| 113 | + |
| 114 | +with TemporaryFile() as tar_buffer: |
| 115 | + with tarfile.open(fileobj=tar_buffer, mode='w') as tar: |
| 116 | + tar.add(source_file) |
| 117 | + |
| 118 | + tar_buffer.seek(0) |
| 119 | + commands = [] |
| 120 | + commands.append(tar_buffer.read()) |
| 121 | + |
| 122 | + while resp.is_open(): |
| 123 | + resp.update(timeout=1) |
| 124 | + if resp.peek_stdout(): |
| 125 | + print("STDOUT: %s" % resp.read_stdout()) |
| 126 | + if resp.peek_stderr(): |
| 127 | + print("STDERR: %s" % resp.read_stderr()) |
| 128 | + if commands: |
| 129 | + c = commands.pop(0) |
| 130 | + resp.write_stdin(c) |
| 131 | + else: |
| 132 | + break |
| 133 | + resp.close() |
| 134 | + |
| 135 | +# Copying file pod -> client |
| 136 | +print('copying pod -> client') |
| 137 | +exec_command = ['tar', 'cf', '-', '/bin/sh'] |
| 138 | + |
| 139 | +with TemporaryFile() as tar_buffer: |
| 140 | + |
| 141 | + resp = stream(api.connect_get_namespaced_pod_exec, name, 'default', |
| 142 | + command=exec_command, |
| 143 | + stderr=True, stdin=True, |
| 144 | + stdout=True, tty=False, |
| 145 | + binary=True, |
| 146 | + _preload_content=False) |
| 147 | + |
| 148 | + while resp.is_open(): |
| 149 | + resp.update(timeout=1) |
| 150 | + if resp.peek_stdout(): |
| 151 | + out = resp.read_stdout() |
| 152 | + print("bytes received: %s" % len(out)) |
| 153 | + tar_buffer.write(out) |
| 154 | + if resp.peek_stderr(): |
| 155 | + print("STDERR: %s" % resp.read_stderr()) |
| 156 | + resp.close() |
| 157 | + |
| 158 | + tar_buffer.flush() |
| 159 | + tar_buffer.seek(0) |
| 160 | + |
| 161 | + with tarfile.open(fileobj=tar_buffer, mode='r:') as tar: |
| 162 | + print('members', tar.getmembers()) |
0 commit comments