11import json
2+ import time
3+ from datetime import datetime
24
35from roboflow .adapters import deploymentapi
46from roboflow .config import load_roboflow_api_key
@@ -19,78 +21,131 @@ def add_deployment_parser(subparsers):
1921 deployment_delete_parser = deployment_subparsers .add_parser ("delete" , help = "delete a dedicated deployment" )
2022
2123 deployment_machine_type_parser .set_defaults (func = list_machine_types )
22- deployment_machine_type_parser .add_argument ("-a" , dest = " api_key" , help = "api key" )
24+ deployment_machine_type_parser .add_argument ("-a" , "-- api_key" , help = "api key" )
2325
2426 deployment_add_parser .set_defaults (func = add_deployment )
25- deployment_add_parser .add_argument ("-a" , dest = "api_key" , help = "api key" )
27+ deployment_add_parser .add_argument ("-a" , "--api_key" , help = "api key" )
28+ deployment_add_parser .add_argument (
29+ "deployment_name" ,
30+ help = "deployment name, must contain 5-15 lowercase characters, first character must be a letter" ,
31+ )
2632 # deployment_add_parser.add_argument(
27- # "-s", dest=" security_level", help="security level (protected)", default="protected"
33+ # "-s", "-- security_level", help="security level (protected)", default="protected"
2834 # )
2935 deployment_add_parser .add_argument (
30- "-m" , dest = " machine_type" , help = "machine type, run `roboflow deployment machine_type` to see available options"
36+ "-m" , "-- machine_type" , help = "machine type, run `roboflow deployment machine_type` to see available options"
3137 )
3238 deployment_add_parser .add_argument (
3339 "-t" ,
34- dest = " duration" ,
40+ "-- duration" ,
3541 help = "duration, how long you want to keep the deployment (unit: hour, default: 3)" ,
3642 type = float ,
3743 default = 3 ,
3844 )
3945 deployment_add_parser .add_argument (
40- "-e" , dest = "delete_on_expiration " , help = "delete when expired (default: True )" , type = bool , default = True
46+ "-e" , "--no_delete_on_expiration " , help = "keep when expired (default: False )" , action = "store_true"
4147 )
4248 deployment_add_parser .add_argument (
43- "-n" , dest = "deployment_name" , help = "deployment name, must contain 3-10 lowercase characters"
49+ "-v" ,
50+ "--inference_version" ,
51+ help = "inference server version (default: latest)" ,
52+ default = "latest" ,
4453 )
4554 deployment_add_parser .add_argument (
46- "-v " , dest = "inference_version " , help = "inference server version (default: latest) " , default = "latest "
55+ "-w " , "--wait_on_pending " , help = "wait if deployment is pending " , action = "store_true "
4756 )
4857
4958 deployment_get_parser .set_defaults (func = get_deployment )
50- deployment_get_parser .add_argument ("-a" , dest = "api_key" , help = "api key" )
51- deployment_get_parser .add_argument ("-d" , dest = "deployment_id" , help = "deployment id" )
59+ deployment_get_parser .add_argument ("-a" , "--api_key" , help = "api key" )
60+ deployment_get_parser .add_argument ("deployment_name" , help = "deployment name" )
61+ deployment_get_parser .add_argument (
62+ "-w" , "--wait_on_pending" , help = "wait if deployment is pending" , action = "store_true"
63+ )
5264
5365 deployment_list_parser .set_defaults (func = list_deployment )
54- deployment_list_parser .add_argument ("-a" , dest = " api_key" , help = "api key" )
66+ deployment_list_parser .add_argument ("-a" , "-- api_key" , help = "api key" )
5567
5668 deployment_delete_parser .set_defaults (func = delete_deployment )
57- deployment_delete_parser .add_argument ("-a" , dest = " api_key" , help = "api key" )
58- deployment_delete_parser .add_argument ("-d " , dest = "deployment_id" , help = "deployment id " )
69+ deployment_delete_parser .add_argument ("-a" , "-- api_key" , help = "api key" )
70+ deployment_delete_parser .add_argument ("deployment_name " , help = "deployment name " )
5971
6072
6173def list_machine_types (args ):
6274 api_key = args .api_key or load_roboflow_api_key (None )
63- ret_json = deploymentapi .list_machine_types (api_key )
64- print (json .dumps (ret_json , indent = 2 ))
75+ if api_key is None :
76+ print ("Please provide an api key" )
77+ return
78+ status_code , msg = deploymentapi .list_machine_types (api_key )
79+ if status_code != 200 :
80+ print (f"{ status_code } : { msg } " )
81+ return
82+ print (json .dumps (msg , indent = 2 ))
6583
6684
6785def add_deployment (args ):
6886 api_key = args .api_key or load_roboflow_api_key (None )
69- ret_json = deploymentapi .add_deployment (
87+ if api_key is None :
88+ print ("Please provide an api key" )
89+ return
90+ status_code , msg = deploymentapi .add_deployment (
7091 api_key ,
7192 # args.security_level,
7293 args .machine_type ,
7394 args .duration ,
74- args .delete_on_expiration ,
95+ ( not args .no_delete_on_expiration ) ,
7596 args .deployment_name ,
7697 args .inference_version ,
7798 )
78- print (json .dumps (ret_json , indent = 2 ))
99+
100+ if status_code != 200 :
101+ print (f"{ status_code } : { msg } " )
102+ return
103+ else :
104+ print (f"Deployment { args .deployment_name } created successfully" )
105+ print (json .dumps (msg , indent = 2 ))
106+
107+ if args .wait_on_pending :
108+ get_deployment (args )
79109
80110
81111def get_deployment (args ):
82112 api_key = args .api_key or load_roboflow_api_key (None )
83- ret_json = deploymentapi .get_deployment (api_key , args .deployment_id )
84- print (json .dumps (ret_json , indent = 2 ))
113+ if api_key is None :
114+ print ("Please provide an api key" )
115+ return
116+ while True :
117+ status_code , msg = deploymentapi .get_deployment (api_key , args .deployment_name )
118+ if status_code != 200 :
119+ print (f"{ status_code } : { msg } " )
120+ return
121+
122+ if (not args .wait_on_pending ) or msg ["status" ] != "pending" :
123+ print (json .dumps (msg , indent = 2 ))
124+ break
125+
126+ print (f'{ datetime .now ().strftime ("%H:%M:%S" )} Waiting for deployment { args .deployment_name } to be ready...\n ' )
127+ time .sleep (30 )
85128
86129
87130def list_deployment (args ):
88131 api_key = args .api_key or load_roboflow_api_key (None )
89- ret_json = deploymentapi .list_deployment (api_key )
90- print (json .dumps (ret_json , indent = 2 ))
132+ if api_key is None :
133+ print ("Please provide an api key" )
134+ return
135+ status_code , msg = deploymentapi .list_deployment (api_key )
136+ if status_code != 200 :
137+ print (f"{ status_code } : { msg } " )
138+ return
139+ print (json .dumps (msg , indent = 2 ))
91140
92141
93142def delete_deployment (args ):
94143 api_key = args .api_key or load_roboflow_api_key (None )
95- ret_json = deploymentapi .delete_deployment (api_key , args .deployment_id )
96- print (json .dumps (ret_json , indent = 2 ))
144+ if api_key is None :
145+ print ("Please provide an api key" )
146+ return
147+ status_code , msg = deploymentapi .delete_deployment (api_key , args .deployment_name )
148+ if status_code != 200 :
149+ print (f"{ status_code } : { msg } " )
150+ return
151+ print (json .dumps (msg , indent = 2 ))
0 commit comments