-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathInitialize-C4bSetup.ps1
More file actions
238 lines (206 loc) · 10.4 KB
/
Initialize-C4bSetup.ps1
File metadata and controls
238 lines (206 loc) · 10.4 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<#
.SYNOPSIS
C4B Quick-Start Guide initial bootstrap script
.DESCRIPTION
- Performs the following C4B Server initial bootstrapping
- Install of Chocolatey
- Prompt for C4B license, with validation
- Script to help turn your C4B license into a Chocolatey package
- Setup of local `choco-setup` directories
- Download of Chocolatey packages required for setup
#>
[CmdletBinding(DefaultParameterSetName = 'Prepare')]
param(
# Full path to Chocolatey license file.
# Accepts any file, and moves and renames it correctly.
# You can either define this as a parameter, or
# script will prompt you for it.
# Script will also validate expiry.
[Parameter(ParameterSetName = 'Install')]
[string]
$LicenseFile = $(
if (Test-Path $PSScriptRoot\files\chocolatey.license.xml) {
# Offline setup has been run, we should use that license.
Join-Path $PSScriptRoot "files\chocolatey.license.xml"
} elseif (Test-Path $env:ChocolateyInstall\license\chocolatey.license.xml) {
# Chocolatey is already installed, we can use that license.
Join-Path $env:ChocolateyInstall "license\chocolatey.license.xml"
} else {
# Prompt the user for the license.
$Wshell = New-Object -ComObject Wscript.Shell
$null = $Wshell.Popup('You will need to provide the license file location. Please select your Chocolatey License in the next file dialog.')
$null = [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = "$env:USERPROFILE\Downloads"
$OpenFileDialog.filter = 'All Files (*.*)| *.*'
$null = $OpenFileDialog.ShowDialog()
$OpenFileDialog.filename
}
),
# Specify a credential used for the ChocolateyManagement DB user.
# Only required in install mode for the CCM setup script.
# If not populated, the script will prompt for credentials.
[Parameter(ParameterSetName = 'Install')]
[System.Management.Automation.PSCredential]
$DatabaseCredential = $(
if ((Test-Path C:\choco-setup\clixml\chocolatey-for-business.xml) -and (Import-Clixml C:\choco-setup\clixml\chocolatey-for-business.xml).DatabaseUser) {
(Import-Clixml C:\choco-setup\clixml\chocolatey-for-business.xml).DatabaseUser
} else {
[PSCredential]::new(
"chocodbuser",
(ConvertTo-SecureString "$(New-Guid)-$(New-Guid)" -Force -AsPlainText)
)
}
),
# The certificate thumbprint that identifies the target SSL certificate in
# the local machine certificate stores.
# Only used in install mode for the SSL setup script.
[Parameter(ParameterSetName = 'Install')]
[ArgumentCompleter({
Get-ChildItem Cert:\LocalMachine\TrustedPeople | ForEach-Object {
[System.Management.Automation.CompletionResult]::new(
$_.Thumbprint,
$_.Thumbprint,
"ParameterValue",
($_.Subject -replace "^CN=(?<FQDN>.+),?.*$", '${FQDN}')
)
}
})]
[string]
$Thumbprint = $(
if ((Test-Path C:\choco-setup\clixml\chocolatey-for-business.xml) -and (Import-Clixml C:\choco-setup\clixml\chocolatey-for-business.xml).CertThumbprint) {
(Import-Clixml C:\choco-setup\clixml\chocolatey-for-business.xml).CertThumbprint
} else {
Get-ChildItem Cert:\LocalMachine\TrustedPeople -Recurse | Sort-Object {
$_.Issuer -eq $_.Subject # Prioritise any certificates above self-signed
} | Select-Object -ExpandProperty Thumbprint -First 1
}
),
# If using a wildcard certificate, provide a DNS name you want to use to access services secured by the certificate.\
[Parameter(ParameterSetName = 'Install')]
[Alias("FQDN")]
[string]
$CertificateDnsName = $(
if ((Test-Path C:\choco-setup\clixml\chocolatey-for-business.xml) -and (Import-Clixml C:\choco-setup\clixml\chocolatey-for-business.xml).CertSubject) {
(Import-Clixml C:\choco-setup\clixml\chocolatey-for-business.xml).CertSubject
}
),
# If provided, shows all Chocolatey output. Otherwise, blissful quiet.
[switch]
$ShowChocoOutput,
# The branch or Pull Request to download the C4B setup scripts from.
# Defaults to main.
[Alias('PR')]
[string]
$Branch = $env:CHOCO_QSG_BRANCH,
# If provided, will skip launching the browser at the end of setup.
[Parameter(ParameterSetName = 'Install')]
[switch]$SkipBrowserLaunch
)
if ($ShowChocoOutput) {
$global:PSDefaultParameterValues["Invoke-Choco:InformationAction"] = "Continue"
}
$QsRepo = if ($Branch) {
if ((Invoke-RestMethod -Uri "https://api.github.com/repos/chocolatey/choco-quickstart-scripts/branches").name -contains $Branch) {
"https://api.github.com/repos/chocolatey/choco-quickstart-scripts/zipball/$Branch"
} elseif ($PullRequest = Invoke-RestMethod -Uri "https://api.github.com/repos/chocolatey/choco-quickstart-scripts/pulls/$Branch" -ErrorAction SilentlyContinue) {
$PullRequest.head.repo.archive_url -replace '{archive_format}', 'zipball' -replace '{/ref}', "/$($PullRequest.head.ref)"
} else {
Write-Error "'$($Branch)' is not a valid branch or pull request number. Please provide a valid branch or pull request number."
}
} else {
"https://api.github.com/repos/chocolatey/choco-quickstart-scripts/zipball/main"
}
$DefaultEap, $ErrorActionPreference = $ErrorActionPreference, 'Stop'
Start-Transcript -Path "$env:SystemDrive\choco-setup\logs\Initialize-C4bSetup-$(Get-Date -Format 'yyyyMMdd-HHmmss').txt"
try {
# Setup initial choco-setup directories
Write-Host "Setting up initial directories in"$env:SystemDrive\choco-setup"" -ForegroundColor Green
$ChocoPath = "$env:SystemDrive\choco-setup"
$FilesDir = Join-Path $ChocoPath "files"
$PkgsDir = Join-Path $FilesDir "files"
$TempDir = Join-Path $ChocoPath "temp"
$TestDir = Join-Path $ChocoPath "tests"
$xmlDir = Join-Path $ChocoPath "clixml"
@($ChocoPath, $FilesDir, $PkgsDir, $TempDir, $TestDir, $xmlDir) | ForEach-Object {
$null = New-Item -Path $_ -ItemType Directory -Force -ErrorAction Stop
}
if (-not $PSScriptRoot -or $PSScriptRoot -ne $FilesDir) {
# Download and extract C4B setup files from repo
try {
Invoke-WebRequest -Uri $QsRepo -UseBasicParsing -OutFile "$TempDir\choco-quickstart-scripts.zip"
Expand-Archive "$TempDir\choco-quickstart-scripts.zip" $TempDir
Copy-Item "$TempDir\*\*" $FilesDir -Recurse -Force
} finally {
Remove-Item "$TempDir" -Recurse -Force
}
}
# Add the Module Path and Import Helper Functions
if (-not (Get-Module C4B-Environment -ListAvailable)) {
if ($env:PSModulePath.Split(';') -notcontains "$FilesDir\modules") {
[Environment]::SetEnvironmentVariable("PSModulePath", "$env:PSModulePath;$FilesDir\modules" , "Machine")
$env:PSModulePath = [Environment]::GetEnvironmentVariables("Machine").PSModulePath
}
}
Import-Module C4B-Environment -Verbose:$false
# Downloading all CCM setup packages below
Write-Host "Downloading missing nupkg files to $($PkgsDir)." -ForegroundColor Green
Write-Host "This will take some time. Feel free to get a tea or coffee." -ForegroundColor Green
& $FilesDir\OfflineInstallPreparation.ps1 -LicensePath $LicenseFile
# Kick off unattended running of remaining setup scripts, if we're running from a saved-script.
if ($PSScriptRoot -or $PSCmdlet.ParameterSetName -eq 'Install') {
Update-Clixml -Properties @{
InitialDeployment = Get-Date
}
if ($Thumbprint) {
Set-ChocoEnvironmentProperty CertThumbprint $Thumbprint
if ($CertificateDnsName) {
Set-ChocoEnvironmentProperty CertSubject $CertificateDnsName
}
# Collect current certificate configuration
$Certificate = Get-Certificate -Thumbprint $Thumbprint
Copy-CertToStore -Certificate $Certificate
$null = Test-CertificateDomain -Thumbprint $Thumbprint
} elseif ($PSScriptRoot) {
# We're going to be using a self-signed certificate
if (-not $CertificateDnsName) {
$CertificateDnsName = $env:ComputerName
}
$CertificateArgs = @{
CertStoreLocation = "Cert:\LocalMachine\My"
KeyUsage = "KeyEncipherment", "DigitalSignature"
DnsName = $CertificateDnsName
NotAfter = (Get-Date).AddYears(10)
}
$Certificate = New-SelfSignedCertificate @CertificateArgs
Copy-CertToStore -Certificate $Certificate
$Thumbprint = $Certificate.Thumbprint
Set-ChocoEnvironmentProperty CertThumbprint $Thumbprint
Set-ChocoEnvironmentProperty CertSubject $CertificateDnsName
}
if ($DatabaseCredential) {
Set-ChocoEnvironmentProperty DatabaseUser $DatabaseCredential
}
if (Test-Path $FilesDir\files\*.nupkg) {
Invoke-Choco source add --name LocalChocolateySetup --source $FilesDir\files\ --Priority 1
}
# Set Choco Server Chocolatey Configuration
Invoke-Choco feature enable --name="'excludeChocolateyPackagesDuringUpgradeAll'"
Invoke-Choco feature enable --name="'usePackageHashValidation'"
# Convert license to a "choco-license" package, and install it locally to test
Write-Host "Creating a 'chocolatey-license' package, and testing install." -ForegroundColor Green
Set-Location $FilesDir
.\scripts\Create-ChocoLicensePkg.ps1
Remove-Item "$env:SystemDrive\choco-setup\packaging" -Recurse -Force
$Certificate = @{}
if ($Thumbprint) { $Certificate.Thumbprint = $Thumbprint }
Set-Location "$env:SystemDrive\choco-setup\files"
.\Start-C4BNexusSetup.ps1 @Certificate
.\Start-C4bCcmSetup.ps1 @Certificate -DatabaseCredential $DatabaseCredential
.\Start-C4bJenkinsSetup.ps1 @Certificate
Complete-C4bSetup -SkipBrowserLaunch:$SkipBrowserLaunch
}
} finally {
$ErrorActionPreference = $DefaultEap
Stop-Transcript
}