|
4 | 4 | import requests |
5 | 5 | import os, stat, time |
6 | 6 | from pathlib import Path |
7 | | -from shutil import which |
8 | | -import platform |
9 | | -import subprocess |
10 | | -import wget |
11 | | -import atexit |
12 | | - |
13 | | -CHISEL_VERSION="1.6.0" |
14 | | -CHISEL_ARCH_MAP={ |
15 | | - "x86_64": "amd64", |
16 | | - "i386": "386" |
17 | | -} |
18 | 7 |
|
19 | 8 | class Ods: |
20 | 9 | def __init__(self, staroid=None, ske=None, cache_dir=None, chisel_path=None): |
@@ -48,40 +37,8 @@ def create_or_get_cache_dir(self, module = ""): |
48 | 37 | os.makedirs(cache_dir) |
49 | 38 | return cache_dir |
50 | 39 |
|
51 | | - def __check_cmd(self, cmd): |
52 | | - if which(cmd) == None: |
53 | | - raise Exception("'{}' command not found".format(cmd)) |
54 | | - |
55 | 40 | def download_chisel_if_not_exists(self): |
56 | | - # check gunzip available |
57 | | - self.__check_cmd("gunzip") |
58 | | - |
59 | | - if self.__chisel_path == None: |
60 | | - # download chisel binary for secure tunnel if not exists |
61 | | - uname = platform.uname() |
62 | | - uname.system.lower() |
63 | | - if uname.machine not in CHISEL_ARCH_MAP.keys(): |
64 | | - raise Exception("Can not download chisel automatically. Please download manually from 'https://github.com/jpillora/chisel/releases/download/v{}' and set 'chisel_path' argument".format(CHISEL_VERSION)) |
65 | | - |
66 | | - download_url = "https://github.com/jpillora/chisel/releases/download/v{}/chisel_{}_{}_{}.gz".format( |
67 | | - CHISEL_VERSION, CHISEL_VERSION, uname.system.lower(), CHISEL_ARCH_MAP[uname.machine]) |
68 | | - cache_bin = self.create_or_get_cache_dir("bin") |
69 | | - chisel_path = "{}/chisel".format(cache_bin) |
70 | | - |
71 | | - if not os.path.exists(chisel_path): |
72 | | - # download |
73 | | - filename = wget.download(download_url, cache_bin) |
74 | | - |
75 | | - # extract |
76 | | - subprocess.run(["gunzip", "-f", filename]) |
77 | | - |
78 | | - # rename |
79 | | - subprocess.run(["mv", filename.replace(".gz", ""), chisel_path]) |
80 | | - |
81 | | - # chmod |
82 | | - os.chmod(chisel_path, stat.S_IRWXU) |
83 | | - |
84 | | - self.__chisel_path = chisel_path |
| 41 | + self._staroid.get_chisel_path() |
85 | 42 |
|
86 | 43 | def _start_instance_on_staroid(self, instance_name, commit_url): |
87 | 44 | cluster = self._staroid.cluster().get(self.__ske) |
@@ -118,53 +75,14 @@ def _start_tunnel(self, instance_name, tunnels): |
118 | 75 | ns_api = self._staroid.namespace(cluster) |
119 | 76 | ns = ns_api.get(instance_name) |
120 | 77 | ns_api.shell_start(instance_name) |
121 | | - resources = ns_api.get_all_resources(instance_name) |
122 | | - |
123 | | - shell_service = None |
124 | | - for s in resources["services"]["items"]: |
125 | | - if "labels" in s["metadata"]: |
126 | | - if "resource.staroid.com/system" in s["metadata"]["labels"]: |
127 | | - if s["metadata"]["labels"]["resource.staroid.com/system"] == "shell": |
128 | | - shell_service = s |
129 | | - break |
130 | | - |
131 | | - if shell_service == None: |
132 | | - raise Exception("Shell service not found") |
133 | | - |
134 | | - tunnel_server = "https://p{}-{}--{}".format("57682", shell_service["metadata"]["name"], ns.url()[len("https://"):]) |
135 | | - cmd = [ |
136 | | - self.__chisel_path, |
137 | | - "client", |
138 | | - "--header", |
139 | | - "Authorization: token {}".format(self._staroid.get_access_token()), |
140 | | - "--keepalive", |
141 | | - "10s", |
142 | | - tunnel_server |
143 | | - ] |
144 | | - cmd.extend(tunnels) |
145 | | - self.__tunnel_processes[instance_name]=subprocess.Popen(cmd) |
146 | | - atexit.register(self.cleanup) |
147 | | - |
148 | | - def cleanup(self): |
149 | | - timeout_sec = 5 |
150 | | - for p in self.__tunnel_processes.values(): # list of your processes |
151 | | - p_sec = 0 |
152 | | - for second in range(timeout_sec): |
153 | | - if p.poll() == None: |
154 | | - time.sleep(1) |
155 | | - p_sec += 1 |
156 | | - if p_sec >= timeout_sec: |
157 | | - p.kill() # supported from python 2.6 |
| 78 | + ns_api.start_tunnel(instance_name, tunnels) |
158 | 79 |
|
159 | 80 | def _stop_tunnel(self, instance_name): |
160 | | - if self._is_tunnel_running(instance_name): |
161 | | - self.__tunnel_processes[instance_name].kill() |
162 | | - del self.__tunnel_processes[instance_name] |
163 | | - |
164 | 81 | cluster = self._staroid.cluster().get(self.__ske) |
165 | 82 | if cluster == None: |
166 | 83 | raise Exception("Can't get ske cluster") |
167 | 84 | ns_api = self._staroid.namespace(cluster) |
| 85 | + ns_api.stop_tunnel(instance_name) |
168 | 86 | ns_api.shell_stop(instance_name) |
169 | 87 |
|
170 | 88 | def _stop_instance_on_staroid(self, instance_name): |
|
0 commit comments