-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOfflineInstallPreparation.ps1
141 lines (126 loc) · 5.41 KB
/
OfflineInstallPreparation.ps1
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
<#
.Synopsis
Prepares the repository for an offline deployment.
.Description
These playbooks can be run from a network without access to the internet,
but it needs to prepare packages to be run offline.
This script downloads and internalizes packages for such usage.
.Notes
This must be run on a Windows system with access to the internet because
it uses Chocolatey for Business' Package Internalizer.
.Notes
Instead of using this script, you can internalize all required packages manually,
zip them, and drop them in the files directory as shown below.
.Example
.\OfflineInstallPreparation.ps1 -LicensePath C:\ProgramData\chocolatey\license\chocolatey.license.xml
#>
[CmdletBinding()]
param(
[ValidateScript({
if (-not (Test-Path (Convert-Path $_))) {
throw "License file does not exist at '$($_)'. Please provide a valid -LicensePath"
}
$true
})]
[string]$LicensePath = "C:\ProgramData\chocolatey\license\chocolatey.license.xml",
[ValidateScript({
if (-not (Test-Path (Convert-Path $_))) {
throw "Certificate file does not exist at '$($_)'. Please provide a valid -CertificatePath"
}
$true
})]
[Parameter(Mandatory)]
[string]$CertificatePath,
[Parameter(Mandatory)]
[securestring]$CertificatePassword,
[string]$WorkingDirectory = $(Join-Path $env:Temp "choco-offline")
)
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
$LicensePath = Convert-Path $LicensePath
$CertificatePath = Convert-Path $CertificatePath
# Validate License
try {
[xml]$License = Get-Content $LicensePath
$Expiry = Get-Date $License.license.expiration
if (-not $Expiry -or $Expiry -lt (Get-Date)) {throw}
} catch {
throw "License '$($LicensePath)' is not valid.$(if ($Expiry) {" It expired at '$($Expiry)'."})"
}
# Validate Certificate and Password
try {
$null = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new(
$CertificatePath,
$CertificatePassword,
"EphemeralKeySet"
)
} catch {
throw "Certificate '$($CertificatePath)' failed to import with the provided CertificatePassword. Please ensure the Certificate Path and Password are correct."
}
if (-not (Get-Command choco.exe)) {
Get-Content $PSScriptRoot\templates\ChocolateyInstall.ps1.j2 | Invoke-Expression
}
# Initialize environment, ensure Chocolatey For Business, etc.
$Licensed = ($($(choco)[0] -match "^Chocolatey (?<Version>\S+)\s*(?<LicenseType>Business)?$") -and $Matches.LicenseType)
$InstalledLicensePath = "$env:ChocolateyInstall\license\chocolatey.license.xml"
if (-not $Licensed) {
if (-not (Test-Path $InstalledLicensePath)) {
if (-not (Test-Path $env:ChocolateyInstall\license)) {
$null = New-Item $env:ChocolateyInstall\license -ItemType Directory
}
Copy-Item $LicensePath $InstalledLicensePath -Force
}
choco install chocolatey.extension --source https://licensedpackages.chocolatey.org/api/v2/ --confirm
}
# Download each set of packages to the output directories
$PackageWorkingDirectory = Join-Path $WorkingDirectory "Packages"
if (-not (Test-Path $PackageWorkingDirectory)) {
$null = New-Item -Path $PackageWorkingDirectory -ItemType Directory -Force
}
foreach ($Package in (Get-Content $PSScriptRoot\files\chocolatey.json | ConvertFrom-Json).packages) {
$ChocoArgs = @(
"download", "$($Package.Name)"
"--output-directory", $PackageWorkingDirectory
)
$ChocoArgs += switch ($Package.Keys) {
"Version" { "--version", $Package.Version }
"Args" { $Package.Args }
}
if ($Package.Internalize -or $Package.PSObject.Properties.Name -notcontains "Internalize") {
$ChocoArgs += "--internalize" # Default to internalizing
}
try {
if (-not (Test-Path "$($PackageWorkingDirectory)\$($Package.Name)*.nupkg") -and -not (Test-Path "$PSScriptRoot\files\$($Package.Name)*.nupkg")) {
Write-Verbose "Downloading '$($Package.Name)'"
$Output = choco @ChocoArgs
if ($LASTEXITCODE -ne 0) {
$Output
}
}
} catch {
throw $_
}
}
Move-Item -Path $PackageWorkingDirectory\*.nupkg -Destination $PSScriptRoot\files\
# Jenkins Plugins
$PluginsWorkingDirectory = Join-Path $WorkingDirectory "JenkinsPlugins"
if (-not (Test-Path $PluginsWorkingDirectory)) {
$null = New-Item -Path $PluginsWorkingDirectory -ItemType Directory -Force
}
$ProgressPreference = "Ignore"
foreach ($Plugin in (Get-Content $PSScriptRoot\files\jenkins.json | ConvertFrom-Json).plugins) {
$RestArgs = @{
Uri = "https://updates.jenkins-ci.org/latest/$($Plugin.Name).hpi"
OutFile = Join-Path $PluginsWorkingDirectory "$($Plugin.Name).hpi"
}
if ($Plugin.Version -and $Plugin.Version -ne 'latest') {
$RestArgs.Uri = "https://updates.jenkins-ci.org/download/plugins/$($Plugin.Name)/$($Plugin.Version)/$($Plugin.Name).hpi"
}
if (-not (Test-Path $RestArgs.OutFile)) {
Invoke-WebRequest @RestArgs -UseBasicParsing
}
}
Compress-Archive -Path $PluginsWorkingDirectory\* -Destination $PSScriptRoot\files\JenkinsPlugins.zip -Force
# License and Certificate
Copy-Item -Path (Convert-Path $LicensePath) -Destination $PSScriptRoot\files\chocolatey.license.xml
Copy-Item -Path (Convert-Path $CertificatePath) -Destination $PSScriptRoot\files\certificate.pfx