Skip to content

Commit 5b2c19b

Browse files
committed
(#240) Parameterize setup
This script adds parameterization to the ClientSetup.ps1 script that is hosted within the Nexus repository. It also adds parameterization to the registration script that is executed on a client so you can customize the installation.
1 parent 7865b78 commit 5b2c19b

File tree

6 files changed

+299
-63
lines changed

6 files changed

+299
-63
lines changed

Set-SslSecurity.ps1

+6-30
Original file line numberDiff line numberDiff line change
@@ -227,37 +227,13 @@ process {
227227

228228
$ClientSaltValue = New-CCMSalt
229229
$ServiceSaltValue = New-CCMSalt
230-
$ScriptBlock = @"
231-
`$ClientCommunicationSalt = '$ClientSaltValue'
232-
`$ServiceCommunicationSalt = '$ServiceSaltValue'
233-
`$FQDN = '$SubjectWithoutCN'
234-
`$NexusUserPW = '$NexusPw'
235-
236-
# Touch NOTHING below this line
237-
`$User = 'chocouser'
238-
`$SecurePassword = `$NexusUserPW | ConvertTo-SecureString -AsPlainText -Force
239-
`$RepositoryUrl = "https://`$(`$fqdn):8443/repository/ChocolateyInternal/index.json"
240-
241-
`$credential = [pscredential]::new(`$user, `$securePassword)
242-
243-
`$downloader = [System.Net.WebClient]::new()
244-
`$downloader.Credentials = `$credential
245-
246-
`$script = `$downloader.DownloadString("https://`$(`$FQDN):8443/repository/choco-install/ClientSetup.ps1")
247-
248-
`$params = @{
249-
Credential = `$Credential
250-
ClientSalt = `$ClientCommunicationSalt
251-
ServiceSalt = `$ServiceCommunicationSalt
252-
InternetEnabled = `$true
253-
RepositoryUrl = `$RepositoryUrl
254-
}
255-
256-
& ([scriptblock]::Create(`$script)) @params
257-
"@
258-
259-
$ScriptBlock | Set-Content -Path $EndpointScript
260230

231+
Invoke-TextReplacementInFile -Path $EndpointScript -Replacement @{
232+
"{{ ClientSaltValue }}" = $ClientSaltValue
233+
"{{ ServiceSaltValue }}" = $ServiceSaltValue
234+
"{{ FQDN }}" = $SubjectWithoutCn
235+
}
236+
261237
# Agent Setup
262238
$agentArgs = @{
263239
CentralManagementServiceUrl = "https://$($SubjectWithoutCn):24020/ChocolateyManagementService"

modules/C4B-Environment/C4B-Environment.psm1

+4
Original file line numberDiff line numberDiff line change
@@ -2262,6 +2262,10 @@ The host name of the C4B instance.
22622262
"{{ jenkins_fqdn .*?}}" = ([uri]$Data.JenkinsUri).DnsSafeHost
22632263
"{{ jenkins_port .*?}}" = ([uri]$Data.JenkinsUri).Port
22642264
"{{ jenkins_password .*?}}" = [System.Web.HttpUtility]::HtmlEncode($Data.JenkinsCredential.Password.ToPlainText())
2265+
2266+
# Nexus Chocolatey Source Credential values
2267+
"{{ nexus_client_username .*?}}" = 'chocouser'
2268+
"{{ nexus_client_password .*?}}" = $Data.ChocoUserPassword
22652269
}
22662270
}
22672271
}

modules/C4B-Environment/ReadmeTemplate.html.j2

+9
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,15 @@ function CopyToClipboard(id)
199199
<td>Chocolatey Central Management Service Salt</td>
200200
<td><a href="#" class="strip-decoration" onclick="CopyToClipboard('ccmservice');return false;"><div id="ccmservice" class="pw blurry-text">{{ ccm_service_salt | e }}</div></a></td>
201201
</tr>
202+
<tr>
203+
<td>Nexus Repository Source Username</td>
204+
<td><a href="#" class="strip-decoration" onclick="CopyToClipboard('nexususername');return false;"><div id="nexususername" class="pw blurry-text">{{ nexus_client_username | e }}</div></a></td>
205+
</tr>
206+
<tr>
207+
<td>Nexus Repository Source Password</td>
208+
<td><a href="#" class="strip-decoration" onclick="CopyToClipboard('nexuspassword');return false;"><div id="nexuspassword" class="pw blurry-text">{{ nexus_client_password | e }}</div></a></td>
209+
</tr>
210+
202211
</table>
203212
<blockquote>
204213
<h3>📝 <strong>Note</strong></h3>

scripts/ClientSetup.ps1

+173-18
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ param(
1414
# The credential necessary to access the internal Nexus repository. This can
1515
# be ignored if Anonymous authentication is enabled.
1616
# This parameter will be necessary if your C4B server is web-enabled.
17-
[Parameter()]
17+
[Parameter(Mandatory)]
1818
[pscredential]
19-
$Credential,
19+
$RepositoryCredential,
2020

2121
# Specifies a target version of Chocolatey to install. By default, the
2222
# latest stable version is installed.
@@ -41,19 +41,43 @@ param(
4141

4242
# Client salt value used to populate the centralManagementClientCommunicationSaltAdditivePassword
4343
# value in the Chocolatey config file
44-
[Parameter()]
44+
[Parameter(Mandatory)]
4545
[string]
46-
$ClientSalt,
46+
$ClientCommunicationSalt,
4747

4848
# Server salt value used to populate the centralManagementServiceCommunicationSaltAdditivePassword
4949
# value in the Chocolatey config file
50-
[Parameter()]
50+
[Parameter(Mandatory)]
5151
[string]
52-
$ServiceSalt,
52+
$ServiceCommunicationSalt,
5353

54+
#Install the Chocolatey Licensed Extension with right-click context menus available
5455
[Parameter()]
5556
[Switch]
56-
$InternetEnabled
57+
$IncludePackageTools,
58+
59+
# Allows for the application of user-defined configuration that is applied after the base configuration.
60+
# Can override base configuration with this parameter
61+
[Parameter()]
62+
[Hashtable]
63+
$AdditionalConfiguration,
64+
65+
# Allows for the toggling of additonal features that is applied after the base configuration.
66+
# Can override base configuration with this parameter
67+
[Parameter()]
68+
[Hashtable]
69+
$AdditionalFeatures,
70+
71+
# Allows for the installation of additional packages after the system base packages have been installed.
72+
[Parameter()]
73+
[Hashtable[]]
74+
$AdditionalPackages,
75+
76+
# Allows for the addition of alternative sources after the base conifguration has been applied.
77+
# Can override base configuration with this parameter
78+
[Parameter()]
79+
[Hashtable[]]
80+
$AdditionalSources
5781
)
5882

5983
Set-ExecutionPolicy Bypass -Scope Process -Force
@@ -69,25 +93,29 @@ $params = @{
6993

7094
if (-not $IgnoreProxy) {
7195
if ($ProxyUrl) {
96+
$proxy = [System.Net.WebProxy]::new($ProxyUrl, $true <#bypass on local#>)
7297
$params.Add('ProxyUrl', $ProxyUrl)
7398
}
7499

75100
if ($ProxyCredential) {
76101
$params.Add('ProxyCredential', $ProxyCredential)
102+
$proxy.Credentials = $ProxyCredential
103+
77104
}
78105
}
79106

80107
$webClient = New-Object System.Net.WebClient
81-
if ($Credential) {
82-
$webClient.Credentials = $Credential.GetNetworkCredential()
108+
if ($RepositoryCredential) {
109+
$webClient.Credentials = $RepositoryCredential.GetNetworkCredential()
83110
}
84111

85112
# Find the latest version of Chocolatey, if a version was not specified
86113
$NupkgUrl = if (-not $ChocolateyVersion) {
87114
$QueryUrl = ($RepositoryUrl.TrimEnd('/index.json'), "v3/registration/Chocolatey/index.json") -join '/'
88115
$Result = $webClient.DownloadString($QueryUrl) | ConvertFrom-Json
89116
$Result.items.items[-1].packageContent
90-
} else {
117+
}
118+
else {
91119
# Otherwise, assume the URL
92120
"$($RepositoryUrl.TrimEnd('/index.json'))/v3/content/chocolatey/$($ChocolateyVersion)/chocolatey.$($ChocolateyVersion).nupkg"
93121
}
@@ -118,18 +146,19 @@ choco config set commandExecutionTimeoutSeconds 14400
118146
# Nexus NuGet V3 Compatibility
119147
choco feature disable --name="'usePackageRepositoryOptimizations'"
120148

121-
if ($InternetEnabled) {
122-
choco source add --name="'ChocolateyInternal'" --source="'$RepositoryUrl'" --allow-self-service --user="'$($Credential.UserName)'" --password="'$($Credential.GetNetworkCredential().Password)'" --priority=1
123-
}
124-
else {
125-
choco source add --name="'ChocolateyInternal'" --source="'$RepositoryUrl'" --allow-self-service --priority=1
126-
}
127-
149+
# Environment base Source configuration
150+
choco source add --name="'ChocolateyInternal'" --source="'$RepositoryUrl'" --allow-self-service --user="'$($RepositoryCredential.UserName)'" --password="'$($RepositoryCredential.GetNetworkCredential().Password)'" --priority=1
128151
choco source disable --name="'Chocolatey'"
129152
choco source disable --name="'chocolatey.licensed'"
130153

131154
choco upgrade chocolatey-license -y --source="'ChocolateyInternal'"
132-
choco upgrade chocolatey.extension -y --params="'/NoContextMenu'" --source="'ChocolateyInternal'" --no-progress
155+
if (-not $IncludePackageTools) {
156+
choco upgrade chocolatey.extension -y --params="'/NoContextMenu'" --source="'ChocolateyInternal'" --no-progress
157+
}
158+
else {
159+
Write-Warning "IncludePackageTools was passed. Right-Click context menus will be available for installers, .nupkg, and .nuspec file types!"
160+
choco upgrade chocolatey.extension -y --source="'ChocolateyInternal'" --no-progress
161+
}
133162
choco upgrade chocolateygui -y --source="'ChocolateyInternal'" --no-progress
134163
choco upgrade chocolateygui.extension -y --source="'ChocolateyInternal'" --no-progress
135164

@@ -158,3 +187,129 @@ if ($ServiceSalt) {
158187
}
159188
choco feature enable --name="'useChocolateyCentralManagement'"
160189
choco feature enable --name="'useChocolateyCentralManagementDeployments'"
190+
191+
192+
if ($AdditionalConfiguration -or $AdditionalFeatures -or $AdditionalSources -or $AdditionalPackages) {
193+
Write-Host "Applying user supplied configuration" -ForegroundColor Cyan
194+
}
195+
# How we call choco from here changes as we need to be more dynamic with thingsii .
196+
if ($AdditionalConfiguration) {
197+
<#
198+
We expect to pass in a hashtable with configuration information with the following shape:
199+
200+
@{
201+
Name = BackgroundServiceAllowedCommands
202+
Value = 'install,upgrade,uninstall'
203+
}
204+
#>
205+
206+
$AdditionalConfiguration.GetEnumerator() | ForEach-Object {
207+
$c = [System.Collections.Generic.list[string]]::new()
208+
$c.Add('config')
209+
$c.Add('set')
210+
$c.Add("--name='$($_.Key)'")
211+
$c.Add("--value='$($_.Value)'")
212+
213+
& choco @c
214+
}
215+
}
216+
217+
if ($AdditionalFeatures) {
218+
<#
219+
We expect to pass in feature information as a hashtable with the following shape:
220+
221+
@{
222+
useBackgroundservice = 'Enabled'
223+
}
224+
#>
225+
$AdditionalFeatures.GetEnumerator() | ForEach-Object {
226+
227+
$c = [System.Collections.Generic.list[string]]::new()
228+
$c.Add('feature')
229+
230+
$state = switch ($_.Value) {
231+
'Enabled' { 'enable' }
232+
'Disabled' { 'disable' }
233+
default { Write-Error 'State must be either Enabled or Disabled' }
234+
}
235+
236+
$c.Add($state)
237+
$c.add("--name='$($_.Key)'")
238+
& choco @c
239+
}
240+
}
241+
242+
if ($AdditionalSources) {
243+
244+
<#
245+
We expect a user to pass in a hashtable with source information with the folllowing shape:
246+
@{
247+
Name = 'MySource'
248+
Source = 'https://nexus.fabrikam.com/repository/MyChocolateySource'
249+
#Optional items
250+
Credentials = $MySourceCredential
251+
AllowSelfService = $true
252+
AdminOnly = $true
253+
BypassProxy = $true
254+
Priority = 10
255+
Certificate = 'C:\cert.pfx'
256+
CertificatePassword = 's0mepa$$'
257+
}
258+
#>
259+
Foreach ($a in $AdditionalSources) {
260+
$c = [System.Collections.Generic.List[string]]::new()
261+
# Required items
262+
$c.Add('source')
263+
$c.Add('add')
264+
$c.Add("--name='$($a.Name)'")
265+
$c.Add("--source='$($a.Source)'")
266+
267+
# Add credentials if source has them
268+
if ($a.ContainsKey('Credentials')) {
269+
$c.Add("--user='$($a.Credentials.Username)'")
270+
$c.Add("--password='$($a.Credentials.GetNetworkCredential().Password)'")
271+
}
272+
273+
switch ($true) {
274+
$a['AllowSelfService'] { $c.add('--allow-self-service') }
275+
$a['AdminOnly'] { $c.Add('--admin-only') }
276+
$a['BypassProxy'] { $c.Add('--bypass-proxy') }
277+
$a.ContainsKey('Priority') { $c.Add("--priority='$($a.Priority)'") }
278+
$a.ContainsKey('Certificate') { $c.Add("--cert='$($a.Certificate)'") }
279+
$a.ContainsKey('CerfificatePassword') { $c.Add("--certpassword='$($a.CertificatePassword)'") }
280+
}
281+
}
282+
283+
& choco @c
284+
}
285+
286+
if ($AdditionalPackages) {
287+
288+
<#
289+
We expect to pass in a hashtable with package information with the following shape:
290+
291+
@{
292+
Id = 'firefox'
293+
#Optional
294+
Version = 123.4.56
295+
Pin = $true
296+
}
297+
#>
298+
foreach ($package in $AdditionalPackages.GetEnumerator()) {
299+
300+
$c = [System.Collections.Generic.list[string]]::new()
301+
$c.add('install')
302+
$c.add($package['Id'])
303+
304+
switch ($true) {
305+
$package.ContainsKey('Version') { $c.Add("--version='$($package.version)'") }
306+
$package.ContainsKey('Pin') { $c.Add('--pin') }
307+
}
308+
309+
# Ensure packages install and they don't flood the console output
310+
$c.Add('-y')
311+
$c.Add('--no-progress')
312+
313+
& choco @c
314+
}
315+
}

scripts/New-IISCertificateHost.ps1

-6
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,6 @@ if (-not (Get-Website -Name $siteName)) {
4949
Write-Host "Website for hosting certificate import already created" -ForegroundColor Green
5050
}
5151

52-
if ((Get-Website -Name 'Default Web Site')) {
53-
Get-Website -Name 'Default Web Site' | Remove-Website
54-
} else {
55-
Write-Host "Default website already removed" -ForegroundColor Green
56-
}
57-
5852
Write-Host "Restarting IIS to refresh bindings" -ForegroundColor Green
5953
$null = iisreset
6054

0 commit comments

Comments
 (0)