-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovision_sqlserver_i.ps1
More file actions
408 lines (355 loc) · 13.1 KB
/
Copy pathprovision_sqlserver_i.ps1
File metadata and controls
408 lines (355 loc) · 13.1 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
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Copyright (c) 2017 by Delphix. All rights reserved.
#
# Program Name : provision_sqlserver_i.ps1
# Description : Delphix PowerShell API provision VDB Example
# Author : Alan Bitterman
# Created : 2017-12-15
# Version : v1.2
#
# Requirements :
# 1.) ConvertFrom-Json Commandlet
# 2.) Populate Delphix Engine Connection Information . .\delphix_engine_conf.ps1
# 3.) Include Delphix Functions . .\delphixFunctions.ps1
# 4.) Change values below as required
#
# Interactive Usage:
# . .\provision_sqlserver_i.ps1
#
# Non-Interactive Usage:
# . .\provision_sqlserver_i.ps1 [dSource] [VDB_Name] [Group] [Environment] [Instance]
# . .\provision_sqlserver_i.ps1 delphixdb Vdelphixdb "Windows_Target" "Windows Host" "MSSQLSERVER"
# . .\provision_sqlserver_i.ps1 delphix_demo vdelphix_demo "SQL Server" "Windows" MSSQLSERVER
#
#########################################################
# DELPHIX CORP #
# Please make changes to the parameters below as req'd! #
#########################################################
#########################################################
## Get Command Line Arguements ...
## (must be first thing in file)
param (
[string]$SOURCE_SID = "",
[string]$VDB_NAME = "",
[string]$TARGET_GRP = "",
[string]$TARGET_ENV = "",
[string]$TARGET_REP = ""
)
#########################################################
## Parameter Initialization ...
. .\delphix_engine_conf.ps1
## Default Values if not provided on Command Line ...
#
# For non-interactive defaults ...
#
# $DEF_SOURCE_SID="delphixdb" # dSource name used to get db container reference value
# $DEF_VDB_NAME="Vdelphixdb" # Delphix VDB Name
# $DEF_TARGET_GRP="Windows_Target" # Delphix Engine Group Name
# $DEF_TARGET_ENV="Windows Host" # Target Environment used to get repository reference value
# $DEF_TARGET_REP="MSSQLSERVER" # Target Environment Repository / Instance name
#
# For full interactive option, set default values to nothing ...
#
$DEF_SOURCE_SID=""
$DEF_VDB_NAME=""
$DEF_TARGET_GRP=""
$DEF_TARGET_ENV=""
$DEF_TARGET_REP=""
#echo "Source: $SOURCE_SID"
#echo "VDB: $VDB_NAME"
#echo "Target Group: $TARGET_GRP"
#echo "Target Env: $TARGET_ENV"
#echo "Target Repo: $TARGET_REP"
#########################################################
# NO CHANGES REQUIRED BELOW THIS POINT #
#########################################################
#########################################################
## Local Functions ...
. .\delphixFunctions.ps1
#########################################################
## Authentication ...
Write-Output "Authenticating on ${BaseURL} ... ${nl}"
$session=RestSession "${DMUSER}" "${DMPASS}" "${BaseURL}" "${COOKIE}" "${CONTENT_TYPE}"
#Write-Output "${nl} Results are ${results} ..."
Write-Output "Login Successful ..."
#########################################################
## API Version ...
$apival=Get_APIVAL "${BaseURL}" $session "${CONTENT_TYPE}"
Write-Output "API Version: ${apival} "
#########################################################
## Get database container ...
#Write-Output "Database API ..."
$results = Invoke-RestMethod -Method Get -ContentType "application/json" -WebSession $session -URI "${BaseURL}/database"
$status = ParseStatus $results "${ignore}"
#Write-Output "Database API Results: ${results}"
#
# Convert Results String to JSON Object and Get Results ...
#
#$o = ConvertFrom-Json $results
$a = $results.result
#$b = $a | where { $_.type -eq "MSSqlDatabaseContainer" } | Select-Object
$b = $a | where { $_.os -eq "Windows" } | Select-Object
#
# Prompt for dSource or VDB Name ...
#
if ("${SOURCE_SID}" -eq "") {
$ZTMP="dSource or VDB Source"
if ( "${DEF_SOURCE_SID}" -eq "" ) {
Write-Output "--------------------------------------"
Write-Output "${ZTMP}: [copy-n-paste]"
$b.name
$SOURCE_SID = Read-Host -Prompt "Please Enter ${ZTMP} (case sensitive)"
if ("${SOURCE_SID}" -eq "" ) {
Write-Output "No ${ZTMP} Provided, Exiting ..."
exit 1
}
} else {
echo "No ${ZTMP} Provided, using Default ..."
$SOURCE_SID=${DEF_SOURCE_SID}
}
}
#
# Parse Results (cont) ...
#
$b = $a | where { $_.name -eq "${SOURCE_SID}" -and $_.type -eq "MSSqlDatabaseContainer" } | Select-Object
$CONTAINER_REFERENCE=$b.reference
Write-Output "container reference: ${CONTAINER_REFERENCE}"
if ("${CONTAINER_REFERENCE}" -eq "" ) {
Write-Output "Error: No container found for ${SOURCE_ID} ${CONTAINER_REFERENCE}, Exiting ..."
exit 1
}
#########################################################
## VDB Name from Command Line Parameters ...
$ZTMP="New VDB Name"
if ( "${VDB_NAME}" -eq "" ) {
if ( "${DEF_VDB_NAME}" -eq "" ) {
Write-Output "---------------------------------"
$VDB_NAME = Read-Host -Prompt "Please Enter ${ZTMP} (case sensitive)"
if ( "${VDB_NAME}" -eq "" ) {
Write-Output "No ${ZTMP} Provided, Exiting ..."
exit 1;
}
} else {
Write-Output "No ${ZTMP} Provided, using Default ..."
$VDB_NAME=${DEF_VDB_NAME}
}
}
echo "${ZTMP}: ${VDB_NAME}"
#########################################################
## Get Group Reference ...
#Write-Output "Group API ..."
$results = Invoke-RestMethod -Method Get -ContentType "application/json" -WebSession $session -URI "${BaseURL}/group"
$status = ParseStatus $results "${ignore}"
#Write-Output "Group API Results: ${results}"
#
# Convert Results String to JSON Object and Get Results ...
#
#$o = ConvertFrom-Json $results
$a = $results.result
#
# Prompt for Group Name ...
#
$ZTMP="Delphix Target Group/Folder"
if ("${TARGET_GRP}" -eq "") {
if ( "${DEF_TARGET_GRP}" -eq "" ) {
Write-Output "--------------------------------------"
Write-Output "${ZTMP}: [copy-n-paste]"
$a.name
$TARGET_GRP = Read-Host -Prompt "Please Enter ${ZTMP} (case sensitive)"
if ("${TARGET_GRP}" -eq "" ) {
Write-Output "No ${ZTMP} Provided, Exiting ..."
exit 1
}
} else {
echo "No ${ZTMP} Provided, using Default ..."
$TARGET_GRP=${DEF_TARGET_GRP}
}
}
#
# Parse Results (cont) ...
#-and $_.type -eq "MSSqlDatabaseContainer"
#
$b = $a | where { $_.name -eq "${TARGET_GRP}"} | Select-Object
$GROUP_REFERENCE=$b.reference
Write-Output "group reference: ${GROUP_REFERENCE}"
if ("${GROUP_REFERENCE}" -eq "" ) {
Write-Output "Error: No ${ZTMP} found for ${TARGET_GRP} ${GROUP_REFERENCE}, Exiting ..."
exit 1
}
#########################################################
## Get Environment reference ...
#Write-Output "Environment API ..."
$results = Invoke-RestMethod -Method Get -ContentType "application/json" -WebSession $session -URI "${BaseURL}/environment"
$status = ParseStatus $results "${ignore}"
#Write-Output "environment API Results: ${results}"
#
# Convert Results String to JSON Object and Get Results ...
#
#$o = ConvertFrom-Json $results
$a = $results.result
$b = $a | where { $_.type -eq "WindowsHostEnvironment" } | Select-Object
#
# Prompt for Group Name ...
#
$ZTMP="Target Environment"
if ("${TARGET_ENV}" -eq "") {
if ( "${DEF_TARGET_ENV}" -eq "" ) {
Write-Output "--------------------------------------"
Write-Output "${ZTMP}: [copy-n-paste]"
$b.name
$TARGET_ENV = Read-Host -Prompt "Please Enter ${ZTMP} (case sensitive)"
if ("${TARGET_ENV}" -eq "" ) {
Write-Output "No ${ZTMP} Provided, Exiting ..."
exit 1
}
} else {
echo "No ${ZTMP} Provided, using Default ..."
$TARGET_ENV=${DEF_TARGET_ENV}
}
}
#
# Parse Results (cont) ...
#
$b = $a | where { $_.name -eq "${TARGET_ENV}"} | Select-Object
$ENV_REFERENCE=$b.reference
Write-Output "environment reference: ${ENV_REFERENCE}"
if ("${ENV_REFERENCE}" -eq "" ) {
Write-Output "Error: No ${ZTMP} found for ${TARGET_ENV} ${ENV_REFERENCE}, Exiting ..."
exit 1
}
#########################################################
## Get Repository reference ...
#Write-Output "${nl}Calling repository API ...${nl}"
$results = Invoke-RestMethod -Method Get -ContentType "application/json" -WebSession $session -URI "${BaseURL}/repository"
$status = ParseStatus $results "${ignore}"
#Write-Output "repository API Results: ${results}"
#
# Convert Results String to JSON Object and Get Results ...
#
#$o = ConvertFrom-Json $results
$a = $results.result
$b = $a | where { $_.environment -eq "${ENV_REFERENCE}" } | Select-Object
#
# Prompt for Group Name ...
#
$ZTMP="Target SQL Server Instance"
if ("${TARGET_REP}" -eq "") {
if ( "${DEF_TARGET_REP}" -eq "" ) {
Write-Output "--------------------------------------"
Write-Output "${ZTMP}: [copy-n-paste]"
$b.name
$TARGET_REP = Read-Host -Prompt "Please Enter ${ZTMP} (case sensitive)"
if ("${TARGET_REP}" -eq "" ) {
Write-Output "No ${ZTMP} Provided, Exiting ..."
exit 1
}
} else {
echo "No ${ZTMP} Provided, using Default ..."
$TARGET_REP=${DEF_TARGET_REP}
}
}
#
# Parse Results (cont) ...
#
$b = $a | where { $_.name -eq "${TARGET_REP}" -and $_.environment -eq "${ENV_REFERENCE}"} | Select-Object
##$b = $a | where { $_.name -eq "${TARGET_REP}" } | Select-Object
$REP_REFERENCE=$b.reference
Write-Output "repository/instance reference: ${REP_REFERENCE}"
if ("${REP_REFERENCE}" -eq "" ) {
Write-Output "Error: No ${ZTMP} found for ${TARGET_REP} ${REP_REFERENCE}, Exiting ..."
exit 1
}
#########################################################
## Provision a SQL Server Database ...
# Constants ...
#$true="true"
#$false="false"
$json = @"
{
\"type\": \"MSSqlProvisionParameters\",
\"container\": {
\"type\": \"MSSqlDatabaseContainer\",
\"name\": \"${VDB_NAME}\",
\"group\": \"${GROUP_REFERENCE}\",
\"sourcingPolicy\": {
\"type\": \"SourcingPolicy\",
\"loadFromBackup\": false,
\"logsyncEnabled\": false
},
\"validatedSyncMode\": \"TRANSACTION_LOG\"
},
\"source\": {
\"type\": \"MSSqlVirtualSource\",
\"allowAutoVDBRestartOnHostReboot\": false,
\"operations\": {
\"type\": \"VirtualSourceOperations\",
\"configureClone\": [ {
\"type\": \"RunPowerShellOnSourceOperation\",
\"name\": \"hookme4\",
\"command\": \"Set-Alias pslocal64 C:/Windows/SysWOW64/WindowsPowerShell/v1.0/powershell.exe \npslocal64 . .\\hook.ps1\"} ],
\"postRefresh\": [ {
\"type\": \"RunPowerShellOnSourceOperation\",
\"name\": \"hookme4\",
\"command\": \"Set-Alias pslocal64 C:/Windows/SysWOW64/WindowsPowerShell/v1.0/powershell.exe \npslocal64 . .\\hook.ps1\"} ],
\"postRollback\": [],
\"postSnapshot\": [],
\"preRefresh\": [],
\"preSnapshot\": []
}
},
\"sourceConfig\": {
\"type\": \"MSSqlSIConfig\",
\"linkingEnabled\": false,
\"repository\": \"${REP_REFERENCE}\",
\"databaseName\": \"${VDB_NAME}\",
\"recoveryModel\": \"SIMPLE\",
\"instance\": {
\"type\": \"MSSqlInstanceConfig\",
\"host\": \"${ENV_REFERENCE}\"
}
},
\"timeflowPointParameters\": {
\"type\": \"TimeflowPointSemantic\",
\"container\": \"${CONTAINER_REFERENCE}\",
\"location\": \"LATEST_SNAPSHOT\"
}
}
"@
$json = $json -replace '\\"', '"'
Write-Output "JSON: $json"
#Write-Output "${nl}Calling database provision API ...${nl}"
$results = Invoke-RestMethod -URI "${BaseURL}/database/provision" -WebSession $session -Method Post -Body $json -ContentType 'application/json'
$results.error
$results.error.details.source.operations
$status = ParseStatus $results "${ignore}"
Write-Output "database provision API Results: ${results}"
#
# Convert Results String to JSON Object and Get Results Status ...
#
##$o = ConvertFrom-Json $results
$JOB=$results.job
Write-Output "Job # ${JOB}"
#
# Allow job to submit internally before while loop ...
#
sleep 1
Monitor_JOB "$BaseURL" $session "$CONTENT_TYPE" "$JOB"
############## E O F ####################################
## Clean up and Done ...
##Remove-Variable -Name * -ErrorAction SilentlyContinue
Write-Output " "
Write-Output "Done ..."
Write-Output " "
exit 0