-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathDatabase.ps1
More file actions
154 lines (127 loc) · 6.21 KB
/
Copy pathDatabase.ps1
File metadata and controls
154 lines (127 loc) · 6.21 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
<#
This example demonstrates working with various resources in Database Service.
This example requires:
1) Modules OCI.PSModules.Database,OCI.PSModules.Identity,OCI.PSModules.Core. Install the modules from Powershell Gallery.
2) Setting up the environment variable CompartmentId to a Compartment OCID.
#>
$UserErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = "Stop"
if ([string]::IsNullOrEmpty($env:CompartmentId)) {
Throw 'Configure $env:CompartmentId in the PS Session'
}
try {
#Import the modules
Import-Module OCI.PSModules.Database
Import-Module OCI.PSModules.Identity
Import-Module OCI.PSModules.Core
#Read CompartmentId environment variable
$CompartmentId = $env:CompartmentId
#Setup
$Chars = [Char[]]'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
$RandomSufix = Get-Random -Minimum 1 -Maximum 1000
$DisplayName = "PSExample" + $RandomSufix
$CidrBlock = '10.0.0.0/16'
$HostName = $DisplayName + 'Host'
$DbName = 'Db' + $RandomSufix
$DbDomain = $DisplayName + 'Domain'
$DbHomeName = $DisplayName + 'DbHome'
$DbShape = 'BM.DenseIO2.52'
$AdminPassword = (Get-Random -Count 15 -InputObject $Chars) -join ''
#Get Availability domain list
Write-Host "Get-OCIIdentityAvailabilityDomainsList -CompartmentId $CompartmentId"
Get-OCIIdentityAvailabilityDomainsList -CompartmentId $CompartmentId | Out-Host
Write-Host "Picking AD :"
$AD = $OCICmdletHistory.LastResponse.Items | Select-Object -first 1
$AD
#Create Vcn Details
$CreateVcnDetails = New-Object "Oci.CoreService.Models.CreateVcnDetails"
$CreateVcnDetails.CidrBlock = $CidrBlock
$CreateVcnDetails.CompartmentId = $CompartmentId
$CreateVcnDetails.DisplayName = $DisplayName
#Create a Virtual Network VCN
Write-Host "New-OCIVirtualNetworkVcn -CreateVcnDetails $CreateVcnDetails"
$Vcn = New-OCIVirtualNetworkVcn -CreateVcnDetails $CreateVcnDetails
#Inspect the created VCN
$Vcn
#Wait for the VCN to become 'Available'
Write-Host "Get-OCIVirtualNetworkVcn -VcnId $Vcn.Id -WaitForLifecycleState 'Available' -WaitIntervalSeconds 30"
Get-OCIVirtualNetworkVcn -VcnId $Vcn.Id -WaitForLifecycleState 'Available' -WaitIntervalSeconds 30 | Out-Host
#Set Subnet properties
$SubnetProperties = @{
displayName = $DisplayName + "Subnet"
cidrBlock = $CidrBlock
vcnId = $Vcn.Id
compartmentId = $CompartmentId
}
#Create a subnet details object
$SubnetDetails = New-Object -TypeName 'Oci.CoreService.Models.CreateSubnetDetails' -Property $SubnetProperties
#Create a Subnet
Write-Host "New-OCIVirtualNetworkSubnet -CreateSubnetDetails $SubnetDetails"
$Subnet = New-OCIVirtualNetworkSubnet -CreateSubnetDetails $SubnetDetails
#Examine the created subnet
$Subnet
#Create SSH-Keys
$Basepath = Get-Location
$Filepath = Join-Path $Basepath 'examplekey'
$Pubkeypath = $Filepath + ".pub"
ssh-keygen -f $Filepath -N "encrypt" #This example uses bare minimum detils to generate keys. In production specify more secure key gen options.
$Pubkey = [IO.File]::ReadAllText($Pubkeypath);
$Keylist = New-Object 'Collections.Generic.List[string]'
$Keylist.Add($Pubkey)
$Pubkey
#Create DB Details
$CreateDbDetails = New-Object "Oci.DatabaseService.Models.CreateDatabaseDetails"
$CreateDbDetails.AdminPassword = $AdminPassword
$CreateDbDetails.DbName = $DbName
$CreateDbHomeDetails = New-Object "Oci.DatabaseService.Models.CreateDbHomeDetails"
$CreateDbHomeDetails.DbVersion = '12.1.0.2'
$CreateDbHomeDetails.DisplayName = $DbHomeName
$CreateDbHomeDetails.Database = $CreateDbDetails
$LaunchDbSystemDetails = New-Object "Oci.DatabaseService.Models.LaunchDbSystemDetails"
$LaunchDbSystemDetails.AvailabilityDomain = $AD.Name
$LaunchDbSystemDetails.CompartmentId = $CompartmentId
$LaunchDbSystemDetails.CpuCoreCount = 4
$LaunchDbSystemDetails.Shape = $DbShape
$LaunchDbSystemDetails.SshPublicKeys = $Keylist
$LaunchDbSystemDetails.SubnetId = $Subnet.Id
$LaunchDbSystemDetails.DatabaseEdition = [Oci.DatabaseService.Models.LaunchDbSystemDetails+DatabaseEditionEnum]::StandardEdition
$LaunchDbSystemDetails.DisplayName = $DisplayName
$LaunchDbSystemDetails.Domain = $DbDomain
$LaunchDbSystemDetails.Hostname = $HostName
$LaunchDbSystemDetails.DbHome = $CreateDbHomeDetails
#Create new database system
Write-Host "New-OCIDatabaseDbSystem -LaunchDbSystemDetails $LaunchDbSystemDetails"
$DB = New-OCIDatabaseDbSystem -LaunchDbSystemDetails $LaunchDbSystemDetails
$DB
#Wait for the dbsystem to become available.
#Since DBSystems usually take a long time to become available, attempt to wait for 1 hour (60 attempts with 60 sec duration between attempts)
Write-Host "Get-OCIDatabaseDbSystem -DbSystemId $Db.Id -WaitForLifecycleState Available -MaxWaitAttempts 60 -WaitIntervalSeconds 60"
Get-OCIDatabaseDbSystem -DbSystemId $Db.Id -WaitForLifecycleState Available -MaxWaitAttempts 60 -WaitIntervalSeconds 60 | Out-Host
}
finally {
#To Maximize possible clean ups, continue on error
$ErrorActionPreference = "Continue"
#Terminate the dbsystem asking user's confirmation
if ($null -ne $Db.Id) {
Write-Host "Invoke-OCIDatabaseTerminateDbSystem -DbSystemId $Db.Id -WaitForStatus Succeeded -MaxWaitAttempts 10"
Invoke-OCIDatabaseTerminateDbSystem -DbSystemId $Db.Id -WaitForStatus Succeeded -MaxWaitAttempts 10 | Out-Host
}
if ($null -ne $Subnet.Id) {
#Force Remove the Subnet
Write-Host "Remove-OCIVirtualNetworkSubnet -SubnetId $Subnet.Id -Force -FullResponse"
Remove-OCIVirtualNetworkSubnet -SubnetId $Subnet.Id -Force -FullResponse | Out-Host
}
if ($null -ne $Vcn.Id) {
#Force Remove the created Vcn and associated resources (defaults)
Write-Host "Remove-OCIVirtualNetworkVcn -VcnId $Vcn.Id -Force -FullResponse"
Remove-OCIVirtualNetworkVcn -VcnId $Vcn.Id -Force -FullResponse | Out-Host
}
#Cleanup
if (Test-Path -Path $Filepath) {
rm $Filepath
}
if (Test-Path -Path $Pubkeypath) {
rm $Pubkeypath
}
$ErrorActionPreference = $UserErrorActionPreference
}