Skip to content

Commit b7366f3

Browse files
committed
Pass IAAS to powershell functions
Some powershell functions in the two copies that exist today differ implicitly by IAAS. This change explicitly passes an `$IaaS` parameter to the two functions which need to have IAAS conditional behavior explicitly once the two copies of powershell modules have been consolidated.
1 parent 5c371c9 commit b7366f3

File tree

10 files changed

+50
-31
lines changed

10 files changed

+50
-31
lines changed

lib/packer/config/templates/provision_windows2019.json.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"inline": [
5656
"$ErrorActionPreference = \"Stop\";",
5757
"trap { $host.SetShouldExit(1) }",
58-
"Install-CFFeatures"
58+
"Install-CFFeatures -IaaS <%= iaas %>"
5959
]
6060
},
6161
{
@@ -129,7 +129,7 @@
129129
"inline": [
130130
"$ErrorActionPreference = \"Stop\";",
131131
"trap { $host.SetShouldExit(1) }",
132-
"Protect-CFCell"
132+
"Protect-CFCell -IaaS <%= iaas %>"
133133
]
134134
},
135135
{

modules/BOSH.CFCell/BOSH.CFCell.Tests.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Describe "Protect-CFCell" {
3333
Get-Service "Termservice" | Set-Service -StartupType "Automatic"
3434
netstat /p tcp /a | findstr ":3389 " | Should -Not -BeNullOrEmpty
3535

36-
Protect-CFCell
36+
Protect-CFCell -IaaS "ignored"
3737

3838
Get-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\Terminal Server" | select -exp fDenyTSConnections | Should -Be 1
3939
netstat /p tcp /a | findstr ":3389 " | Should -BeNullOrEmpty
@@ -44,7 +44,7 @@ Describe "Protect-CFCell" {
4444
It "disables the services" {
4545
Get-Service | Where-Object {$_.Name -eq "WinRM" } | Set-Service -StartupType Automatic
4646
Get-Service | Where-Object {$_.Name -eq "W3Svc" } | Set-Service -StartupType Automatic
47-
Protect-CFCell
47+
Protect-CFCell -IaaS "ignored"
4848
(Get-Service | Where-Object {$_.Name -eq "WinRM" } ).StartType| Should -Be "Disabled"
4949
$w3svcStartType = (Get-Service | Where-Object {$_.Name -eq "W3Svc" } ).StartType
5050
"Disabled", $null -contains $w3svcStartType | Should -Be $true
@@ -55,7 +55,7 @@ Describe "Protect-CFCell" {
5555
get-firewall "public" | Should -Be "public,Allow,Allow"
5656
get-firewall "private" | Should -Be "private,Allow,Allow"
5757
get-firewall "domain" | Should -Be "domain,Allow,Allow"
58-
Protect-CFCell
58+
Protect-CFCell -IaaS "ignored"
5959
get-firewall "public" | Should -Be "public,Block,Allow"
6060
get-firewall "private" | Should -Be "private,Block,Allow"
6161
get-firewall "domain" | Should -Be "domain,Block,Allow"
@@ -75,25 +75,25 @@ Describe "Install-CFFeatures" {
7575
}
7676

7777
It "triggers a machine restart when the -ForceReboot flag is set" {
78-
{ Install-CFFeatures -ForceReboot } | Should -Not -Throw
78+
{ Install-CFFeatures -IaaS "ignored" -ForceReboot } | Should -Not -Throw
7979

8080
Assert-MockCalled Restart-Computer -Times 1 -Scope It -ModuleName BOSH.CFCell
8181
}
8282

8383
It "doesn't trigger a machine restart if -ForceReboot flag not set" {
84-
{ Install-CFFeatures } | Should -Not -Throw
84+
{ Install-CFFeatures -IaaS "ignored" } | Should -Not -Throw
8585

8686
Assert-MockCalled Restart-Computer -Times 0 -Scope It -ModuleName BOSH.CFCell
8787
}
8888

8989
It "logs Installing CloudFoundry Cell Windows Features" {
90-
{ Install-CFFeatures } | Should -Not -Throw
90+
{ Install-CFFeatures -IaaS "ignored" } | Should -Not -Throw
9191

9292
Assert-MockCalled Write-Log -Times 1 -Scope It -ModuleName BOSH.CFCell -ParameterFilter { $Message -eq "Installing CloudFoundry Cell Windows Features" }
9393
}
9494

9595
It "logs Installed CloudFoundry Cell Windows Features after installation" {
96-
{ Install-CFFeatures } | Should -Not -Throw
96+
{ Install-CFFeatures -IaaS "ignored" } | Should -Not -Throw
9797

9898
Assert-MockCalled Write-Log -Times 1 -Scope It -ModuleName BOSH.CFCell -ParameterFilter { $Message -eq "Installed CloudFoundry Cell Windows Features" }
9999
}

modules/BOSH.CFCell/BOSH.CFCell.psm1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
This cmdlet installs the minimum set of features for a CloudFoundry Cell on Windows
66
#>
77
function Install-CFFeatures {
8-
param([switch]$ForceReboot)
8+
param(
9+
[string]$IaaS = $(Throw "Provide the IaaS of your VM"),
10+
[switch]$ForceReboot
11+
)
912

1013
Write-Log "Getting WinRM config"
1114
$winrm_config = Get-WinRMConfig
@@ -53,6 +56,10 @@ function Wait-ForNewIfaces {
5356
}
5457

5558
function Protect-CFCell {
59+
param(
60+
[string]$IaaS = $(Throw "Provide the IaaS of your VM")
61+
)
62+
5663
Write-Log "Getting WinRM config"
5764
$winrm_config = Get-WinRMConfig
5865
Write-Log "$winrm_config"

spec/packer/config/aws_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "New-Provisioner"]},
179179
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Remove-DockerPackage"]},
180180
{"type" => "windows-restart", "restart_timeout" => "1h", "check_registry" => true},
181-
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Install-CFFeatures"]},
181+
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Install-CFFeatures -IaaS aws"]},
182182
{"type" => "windows-restart", "restart_timeout" => "1h", "check_registry" => true},
183183
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Add-Account -User Provisioner -Password some-password!"]},
184184
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Register-WindowsUpdatesTask"]},
@@ -188,7 +188,7 @@
188188
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Get-HotFix > hotfixes.log"]},
189189
{"type" => "file", "source" => "hotfixes.log", "destination" => "hotfixes.log", "direction" => "download"},
190190
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Remove-Account -User Provisioner"]},
191-
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Protect-CFCell"]},
191+
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Protect-CFCell -IaaS aws"]},
192192
{"type" => "file", "source" => "../sshd/OpenSSH-Win64.zip", "destination" => "C:\\provision\\OpenSSH-Win64.zip"},
193193
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Install-SSHD -SSHZipFile 'C:\\provision\\OpenSSH-Win64.zip'"]},
194194
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Enable-SSHD"]},

spec/packer/config/azure_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "New-Provisioner"]},
120120
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Remove-DockerPackage"]},
121121
{"type" => "windows-restart", "restart_timeout" => "1h", "check_registry" => true},
122-
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Install-CFFeatures"]},
122+
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Install-CFFeatures -IaaS azure"]},
123123
{"type" => "windows-restart", "restart_timeout" => "1h", "check_registry" => true},
124124
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Add-Account -User Provisioner -Password some-password!"]},
125125
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Register-WindowsUpdatesTask"]},
@@ -129,7 +129,7 @@
129129
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Get-HotFix > hotfixes.log"]},
130130
{"type" => "file", "source" => "hotfixes.log", "destination" => "hotfixes.log", "direction" => "download"},
131131
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Remove-Account -User Provisioner"]},
132-
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Protect-CFCell"]},
132+
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Protect-CFCell -IaaS azure"]},
133133
{"type" => "file", "source" => "../sshd/OpenSSH-Win64.zip", "destination" => "C:\\provision\\OpenSSH-Win64.zip"},
134134
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Install-SSHD -SSHZipFile 'C:\\provision\\OpenSSH-Win64.zip'"]},
135135
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Enable-SSHD"]},

spec/packer/config/gcp_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "New-Provisioner"]},
105105
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Remove-DockerPackage"]},
106106
{"type" => "windows-restart", "restart_timeout"=>"1h", "check_registry" => true},
107-
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Install-CFFeatures"]},
107+
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Install-CFFeatures -IaaS gcp"]},
108108
{"type" => "windows-restart", "restart_timeout"=>"1h", "check_registry" => true},
109109
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Add-Account -User Provisioner -Password some-password!"]},
110110
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Register-WindowsUpdatesTask"]},
@@ -114,7 +114,7 @@
114114
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Get-HotFix > hotfixes.log"]},
115115
{"type" => "file", "source" => "hotfixes.log", "destination" => "hotfixes.log", "direction" => "download"},
116116
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Remove-Account -User Provisioner"]},
117-
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Protect-CFCell"]},
117+
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Protect-CFCell -IaaS gcp"]},
118118
{"type" => "file", "source" => "../sshd/OpenSSH-Win64.zip", "destination" => "C:\\provision\\OpenSSH-Win64.zip"},
119119
{"type" => "powershell", "inline" => ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Install-SSHD -SSHZipFile 'C:\\provision\\OpenSSH-Win64.zip'"]},
120120
{"type"=>"powershell", "inline"=> ["$ErrorActionPreference = \"Stop\";", "trap { $host.SetShouldExit(1) }", "Enable-SSHD"]},

stembuild/modules/BOSH.CFCell/BOSH.CFCell.Tests.ps1

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Describe "Protect-CFCell" {
3333
Get-Service "Termservice" | Set-Service -StartupType "Automatic"
3434
netstat /p tcp /a | findstr ":3389 " | Should -Not -BeNullOrEmpty
3535

36-
Protect-CFCell
36+
Protect-CFCell -IaaS "ignored"
3737

3838
Get-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\Terminal Server" | select -exp fDenyTSConnections | Should -Be 1
3939
netstat /p tcp /a | findstr ":3389 " | Should -BeNullOrEmpty
@@ -44,7 +44,7 @@ Describe "Protect-CFCell" {
4444
It "disables the services" {
4545
Get-Service | Where-Object {$_.Name -eq "WinRM" } | Set-Service -StartupType Automatic
4646
Get-Service | Where-Object {$_.Name -eq "W3Svc" } | Set-Service -StartupType Automatic
47-
Protect-CFCell
47+
Protect-CFCell -IaaS "ignored"
4848
(Get-Service | Where-Object {$_.Name -eq "WinRM" } ).StartType| Should -Be "Disabled"
4949
$w3svcStartType = (Get-Service | Where-Object {$_.Name -eq "W3Svc" } ).StartType
5050
"Disabled", $null -contains $w3svcStartType | Should -Be $true
@@ -55,7 +55,7 @@ Describe "Protect-CFCell" {
5555
get-firewall "public" | Should -Be "public,Allow,Allow"
5656
get-firewall "private" | Should -Be "private,Allow,Allow"
5757
get-firewall "domain" | Should -Be "domain,Allow,Allow"
58-
Protect-CFCell
58+
Protect-CFCell -IaaS "ignored"
5959
get-firewall "public" | Should -Be "public,Block,Allow"
6060
get-firewall "private" | Should -Be "private,Block,Allow"
6161
get-firewall "domain" | Should -Be "domain,Block,Allow"
@@ -74,7 +74,7 @@ Describe "Protect-CFCell" {
7474
}
7575
Mock -ModuleName BOSH.CFCell Set-MpPreference { }
7676

77-
Protect-CFCell
77+
Protect-CFCell -IaaS "ignored"
7878

7979
Assert-MockCalled Write-Log -Exactly 1 -Scope It -ModuleName BOSH.CFCell -ParameterFilter { $Message -eq "Disabling Windows Defender Features" }
8080

@@ -88,7 +88,7 @@ Describe "Protect-CFCell" {
8888
Mock -ModuleName BOSH.CFCell Get-Command { $false }
8989
Mock -ModuleName BOSH.CFCell Set-MpPreference { }
9090

91-
Protect-CFCell
91+
Protect-CFCell -IaaS "ignored"
9292

9393
Assert-MockCalled Write-Log -Exactly 1 -Scope It -ModuleName BOSH.CFCell -ParameterFilter { $Message -eq "Set-MpPreference command not found, assuming Windows Defender is not installed" }
9494
Assert-MockCalled Set-MpPreference -Scope It -Exactly 0 -ModuleName BOSH.CFCell
@@ -108,25 +108,25 @@ Describe "Install-CFFeatures" {
108108
}
109109

110110
It "triggers a machine restart when the -ForceReboot flag is set" {
111-
{ Install-CFFeatures -ForceReboot } | Should -Not -Throw
111+
{ Install-CFFeatures -IaaS "ignored" -ForceReboot } | Should -Not -Throw
112112

113113
Assert-MockCalled Restart-Computer -Times 1 -Scope It -ModuleName BOSH.CFCell
114114
}
115115

116116
It "doesn't trigger a machine restart if -ForceReboot flag not set" {
117-
{ Install-CFFeatures } | Should -Not -Throw
117+
{ Install-CFFeatures -IaaS "ignored" } | Should -Not -Throw
118118

119119
Assert-MockCalled Restart-Computer -Times 0 -Scope It -ModuleName BOSH.CFCell
120120
}
121121

122122
It "logs Installing CloudFoundry Cell Windows Features" {
123-
{ Install-CFFeatures } | Should -Not -Throw
123+
{ Install-CFFeatures -IaaS "ignored" } | Should -Not -Throw
124124

125125
Assert-MockCalled Write-Log -Times 1 -Scope It -ModuleName BOSH.CFCell -ParameterFilter { $Message -eq "Installing CloudFoundry Cell Windows Features" }
126126
}
127127

128128
It "logs Installed CloudFoundry Cell Windows Features after installation" {
129-
{ Install-CFFeatures } | Should -Not -Throw
129+
{ Install-CFFeatures -IaaS "ignored" } | Should -Not -Throw
130130

131131
Assert-MockCalled Write-Log -Times 1 -Scope It -ModuleName BOSH.CFCell -ParameterFilter { $Message -eq "Installed CloudFoundry Cell Windows Features" }
132132
}

stembuild/modules/BOSH.CFCell/BOSH.CFCell.psm1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
This cmdlet installs the minimum set of features for a CloudFoundry Cell on Windows
66
#>
77
function Install-CFFeatures {
8-
param([switch]$ForceReboot)
8+
param(
9+
[string]$IaaS = $(Throw "Provide the IaaS of your VM"),
10+
[switch]$ForceReboot
11+
)
912

1013
Write-Log "Getting WinRM config"
1114
$winrm_config = Get-WinRMConfig
@@ -52,6 +55,10 @@ function Wait-ForNewIfaces {
5255
}
5356

5457
function Protect-CFCell {
58+
param(
59+
[string]$IaaS = $(Throw "Provide the IaaS of your VM")
60+
)
61+
5562
Write-Log "Getting WinRM config"
5663
$winrm_config = Get-WinRMConfig
5764
Write-Log "$winrm_config"

stembuild/stemcell-automation/AutomationHelpers.Tests.ps1

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ BeforeAll {
55
Import-Module ../modules/BOSH.SSH
66
Import-Module ../modules/BOSH.Utils
77
Import-Module ../modules/BOSH.Agent
8+
Import-Module ../modules/BOSH.CFCell
89
Import-Module ./AutomationHelpers.psm1
910

1011
function Get-WuCerts { }
@@ -249,7 +250,9 @@ Describe "InstallCFFeatures" {
249250

250251
{ InstallCFFeatures } | Should -Not -Throw
251252

252-
Should -Invoke -ModuleName AutomationHelpers -CommandName Install-CFFeatures -Times 1
253+
Should -Invoke -ModuleName AutomationHelpers -CommandName Install-CFFeatures -Times 1 -ParameterFilter {
254+
$IaaS -eq "vsphere"
255+
}
253256
Should -Invoke -ModuleName AutomationHelpers -CommandName Write-Log -Times 1 -ParameterFilter {
254257
$Message -eq "Successfully installed CF features"
255258
}
@@ -279,7 +282,9 @@ Describe "InstallCFCell" {
279282

280283
{ InstallCFCell } | Should -Not -Throw
281284

282-
Should -Invoke -ModuleName AutomationHelpers -CommandName Protect-CFCell -Times 1
285+
Should -Invoke -ModuleName AutomationHelpers -CommandName Protect-CFCell -Times 1 -ParameterFilter {
286+
$IaaS -eq "vsphere"
287+
}
283288
Should -Invoke -ModuleName AutomationHelpers -CommandName Write-Log -Times 1 -ParameterFilter {
284289
$Message -eq "Succesfully ran Protect-CFCell"
285290
}

stembuild/stemcell-automation/AutomationHelpers.psm1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function InstallCFFeatures
110110
{
111111
try
112112
{
113-
Install-CFFeatures
113+
Install-CFFeatures -IaaS "vsphere"
114114
Write-Log "Successfully installed CF features"
115115
}
116116
catch [Exception]
@@ -131,7 +131,7 @@ function InstallCFCell
131131
{
132132
try
133133
{
134-
Protect-CFCell
134+
Protect-CFCell -IaaS "vsphere"
135135
Write-Log "Succesfully ran Protect-CFCell"
136136
}
137137
catch [Exception]
@@ -146,7 +146,7 @@ function InstallBoshAgent
146146
{
147147
try
148148
{
149-
Install-Agent -Iaas "vsphere" -agentZipPath ".\agent.zip"
149+
Install-Agent -IaaS "vsphere" -agentZipPath ".\agent.zip"
150150
Write-Log "Bosh agent successfully installed"
151151
}
152152
catch [Exception]

0 commit comments

Comments
 (0)