-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.ps1
190 lines (159 loc) · 7.86 KB
/
import.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
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
param(
[string] [Parameter(Mandatory=$true)] $tenant,
[string] [Parameter(Mandatory=$true)] $shortcode,
[string] [Parameter(Mandatory=$true)] $tenantname,
[string] $maxNumberOfUsers = "10",
[string] $subGroupCode = "DefaultSubGroup",
[string] [Parameter(Mandatory=$true)] $tenantAdmin,
[string] $tenantAdminPwd,
[string] [Parameter(Mandatory=$true)] $oem,
[string] [Parameter(Mandatory=$true)] $master,
[string] [Parameter(Mandatory=$true)] $masterpwd,
[string] [Parameter(Mandatory=$true)] $WebsiteName, #http or https://*.azurewebsites.net
[string] $SubscriptionName, #as in -subscriptionname param elsewhere
[string] [Parameter(Mandatory=$true)] $ResourceGroupName, #as in -resourcegroupname param elsewhere
[string] $SubscriptionID, #as in -subscriptionID param elsewhere,
[switch] $overwriteIfExists = $false,
[string] [ValidateSet('Template','Demonstration','Full')]$tenantType = "Demonstration",
[switch] $ignoreCommands = $false,
[string] $customerName,
[string] $customerID
)
$ErrorActionPreference = "Stop"
$thisScriptVersion = 2.3
Write-Host "Omnia Platform import tool - version $thisScriptVersion"
if ($SubscriptionName){
Set-AzContext -SubscriptionName $SubscriptionName
}
elseif ($SubscriptionID){
Set-AzContext -SubscriptionId $SubscriptionID
}
else{
throw "Process does not work without an Azure Subscription!"
}
$workingFolder = $PSScriptRoot
$siteName = (($WebsiteName -split "://")[1] -split ".azurewebsites.net")[0]
$webApp = Get-AzWebAppSlot -ResourceGroupName $ResourceGroupName -Name $siteName -Slot "Production"
foreach ($setting in $webApp.SiteConfig.ConnectionStrings){
if ($setting.Name -eq "MyMis.Storage.ConnectionString"){
$storageAccountName = (($setting.ConnectionString -split ";AccountName=")[1] -split ";AccountKey=")[0]
$storageAccessKey = ($setting.ConnectionString -split ";AccountKey=")[1]
}
elseif ($setting.Name -eq "MyMis.SQL.Core.ConnectionString"){
$builder = New-Object System.Data.SqlClient.SqlConnectionStringBuilder -argumentlist ($setting.ConnectionString);
$server = $builder.DataSource
$database = $builder.InitialCatalog
$user = $builder.UserID + "@" + (($builder.DataSource -split (".database.windows.net")).Get(0))
$passwd = $builder.Password
}
}
foreach ($setting in $webApp.SiteConfig.AppSettings){
if ($setting.Name -eq "MyMis.OAuth2.ClientID"){
$apiID = $setting.Value
}
if ($setting.Name -eq "MyMis.API.Account.Endpoint"){
$apiEndpoint = $setting.Value
}
}
#Preflight checks
if (-not $storageAccountName -or -not $storageAccessKey){
throw "Invalid Storage configuration!"
}
if (-not $server -or -not $database -or -not $user -or -not $passwd){
throw "Invalid SQL Database configuration!"
}
if (-not $apiID -or -not $apiEndpoint){
throw "Invalid API configuration!"
}
if (-not $oem -or -not $shortcode -or -not $tenantname -or -not $tenantAdmin -or -not $oem -or -not $master -or -not $masterpwd){
throw "Invalid tenant creation configuration!"
}
if ($tenantType -eq "Full" -and ($customerName -eq "" -or $customerName -eq $null) -and ($customerID -eq "" -or $customerID -eq $null)){
throw "Full type tenants require setting a customer Name and ID!"
}
#Begin Work
WRITE-HOST "$(Get-Date -format 'u') - Starting..."
if ($overwriteIfExists){
Write-Host "Checking for existing tenant $shortCode, and deleting it if it exists."
cd $workingFolder\ImportScripts
try{
& .\script-tenant-delete.ps1 -tenant $tenant -apiID $apiID -apiEndpoint $apiEndpoint -master $master -masterpwd $masterpwd -IfExists $true -tenantShortCode $shortCode
}
catch{
if ($_.Exception.Message -ne "Failed getting tenant. Not attempting deletion."){
throw ($_.Exception.Message)
}
else{
Write-Host "Tenant does not exist. Creating it."
}
}
cd $workingFolder
}
if ($tenantType -eq "Demonstration"){
$tenantTypeCode = 0
$isTemplate = $false
}
elseif ($tenantType -eq "Full"){
$tenantTypeCode = 1
$isTemplate = $false
}
elseif ($tenantType -eq "Template"){
$tenantTypeCode = 2
$isTemplate = $true
}
Write-Progress -id 1 -activity "Importing Data" -Status "Creating Tenant"
cd $workingFolder\ImportScripts
& .\script-tenant-create.ps1 -code $tenant -shortcode $shortcode -name $tenantname -maxNumberOfUsers $maxNumberOfUsers -subGroupCode $subGroupCode -tenantAdmin $tenantAdmin -tenantAdminPwd $tenantAdminPwd -oem $oem -apiID $apiID -apiEndpoint $apiEndpoint -master $master -masterpwd $masterpwd -tenantType $tenantTypeCode -customerName $customerName -customerID $customerID
cd $workingFolder
WRITE-HOST "$(Get-Date -format 'u') - Tenant created..."
try{
Write-Progress -id 1 -activity "Importing Data" -Status "Importing Database" -PercentComplete 12.5
cd $workingFolder\ImportScripts
& .\script-import.ps1 -server $server -database $database -user $user -passwd $passwd -tenant $tenant
cd $workingFolder
WRITE-HOST "$(Get-Date -format 'u') - Data imported..."
Write-Progress -id 1 -activity "Importing Data" -Status "Importing Tables" -PercentComplete 25
if (-not $isTemplate){
cd $workingFolder\ImportScripts
& .\script-table-import.ps1 -storageAccountName $storageAccountName -storageAccessKey $storageAccessKey -tenant $tenant
cd $workingFolder
}
WRITE-HOST "$(Get-Date -format 'u') - Tables imported..."
Write-Progress -id 1 -activity "Importing Data" -Status "Importing Blobs" -PercentComplete 37.5
cd $workingFolder\ImportScripts
& .\script-blob-import.ps1 -resourceGroupName $resourceGroupName -storageAccountName $storageAccountName -tenant $tenant -storageAccessKey $storageAccessKey -isTemplate $isTemplate -ignoreCommands $ignoreCommands.IsPresent
cd $workingFolder
WRITE-HOST "$(Get-Date -format 'u') - Blobs imported..."
Write-Progress -id 1 -activity "Importing Data" -Status "Importing Tenant Image" -PercentComplete 50
cd $workingFolder\ImportScripts
& .\script-tenant-image-import.ps1 -resourceGroupName $resourceGroupName -storageAccountName $storageAccountName -storageAccessKey $storageAccessKey
cd $workingFolder
WRITE-HOST "$(Get-Date -format 'u') - Tenant Image imported..."
Write-Progress -id 1 -activity "Importing Data" -Status "Importing User Images" -PercentComplete 62.5
cd $workingFolder\ImportScripts
& .\script-users-image-import.ps1 -resourceGroupName $resourceGroupName -storageAccountName $storageAccountName -storageAccessKey $storageAccessKey
cd $workingFolder
WRITE-HOST "$(Get-Date -format 'u') - Users Image imported..."
Write-Progress -id 1 -activity "Importing Data" -Status "Importing Users" -PercentComplete 75
cd $workingFolder\ImportScripts
& .\script-users-import.ps1 -server $server -database $database -user $user -passwd $passwd -tenant $tenant -tenantAdmin $tenantAdmin
cd $workingFolder
WRITE-HOST "$(Get-Date -format 'u') - Users imported..."
Write-Progress -id 1 -activity "Importing Data" -Status "Rebuilding DB Indexes for tenant" -PercentComplete 87.5
cd $workingFolder\ImportScripts
& .\script-rebuild-indexs.ps1 -server $server -database $database -user $user -passwd $passwd -tenant $tenant
cd $workingFolder
Write-Progress -id 1 -activity "Importing Data" -Status "Completed" -Completed
WRITE-HOST "$(Get-Date -format 'u') - DB Indexs rebuilt..."
Write-Host "Data imported sucessfully. Please recreate the connectors.
If the tenant was in a different database version that in the destination, it will be necessary to trigger a database migration."
}
catch{
Write-Host ("Process failed! " + $_.Exception)
$confirmation = Read-Host ("Do you want to delete the created tenant? Y to confirm")
if ($confirmation -eq 'y' -or $confirmation -eq 'yes') {
cd $workingFolder\ImportScripts
& .\script-tenant-delete.ps1 -tenant $tenant -apiID $apiID -apiEndpoint $apiEndpoint -master $master -masterpwd $masterpwd -tenantShortCode $shortCode
}
cd $workingFolder
}