-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathutils.py
220 lines (173 loc) · 6.83 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import argparse
def get_workspace():
import os
import json
from azureml.core.authentication import ServicePrincipalAuthentication
from azureml.core import Workspace
base_dir = "."
config_json = os.path.join(base_dir, "config.json")
with open(config_json, "r") as f:
config = json.load(f)
try:
svc_pr = ServicePrincipalAuthentication(
tenant_id=config["tenant_id"],
service_principal_id=config["service_principal_id"],
service_principal_password=config["service_principal_password"],
)
except KeyError:
print("Getting Service Principal Authentication from Azure Devops.")
svc_pr = None
pass
ws = Workspace.from_config(path=config_json, auth=svc_pr)
return ws
def disable_pipeline(pipeline_name="", dry_run=True):
from azureml.pipeline.core import PublishedPipeline
from azureml.pipeline.core.schedule import Schedule
if dry_run:
print("Dry run: only printing what would be done")
else:
print("Disabling pipelines")
ws = get_workspace()
# Get all published pipeline objects in the workspace
all_pub_pipelines = PublishedPipeline.list(ws)
# We will iterate through the list of published pipelines and
# use the last ID in the list for Schedule operations:
print("Published pipelines found in the workspace:")
for pub_pipeline in all_pub_pipelines:
if (
pub_pipeline.name.startswith("prednet")
and pub_pipeline.name == pipeline_name
or pipeline_name == ""
):
print("Found pipeline:", pub_pipeline.name, pub_pipeline.id)
pub_pipeline_id = pub_pipeline.id
schedules = Schedule.list(ws, pipeline_id=pub_pipeline_id)
# We will iterate through the list of schedules and
# use the last ID in the list for further operations:
print(
"Found these schedules for the pipeline id {}:".format(
pub_pipeline_id
)
)
for schedule in schedules:
print(schedule.name, schedule.id)
if not dry_run:
schedule_id = schedule.id
print(
"Schedule id to be used for schedule "
"operations: {}".format(
schedule_id
)
)
fetched_schedule = Schedule.get(ws, schedule_id)
print(
"Using schedule with id: {}".format(
fetched_schedule.id
)
)
fetched_schedule.disable(wait_for_provisioning=True)
fetched_schedule = Schedule.get(ws, schedule_id)
print(
"Disabled schedule {}. New status is: {}".format(
fetched_schedule.id, fetched_schedule.status
)
)
if not dry_run:
print("Disabling pipeline")
pub_pipeline.disable()
def upload_data(folder='UCSDped1'):
import os
ws = get_workspace()
ds = ws.get_default_datastore()
src_dir = os.path.join("./data/UCSD_Anomaly_Dataset.v1p2", folder)
target_path = os.path.join("prednet/data/raw_data", folder)
ds.upload(
src_dir=src_dir,
target_path=target_path,
)
with open("placeholder.txt", "w") as f:
f.write(
"This is just a placeholder to ensure that this path exists in the blobstore.\n"
"The scheduler of the master pipeline checks whether the time stamp of this file has changed.\n"
"This was last updated when this folder was uploaded: %s\n" % folder
)
ds.upload_files(
[os.path.join(os.getcwd(), "placeholder.txt")],
target_path="prednet/data/raw_data/",
overwrite=True
)
def delete_data_from_blob(prefix):
from azure.storage.blob.blockblobservice import BlockBlobService
ws = get_workspace()
def_blob_store = ws.get_default_datastore()
print("Deleting blobs from folder:", prefix)
blob_service = BlockBlobService(
def_blob_store.account_name, def_blob_store.account_key
)
generator = blob_service.list_blobs(
def_blob_store.container_name, prefix=prefix
)
for blob in generator:
if blob.name.endswith("mp4"):
print("Deleting: " + blob.name)
blob_service.delete_blob(def_blob_store.container_name, blob.name)
generator = blob_service.list_blobs(
def_blob_store.container_name, prefix=prefix
)
for blob in generator:
print("Deleting: " + blob.name)
blob_service.delete_blob(def_blob_store.container_name, blob.name)
def cancel_all_runs(exp_name, run_id=None):
from azureml.core import Experiment
from azureml.core import get_run
ws = get_workspace()
exp = Experiment(ws, exp_name)
if run_id:
r = get_run(experiment=exp, run_id=run_id, rehydrate=True)
# check the returned run type and status
print(type(r), r.get_status())
# you can cancel a run if it hasn't completed or failed
if r.get_status() not in ["Complete", "Failed"]:
r.cancel()
else:
# if you don't know the run id, you can list all
# runs under an experiment
for r in exp.get_runs():
run = get_run(experiment=exp, run_id=r.id, rehydrate=True)
for c in run.get_children():
for gc in c.get_children():
if (
gc.get_status() == "Running"
or gc.get_status() == "Queued"
):
print(gc.id, gc.get_status())
gc.cancel()
if c.get_status() == "Running" or c.get_status() == "Queued":
print(c.id, c.get_status())
c.cancel()
if r.get_status() == "Running" or r.get_status() == "Queued":
print(r.id, r.get_status())
r.cancel()
def delete_models(model_names=[]):
from azureml.core.model import Model
ws = get_workspace()
model_list = Model.list(ws)
for model in model_list:
if model.name in model_names or len(model_names) == 0:
model.delete()
def main():
parser = argparse.ArgumentParser(description="Process input arguments")
parser.add_argument(
"--disable-pipelines",
action="store_true",
dest="disable_pipelines",
help="disable published pipelines",
)
args = parser.parse_args()
if args.disable_pipelines:
disable_pipeline(dry_run=False)
else:
parser.print_usage()
if __name__ == "__main__":
# execute only if run as a script
main()