-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.ps1
138 lines (127 loc) · 5.25 KB
/
setup.ps1
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
Clear-Host
write-host "Starting script at $(Get-Date)"
# Generate unique random suffix
[string]$suffix = -join ((48..57) + (97..122) | Get-Random -Count 7 | % {[char]$_})
$resourceGroupName = "msl-$suffix"
# Handle cases where the user has multiple subscriptions
$subs = Get-AzSubscription | Select-Object
if($subs.GetType().IsArray -and $subs.length -gt 1){
Write-Host "You have multiple Azure subscriptions - please select the one you want to use:"
for($i = 0; $i -lt $subs.length; $i++)
{
Write-Host "[$($i)]: $($subs[$i].Name) (ID = $($subs[$i].Id))"
}
$selectedIndex = -1
$selectedValidIndex = 0
while ($selectedValidIndex -ne 1)
{
$enteredValue = Read-Host("Enter 0 to $($subs.Length - 1)")
if (-not ([string]::IsNullOrEmpty($enteredValue)))
{
if ([int]$enteredValue -in (0..$($subs.Length - 1)))
{
$selectedIndex = [int]$enteredValue
$selectedValidIndex = 1
}
else
{
Write-Output "Please enter a valid subscription number."
}
}
else
{
Write-Output "Please enter a valid subscription number."
}
}
$selectedSub = $subs[$selectedIndex].Id
Select-AzSubscription -SubscriptionId $selectedSub
az account set --subscription $selectedSub
}
# Register resource providers
Write-Host "Registering resource providers...";
$provider_list = "Microsoft.Storage", "Microsoft.Compute", "Microsoft.Databricks"
foreach ($provider in $provider_list){
$result = Register-AzResourceProvider -ProviderNamespace $provider
$status = $result.RegistrationState
Write-Host "$provider : $status"
}
# Choose a region
Write-Host "Preparing to deploy. This may take several minutes...";
$delay = 0, 30, 60, 90, 120 | Get-Random
Start-Sleep -Seconds $delay # random delay to stagger requests from multi-student classes
# Get a list of locations for Azure Databricks
$supported_regions = "centralus","eastus","eastus2","northcentralus","northeurope","westeurope","westus"
$locations = Get-AzLocation | Where-Object {
$_.Providers -contains "Microsoft.Databricks" -and
$_.Providers -contains "Microsoft.Compute" -and
$_.Location -in $supported_regions
}
$max_index = $locations.Count - 1
$rand = (0..$max_index) | Get-Random
# Start with preferred region if specified, otherwise choose one at random
if ($args.count -gt 0 -And $args[0] -in $locations.Location)
{
$Region = $args[0]
}
else {
$Region = $locations.Get($rand).Location
}
# Try to create an Azure Databricks workspace in a region that has capacity
$stop = 0
$tried_regions = New-Object Collections.Generic.List[string]
while ($stop -ne 1){
write-host "Trying $Region..."
# Check that the required SKU is available
$skuOK = 1
$skus = Get-AzComputeResourceSku $Region | Where-Object {$_.ResourceType -eq "VirtualMachines" -and $_.Name -eq "Standard_D4ds_v5"}
if ($skus.length -gt 0)
{
$r = $skus.Restrictions
if ($r -ne $null)
{
$skuOK = 0
Write-Host $r[0].ReasonCode
}
}
# Get the available quota
$available_quota = 0
if ($skuOK -eq 1)
{
$quota = @(Get-AzVMUsage -Location $Region).where{$_.name.LocalizedValue -match 'Standard DDSv5 Family vCPUs'}
$cores = $quota.currentvalue
$maxcores = $quota.limit
write-host "$cores of $maxcores cores in use."
$available_quota = $quota.limit - $quota.currentvalue
}
if (($available_quota -lt 8) -or ($skuOK -eq 0))
{
Write-Host "$Region has insufficient capacity."
$tried_regions.Add($Region)
$locations = $locations | Where-Object {$_.Location -notin $tried_regions}
if ($locations.Count -ne 1){
$rand = (0..$($locations.Count - 1)) | Get-Random
$Region = $locations.Get($rand).Location
$stop = 0
}
else {
Write-Host "Could not create a Databricks workspace."
Write-Host "Try using the Azure portal to add one to the $resourceGroupName resource group."
$stop = 1
}
}
else {
Write-Host "Creating $resourceGroupName resource group ..."
New-AzResourceGroup -Name $resourceGroupName -Location $Region | Out-Null
$dbworkspace = "databricks-$suffix"
Write-Host "Creating $dbworkspace Azure Databricks workspace in $resourceGroupName resource group..."
New-AzDatabricksWorkspace -Name $dbworkspace -ResourceGroupName $resourceGroupName -Location $Region -Sku premium | Out-Null
# Make the current user an owner of the databricks workspace
write-host "Granting permissions on the $dbworkspace resource..."
write-host "(you can ignore any warnings!)"
$subscriptionId = (Get-AzContext).Subscription.Id
$userName = ((az ad signed-in-user show) | ConvertFrom-JSON).UserPrincipalName
New-AzRoleAssignment -SignInName $userName -RoleDefinitionName "Owner" -Scope "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Databricks/workspaces/$dbworkspace";
$stop = 1
}
}
write-host "Script completed at $(Get-Date)"