2
2
3
3
from roboflow .adapters import deploymentapi
4
4
from roboflow .config import load_roboflow_api_key
5
+ import time
6
+ from datetime import datetime
5
7
6
8
7
9
def add_deployment_parser (subparsers ):
@@ -19,78 +21,121 @@ 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" )
26
- # deployment_add_parser.add_argument(
27
- # "-s", dest="security_level", help="security level (protected)", default="protected"
28
- # )
27
+ deployment_add_parser .add_argument ("-a" , "--api_key" , help = "api key" )
29
28
deployment_add_parser .add_argument (
30
- "-m " , dest = "machine_type" , help = "machine type, run `roboflow deployment machine_type` to see available options "
29
+ "deployment_name " , help = "deployment name, must contain 5-15 lowercase characters, first character must be a letter "
31
30
)
31
+ # deployment_add_parser.add_argument(
32
+ # "-s", "--security_level", help="security level (protected)", default="protected"
33
+ # )
32
34
deployment_add_parser .add_argument (
33
- "-t" ,
34
- dest = "duration" ,
35
- help = "duration, how long you want to keep the deployment (unit: hour, default: 3)" ,
36
- type = float ,
37
- default = 3 ,
35
+ "-m" , "--machine_type" , help = "machine type, run `roboflow deployment machine_type` to see available options"
38
36
)
39
37
deployment_add_parser .add_argument (
40
- "-e" , dest = "delete_on_expiration" , help = "delete when expired (default: True)" , type = bool , default = True
38
+ "-t" , "--duration" , help = "duration, how long you want to keep the deployment (unit: hour, default: 3)" ,
39
+ type = float , default = 3
41
40
)
42
41
deployment_add_parser .add_argument (
43
- "-n " , dest = "deployment_name " , help = "deployment name, must contain 3-10 lowercase characters "
42
+ "-e " , "--no_delete_on_expiration " , help = "keep when expired (default: False)" , action = "store_true "
44
43
)
45
44
deployment_add_parser .add_argument (
46
- "-v" , dest = " inference_version" , help = "inference server version (default: latest)" , default = "latest"
45
+ "-v" , "-- inference_version" , help = "inference server version (default: latest)" , default = "latest" ,
47
46
)
47
+ deployment_add_parser .add_argument ("-w" , "--wait_on_pending" , help = "wait if deployment is pending" , action = "store_true" )
48
48
49
49
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" )
50
+ deployment_get_parser .add_argument ("-a" , "--api_key" , help = "api key" )
51
+ deployment_get_parser .add_argument ("deployment_name" , help = "deployment name" )
52
+ deployment_get_parser .add_argument ("-w" , "--wait_on_pending" , help = "wait if deployment is pending" , action = "store_true" )
52
53
53
54
deployment_list_parser .set_defaults (func = list_deployment )
54
- deployment_list_parser .add_argument ("-a" , dest = " api_key" , help = "api key" )
55
+ deployment_list_parser .add_argument ("-a" , "-- api_key" , help = "api key" )
55
56
56
57
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 " )
58
+ deployment_delete_parser .add_argument ("-a" , "-- api_key" , help = "api key" )
59
+ deployment_delete_parser .add_argument ("deployment_name " , help = "deployment name " )
59
60
60
61
61
62
def list_machine_types (args ):
62
63
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 ))
64
+ if api_key is None :
65
+ print ("Please provide an api key" )
66
+ return
67
+ status_code , msg = deploymentapi .list_machine_types (api_key )
68
+ if status_code != 200 :
69
+ print (f"{ status_code } : { msg } " )
70
+ return
71
+ print (json .dumps (msg , indent = 2 ))
65
72
66
73
67
74
def add_deployment (args ):
68
75
api_key = args .api_key or load_roboflow_api_key (None )
69
- ret_json = deploymentapi .add_deployment (
76
+ if api_key is None :
77
+ print ("Please provide an api key" )
78
+ return
79
+ status_code , msg = deploymentapi .add_deployment (
70
80
api_key ,
71
81
# args.security_level,
72
82
args .machine_type ,
73
83
args .duration ,
74
- args .delete_on_expiration ,
84
+ ( not args .no_delete_on_expiration ) ,
75
85
args .deployment_name ,
76
86
args .inference_version ,
77
87
)
78
- print (json .dumps (ret_json , indent = 2 ))
88
+
89
+ if status_code != 200 :
90
+ print (f"{ status_code } : { msg } " )
91
+ return
92
+ else :
93
+ print (f"Deployment { args .deployment_name } created successfully" )
94
+ print (json .dumps (msg , indent = 2 ))
95
+
96
+ if args .wait_on_pending :
97
+ get_deployment (args )
79
98
80
99
81
100
def get_deployment (args ):
82
101
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 ))
102
+ if api_key is None :
103
+ print ("Please provide an api key" )
104
+ return
105
+ while True :
106
+ status_code , msg = deploymentapi .get_deployment (api_key , args .deployment_name )
107
+ if status_code != 200 :
108
+ print (f"{ status_code } : { msg } " )
109
+ return
110
+
111
+ if (not args .wait_on_pending ) or msg ["status" ] != "pending" :
112
+ print (json .dumps (msg , indent = 2 ))
113
+ break
114
+
115
+ print (f'{ datetime .now ().strftime ("%Y-%m-%d %H:%M:%S" )} Waiting for deployment { args .deployment_name } to be ready...\n ' )
116
+ time .sleep (30 )
85
117
86
118
87
119
def list_deployment (args ):
88
120
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 ))
121
+ if api_key is None :
122
+ print ("Please provide an api key" )
123
+ return
124
+ status_code , msg = deploymentapi .list_deployment (api_key )
125
+ if status_code != 200 :
126
+ print (f"{ status_code } : { msg } " )
127
+ return
128
+ print (json .dumps (msg , indent = 2 ))
91
129
92
130
93
131
def delete_deployment (args ):
94
132
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 ))
133
+ if api_key is None :
134
+ print ("Please provide an api key" )
135
+ return
136
+ status_code , msg = deploymentapi .delete_deployment (api_key , args .deployment_name )
137
+ if status_code != 200 :
138
+ print (f"{ status_code } : { msg } " )
139
+ return
140
+ print (json .dumps (msg , indent = 2 ))
141
+
0 commit comments