1
1
import asyncio
2
2
import json
3
+ import os
3
4
import shlex
4
5
import signal
6
+ import sys
5
7
from typing import (
6
8
Union , Optional ,
7
9
MutableMapping , Dict ,
12
14
import click
13
15
14
16
from . import main
15
- from .pretty import print_info , print_warn , print_error
17
+ from .pretty import print_info , print_warn , print_fail , print_error
16
18
from ..config import DEFAULT_CHUNK_SIZE
17
19
from ..request import Request
18
20
from ..session import AsyncSession
@@ -120,6 +122,7 @@ class ProxyRunnerContext:
120
122
'protocol' , 'host' , 'port' ,
121
123
'args' , 'envs' ,
122
124
'api_session' , 'local_server' ,
125
+ 'exit_code' ,
123
126
)
124
127
125
128
session_name : str
@@ -131,6 +134,7 @@ class ProxyRunnerContext:
131
134
envs : Dict [str , str ]
132
135
api_session : Optional [AsyncSession ]
133
136
local_server : Optional [asyncio .AbstractServer ]
137
+ exit_code : int
134
138
135
139
def __init__ (self , host : str , port : int ,
136
140
session_name : str , app_name : str , * ,
@@ -145,6 +149,7 @@ def __init__(self, host: str, port: int,
145
149
146
150
self .api_session = None
147
151
self .local_server = None
152
+ self .exit_code = 0
148
153
149
154
self .args , self .envs = {}, {}
150
155
if len (args ) > 0 :
@@ -187,24 +192,26 @@ async def handle_connection(self, reader: asyncio.StreamReader,
187
192
print_error (e )
188
193
189
194
async def __aenter__ (self ) -> None :
195
+ self .exit_code = 0
190
196
self .api_session = AsyncSession ()
191
197
await self .api_session .__aenter__ ()
192
- self .local_server = await asyncio .start_server (
193
- self .handle_connection , self .host , self .port )
194
198
195
199
user_url_template = "{protocol}://{host}:{port}"
196
- try :
197
- compute_session = self .api_session .ComputeSession (self .session_name )
198
- data = await compute_session .stream_app_info (self .app_name )
199
- if 'url_template' in data .keys ():
200
- user_url_template = data ['url_template' ]
201
- except :
202
- if self .app_name == 'vnc-web' :
203
- user_url_template = \
204
- "{protocol}://{host}:{port}/vnc.html" \
205
- "?host={host}&port={port}" \
206
- "&password=backendai&autoconnect=true"
200
+ compute_session = self .api_session .ComputeSession (self .session_name )
201
+ all_apps = await compute_session .stream_app_info ()
202
+ for app_info in all_apps :
203
+ if app_info ['name' ] == self .app_name :
204
+ if 'url_template' in app_info .keys ():
205
+ user_url_template = app_info ['url_template' ]
206
+ break
207
+ else :
208
+ print_fail (f'The app "{ self .app_name } " is not supported by the session.' )
209
+ self .exit_code = 1
210
+ os .kill (0 , signal .SIGINT )
211
+ return
207
212
213
+ self .local_server = await asyncio .start_server (
214
+ self .handle_connection , self .host , self .port )
208
215
user_url = user_url_template .format (
209
216
protocol = self .protocol ,
210
217
host = self .host ,
@@ -220,13 +227,16 @@ async def __aenter__(self) -> None:
220
227
'to connect with the CLI app proxy.' )
221
228
222
229
async def __aexit__ (self , * exc_info ) -> None :
223
- print_info ("Shutting down...." )
224
- self .local_server .close ()
225
- await self .local_server .wait_closed ()
230
+ if self .local_server is not None :
231
+ print_info ("Shutting down...." )
232
+ self .local_server .close ()
233
+ await self .local_server .wait_closed ()
226
234
await self .api_session .__aexit__ (* exc_info )
227
235
assert self .api_session .closed
228
- print_info ("The local proxy to \" {}\" has terminated."
229
- .format (self .app_name ))
236
+ if self .local_server is not None :
237
+ print_info ("The local proxy to \" {}\" has terminated."
238
+ .format (self .app_name ))
239
+ self .local_server = None
230
240
231
241
232
242
@main .command ()
@@ -266,6 +276,7 @@ def app(session_name, app, protocol, bind, arg, env):
266
276
)
267
277
stop_signals = {signal .SIGINT , signal .SIGTERM }
268
278
asyncio_run_forever (proxy_ctx , stop_signals = stop_signals )
279
+ sys .exit (proxy_ctx .exit_code )
269
280
270
281
271
282
@main .command ()
0 commit comments