-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathOneClickDeploy.ps1
More file actions
1736 lines (1490 loc) · 72.5 KB
/
Copy pathOneClickDeploy.ps1
File metadata and controls
1736 lines (1490 loc) · 72.5 KB
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Parameters passed from ARM template
param(
[string] $RG_NAME,
[string] $REGION,
[string] $WORKSPACE_NAME,
[string] $SA_NAME,
[bool] $SA_EXISTS,
[int] $LIFETIME_SECONDS,
[string] $COMMENT,
[string] $CLUSTER_NAME,
[string] $SPARK_VERSION,
[int] $AUTOTERMINATION_MINUTES,
[string] $NUM_WORKERS,
[string] $NODE_TYPE_ID,
[string] $DRIVER_NODE_TYPE_ID,
[int] $RETRY_LIMIT,
[int] $RETRY_TIME,
[bool] $CTRL_DEPLOY_CLUSTER,
[int] $MINWORKERS,
[int] $MAXWORKERS,
[string] $PIPELINENAME,
[string] $STORAGE,
[string] $TARGETSCHEMA,
[bool] $CTRL_DEPLOY_NOTEBOOK,
[bool] $CTRL_DEPLOY_PIPELINE,
[string] $NOTEBOOK_PATH,
[bool] $SRC_FILESOURCE,
[bool] $SRC_AZSQL,
[bool] $SRC_AZMYSQL,
[bool] $SRC_AZPSQL,
[bool] $SRC_SQL_ONPREM,
[bool] $SRC_PSQL_ONPREM,
[bool] $SRC_ORACLE,
[bool] $SRC_EVENTHUB ,
[string] $CTRL_SYNTAX,
[string] $SUBSCRIPTION_ID,
[bool] $CTRL_DEPLOY_SAMPLE
)
[string] $REF_BRANCH = "main"
[string] $EXAMPLE_DATASET = "RetailOrg"
# Generating Databricks Workspace URL
Write-Output "Task: Generating Databricks Workspace URL"
try {
$token = (Get-AzAccessToken).Token
# https url for getting workspace details
$url = "https://management.azure.com/subscriptions/" + $SUBSCRIPTION_ID + "/resourceGroups/" + $RG_NAME + "/providers/Microsoft.Databricks/workspaces/" + $WORKSPACE_NAME + "?api-version=2023-02-01"
# Set the headers
$headerstkn = @{ Authorization = "Bearer $token"; 'ContentType' = "application/json" }
#call http method to get workspace url
$resurl = Invoke-RestMethod -Method Get -ContentType "application/json" -Uri $url -Headers $headerstkn
$WorkspaceUrl = $resurl.properties.workspaceUrl
Write-Host "Successful: Databricks workspace url is generated"
}
catch {
Write-Host "Error while getting the Workspace URL"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
# Generating Databricks Workspace resource ID
Write-Output "Task: Generating Databricks Workspace resource ID"
try {
$WORKSPACE_ID = Get-AzResource -ResourceType Microsoft.Databricks/workspaces -ResourceGroupName $RG_NAME -Name $WORKSPACE_NAME
$ACTUAL_WORKSPACE_ID = $WORKSPACE_ID.ResourceId
Write-Host "Successful: Databricks workspace resource ID is generated"
}
catch {
Write-Host "Error while getting workspace ID"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
# Generating Databricks resource token
Write-Output "Task: Generating Databricks resource token"
try {
# unique resource ID for the Azure Databricks service
[string] $TOKEN = (Get-AzAccessToken -Resource '2ff814a6-3304-4ab8-85cb-cd0e6f879c1d').Token
Write-Host "Successful: Resource Token generated"
}
catch {
Write-Host "Error while getting the resource token"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
# Generating Databricks management token
Write-Output "Task: Generating management token"
try {
[string] $AZ_TOKEN = (Get-AzAccessToken -ResourceUrl 'https://management.core.windows.net/').Token
Write-Host "Successful: Management token generated"
}
catch {
Write-Host "Error while getting the management token"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
# Generating Databricks Personal access token
Write-Output "Task: Generating Databricks Personal access token"
# Set the headers
$HEADERS = @{
"Authorization" = "Bearer $TOKEN"
"X-Databricks-Azure-SP-Management-Token" = "$AZ_TOKEN"
"X-Databricks-Azure-Workspace-Resource-Id" = "$ACTUAL_WORKSPACE_ID"
}
# Set the request body
$BODY = @"
{ "lifetime_seconds": $LIFETIME_SECONDS, "comment": "$COMMENT" }
"@
try {
#https request for generating token
Write-Host "Attempt 1 : Generating Personal Access Token"
$DB_PAT = ((Invoke-RestMethod -Method POST -Uri "https://$WorkspaceUrl/api/2.0/token/create" -Headers $HEADERS -Body $BODY).token_value)
Write-Output "Successful: Personal Access Token generated"
}
catch {
Write-Host "Attempt 1 : Error while calling the Databricks API for generating Personal Access Token"
$errorMessage = $_.Exception.Message
Write-Host $_
Write-Host "Error message: $errorMessage"
try {
Write-Host "Attempt 2 : generating Personal Access Token"
$DB_PAT = ((Invoke-RestMethod -Method POST -Uri "https://$WorkspaceUrl/api/2.0/token/create" -Headers $HEADERS -Body $BODY).token_value)
Write-Output "Successful: Personal Access Token generated"
}
catch {
Write-Host "Attempt 2 : Error while calling the Databricks API for generating Personal Access Token"
$errorMessage = $_.Exception.Message
Write-Host $_
Write-Host "Error message: $errorMessage"
}
}
# Creating All-purpose compute cluster
Write-Output "Task: Creating all-purpose compute cluster"
if ($CTRL_DEPLOY_CLUSTER -and ($null -ne $DB_PAT)) {
# Set the headers
$HEADERS = @{
"Authorization" = "Bearer $DB_PAT"
"Content-Type" = "application/json"
}
# Set the request body
$BODY = @"
{"cluster_name": "$CLUSTER_NAME", "spark_version": "$SPARK_VERSION", "autotermination_minutes": $AUTOTERMINATION_MINUTES, "num_workers": 0, "node_type_id": "$NODE_TYPE_ID", "spark_conf": {
"spark.master": "local[*, 4]",
"spark.databricks.cluster.profile": "singleNode"
}, "custom_tags": {
"ResourceClass": "SingleNode"
} }
"@
try {
#https request for creating cluster
Write-Host "Attempt 1: Databricks API for creating the cluster"
$CLUSTER_ID = ((Invoke-RestMethod -Method POST -Uri "https://$WorkspaceUrl/api/2.0/clusters/create" -Headers $HEADERS -Body $BODY).cluster_id)
Write-Output "Successful: Databricks API for creating the cluster is called"
}
catch {
Write-Host "Attempt 1: Error while calling the Databricks API for creating the cluster"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
try {
Write-Host "Attempt 2: Databricks API for creating the cluster"
$CLUSTER_ID = ((Invoke-RestMethod -Method POST -Uri "https://$WorkspaceUrl/api/2.0/clusters/create" -Headers $HEADERS -Body $BODY).cluster_id)
Write-Output "Successful: Databricks API for creating the cluster is called"
}
catch {
Write-Host "Attempt 2: Error while calling the Databricks API for creating the cluster"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
}
# Checking the All-purpose compute cluster status
if ( $CLUSTER_ID -ne "null" ) {
Write-Output "[INFO] Cluster is being created"
Write-Output "Task: Checking cluster status"
$RETRY_COUNT = 0
for ( $RETRY_COUNT = 1; $RETRY_COUNT -le $RETRY_LIMIT; $RETRY_COUNT++ ) {
Write-Output "[INFO] Attempt $RETRY_COUNT of $RETRY_LIMIT"
$HEADERS = @{
"Authorization" = "Bearer $DB_PAT"
}
try {
#https request for getting cluster details
$STATE = ((Invoke-RestMethod -Method GET -Uri "https://$WorkspaceUrl/api/2.0/clusters/get?cluster_id=$CLUSTER_ID" -Headers $HEADERS).state)
Write-Output "Cluster is still getting ready"
}
catch {
Write-Host "Error while calling the Databricks API for checking the clusters' state"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
if ($STATE -eq "RUNNING") {
Write-Output "Successful: Cluster is healthy and in running state"
break
}
else {
Write-Output "[INFO] Cluster is still not ready, current state: $STATE Next check in $RETRY_TIME seconds.."
Start-Sleep -Seconds $RETRY_TIME
if ($RETRY_COUNT -eq $RETRY_LIMIT) {
Write-Output "No more attempts left, breaking.."
}
}
}
}
else {
Write-Output "[ERROR]: Cluster was not created"
}
}
# Creating Folder strucrure and Importing Notebooks
Write-Output "Task: Importing Notebooks"
if ($null -ne $DB_PAT) {
# Set the headers
$headers = @{
"Authorization" = "Bearer $DB_PAT"
"Content-Type" = "application/json"
}
# Create folder strucrure based on the syntax
Write-Host "Create $CTRL_SYNTAX folder"
try {
$requestBodyFolder = @{
"path" = "/Shared/$CTRL_SYNTAX"
}
$jsonBodyFolder = ConvertTo-Json -Depth 100 $requestBodyFolder
if ($CTRL_SYNTAX -eq "DeltaLiveTable") {
#https request for creating folder
Invoke-RestMethod -Method POST -Uri "https://$WorkspaceUrl/api/2.0/workspace/mkdirs" -Headers $headers -Body $jsonBodyFolder
Write-Output "Successful: Created $CTRL_SYNTAX folder"
}
else {
#https request for creating folder
Invoke-RestMethod -Method POST -Uri "https://$WorkspaceUrl/api/2.0/workspace/mkdirs" -Headers $headers -Body $jsonBodyFolder
Write-Output "Successful: Created $CTRL_SYNTAX folder"
}
$mkdirDelta = $true
}
catch {
$mkdirDelta = $false
Write-Host "Error while calling the Databricks API for creating the $CTRL_SYNTAX folder. Folder not created"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
# Create folder structure for examples
if ($CTRL_DEPLOY_SAMPLE) {
Write-Host "Create folder for Examples/$EXAMPLE_DATASET/DeltaLiveTable"
try {
$requestBodyFolder = @{
"path" = "/Shared/Example/$EXAMPLE_DATASET/DeltaLiveTable"
}
$jsonBodyFolder = ConvertTo-Json -Depth 100 $requestBodyFolder
#https request for creating folder
Invoke-RestMethod -Method POST -Uri "https://$WorkspaceUrl/api/2.0/workspace/mkdirs" -Headers $headers -Body $jsonBodyFolder
Write-Output "Successful: Created Examples folder"
$mkdirExample = $true
}
catch {
$mkdirExample = $false
Write-Host "Error while calling the Databricks API for creating the Example folder. Folder not created"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
Write-Host "Create folder for Examples/$EXAMPLE_DATASET/DeltaTable"
try {
$requestBodyFolder = @{
"path" = "/Shared/Example/$EXAMPLE_DATASET/DeltaTable"
}
$jsonBodyFolder = ConvertTo-Json -Depth 100 $requestBodyFolder
#https request for creating folder
Invoke-RestMethod -Method POST -Uri "https://$WorkspaceUrl/api/2.0/workspace/mkdirs" -Headers $headers -Body $jsonBodyFolder
Write-Output "Successful: Created Examples folder"
$mkdirExample = $true
}
catch {
$mkdirExample = $false
Write-Host "Error while calling the Databricks API for creating the Example folder. Folder not created"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
# Import example notebooks to Example folder
if ($mkdirExample) {
Write-Host "Importing DeltaLiveTable example notebooks"
#github api for a folder
$Artifactsuri = "https://api.github.com/repos/DatabricksFactory/databricks-migration/contents/Artifacts/Example/RetailOrg/DeltaLiveTable?ref=$REF_BRANCH" # change to respective git branch
# Calling GitHub API for getting the filenames under Artifacts/Example/<Dataset> folder
try {
$wr = Invoke-WebRequest -Uri $Artifactsuri
$objects = $wr.Content | ConvertFrom-Json
$fileNames = $objects | Where-Object { $_.type -eq "file" } | Select-Object -exp name
Write-Host "Successful: getting the filenames under Artifacts/Example/$EXAMPLE_DATASET/DeltaLiveTable folder is successful"
$getExmpFilenames = $true
}
catch {
$getExmpFilenames = $false
Write-Host "Error while calling the GitHub API for getting the filenames under Artifacts/Example/$EXAMPLE_DATASET/DeltaLiveTable"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
if ($getExmpFilenames) {
Foreach ($filename in $fileNames) {
try {
# Set the path to the notebook to be imported
$url = "$NOTEBOOK_PATH/Example/$EXAMPLE_DATASET/DeltaLiveTable/$filename"
# Get the notebook
$Webresults = Invoke-WebRequest $url -UseBasicParsing
# Read the notebook file
$notebookContent = $Webresults.Content
# Base64 encode the notebook content
$notebookBase64 = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($notebookContent))
# Set the path
$splitfilename = $filename.Split(".")
$filenamewithoutextension = $splitfilename[0]
$path = "/Shared/Example/$EXAMPLE_DATASET/DeltaLiveTable/$filenamewithoutextension";
# Set the request body
$requestBody = @{
"content" = $notebookBase64
"path" = $path
"language" = "PYTHON"
"format" = "JUPYTER"
}
# Convert the request body to JSON
$jsonBody = ConvertTo-Json -Depth 100 $requestBody
}
catch {
Write-Host "Error while reading the raw example notebook: $filename"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
try {
# Make the HTTP request to import the notebook
$response = Invoke-RestMethod -Method POST -Uri "https://$WorkspaceUrl/api/2.0/workspace/import" -Headers $headers -Body $jsonBody
Write-Host "Successful: $filename is imported"
}
catch {
Write-Host "Error while calling the Azure Databricks API for importing example notebook: $filename"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
}
}
Write-Host "Importing blob to adls copy notebook"
#github api for a folder
$Artifactsuri1 = "https://api.github.com/repos/DatabricksFactory/databricks-migration/contents/Artifacts/Example/" + $EXAMPLE_DATASET + "?ref=$REF_BRANCH" # change to respective git branch
# Calling GitHub API for getting the filenames under Artifacts/Example/ folder
try {
$wr = Invoke-WebRequest -Uri $Artifactsuri1
$objects = $wr.Content | ConvertFrom-Json
$fileNames = $objects | Where-Object { $_.type -eq "file" } | Select-Object -exp name
Write-Host "Successful: getting the filenames under Artifacts/Example/ folder is successful"
$getExmpFilenames = $true
}
catch {
$getExmpFilenames = $false
Write-Host "Error while calling the GitHub API for getting the filenames under Artifacts/Example"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
if ($getExmpFilenames) {
Foreach ($filename in $fileNames) {
try {
# Set the path to the notebook to be imported
$url = "$NOTEBOOK_PATH/Example/$EXAMPLE_DATASET/$filename"
# Get the notebook
$Webresults = Invoke-WebRequest $url -UseBasicParsing
# Read the notebook file
$notebookContent = $Webresults.Content
# Base64 encode the notebook content
$notebookBase64 = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($notebookContent))
# Set the path
$splitfilename = $filename.Split(".")
$filenamewithoutextension = $splitfilename[0]
$path = "/Shared/Example/$EXAMPLE_DATASET/$filenamewithoutextension";
# Set the request body
$requestBody = @{
"content" = $notebookBase64
"path" = $path
"language" = "PYTHON"
"format" = "JUPYTER"
}
# Convert the request body to JSON
$jsonBody = ConvertTo-Json -Depth 100 $requestBody
}
catch {
Write-Host "Error while reading the raw copy notebook: $filename"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
try {
# Make the HTTP request to import the notebook
$response = Invoke-RestMethod -Method POST -Uri "https://$WorkspaceUrl/api/2.0/workspace/import" -Headers $headers -Body $jsonBody
Write-Host "Successful: $filename is imported"
}
catch {
Write-Host "Error while calling the Azure Databricks API for importing copy notebook: $filename"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
}
}
#github api for a DeltaTable folder
$Artifactsuri = "https://api.github.com/repos/DatabricksFactory/databricks-migration/contents/Artifacts/Example/RetailOrg/DeltaTable?ref=$REF_BRANCH" # change to respective git branch
# Calling GitHub API for getting the filenames under Artifacts/Example/<Dataset> folder
try {
$wr = Invoke-WebRequest -Uri $Artifactsuri
$objects = $wr.Content | ConvertFrom-Json
$fileNames = $objects | Where-Object { $_.type -eq "file" } | Select-Object -exp name
Write-Host "Successful: getting the filenames under Artifacts/Example/$EXAMPLE_DATASET/DeltaTable folder is successful"
$getExmpFilenames = $true
}
catch {
$getExmpFilenames = $false
Write-Host "Error while calling the GitHub API for getting the filenames under Artifacts/Example/$EXAMPLE_DATASET/DeltaTable"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
if ($getExmpFilenames) {
Foreach ($filename in $fileNames) {
try {
# Set the path to the notebook to be imported
$url = "$NOTEBOOK_PATH/Example/$EXAMPLE_DATASET/DeltaTable/$filename"
# Get the notebook
$Webresults = Invoke-WebRequest $url -UseBasicParsing
# Read the notebook file
$notebookContent = $Webresults.Content
# Base64 encode the notebook content
$notebookBase64 = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($notebookContent))
# Set the path
$splitfilename = $filename.Split(".")
$filenamewithoutextension = $splitfilename[0]
$path = "/Shared/Example/$EXAMPLE_DATASET/DeltaTable/$filenamewithoutextension";
# Set the request body
$requestBody = @{
"content" = $notebookBase64
"path" = $path
"language" = "PYTHON"
"format" = "JUPYTER"
}
# Convert the request body to JSON
$jsonBody = ConvertTo-Json -Depth 100 $requestBody
}
catch {
Write-Host "Error while reading the raw example notebook: $filename"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
try {
# Make the HTTP request to import the notebook
$response = Invoke-RestMethod -Method POST -Uri "https://$WorkspaceUrl/api/2.0/workspace/import" -Headers $headers -Body $jsonBody
Write-Host "Successful: $filename is imported"
}
catch {
Write-Host "Error while calling the Azure Databricks API for importing example notebook: $filename"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
}
}
}
}
# Upload Silver and Gold Layer notebooks for a batch source to its respective syntax folder
if (!$SRC_EVENTHUB -and $mkdirDelta) {
Write-Host "Task: Import Silver and Gold Layer notebooks for a batch source"
#github api for a folder
$Artifactsuri = "https://api.github.com/repos/DatabricksFactory/databricks-migration/contents/Artifacts/" + $CTRL_SYNTAX + "?ref=$REF_BRANCH" # change to respective git branch
# Calling GitHub API for getting the filenames under Artifacts/$CTRL_SYNTAX folder
try {
$wr = Invoke-WebRequest -Uri $Artifactsuri
$objects = $wr.Content | ConvertFrom-Json
$fileNames = $objects | Where-Object { $_.type -eq "file" } | Select-Object -exp name
Write-Host "Successful: getting the filenames under Artifacts/$CTRL_SYNTAX folder is successful"
$getSGFilenames = $true
}
catch {
$getSGFilenames = $false
Write-Host "Error while calling the GitHub API for getting the filenames under Artifacts/$CTRL_SYNTAX"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
if ($getSGFilenames) {
Foreach ($filename in $fileNames) {
try {
# Set the path to the notebook to be imported
$url = "$NOTEBOOK_PATH/$CTRL_SYNTAX/$filename"
# Get the notebook
$Webresults = Invoke-WebRequest $url -UseBasicParsing
# Read the notebook file
$notebookContent = $Webresults.Content
# Base64 encode the notebook content
$notebookBase64 = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($notebookContent))
# Set the path
$splitfilename = $filename.Split(".")
$filenamewithoutextension = $splitfilename[0]
$path = "/Shared/$CTRL_SYNTAX/$filenamewithoutextension";
# Set the request body
$requestBody = @{
"content" = $notebookBase64
"path" = $path
"language" = "PYTHON"
"format" = "JUPYTER"
}
# Convert the request body to JSON
$jsonBody = ConvertTo-Json -Depth 100 $requestBody
}
catch {
Write-Host "Error while reading the notebook: $filename"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
try {
# Make the HTTP request to import the notebook
$response = Invoke-RestMethod -Method POST -Uri "https://$WorkspaceUrl/api/2.0/workspace/import" -Headers $headers -Body $jsonBody
Write-Host "Successful: $filename is imported"
}
catch {
Write-Host "Error while calling the Azure Databricks API for importing notebook: $filename"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
}
}
}
# Import bronze layer notebook for raw file source
if ($SRC_FILESOURCE -and $mkdirDelta) {
Write-Host "Task: Import Bronze Layer notebook for raw file source"
#github api for a folder
$Artifactsuri = "https://api.github.com/repos/DatabricksFactory/databricks-migration/contents/Artifacts/$CTRL_SYNTAX/Batch/FileSource?ref=$REF_BRANCH" # change to respective git branch
# Calling GitHub API for getting the filenames under Artifacts/$CTRL_SYNTAX folder
try {
$wr = Invoke-WebRequest -Uri $Artifactsuri
$objects = $wr.Content | ConvertFrom-Json
$fileNames = $objects | Where-Object { $_.type -eq "file" } | Select-Object -exp name
Write-Host "Successful: getting the filenames under Artifacts/$CTRL_SYNTAX/Batch/FileSource folder is successful"
$getFSFilename = $true
}
catch {
$getFSFilename = $false
Write-Host "Error while calling the GitHub API for getting the filenames under Artifacts/$CTRL_SYNTAX/Batch/FileSource"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
if ($getFSFilename) {
Foreach ($filename in $fileNames) {
try {
# Set the path to the notebook to be imported
$url = "$NOTEBOOK_PATH/$CTRL_SYNTAX/Batch/FileSource/$filename"
# Get the notebook
$Webresults = Invoke-WebRequest $url -UseBasicParsing
# Read the notebook file
$notebookContent = $Webresults.Content
# Base64 encode the notebook content
$notebookBase64 = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($notebookContent))
# Set the path
$splitfilename = $filename.Split(".")
$filenamewithoutextension = $splitfilename[0]
$path = "/Shared/$CTRL_SYNTAX/$filenamewithoutextension";
# Set the request body
$requestBody = @{
"content" = $notebookBase64
"path" = $path
"language" = "PYTHON"
"format" = "JUPYTER"
}
# Convert the request body to JSON
$jsonBody = ConvertTo-Json -Depth 100 $requestBody
}
catch {
Write-Host "Error while reading the notebook: $filename"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
try {
# Make the HTTP request to import the notebook
$response = Invoke-RestMethod -Method POST -Uri "https://$WorkspaceUrl/api/2.0/workspace/import" -Headers $headers -Body $jsonBody
Write-Host "Successful: $filename is imported"
}
catch {
Write-Host "Error while calling the Azure Databricks API for importing notebook: $filename"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
}
}
}
# # Import bronze layer notebook for Azure SQL
if ($SRC_AZSQL -and $mkdirDelta) {
Write-Host "Task: Import Bronze Layer notebook for Azure SQL"
# Get files under directory #github api for a folder
$Artifactsuri = "https://api.github.com/repos/DatabricksFactory/databricks-migration/contents/Artifacts/$CTRL_SYNTAX/Batch/AzureSQLDb?ref=$REF_BRANCH" # change to respective git branch
# Calling GitHub API for getting the filenames under Artifacts/$CTRL_SYNTAX folder
try {
$wr = Invoke-WebRequest -Uri $Artifactsuri
$objects = $wr.Content | ConvertFrom-Json
$fileNames = $objects | Where-Object { $_.type -eq "file" } | Select-Object -exp name
Write-Host "Successful: getting the filenames under Artifacts/$CTRL_SYNTAX/Batch/AzureSQLDb folder is successful"
$getAZSQLFilenames = $true
}
catch {
$getAZSQLFilenames = $false
Write-Host "Error while calling the GitHub API for getting the filenames under Artifacts/$CTRL_SYNTAX/Batch/AzureSQLDb"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
if ($getAZSQLFilenames) {
Foreach ($filename in $fileNames) {
try {
# Set the path to the notebook to be imported
$url = "$NOTEBOOK_PATH/$CTRL_SYNTAX/Batch/AzureSQLDb/$filename"
# Get the notebook
$Webresults = Invoke-WebRequest $url -UseBasicParsing
# Read the notebook file
$notebookContent = $Webresults.Content
# Base64 encode the notebook content
$notebookBase64 = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($notebookContent))
# Set the path
$splitfilename = $filename.Split(".")
$filenamewithoutextension = $splitfilename[0]
$path = "/Shared/$CTRL_SYNTAX/$filenamewithoutextension";
# Set the request body
$requestBody = @{
"content" = $notebookBase64
"path" = $path
"language" = "PYTHON"
"format" = "JUPYTER"
}
# Convert the request body to JSON
$jsonBody = ConvertTo-Json -Depth 100 $requestBody
}
catch {
Write-Host "Error while reading the notebook: $filename"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
try {
# Make the HTTP request to import the notebook
$response = Invoke-RestMethod -Method POST -Uri "https://$WorkspaceUrl/api/2.0/workspace/import" -Headers $headers -Body $jsonBody
Write-Host "Successful: $filename is imported"
}
catch {
Write-Host "Error while calling the Azure Databricks API for importing notebook: $filename"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
}
}
}
# Import bronze layer notebook for Azure MySQL
if ($SRC_AZMYSQL -and $mkdirDelta) {
Write-Host "Task: Import bronze layer notebook for Azure MySQL"
# Get files under directory #github api for a folder
$Artifactsuri = "https://api.github.com/repos/DatabricksFactory/databricks-migration/contents/Artifacts/$CTRL_SYNTAX/Batch/AzureMySQL?ref=$REF_BRANCH" # change to respective git branch
# Calling GitHub API for getting the filenames under Artifacts/$CTRL_SYNTAX folder
try {
$wr = Invoke-WebRequest -Uri $Artifactsuri
$objects = $wr.Content | ConvertFrom-Json
$fileNames = $objects | Where-Object { $_.type -eq "file" } | Select-Object -exp name
Write-Host "Successful: getting the filenames under Artifacts/$CTRL_SYNTAX/Batch/AzureMySQL folder is successful"
$getAZMYSQLFilenames = $true
}
catch {
$getAZMYSQLFilenames = $false
Write-Host "Error while calling the GitHub API for getting the filenames under Artifacts/$CTRL_SYNTAX/Batch/AzureMySQL"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
if ($getAZMYSQLFilenames) {
Foreach ($filename in $fileNames) {
try {
# Set the path to the notebook to be imported
$url = "$NOTEBOOK_PATH/$CTRL_SYNTAX/Batch/AzureMySQL/$filename"
# Get the notebook
$Webresults = Invoke-WebRequest $url -UseBasicParsing
# Read the notebook file
$notebookContent = $Webresults.Content
# Base64 encode the notebook content
$notebookBase64 = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($notebookContent))
# Set the path
$splitfilename = $filename.Split(".")
$filenamewithoutextension = $splitfilename[0]
$path = "/Shared/$CTRL_SYNTAX/$filenamewithoutextension";
# Set the request body
$requestBody = @{
"content" = $notebookBase64
"path" = $path
"language" = "PYTHON"
"format" = "JUPYTER"
}
# Convert the request body to JSON
$jsonBody = ConvertTo-Json -Depth 100 $requestBody
}
catch {
Write-Host "Error while reading the notebook: $filename"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
try {
# Make the HTTP request to import the notebook
$response = Invoke-RestMethod -Method POST -Uri "https://$WorkspaceUrl/api/2.0/workspace/import" -Headers $headers -Body $jsonBody
Write-Host "Successful: $filename is imported"
}
catch {
Write-Host "Error while calling the Azure Databricks API for importing notebook: $filename"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
}
}
}
# Import bronze layer notebook for Azure PSQL
if ($SRC_AZPSQL -and $mkdirDelta) {
Write-Host "Task: Import Bronze Layer notebooks for Azure PSQL"
# Get files under directory #github api for a folder
$Artifactsuri = "https://api.github.com/repos/DatabricksFactory/databricks-migration/contents/Artifacts/$CTRL_SYNTAX/Batch/AzurePostgreSQL?ref=$REF_BRANCH" # change to respective git branch
# Calling GitHub API for getting the filenames under Artifacts/$CTRL_SYNTAX folder
try {
$wr = Invoke-WebRequest -Uri $Artifactsuri
$objects = $wr.Content | ConvertFrom-Json
$fileNames = $objects | Where-Object { $_.type -eq "file" } | Select-Object -exp name
Write-Host "Successful: getting the filenames under Artifacts/$CTRL_SYNTAX/Batch/AzurePostgreSQL folder is successful"
$getAZPSQLFilename = $true
}
catch {
$getAZPSQLFilename = $false
Write-Host "Error while calling the GitHub API for getting the filenames under Artifacts/$CTRL_SYNTAX/Batch/AzurePostgreSQL"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
if ($getAZPSQLFilename) {
Foreach ($filename in $fileNames) {
try {
# Set the path to the notebook to be imported
$url = "$NOTEBOOK_PATH/$CTRL_SYNTAX/Batch/AzurePostgreSQL/$filename"
# Get the notebook
$Webresults = Invoke-WebRequest $url -UseBasicParsing
# Read the notebook file
$notebookContent = $Webresults.Content
# Base64 encode the notebook content
$notebookBase64 = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($notebookContent))
# Set the path
$splitfilename = $filename.Split(".")
$filenamewithoutextension = $splitfilename[0]
$path = "/Shared/$CTRL_SYNTAX/$filenamewithoutextension";
# Set the request body
$requestBody = @{
"content" = $notebookBase64
"path" = $path
"language" = "PYTHON"
"format" = "JUPYTER"
}
# Convert the request body to JSON
$jsonBody = ConvertTo-Json -Depth 100 $requestBody
}
catch {
Write-Host "Error while reading the notebook: $filename"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
try {
# Make the HTTP request to import the notebook
$response = Invoke-RestMethod -Method POST -Uri "https://$WorkspaceUrl/api/2.0/workspace/import" -Headers $headers -Body $jsonBody
Write-Host "Successful: $filename is imported"
}
catch {
Write-Host "Error while calling the Azure Databricks API for importing notebook: $filename"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
}
}
}
# Import bronze layer notebook for SQL on-prem
if ($SRC_SQL_ONPREM -and $mkdirDelta) {
Write-Host "Task: Import Bronze Layer notebooks for SQL on-prem"
# Get files under directory #github api for a folder
$Artifactsuri = "https://api.github.com/repos/DatabricksFactory/databricks-migration/contents/Artifacts/$CTRL_SYNTAX/Batch/SQLDbOnPrem?ref=$REF_BRANCH" # change to respective git branch
# Calling GitHub API for getting the filenames under Artifacts/$CTRL_SYNTAX folder
try {
$wr = Invoke-WebRequest -Uri $Artifactsuri
$objects = $wr.Content | ConvertFrom-Json
$fileNames = $objects | Where-Object { $_.type -eq "file" } | Select-Object -exp name
Write-Host "Successful: getting the filenames under Artifacts/$CTRL_SYNTAX/Batch/SQLDbOnPrem folder is successful"
$getSQLOPFilenames = $true
}
catch {
$getSQLOPFilenames = $false
Write-Host "Error while calling the GitHub API for getting the filenames under Artifacts/$CTRL_SYNTAX/Batch/SQLDbOnPrem"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
if ($getSQLOPFilenames) {
Foreach ($filename in $fileNames) {
try {
# Set the path to the notebook to be imported
$url = "$NOTEBOOK_PATH/$CTRL_SYNTAX/Batch/SQLDbOnPrem/$filename"
# Get the notebook
$Webresults = Invoke-WebRequest $url -UseBasicParsing
# Read the notebook file
$notebookContent = $Webresults.Content
# Base64 encode the notebook content
$notebookBase64 = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($notebookContent))
# Set the path
$splitfilename = $filename.Split(".")
$filenamewithoutextension = $splitfilename[0]
$path = "/Shared/$CTRL_SYNTAX/$filenamewithoutextension";
# Set the request body
$requestBody = @{
"content" = $notebookBase64
"path" = $path
"language" = "PYTHON"
"format" = "JUPYTER"
}
# Convert the request body to JSON
$jsonBody = ConvertTo-Json -Depth 100 $requestBody
}
catch {
Write-Host "Error while reading the notebook: $filename"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
try {
# Make the HTTP request to import the notebook
$response = Invoke-RestMethod -Method POST -Uri "https://$WorkspaceUrl/api/2.0/workspace/import" -Headers $headers -Body $jsonBody
Write-Host "Successful: $filename is imported"
}
catch {
Write-Host "Error while calling the Azure Databricks API for importing notebook: $filename"
$errorMessage = $_.Exception.Message
Write-Host "Error message: $errorMessage"
}
}
}
}
# Import bronze layer notebook for PSQL on-prem
if ($SRC_PSQL_ONPREM -and $mkdirDelta) {
Write-Host "Task: Import Bronze Layer notebooks for PSQL on-prem"
# Get files under directory #github api for a folder
$Artifactsuri = "https://api.github.com/repos/DatabricksFactory/databricks-migration/contents/Artifacts/$CTRL_SYNTAX/Batch/PostgreSQL?ref=$REF_BRANCH" # change to respective git branch
# Calling GitHub API for getting the filenames under Artifacts/$CTRL_SYNTAX folder
try {
$wr = Invoke-WebRequest -Uri $Artifactsuri
$objects = $wr.Content | ConvertFrom-Json
$fileNames = $objects | Where-Object { $_.type -eq "file" } | Select-Object -exp name
Write-Host "Successful: getting the filenames under Artifacts/$CTRL_SYNTAX/Batch/PostgreSQL folder is successful"
$getPSQLOPFilename = $true
}
catch {
$getPSQLOPFilename = $false
Write-Host "Error while calling the GitHub API for getting the filenames under Artifacts/$CTRL_SYNTAX/Batch/PostgreSQL"