1
1
import json
2
+ import time
3
+ from datetime import datetime
2
4
3
5
from roboflow .adapters import deploymentapi
4
6
from roboflow .config import load_roboflow_api_key
@@ -19,78 +21,131 @@ def add_deployment_parser(subparsers):
19
21
deployment_delete_parser = deployment_subparsers .add_parser ("delete" , help = "delete a dedicated deployment" )
20
22
21
23
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" )
23
25
24
26
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
+ )
26
32
# 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"
28
34
# )
29
35
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"
31
37
)
32
38
deployment_add_parser .add_argument (
33
39
"-t" ,
34
- dest = " duration" ,
40
+ "-- duration" ,
35
41
help = "duration, how long you want to keep the deployment (unit: hour, default: 3)" ,
36
42
type = float ,
37
43
default = 3 ,
38
44
)
39
45
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"
41
47
)
42
48
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" ,
44
53
)
45
54
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 "
47
56
)
48
57
49
58
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
+ )
52
64
53
65
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" )
55
67
56
68
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 " )
59
71
60
72
61
73
def list_machine_types (args ):
62
74
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 ))
65
83
66
84
67
85
def add_deployment (args ):
68
86
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 (
70
91
api_key ,
71
92
# args.security_level,
72
93
args .machine_type ,
73
94
args .duration ,
74
- args .delete_on_expiration ,
95
+ ( not args .no_delete_on_expiration ) ,
75
96
args .deployment_name ,
76
97
args .inference_version ,
77
98
)
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 )
79
109
80
110
81
111
def get_deployment (args ):
82
112
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 )
85
128
86
129
87
130
def list_deployment (args ):
88
131
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 ))
91
140
92
141
93
142
def delete_deployment (args ):
94
143
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