Skip to content

Commit e741894

Browse files
authored
Merge pull request #52 from sqlcollaborative/packages
Auto-installation of dependencies upon first run
2 parents c61464a + 7a26cda commit e741894

10 files changed

+158
-104
lines changed

dbops.psm1

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Register-PSFConfigValidation -Name "transaction" -ScriptBlock {
2020
Param (
2121
$Value
2222
)
23-
24-
$Result = New-Object PSOBject -Property @{
23+
24+
$Result = New-Object PSObject -Property @{
2525
Success = $True
2626
Value = $null
2727
Message = ""
@@ -39,16 +39,16 @@ Register-PSFConfigValidation -Name "transaction" -ScriptBlock {
3939
$Result.Message = "Failed to convert value to string"
4040
$Result.Success = $False
4141
}
42-
42+
4343
return $Result
4444
}
4545

4646
Register-PSFConfigValidation -Name "securestring" -ScriptBlock {
4747
Param (
4848
$Value
4949
)
50-
51-
$Result = New-Object PSOBject -Property @{
50+
51+
$Result = New-Object PSObject -Property @{
5252
Success = $True
5353
Value = $null
5454
Message = ""
@@ -67,8 +67,8 @@ Register-PSFConfigValidation -Name "hashtable" -ScriptBlock {
6767
Param (
6868
$Value
6969
)
70-
71-
$Result = New-Object PSOBject -Property @{
70+
71+
$Result = New-Object PSObject -Property @{
7272
Success = $True
7373
Value = $null
7474
Message = ""
@@ -89,6 +89,36 @@ Register-PSFConfigValidation -Name "hashtable" -ScriptBlock {
8989
return $Result
9090
}
9191

92+
Register-PSFConfigValidation -Name "connectionType" -ScriptBlock {
93+
Param (
94+
$Value
95+
)
96+
$allowedTypes = @(
97+
SQLServer
98+
Oracle
99+
)
100+
$failMessage = "Only the following values are allowed: $($allowedTypes -join ', ')"
101+
$Result = New-Object PSObject -Property @{
102+
Success = $True
103+
Value = $null
104+
Message = ""
105+
}
106+
try {
107+
if (([string]$Value) -is [string] -and [string]$Value -in $allowedTypes) {
108+
$Result.Value = [string]$Value
109+
}
110+
else {
111+
$Result.Message = $failMessage
112+
$Result.Success = $False
113+
}
114+
}
115+
catch {
116+
$Result.Message = "Failed to convert value to string. $failMessage"
117+
$Result.Success = $False
118+
}
119+
return $Result
120+
}
121+
92122
# defining defaults
93123

94124
Set-PSFConfig -FullName dbops.ApplicationName -Value "dbops" -Initialize -Description "Application name in the connection string"
@@ -116,6 +146,7 @@ Set-PSFConfig -FullName dbops.mail.To -Value "" -Initialize -Description "'To' f
116146
Set-PSFConfig -FullName dbops.mail.Subject -Value "DBOps deployment status" -Initialize -Description "'Subject' field in the outgoing emails."
117147
Set-PSFConfig -FullName dbops.security.encryptionkey -Value "~/.dbops.key" -Initialize -Description "Path to a custom encryption key used to encrypt/decrypt passwords. The key should be a binary file with a length of 128, 192 or 256 bits. Key will be generated automatically if not exists."
118148
Set-PSFConfig -FullName dbops.security.usecustomencryptionkey -Value ($PSVersionTable.Platform -eq 'Unix') -Validation bool -Initialize -Description "Determines whether to use a custom encryption key for storing passwords. Enabled by default only on Unix platforms."
149+
Set-PSFConfig -FullName dbops.rdbms.type -Value 'SQLServer' -Validation connectionType -Initialize -Description "Assumes a certain RDBMS as a default one for each command. SQLServer by default"
119150

120151
# extensions for SMO
121152
$typeData = Get-TypeData -TypeName 'Microsoft.SqlServer.Management.Smo.Database'

functions/Install-DBOPackage.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
.PARAMETER CreateDatabase
9595
Will create an empty database if missing on supported RDMBS
9696
97-
.PARAMETER ConnectionType
97+
.PARAMETER Type
9898
Defines the driver to use when connecting to the database server.
9999
Available options: SqlServer (default), Oracle
100100
@@ -167,8 +167,8 @@
167167
[AllowNull()]
168168
[string]$ConnectionString,
169169
[ValidateSet('SQLServer', 'Oracle')]
170-
[Alias('Type', 'ServerType')]
171-
[string]$ConnectionType = 'SQLServer'
170+
[Alias('ConnectionType', 'ServerType')]
171+
[string]$Type = (Get-DBODefaultSetting -Name rdbms.type -Value)
172172
)
173173

174174
begin {
@@ -204,7 +204,7 @@
204204
}
205205
foreach ($key in ($PSBoundParameters.Keys)) {
206206
#If any custom properties were specified
207-
if ($key -in @('OutputFile', 'Append', 'ConnectionType', 'Build')) {
207+
if ($key -in @('OutputFile', 'Append', 'Type', 'Build')) {
208208
$params += @{ $key = $PSBoundParameters[$key] }
209209
}
210210
}

functions/Install-DBOSqlScript.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
.PARAMETER CreateDatabase
9292
Will create an empty database if missing on supported RDMBS
9393
94-
.PARAMETER ConnectionType
94+
.PARAMETER Type
9595
Defines the driver to use when connecting to the database server.
9696
Available options: SqlServer (default), Oracle
9797
@@ -163,8 +163,8 @@
163163
[AllowNull()]
164164
[string]$ConnectionString,
165165
[ValidateSet('SQLServer', 'Oracle')]
166-
[Alias('Type', 'ServerType')]
167-
[string]$ConnectionType = 'SQLServer'
166+
[Alias('ConnectionType', 'ServerType')]
167+
[string]$Type = (Get-DBODefaultSetting -Name rdbms.type -Value)
168168
)
169169

170170
begin {
@@ -205,7 +205,7 @@
205205
}
206206
foreach ($key in ($PSBoundParameters.Keys)) {
207207
#If any custom properties were specified
208-
if ($key -in @('OutputFile', 'Append', 'ConnectionType')) {
208+
if ($key -in @('OutputFile', 'Append', 'Type')) {
209209
$params += @{ $key = $PSBoundParameters[$key] }
210210
}
211211
}

functions/Install-DBOSupportLibrary.ps1

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,33 @@ Function Install-DBOSupportLibrary {
22
<#
33
.SYNOPSIS
44
Installs external dependencies for a defined RDBMS
5-
5+
66
.DESCRIPTION
77
This command will download nuget packages from NuGet website in order to support deployments for certain RDBMS.
8-
8+
99
.PARAMETER Type
1010
RDBMS Type: Oracle, SQLServer
11-
11+
1212
.PARAMETER Force
1313
Enforce installation
14-
14+
1515
.PARAMETER Scope
1616
Choose whether to install for CurrentUser or for AllUsers
17-
17+
18+
.PARAMETER Confirm
19+
Prompts to confirm certain actions
20+
21+
.PARAMETER WhatIf
22+
Shows what would happen if the command would execute, but does not actually perform the command
23+
24+
1825
.EXAMPLE
1926
#Installs all dependencies for Oracle
2027
Install-DBOSupportLibrary Oracle
2128
.NOTES
22-
29+
2330
#>
31+
[CmdletBinding(SupportsShouldProcess)]
2432
Param (
2533
[Parameter(Mandatory = $true,
2634
ValueFromPipeline = $true,
@@ -34,24 +42,42 @@ Function Install-DBOSupportLibrary {
3442
)
3543
begin {
3644
$nugetAPI = "http://www.nuget.org/api/v2"
37-
$packageSource = Get-PackageSource -Name nuget.org -ErrorAction SilentlyContinue
45+
# trying to use one of the existing repos
46+
try {
47+
$packageSource = Get-PackageSource -Name nuget.org.dbops -ProviderName nuget -ErrorAction Stop
48+
}
49+
catch {
50+
$packageSource = Get-PackageSource -Name nuget.org -ProviderName nuget -ErrorAction SilentlyContinue
51+
}
3852
# checking if nuget has an incorrect API url
3953
if ($packageSource.Location -like 'https://api.nuget.org/v3*') {
40-
Write-PSFMessage -Level Warning -Message "NuGet package source is registered using API v3, which prevents Install-Package to download nuget packages. Registering a new package source nuget.org.dbops to download packages."
41-
$packageSource = Register-PackageSource -Name nuget.org.dbops -Location $nugetAPI -ProviderName nuget -Force:$Force -ErrorAction Stop
54+
if ($PSCmdlet.ShouldProcess('NuGet package source is registered using API v3, installing a new repository source nuget.org.dbops')) {
55+
Write-PSFMessage -Level Verbose -Message "NuGet package source is registered using API v3, which prevents Install-Package to download nuget packages. Registering a new package source nuget.org.dbops to download packages."
56+
$packageSource = Register-PackageSource -Name nuget.org.dbops -Location $nugetAPI -ProviderName nuget -Force:$Force -ErrorAction Stop
57+
}
4258
}
4359
if (!$packageSource) {
44-
Write-PSFMessage -Level Verbose -Message "Registering nuget.org package source $nugetAPI"
45-
$packageSource = Register-PackageSource -Name nuget.org -Location $nugetAPI -ProviderName nuget -Force:$Force -ErrorAction Stop
60+
if ($PSCmdlet.ShouldProcess("Registering package source repository nuget.org ($nugetAPI)")) {
61+
Write-PSFMessage -Level Verbose -Message "Registering nuget.org package source $nugetAPI"
62+
$packageSource = Register-PackageSource -Name nuget.org -Location $nugetAPI -ProviderName nuget -Force:$Force -ErrorAction Stop
63+
}
4664
}
4765
}
4866
process {
4967
$dependencies = Get-ExternalLibrary
68+
$packagesToUpdate = @()
5069
foreach ($t in $Type) {
51-
# Install dependencies
70+
# Check existance
5271
foreach ($package in $dependencies.$t) {
72+
$p = Get-Package -name $package.Name -RequiredVersion $package.Version -ProviderName nuget -ErrorAction SilentlyContinue
73+
if (-Not $p -or $Force) { $packagesToUpdate += $package }
74+
}
75+
}
76+
if ($packagesToUpdate -and $PSCmdlet.ShouldProcess("Scope: $Scope", "Installing dependent package(s) $($packagesToUpdate.Name -join ', ') from nuget.org")) {
77+
# Install dependencies
78+
foreach ($package in $packagesToUpdate) {
5379
Write-PSFMessage -Level Verbose -Message "Installing package $($package.Name)($($package.Version))"
54-
Install-Package -Source $packageSource.Name -Name $package.Name -MinimumVersion $package.Version -Force:$Force -Scope:$Scope
80+
$null = Install-Package -Source $packageSource.Name -Name $package.Name -RequiredVersion $package.Version -Force:$Force -Scope:$Scope
5581
}
5682
}
5783
}

0 commit comments

Comments
 (0)