1- #!/usr/bin/env python
2-
31import sys
42import asyncio
53
86from gitpod .types .environment_spec_param import EnvironmentSpecParam
97from gitpod .types .environment_initializer_param import Spec
108
9+ from .scm_auth import verify_context_url # type: ignore
10+
1111
1212# Examples:
13- # - ./ examples/run_service.py
14- # - ./ examples/run_service.py https://github.com/gitpod-io/empty
13+ # - python -m examples.run_service
14+ # - python -m examples.run_service https://github.com/gitpod-io/empty
1515async def main (cleanup : util .Disposables ) -> None :
1616 client = AsyncGitpod ()
1717
1818 context_url = sys .argv [1 ] if len (sys .argv ) > 1 else None
19-
19+
2020 env_class = await util .find_most_used_environment_class (client )
2121 if not env_class :
2222 print ("Error: No environment class found. Please create one first." )
2323 sys .exit (1 )
2424 print (f"Found environment class: { env_class .display_name } ({ env_class .description } )" )
25- env_class_id = env_class .id
26- assert env_class_id is not None
2725
2826 port = 8888
2927 spec : EnvironmentSpecParam = {
3028 "desired_phase" : "ENVIRONMENT_PHASE_RUNNING" ,
31- "machine" : {"class" : env_class_id },
29+ "machine" : {"class" : env_class . id },
3230 "ports" : [{
3331 "name" : "Lama Service" ,
3432 "port" : port ,
3533 "admission" : "ADMISSION_LEVEL_EVERYONE"
3634 }]
3735 }
3836 if context_url :
37+ await verify_context_url (client , context_url , env_class .runner_id )
3938 spec ["content" ] = {
4039 "initializer" : {"specs" : [Spec (
41- context_url = {
42- "url" : context_url
43- }
44- )]}
45- }
40+ context_url = {
41+ "url" : context_url
42+ }
43+ )]}
44+ }
4645
47- print ("Creating environment" )
4846 environment = (await client .environments .create (spec = spec )).environment
49- assert environment is not None
50- environment_id = environment .id
51- assert environment_id is not None
52- cleanup .add (lambda : asyncio .run (client .environments .delete (environment_id = environment_id )))
47+ cleanup .adda (lambda : client .environments .delete (environment_id = environment .id ))
5348
5449 print ("Waiting for environment to be ready" )
55- env = util .EnvironmentState (client , environment_id )
56- cleanup .add (lambda : asyncio . run ( env .close () ))
50+ env = util .EnvironmentState (client , environment . id )
51+ cleanup .adda (lambda : env .close ())
5752 await env .wait_until_running ()
5853
5954 print ("Starting Lama Service" )
60- lines = await util .run_service (client , environment_id , {
55+ lines = await util .run_service (client , environment . id , {
6156 "name" :"Lama Service" ,
6257 "description" :"Lama Service" ,
6358 "reference" :"lama-service"
@@ -75,6 +70,4 @@ async def main(cleanup: util.Disposables) -> None:
7570 print (line )
7671
7772if __name__ == "__main__" :
78- disposables = util .Disposables ()
79- with disposables :
80- asyncio .run (main (disposables ))
73+ asyncio .run (util .with_disposables (main ))
0 commit comments