Skip to content

Commit a6ffcf3

Browse files
committed
(#91) Improves Readme
This brings in a slightly modified version of the C4B-Ansible template and adds some functionality to: - Replace values in it from the output JSON - Update the JSON when we change the values in there - Store slightly more in the JSON, so we can hand the user the rest of the keys
1 parent c85cd54 commit a6ffcf3

File tree

3 files changed

+304
-84
lines changed

3 files changed

+304
-84
lines changed

Set-SslSecurity.ps1

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,14 @@ process {
124124
$response = try {
125125
Invoke-WebRequest "https://${SubjectWithoutCn}:8443" -UseBasicParsing -ErrorAction Stop
126126
Start-Sleep -Seconds 3
127-
}
128-
catch {
129-
130-
}
131-
127+
} catch {}
132128
} until($response.StatusCode -eq '200')
133129
Write-Host "Nexus is ready!"
134130

135131
choco source remove --name="'ChocolateyInternal'"
136132
$RepositoryUrl = "https://${SubjectWithoutCn}:8443/repository/ChocolateyInternal/index.json"
137133

138-
#Build Credential Object, Connect to Nexus
134+
# Build Credential Object, Connect to Nexus
139135
$securePw = (Get-Content 'C:\programdata\sonatype-work\nexus3\admin.password') | ConvertTo-SecureString -AsPlainText -Force
140136
$Credential = [System.Management.Automation.PSCredential]::new('admin', $securePw)
141137

@@ -144,13 +140,13 @@ process {
144140

145141
# Add updated scripts to raw repo in Nexus
146142

147-
#Push ChocolateyInstall.ps1 to raw repo
143+
# Push ChocolateyInstall.ps1 to raw repo
148144
$ScriptDir = "$env:SystemDrive\choco-setup\files\scripts"
149145
$ChocoInstallScript = "$ScriptDir\ChocolateyInstall.ps1"
150146
(Get-Content -Path $ChocoInstallScript) -replace "{{hostname}}", $SubjectWithoutCn | Set-Content -Path $ChocoInstallScript
151147
New-NexusRawComponent -RepositoryName 'choco-install' -File "$ChocoInstallScript"
152148

153-
#Push ClientSetup.ps1 to raw repo
149+
# Push ClientSetup.ps1 to raw repo
154150
$ClientScript = "$ScriptDir\ClientSetup.ps1"
155151
(Get-Content -Path $ClientScript) -replace "{{hostname}}", $SubjectWithoutCn | Set-Content -Path $ClientScript
156152
New-NexusRawComponent -RepositoryName 'choco-install' -File $ClientScript
@@ -213,6 +209,12 @@ process {
213209
$chocoArgs = @('apikey', "--source='$RepositoryUrl'", "--api-key='$NuGetApiKey'")
214210
& choco @chocoArgs
215211

212+
Update-JsonFile -Path "$env:SystemDrive\choco-setup\logs\nexus.json" -Properties @{
213+
NexusUri = "https://$($SubjectWithoutCn):8443"
214+
NexusRepo = $RepositoryUrl
215+
ChocoUserPassword = $NexusPw
216+
}
217+
216218
<# Jenkins #>
217219
$JenkinsHome = "C:\ProgramData\Jenkins\.jenkins"
218220

@@ -227,6 +229,10 @@ process {
227229
# Add firewall rule for Jenkins
228230
netsh advfirewall firewall add rule name="Jenkins-7443" dir=in action=allow protocol=tcp localport=7443
229231

232+
Update-JsonFile -Path "$env:SystemDrive\choco-setup\logs\jenkins.json" -Properties @{
233+
JenkinsUri = "https://$($SubjectWithoutCn):7443"
234+
}
235+
230236
<# CCM #>
231237
# Remove old CCM web binding, and add new CCM web binding
232238
Stop-CcmService
@@ -309,6 +315,13 @@ Invoke-Expression (`$downloader.DownloadString("http://`$(`$HostName):80/Import-
309315
}
310316
}
311317

318+
Update-JsonFile -Path "$env:SystemDrive\choco-setup\logs\ccm.json" -Properties @{
319+
CCMWebPortal = "https://$($SubjectWithoutCn)/Account/Login"
320+
CCMServiceURL = "https://$($SubjectWithoutCn):24020/ChocolateyManagementService"
321+
ServiceSalt = $ServiceSaltValue
322+
ClientSalt = $ClientSaltValue
323+
}
324+
312325
# Save useful params to JSON
313326
$SslJson = @{
314327
CertSubject = $SubjectWithoutCn

scripts/Get-Helpers.ps1

Lines changed: 51 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,6 +1839,22 @@ function Invoke-TextReplacementInFile {
18391839
}
18401840
}
18411841

1842+
function Update-JsonFile {
1843+
[CmdletBinding()]
1844+
param(
1845+
[Parameter(Mandatory)]
1846+
[string]$Path,
1847+
1848+
[Parameter(Mandatory)]
1849+
[hashtable]$Properties
1850+
)
1851+
$Json = Get-Content -Path $Path | ConvertFrom-Json
1852+
$Properties.GetEnumerator().ForEach{
1853+
Add-Member -InputObject $Json -MemberType NoteProperty -Name $_.Key -Value $_.Value -Force
1854+
}
1855+
$Json | ConvertTo-Json | Set-Content -Path $Path
1856+
}
1857+
18421858
function Set-JenkinsCertificate {
18431859
<#
18441860
.Synopsis
@@ -1887,8 +1903,8 @@ function Set-JenkinsCertificate {
18871903
$CurrentAlias = ($($CertificatePassword.Password | & $KeyTool -list -v -storetype PKCS12 -keystore $CertificatePath) -match "^Alias.*").Split(':')[1].Trim()
18881904
Write-Host "" # Adds a newline, after this command has finished.
18891905

1890-
$null = & $KeyTool -importkeystore -srckeystore $CertificatePath -srcstoretype PKCS12 -srcstorepass $CertificatePassword.Password -destkeystore $KeyStore -deststoretype JKS -alias $currentAlias -destalias jetty -deststorepass $Passkey *>&1
1891-
$null = & $KeyTool -keypasswd -keystore $KeyStore -alias jetty -storepass $Passkey -keypass $CertificatePassword.Password -new $Passkey *>&1
1906+
$null = & $KeyTool -importkeystore -srckeystore $CertificatePath -srcstoretype PKCS12 -srcstorepass $CertificatePassword.Password -destkeystore $KeyStore -deststoretype JKS -alias $currentAlias -destalias jetty -deststorepass $Passkey
1907+
$null = & $KeyTool -keypasswd -keystore $KeyStore -alias jetty -storepass $Passkey -keypass $CertificatePassword.Password -new $Passkey
18921908
} finally {
18931909
# Clean up the exported certificate
18941910
Remove-Item $CertificatePath
@@ -1955,83 +1971,42 @@ The host name of the C4B instance.
19551971
./New-QuickstartReadme.ps1 -HostName c4b.example.com
19561972
#>
19571973
[CmdletBinding()]
1958-
Param(
1959-
[Parameter()]
1960-
[string]
1961-
$HostName = $(Get-Content "$env:SystemDrive\choco-setup\logs\ssl.json" | ConvertFrom-Json).CertSubject
1962-
1963-
)
1964-
1965-
1974+
param()
19661975
process {
1967-
$nexusPassword = if (Test-Path "$env:SystemDrive\choco-setup\logs\nexus.json") {
1968-
(Get-Content "$env:SystemDrive\choco-setup\logs\nexus.json" | ConvertFrom-Json).NexusPw
1969-
} elseif (Test-Path 'C:\ProgramData\sonatype-work\nexus3\admin.password') {
1970-
Get-Content 'C:\ProgramData\sonatype-work\nexus3\admin.password'
1976+
try {
1977+
$CCMData = Get-Content "$env:SystemDrive\choco-setup\logs\ccm.json" | ConvertFrom-Json
1978+
$NexusData = Get-Content "$env:SystemDrive\choco-setup\logs\nexus.json" | ConvertFrom-Json
1979+
$JenkinsData = Get-Content "$env:SystemDrive\choco-setup\logs\jenkins.json" | ConvertFrom-Json
1980+
} catch {
1981+
Write-Error "Unable to read JSON files. Ensure the Quickstart Guide has been completed."
19711982
}
1972-
$jenkinsPassword = (Get-Content "$env:SystemDrive\choco-setup\logs\jenkins.json" | ConvertFrom-Json).JenkinsPw
1973-
$nexusApiKey = (Get-Content "$env:SystemDrive\choco-setup\logs\nexus.json" | ConvertFrom-Json).NuGetApiKey
1974-
1975-
$tableData = @([pscustomobject]@{
1976-
Name = 'Nexus'
1977-
Url = "https://${HostName}:8443"
1978-
Username = "admin"
1979-
Password = $nexusPassword
1980-
ApiKey = $nexusApiKey
1981-
},
1982-
[pscustomobject]@{
1983-
Name = 'Central Management'
1984-
Url = "https://${HostName}"
1985-
Username = "ccmadmin"
1986-
Password = '123qwe'
1987-
},
1988-
[PSCustomObject]@{
1989-
Name = 'Jenkins'
1990-
Url = "https://${HostName}:7443"
1991-
Username = "admin"
1992-
Password = $jenkinsPassword
1993-
}
1994-
)
1995-
1996-
1997-
$html = @"
1998-
<html>
1999-
<head>
2000-
</head>
2001-
<title>Chocolatey For Business Service Defaults</title>
2002-
<style>
2003-
table {
2004-
border-collapse: collapse;
2005-
}
2006-
td,
2007-
th {
2008-
border: 0.1em solid rgba(0, 0, 0, 0.5);
2009-
padding: 0.25em 0.5em;
2010-
text-align: center;
2011-
}
2012-
blockquote {
2013-
margin-left: 0.5em;
2014-
padding-left: 0.5em;
2015-
border-left: 0.1em solid rgba(0, 0, 0, 0.5);
2016-
}</style>
2017-
<body>
2018-
<blockquote>
2019-
<p><strong>Note</strong></p>
2020-
<p>The following table provides the default credentials to login to each of the services made available as part of the Quickstart Guide setup process.</p>
2021-
You'll be asked to change the credentials upon logging into each service for the first time.
2022-
Document your new credentials in a password manager, or whatever system you use.
2023-
</p>
2024-
</blockquote>
2025-
$(($TableData | ConvertTo-Html -Fragment))
2026-
</body>
2027-
</html>
2028-
"@
2029-
2030-
$folder = Join-Path $env:Public 'Desktop'
2031-
$file = Join-Path $folder 'README.html'
2032-
2033-
$html | Set-Content $file
20341983

1984+
Copy-Item $PSScriptRoot\ReadmeTemplate.html.j2 -Destination $env:Public\Desktop\Readme.html -Force
1985+
1986+
# Working around the existing j2 template, so we can keep them roughly in sync
1987+
Invoke-TextReplacementInFile -Path $env:Public\Desktop\Readme.html -Replacement @{
1988+
# CCM Values
1989+
"{{ ccm_fqdn .*?}}" = ([uri]$CCMData.CCMWebPortal).DnsSafeHost
1990+
"{{ ccm_port .*?}}" = ([uri]$CCMData.CCMWebPortal).Port
1991+
"{{ ccm_password .*?}}" = [System.Web.HttpUtility]::HtmlEncode($CCMData.DefaultPwToBeChanged)
1992+
1993+
# Chocolatey Configuration Values
1994+
"{{ ccm_encryption_password .*?}}" = "Requested on first run."
1995+
"{{ ccm_client_salt .*?}}" = [System.Web.HttpUtility]::HtmlEncode($CCMData.ClientSalt)
1996+
"{{ ccm_service_salt .*?}}" = [System.Web.HttpUtility]::HtmlEncode($CCMData.ServiceSalt)
1997+
"{{ chocouser_password .*?}}" = [System.Web.HttpUtility]::HtmlEncode($NexusData.ChocoUserPassword)
1998+
1999+
# Nexus Values
2000+
"{{ nexus_fqdn .*?}}" = ([uri]$NexusData.NexusUri).DnsSafeHost
2001+
"{{ nexus_port .*?}}" = ([uri]$NexusData.NexusUri).Port
2002+
"{{ nexus_password .*?}}" = [System.Web.HttpUtility]::HtmlEncode($NexusData.NexusPw)
2003+
"{{ lookup\('file', 'credentials\/nexus_apikey'\) .*?}}" = $NexusJson.NuGetApiKey
2004+
2005+
# Jenkins Values
2006+
"{{ jenkins_fqdn .*?}}" = ([uri]$JenkinsData.JenkinsUri).DnsSafeHost
2007+
"{{ jenkins_port .*?}}" = ([uri]$JenkinsData.JenkinsUri).Port
2008+
"{{ jenkins_password .*?}}" = [System.Web.HttpUtility]::HtmlEncode($JenkinsData.JenkinsPw)
2009+
}
20352010
}
20362011
}
20372012
#endregion

0 commit comments

Comments
 (0)