-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchocolateyInstall.ps1
331 lines (283 loc) · 11.7 KB
/
chocolateyInstall.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
$PackageName = 'neo4j-community'
# Per-package parameters
$downloadUrl = 'http://neo4j.com/artifact.php?name=neo4j-community-3.2.3-windows.zip'
$md5Checksum = 'b0c6bb8446f25a03fea3a54d29c22173'
$neozipSubdir = 'neo4j-community-3.2.3'
# major.minor.update.build
# Build is always 14
$privateJavaVersion = "8.0.131.11"
$privateJreChecksumMD5 = "9458b62000daac0f48155323185f1c4c"
# START Helper Functions
Function Get-IsJavaInstalled
{
[cmdletBinding(SupportsShouldProcess=$false,ConfirmImpact='Low',DefaultParameterSetName='Default')]
param ()
Process
{
$javaPath = ''
$javaVersion = ''
$javaCMD = ''
$EnvJavaHome = "$($Env:JAVA_HOME)"
# Is JAVA specified in an environment variable
if (($javaPath -eq '') -and ($EnvJavaHome -ne $null))
{
$javaPath = $EnvJavaHome
# Modify the java path if a JRE install is detected
if (Test-Path -Path "$javaPath\bin\javac.exe") { $javaPath = "$javaPath\jre" }
}
# Attempt to find Java in registry
$regKey = 'Registry::HKLM\SOFTWARE\JavaSoft\Java Runtime Environment'
if (($javaPath -eq '') -and (Test-Path -Path $regKey))
{
$javaVersion = ''
try
{
$javaVersion = [string](Get-ItemProperty -Path $regKey -ErrorAction 'Stop').CurrentVersion
if ($javaVersion -ne '')
{
$javaPath = [string](Get-ItemProperty -Path "$regKey\$javaVersion" -ErrorAction 'Stop').JavaHome
}
}
catch
{
#Ignore any errors
$javaVersion = ''
$javaPath = ''
}
}
# Attempt to find Java in registry (32bit Java on 64bit OS)
$regKey = 'Registry::HKLM\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment'
if (($javaPath -eq '') -and (Test-Path -Path $regKey))
{
$javaVersion = ''
try
{
$javaVersion = [string](Get-ItemProperty -Path $regKey -ErrorAction 'Stop').CurrentVersion
if ($javaVersion -ne '')
{
$javaPath = [string](Get-ItemProperty -Path "$regKey\$javaVersion" -ErrorAction 'Stop').JavaHome
}
}
catch
{
#Ignore any errors
$javaVersion = ''
$javaPath = ''
}
}
# Attempt to find Java in the search path
if ($javaPath -eq '')
{
$javaExe = (Get-Command 'java.exe' -ErrorAction SilentlyContinue)
if ($javaExe -ne $null)
{
$javaCMD = $javaExe.Path
$javaPath = Split-Path -Path $javaCMD -Parent
}
}
if ($javaPath -eq '') { Write-Host "Unable to determine the path to java.exe"; return $false }
if ($javaCMD -eq '') { $javaCMD = "$javaPath\bin\java.exe" }
if (-not (Test-Path -Path $javaCMD)) { Write-Error "Could not find java at $javaCMD"; return $false }
return $true
}
}
function Invoke-ModifyConfig($File,$Key,$Value) {
Write-Verbose "Setting $($Key)=$($Value) in file $File"
$RegexKey = $Key.Replace('.','\.')
$fileContent = [IO.File]::ReadAllText($File)
$found = $false
if ($fileContent -match "(?mi)^$($RegexKey)=") {
Write-Verbose "Found $Key"
$fileContent = $fileContent -replace "(?mi)^$($RegexKey)=.+$","$($Key)=$($Value)"
$found = $true
}
if ($fileContent -match "(?mi)^#$($RegexKey)=") {
Write-Verbose "Found $Key in a comment"
$fileContent = $fileContent -replace "(?mi)^#$($RegexKey)=.+$","$($Key)=$($Value)"
$found = $true
}
if (-not $found) {
Write-Verbose "Adding $key"
$fileContent += "`n$($Key)=$($Value)"
}
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
[IO.File]::WriteAllText($File,$fileContent,$Utf8NoBomEncoding) | Out-NUll
}
function Invoke-InstallPrivateJRE($Destination) {
# Adpated from the server-jre8 chocolatey package
# https://github.com/rgra/choco-packages/tree/master/server-jre8
Write-Host "Installing Server JRE $privateJavaVersion to $Destination"
#8.0.xx to jdk1.8.0_xx
$versionArray = $privateJavaVersion.Split(".")
$majorVersion = $versionArray[0]
$minorVersion = $versionArray[1]
$updateVersion = $versionArray[2]
$buildNumber = $versionArray[3]
$folderVersion = "jdk1.$majorVersion.$($minorVersion)_$updateVersion"
$fileNameBase = "server-jre-$($majorVersion)u$($updateVersion)-windows-x64"
$fileName = "$fileNameBase.tar.gz"
$url = "http://download.oracle.com/otn-pub/java/jdk/$($majorVersion)u$($updateVersion)-b$buildNumber/d54c1d3a095b4ff2b6607d096fa80163/$fileName"
# Download location info
$tempDir = Join-Path -Path $ENV:Temp -ChildPath "choco_jre_$PackageName"
$tarGzFile = "$tempDir\$fileName"
$tarFile = "$tempDir\$fileNameBase.tar"
# Cleanup
if (Test-Path -Path $tempDir) { Remove-Item -Path $tempDir -Force -Recurse -Confirm:$false | Out-Null }
New-Item -Path $tempDir -ItemType 'Directory' | Out-Null
$webClient = New-Object System.Net.WebClient
$result = $webClient.headers.Add('Cookie','gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie')
Write-Host "Downloading $url ..."
$result = $webClient.DownloadFile($url, $tarGzFile)
Get-ChecksumValid $tarGzFile $privateJreChecksumMD5 | Out-Null
#Extract gz to .tar File
Get-ChocolateyUnzip $tarGzFile $tempDir | Out-Null
#Extract tar to destination
Get-ChocolateyUnzip $tarFile $Destination | Out-Null
# Cleanup
if (Test-Path -Path $tempDir) { Remove-Item -Path $tempDir -Force -Recurse -Confirm:$false | Out-Null }
# return the JAVA_HOME path
Write-Output (Get-ChildItem -Path $Destination | Select -First 1).FullName
}
# END Helper Functions
try {
# Taken from https://github.com/chocolatey/chocolatey/wiki/How-To-Parse-PackageParameters-Argument
$arguments = @{};
# Now, we can use the $env:chocolateyPackageParameters inside the Chocolatey package
$packageParameters = $env:chocolateyPackageParameters;
# Default the install root
try {
$InstallDir = Get-ToolsLocation -ErrorAction Stop
} catch {
# On older chocolatey versions Get-ToolsLocation may not exist as a function.
# Fall back to Get-BinRoot
$InstallDir = Get-BinRoot
}
# Default the values
$InstallDir = Join-Path -Path $InstallDir -ChildPath $PackageName
$ImportNeoProperties = ""
$ImportServiceProperties = ""
$WindowsServiceName = ""
$HTTPEndpoint = ""
$HTTPSEndpoint = ""
# Now, let’s parse the packageParameters using good old regular expression
if($packageParameters) {
$MATCH_PATTERN = "\/([a-zA-Z]+):([`"'])?([a-zA-Z0-9- _\\:\.]+)([`"'])?"
$PARAMATER_NAME_INDEX = 1
$VALUE_INDEX = 3
if($packageParameters -match $MATCH_PATTERN ){
$results = $packageParameters | Select-String $MATCH_PATTERN -AllMatches
$results.matches | % {
$arguments.Add(
$_.Groups[$PARAMATER_NAME_INDEX].Value.Trim(),
$_.Groups[$VALUE_INDEX].Value.Trim())
}
}
else
{
Throw "Package Parameters were found but were invalid (REGEX Failure)";
}
if($arguments.ContainsKey("install")) {
Write-Verbose "Install Argument Found";
$InstallDir = $arguments["install"];
}
if($arguments.ContainsKey("importneoproperties")) {
Write-Verbose "ImportNeoProperties Argument Found";
$ImportNeoProperties = $arguments["importneoproperties"];
}
if($arguments.ContainsKey("importserviceproperties")) {
Write-Verbose "ImportServiceProperties Argument Found";
$ImportServiceProperties = $arguments["importserviceproperties"];
}
if($arguments.ContainsKey("servicename")) {
Write-Verbose "ServiceName Argument Found";
$WindowsServiceName = $arguments["servicename"];
}
if($arguments.ContainsKey("httpendpoint")) {
Write-Verbose "HTTPEndPoint Argument Found";
$HTTPEndpoint = $arguments["httpendpoint"];
}
if($arguments.ContainsKey("httpsendpoint")) {
Write-Verbose "HTTPSEndpoint Argument Found";
$HTTPSEndpoint = $arguments["httpsendpoint"];
}
} else {
Write-Verbose "No Package Parameters Passed in";
}
$silentArgs = "/install:" + $InstallDir
if ($WindowsServiceName -ne "") {
$silentArgs += " /servicename:" + $WindowsServiceName
}
if ($HTTPEndpoint -ne "") {
$silentArgs += " /httpendpoint:" + $HTTPEndpoint
}
if ($HTTPSEndpoint -ne "") {
$silentArgs += " /httpsendpoint:" + $HTTPSEndpoint
}
if ($ImportNeoProperties -ne "") {
$silentArgs += " /importneoproperties:" + $ImportNeoProperties
}
if ($ImportServiceProperties -ne "") {
$silentArgs += " /importserviceproperties:" + $ImportServiceProperties
}
Write-Verbose "This would be the Chocolatey Silent Arguments: $silentArgs"
# Sanity Checks
If ($ImportNeoProperties -ne "") {
If (!(Test-Path -Path $ImportNeoProperties)) { Throw "Could not find the NeoProperties file to import. $ImportNeoProperties" }
}
If ($ImportServiceProperties -ne "") {
If (!(Test-Path -Path $ImportServiceProperties)) { Throw "Could not find the ServiceProperties file to import. $ImportServiceProperties" }
}
# Install Neo4j
Install-ChocolateyZipPackage -PackageName $PackageName -URL $downloadUrl -UnzipLocation $InstallDir -CheckSum $md5Checksum -CheckSumType 'md5'
$neoHome = "$($InstallDir)\$($neozipSubdir)"
Install-ChocolateyEnvironmentVariable "NEO4J_HOME" "$neoHome" "Machine"
# Import config files if required
If ($ImportNeoProperties -ne "") {
Write-Verbose "Importing the neo4j.conf from $ImportNeoProperties"
[void] (Copy-Item -Path $ImportNeoProperties -Destination "$($neoHome)\conf\neo4j.conf" -Force -Confirm:$false)
}
If ($ImportServiceProperties -ne "") {
Write-Verbose "Importing the neo4j-wrapper.conf from $ImportServiceProperties"
[void] (Copy-Item -Path $ImportServiceProperties -Destination "$($neoHome)\conf\neo4j-wrapper.conf" -Force -Confirm:$false)
}
# Override with package params
if ($HTTPEndpoint -ne "") {
Invoke-ModifyConfig -File "$($neoHome)\conf\neo4j.conf" -Key "dbms.connector.http.address" -Value $HTTPEndpoint | Out-Null
}
if ($HTTPSEndpoint -ne "") {
Invoke-ModifyConfig -File "$($neoHome)\conf\neo4j.conf" -Key "dbms.connector.https.address" -Value $HTTPSEndpoint | Out-Null
}
if ($WindowsServiceName -ne "") {
Invoke-ModifyConfig -File "$($neoHome)\conf\neo4j-wrapper.conf" -Key "dbms.windows_service_name" -Value $WindowsServiceName | Out-Null
}
# Check if Java is available
# This check will not be required once a suitable Java SDK 8 chocolatey package is available in the public feed.
if (-not (Get-IsJavaInstalled) ) {
Write-Host "Java was not detected. Installing a private JRE for Neo4j"
$privatePath = Invoke-InstallPrivateJRE -Destination "$($neoHome)\java"
Write-Host "--------------------"
Write-Host "Before using Neo4j tools, ensure you have set the JAVA_HOME"
Write-Host "environment variable to $($privatePath)"
Write-Host ""
Write-Host "For example, in a command prompt:"
Write-Host "SET JAVA_HOME=$($privatePath)"
Write-Host ""
Write-Host "For example, in a PowerShell console:"
Write-Host "`$ENV:JAVA_HOME = '$($privatePath)'"
Write-Host ""
Write-Host "--------------------"
$ENV:JAVA_HOME = $privatePath
}
# Install the Neo4j Service
$InstallBatch = "$($neoHome)\bin\Neo4j.bat"
if (!(Test-Path $InstallBatch)) { throw "Could not find the Neo4j Installer Batch file at $InstallBatch" }
Write-Verbose "Installing Neo4j Service..."
$args = "install-service"
$result = Start-Process -FilePath $InstallBatch -ArgumentList $args -Wait -PassThru -NoNewWindow
if ($result.ExitCode -ne 0) { Throw "Neo4j installation returned exit code $($result.ExitCode)"}
Write-Verbose "Starting Neo4j Service..."
$args = "start"
$result = Start-Process -FilePath $InstallBatch -ArgumentList $args -Wait -PassThru -NoNewWindow
} catch {
throw "$($_.Exception.Message)"
}