forked from Azure/azureml-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readme.py
647 lines (578 loc) · 19.9 KB
/
readme.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# imports
import os
import json
import glob
import argparse
# define constants
EXCLUDED_JOBS = ["java", "spark"]
EXCLUDED_ENDPOINTS = ["batch", "online", "amlarc"]
EXCLUDED_RESOURCES = [
"workspace",
"datastore",
"vm-attach",
"instance",
"connections",
"compute/cluster-user-identity",
"compute/attached-spark",
"compute/attached-spark-system-identity",
"compute/attached-spark-user-identity",
"registry",
]
EXCLUDED_ASSETS = ["conda-yamls", "mlflow-models"]
EXCLUDED_SCHEDULES = []
EXCLUDED_SCRIPTS = [
"setup",
"cleanup",
"run-job",
"deploy-custom-container-multimodel-minimal",
"run-pipeline-jobs",
]
READONLY_HEADER = "# This code is autogenerated.\
\n# Code is generated by running custom script: python3 readme.py\
\n# Any manual changes to this file may cause incorrect behavior.\
\n# Any manual changes will be overwritten if the code is regenerated.\n"
CREDENTIALS = "${{secrets.AZUREML_CREDENTIALS}}"
BRANCH = "main" # default - do not change
# Duplicate name in working directory during checkout
# https://github.com/actions/checkout/issues/739
GITHUB_WORKSPACE = "${{ github.workspace }}"
GITHUB_CONCURRENCY_GROUP = (
"${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}"
)
# BRANCH = "sdk-preview" # this should be deleted when this branch is merged to main
# define functions
def main(args):
# get list of notebooks
notebooks = sorted(glob.glob("**/*.ipynb", recursive=True))
# make all notebooks consistent
modify_notebooks(notebooks)
# get list of jobs
jobs = sorted(glob.glob("jobs/**/*job*.yml", recursive=True))
jobs += sorted(glob.glob("jobs/basics/*.yml", recursive=False))
jobs += sorted(glob.glob("jobs/*/basics/**/*job*.yml", recursive=True))
jobs += sorted(glob.glob("jobs/pipelines/**/*pipeline*.yml", recursive=True))
jobs += sorted(
glob.glob("jobs/automl-standalone-jobs/**/cli-automl-*.yml", recursive=True)
)
jobs += sorted(
glob.glob("jobs/pipelines-with-components/**/*pipeline*.yml", recursive=True)
)
jobs += sorted(
glob.glob("jobs/automl-standalone-jobs/**/*cli-automl*.yml", recursive=True)
)
jobs += sorted(glob.glob("responsible-ai/**/cli-*.yml", recursive=True))
jobs = [
job.replace(".yml", "")
for job in jobs
if not any(excluded in job for excluded in EXCLUDED_JOBS)
]
# get list of endpoints
endpoints = sorted(glob.glob("endpoints/**/*.yml", recursive=True))
endpoints = [
endpoint.replace(".yml", "")
for endpoint in endpoints
if not any(excluded in endpoint for excluded in EXCLUDED_ENDPOINTS)
]
# get list of resources
resources = sorted(glob.glob("resources/**/*.yml", recursive=True))
resources = [
resource.replace(".yml", "")
for resource in resources
if not any(excluded in resource for excluded in EXCLUDED_RESOURCES)
]
# get list of assets
assets = sorted(glob.glob("assets/**/*.yml", recursive=True))
assets = [
asset.replace(".yml", "")
for asset in assets
if not any(excluded in asset for excluded in EXCLUDED_ASSETS)
]
# get list of scripts
scripts = sorted(glob.glob("*.sh", recursive=False))
scripts = [
script.replace(".sh", "")
for script in scripts
if not any(excluded in script for excluded in EXCLUDED_SCRIPTS)
]
# get list of schedules
schedules = sorted(glob.glob("schedules/**/*schedule.yml", recursive=True))
schedules = [
schedule.replace(".yml", "")
for schedule in schedules
if not any(excluded in schedule for excluded in EXCLUDED_SCHEDULES)
]
# write workflows
write_workflows(jobs, endpoints, resources, assets, scripts, schedules)
# read existing README.md
with open("README.md", "r") as f:
readme_before = f.read()
# write README.md
write_readme(jobs, endpoints, resources, assets, scripts, schedules)
# read modified README.md
with open("README.md", "r") as f:
readme_after = f.read()
# check if readme matches
if args.check_readme:
if not check_readme(readme_before, readme_after):
print("README.md file did not match...")
exit(2)
def modify_notebooks(notebooks):
# setup variables
kernelspec = {
"display_name": "Python 3.8 - AzureML",
"language": "python",
"name": "python38-azureml",
}
# for each notebooks
for notebook in notebooks:
# read in notebook
with open(notebook, "r") as f:
data = json.load(f)
# update metadata
data["metadata"]["kernelspec"] = kernelspec
# write notebook
with open(notebook, "w") as f:
json.dump(data, f, indent=1)
def write_readme(jobs, endpoints, resources, assets, scripts, schedules):
# read in prefix.md and suffix.md
with open("prefix.md", "r") as f:
prefix = f.read()
with open("suffix.md", "r") as f:
suffix = f.read()
# define markdown tables
jobs_table = "\n**Jobs** ([jobs](jobs))\n\npath|status|description\n-|-|-\n"
endpoints_table = (
"\n**Endpoints** ([endpoints](endpoints))\n\npath|status|description\n-|-|-\n"
)
resources_table = (
"\n**Resources** ([resources](resources))\n\npath|status|description\n-|-|-\n"
)
assets_table = "\n**Assets** ([assets](assets))\n\npath|status|description\n-|-|-\n"
scripts_table = "\n**Scripts**\n\npath|status|\n-|-\n"
schedules_table = "\n**Schedules**\n\npath|status|\n-|-\n"
# process jobs
for job in jobs:
# build entries for tutorial table
status = f"[![{job}](https://github.com/Azure/azureml-examples/workflows/cli-{job.replace('/', '-')}/badge.svg?branch={BRANCH})](https://github.com/Azure/azureml-examples/actions/workflows/cli-{job.replace('/', '-')}.yml)"
description = "*no description*"
try:
with open(f"{job}.yml", "r") as f:
for line in f.readlines():
if "description: " in str(line):
description = line.split(": ")[-1].strip()
break
except:
pass
# add row to tutorial table
row = f"[{job}.yml]({job}.yml)|{status}|{description}\n"
jobs_table += row
# process endpoints
for endpoint in endpoints:
# build entries for tutorial table
status = f"[![{endpoint}](https://github.com/Azure/azureml-examples/workflows/cli-{endpoint.replace('/', '-')}/badge.svg?branch={BRANCH})](https://github.com/Azure/azureml-examples/actions/workflows/cli-{endpoint.replace('/', '-')}.yml)"
description = "*no description*"
try:
with open(f"{endpoint}.yml", "r") as f:
for line in f.readlines():
if "description: " in str(line):
description = line.split(": ")[-1].strip()
break
except:
pass
# add row to tutorial table
row = f"[{endpoint}.yml]({endpoint}.yml)|{status}|{description}\n"
endpoints_table += row
# process resources
for resource in resources:
# build entries for tutorial table
status = f"[![{resource}](https://github.com/Azure/azureml-examples/workflows/cli-{resource.replace('/', '-')}/badge.svg?branch={BRANCH})](https://github.com/Azure/azureml-examples/actions/workflows/cli-{resource.replace('/', '-')}.yml)"
description = "*no description*"
try:
with open(f"{resource}.yml", "r") as f:
for line in f.readlines():
if "description: " in str(line):
description = line.split(": ")[-1].strip()
break
except:
pass
# add row to tutorial table
row = f"[{resource}.yml]({resource}.yml)|{status}|{description}\n"
resources_table += row
# process assets
for asset in assets:
# build entries for tutorial table
status = f"[![{asset}](https://github.com/Azure/azureml-examples/workflows/cli-{asset.replace('/', '-')}/badge.svg?branch={BRANCH})](https://github.com/Azure/azureml-examples/actions/workflows/cli-{asset.replace('/', '-')}.yml)"
description = "*no description*"
try:
with open(f"{asset}.yml", "r") as f:
for line in f.readlines():
if "description: " in str(line):
description = line.split(": ")[-1].strip()
break
except:
pass
# add row to tutorial table
row = f"[{asset}.yml]({asset}.yml)|{status}|{description}\n"
assets_table += row
# process scripts
for script in scripts:
# build entries for tutorial table
status = f"[![{script}](https://github.com/Azure/azureml-examples/workflows/cli-scripts-{script}/badge.svg?branch={BRANCH})](https://github.com/Azure/azureml-examples/actions/workflows/cli-scripts-{script}.yml)"
link = f"https://scripts.microsoft.com/azure/machine-learning/{script}"
# add row to tutorial table
row = f"[{script}.sh]({script}.sh)|{status}\n"
scripts_table += row
# process schedules
for schedule in schedules:
# build entries for tutorial table
status = f"[![{schedule}](https://github.com/Azure/azureml-examples/workflows/cli-schedules-{schedule}/badge.svg?branch={BRANCH})](https://github.com/Azure/azureml-examples/actions/workflows/cli-schedules-{schedule}.yml)"
link = f"https://schedules.microsoft.com/azure/machine-learning/{schedule}"
# add row to tutorial table
row = f"[{schedule}.yml]({schedule}.yml)|{status}\n"
schedules_table += row
# write README.md
print("writing README.md...")
with open("README.md", "w") as f:
f.write(
prefix
+ scripts_table
+ jobs_table
+ endpoints_table
+ resources_table
+ assets_table
+ schedules_table
+ suffix
)
print("Finished writing README.md...")
def write_workflows(jobs, endpoints, resources, assets, scripts, schedules):
print("writing .github/workflows...")
# process jobs
for job in jobs:
# write workflow file
write_job_workflow(job)
# process endpoints
for endpoint in endpoints:
# write workflow file
# write_endpoint_workflow(endpoint)
pass
# process assest
for resource in resources:
# write workflow file
write_asset_workflow(resource)
# process assest
for asset in assets:
# write workflow file
write_asset_workflow(asset)
# process scripts
for script in scripts:
# write workflow file
write_script_workflow(script)
# process schedules
for schedule in schedules:
# write workflow file
write_schedule_workflow(schedule)
def check_readme(before, after):
return before == after
def parse_path(path):
filename = None
project_dir = None
hyphenated = None
try:
filename = path.split("/")[-1]
except:
pass
try:
project_dir = "/".join(path.split("/")[:-1])
except:
pass
try:
hyphenated = path.replace("/", "-")
except:
pass
return filename, project_dir, hyphenated
def write_job_workflow(job):
filename, project_dir, hyphenated = parse_path(job)
is_pipeline_sample = "jobs/pipelines" in job
creds = CREDENTIALS
# Duplicate name in working directory during checkout
# https://github.com/actions/checkout/issues/739
workflow_yaml = f"""{READONLY_HEADER}
name: cli-{hyphenated}
on:
workflow_dispatch:
schedule:
- cron: "0 0/8 * * *"
pull_request:
branches:
- main
paths:
- cli/{project_dir}/**
- infra/**
- .github/workflows/cli-{hyphenated}.yml\n"""
if is_pipeline_sample:
workflow_yaml += " - cli/run-pipeline-jobs.sh\n" ""
workflow_yaml += f""" - cli/setup.sh
concurrency:
group: {GITHUB_CONCURRENCY_GROUP}
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: check out repo
uses: actions/checkout@v2
- name: azure login
uses: azure/login@v1
with:
creds: {creds}
- name: bootstrap resources
run: |
echo '{GITHUB_CONCURRENCY_GROUP}';
bash bootstrap.sh
working-directory: infra
continue-on-error: false
- name: setup-cli
run: |
source "{GITHUB_WORKSPACE}/infra/sdk_helpers.sh";
source "{GITHUB_WORKSPACE}/infra/init_environment.sh";
bash setup.sh
working-directory: cli
continue-on-error: true
- name: run job
run: |
source "{GITHUB_WORKSPACE}/infra/sdk_helpers.sh";
source "{GITHUB_WORKSPACE}/infra/init_environment.sh";\n"""
if "automl" in job and "image" in job:
workflow_yaml += f""" bash \"{GITHUB_WORKSPACE}/infra/sdk_helpers.sh\" replace_template_values \"prepare_data.py\";
pip install azure-identity
bash \"{GITHUB_WORKSPACE}/sdk/python/setup.sh\"
python prepare_data.py --subscription $SUBSCRIPTION_ID --group $RESOURCE_GROUP_NAME --workspace $WORKSPACE_NAME\n"""
workflow_yaml += f""" bash -x {os.path.relpath(".", project_dir)}/run-job.sh {filename}.yml
working-directory: cli/{project_dir}\n"""
# write workflow
with open(f"../.github/workflows/cli-{job.replace('/', '-')}.yml", "w") as f:
f.write(workflow_yaml)
def write_endpoint_workflow(endpoint):
filename, project_dir, hyphenated = parse_path(endpoint)
creds = CREDENTIALS
workflow_yaml = f"""{READONLY_HEADER}
name: cli-{hyphenated}
on:
workflow_dispatch:
schedule:
- cron: "0 0/8 * * *"
pull_request:
branches:
- main
paths:
- cli/{project_dir}/**
- infra/**
- .github/workflows/cli-{hyphenated}.yml
- cli/setup.sh
concurrency:
group: {GITHUB_CONCURRENCY_GROUP}
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: check out repo
uses: actions/checkout@v2
- name: azure login
uses: azure/login@v1
with:
creds: {creds}
- name: bootstrap resources
run: |
bash bootstrap.sh
working-directory: infra
continue-on-error: false
- name: setup-cli
run: |
source "{GITHUB_WORKSPACE}/infra/sdk_helpers.sh";
source "{GITHUB_WORKSPACE}/infra/init_environment.sh";
bash setup.sh
working-directory: cli
continue-on-error: true
- name: create endpoint
run: |
source "{GITHUB_WORKSPACE}/infra/sdk_helpers.sh";
source "{GITHUB_WORKSPACE}/infra/init_environment.sh";
az ml endpoint create -f {endpoint}.yml
working-directory: cli\n"""
# write workflow
with open(f"../.github/workflows/cli-{hyphenated}.yml", "w") as f:
f.write(workflow_yaml)
def write_asset_workflow(asset):
filename, project_dir, hyphenated = parse_path(asset)
creds = CREDENTIALS
workflow_yaml = f"""{READONLY_HEADER}
name: cli-{hyphenated}
on:
workflow_dispatch:
schedule:
- cron: "0 0/8 * * *"
pull_request:
branches:
- main
paths:
- cli/{asset}.yml
- infra/**
- .github/workflows/cli-{hyphenated}.yml
- cli/setup.sh
concurrency:
group: {GITHUB_CONCURRENCY_GROUP}
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: check out repo
uses: actions/checkout@v2
- name: azure login
uses: azure/login@v1
with:
creds: {creds}
- name: bootstrap resources
run: |
bash bootstrap.sh
working-directory: infra
continue-on-error: false
- name: setup-cli
run: |
source "{GITHUB_WORKSPACE}/infra/sdk_helpers.sh";
source "{GITHUB_WORKSPACE}/infra/init_environment.sh";
bash setup.sh
working-directory: cli
continue-on-error: true
- name: create asset
run: |
source "{GITHUB_WORKSPACE}/infra/sdk_helpers.sh";
source "{GITHUB_WORKSPACE}/infra/init_environment.sh";
az ml {asset.split('/')[1]} create -f {asset}.yml
working-directory: cli\n"""
# write workflow
with open(f"../.github/workflows/cli-{hyphenated}.yml", "w") as f:
f.write(workflow_yaml)
def write_script_workflow(script):
filename, project_dir, hyphenated = parse_path(script)
creds = CREDENTIALS
workflow_yaml = f"""{READONLY_HEADER}
name: cli-scripts-{hyphenated}
on:
workflow_dispatch:
schedule:
- cron: "0 0/8 * * *"
pull_request:
branches:
- main
paths:
- cli/{script}.sh
- infra/**
- .github/workflows/cli-scripts-{hyphenated}.yml
- cli/setup.sh
concurrency:
group: {GITHUB_CONCURRENCY_GROUP}
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: check out repo
uses: actions/checkout@v2
- name: azure login
uses: azure/login@v1
with:
creds: {creds}
- name: bootstrap resources
run: |
bash bootstrap.sh
working-directory: infra
continue-on-error: false
- name: setup-cli
run: |
source "{GITHUB_WORKSPACE}/infra/sdk_helpers.sh";
source "{GITHUB_WORKSPACE}/infra/init_environment.sh";
bash setup.sh
working-directory: cli
continue-on-error: true
- name: test script script
run: |
source "{GITHUB_WORKSPACE}/infra/sdk_helpers.sh";
source "{GITHUB_WORKSPACE}/infra/init_environment.sh";
set -e; bash -x {script}.sh
working-directory: cli\n"""
# write workflow
with open(f"../.github/workflows/cli-scripts-{hyphenated}.yml", "w") as f:
f.write(workflow_yaml)
def write_schedule_workflow(schedule):
filename, project_dir, hyphenated = parse_path(schedule)
creds = CREDENTIALS
workflow_yaml = f"""{READONLY_HEADER}
name: cli-schedules-{hyphenated}
on:
workflow_dispatch:
schedule:
- cron: "0 0/8 * * *"
pull_request:
branches:
- main
paths:
- cli/{schedule}.yml
- infra/**
- .github/workflows/cli-schedules-{hyphenated}.yml
- cli/setup.sh
concurrency:
group: {GITHUB_CONCURRENCY_GROUP}
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: check out repo
uses: actions/checkout@v2
- name: azure login
uses: azure/login@v1
with:
creds: {creds}
- name: bootstrap resources
run: |
bash bootstrap.sh
working-directory: infra
continue-on-error: false
- name: setup-cli
run: |
source "{GITHUB_WORKSPACE}/infra/sdk_helpers.sh";
source "{GITHUB_WORKSPACE}/infra/init_environment.sh";
bash setup.sh
working-directory: cli
continue-on-error: true
- name: create schedule
run: |
source "{GITHUB_WORKSPACE}/infra/sdk_helpers.sh";
source "{GITHUB_WORKSPACE}/infra/init_environment.sh";
az ml schedule create -f ./{schedule}.yml --set name="ci_test_{filename}"
working-directory: cli\n
- name: disable schedule
run: |
source "{GITHUB_WORKSPACE}/infra/sdk_helpers.sh";
source "{GITHUB_WORKSPACE}/infra/init_environment.sh";
az ml schedule disable --name ci_test_{filename}
working-directory: cli\n"""
# write workflow
with open(f"../.github/workflows/cli-schedules-{hyphenated}.yml", "w") as f:
f.write(workflow_yaml)
# run functions
if __name__ == "__main__":
# issue #146
if "posix" not in os.name:
print(
"windows is not supported, see issue #146 (https://github.com/Azure/azureml-examples/issues/146)"
)
exit(1)
# setup argparse
parser = argparse.ArgumentParser()
parser.add_argument("--check-readme", type=bool, default=False)
args = parser.parse_args()
# call main
main(args)