Skip to content

Commit 16dafde

Browse files
authored
fix(windows): Fixes the rewrites in the infra file (#1350)
1 parent dce1bd5 commit 16dafde

1 file changed

Lines changed: 41 additions & 56 deletions

File tree

recipes/newrelic/infrastructure/windows.yml

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -228,50 +228,69 @@ install:
228228
$AGENT_VERSION = "{{.INFRASTRUCTURE_AGENT_INSTALLER_VERSION}}"
229229
$MSI_PATH = "$env:TEMP\newrelic-infra.msi"
230230
$EXTRACT_PATH = "$env:TEMP\\msi_extracted_install"
231-
231+
232232
if ($AGENT_VERSION) {
233233
Write-Host "Installing specific version of New Relic Infrastructure Agent: $AGENT_VERSION"
234234
} else {
235235
Write-Host "Installing latest version of New Relic Infrastructure Agent"
236236
}
237237
238-
# Check if service already exists to determine installation method
239238
$existingService = Get-Service -Name "newrelic-infra" -ErrorAction SilentlyContinue
240-
239+
240+
$NEW_RELIC_REGION = "{{.NEW_RELIC_REGION}}"
241+
$customAttributes=@"
242+
{{.NRIA_CUSTOM_ATTRIBUTES}}
243+
"@
244+
245+
function Write-FreshConfig {
246+
param([string]$Path, [string]$LicenseKey, [string]$Region, [string]$CustomAttrs)
247+
Remove-Item -Path $Path -Force -ErrorAction SilentlyContinue
248+
Add-Content -Path $Path -Value "# THIS FILE IS MACHINE GENERATED" -Encoding utf8
249+
if ($Region -ilike "staging") {
250+
Add-Content -Path $Path -Value "staging: true" -Encoding utf8
251+
}
252+
Add-Content -Path $Path -Value "license_key: $LicenseKey" -Encoding utf8
253+
Add-Content -Path $Path -Value "enable_process_metrics: true" -Encoding utf8
254+
Add-Content -Path $Path -Value "status_server_enabled: true" -Encoding utf8
255+
Add-Content -Path $Path -Value "status_server_port: 18003" -Encoding utf8
256+
if (Test-Path env:HTTPS_PROXY) {
257+
Add-Content -Path $Path -Value "proxy: $env:HTTPS_PROXY" -Encoding utf8
258+
}
259+
Add-Content -Path $Path -Value $CustomAttrs -Encoding utf8
260+
Write-Host "Configuration written successfully"
261+
}
262+
241263
if ($existingService) {
242-
Write-Host "Existing service detected."
243-
244-
# Extract MSI to temp folder
245-
Write-Host "Extracting MSI to $EXTRACT_PATH..."
264+
Write-Host "Existing service detected, performing upgrade..."
265+
266+
# Write fresh config before calling installer.ps1 — in upgrade mode
267+
# installer.ps1 preserves the existing config, so this ensures the service
268+
# starts with valid settings (including license_key) on the first start.
269+
Write-FreshConfig -Path $InfraConfig -LicenseKey $LICENSE_KEY -Region $NEW_RELIC_REGION -CustomAttrs $customAttributes
270+
271+
# Extract MSI to get the latest installer.ps1 and binaries
246272
if (Test-Path $EXTRACT_PATH) {
247273
Remove-Item -Path $EXTRACT_PATH -Recurse -Force -ErrorAction SilentlyContinue
248274
}
249275
New-Item -Path $EXTRACT_PATH -ItemType Directory -Force | Out-Null
250-
251-
# Extract using msiexec /a (administrative install - extracts files)
276+
252277
$extractProcess = Start-Process -FilePath "msiexec.exe" -ArgumentList "/a `"$MSI_PATH`" /qn TARGETDIR=`"$EXTRACT_PATH`" /L*v `"$env:TEMP\\msi_extract.log`"" -Wait -NoNewWindow -PassThru
253278
if ($extractProcess.ExitCode -ne 0) {
254279
Write-Host -ForegroundColor Red "Failed to extract MSI: Exit code $($extractProcess.ExitCode)"
255280
exit $extractProcess.ExitCode
256281
}
257-
Write-Host "MSI extracted successfully"
258-
259-
# Locate the installer.ps1 in extracted files
282+
260283
$installerScript = Get-ChildItem -Path $EXTRACT_PATH -Filter "installer.ps1" -Recurse | Select-Object -First 1
261284
if (-not $installerScript) {
262285
Write-Host -ForegroundColor Red "installer.ps1 not found in extracted MSI"
263286
exit 1
264287
}
265288
Write-Host "Found installer.ps1 at: $($installerScript.FullName)"
266-
267-
# Run installer.ps1 directly from extracted location (triggers ZIP installation path)
268-
Write-Host "Running installer.ps1 ..."
289+
269290
$scriptDir = $installerScript.DirectoryName
270291
Push-Location $scriptDir
271-
272292
try {
273293
& "$($installerScript.FullName)" -LicenseKey $LICENSE_KEY
274-
275294
if ($LASTEXITCODE -ne 0) {
276295
Write-Host -ForegroundColor Red "installer.ps1 failed with exit code: $LASTEXITCODE"
277296
if (Test-Path "C:\\ProgramData\\New Relic\\newrelic-infra\\tmp\\newrelic_installer_debug.log") {
@@ -283,12 +302,8 @@ install:
283302
} finally {
284303
Pop-Location
285304
}
286-
287-
Write-Host "installation completed successfully"
288305
} else {
289-
Write-Host "No existing service detected"
290-
291-
# Fresh install - use MSI directly (faster, standard Windows installation)
306+
Write-Host "No existing service detected, performing fresh install..."
292307
$process = Start-Process -FilePath "msiexec.exe" -ArgumentList "/qn /i `"$MSI_PATH`" GENERATE_CONFIG=true LICENSE_KEY=`"$LICENSE_KEY`" /L*v `"$env:TEMP\\msi_install.log`"" -Wait -NoNewWindow -PassThru
293308
if ($process.ExitCode -ne 0) {
294309
Write-Host -ForegroundColor Red "MSI installation failed with exit code: $($process.ExitCode)"
@@ -298,41 +313,11 @@ install:
298313
}
299314
exit $process.ExitCode
300315
}
301-
302-
Write-Host "Installation completed successfully"
303-
}
304316
305-
$NEW_RELIC_REGION = "{{.NEW_RELIC_REGION}}"
306-
if ($NEW_RELIC_REGION -ilike "staging") {
307-
Add-Content -Path $InfraConfig -Value "staging: true" -Force -Encoding utf8
308-
}
309-
Add-Content -Path $InfraConfig -Value "enable_process_metrics: true" -Force -Encoding utf8
310-
Add-Content -Path $InfraConfig -Value "status_server_enabled: true" -Force -Encoding utf8
311-
Add-Content -Path $InfraConfig -Value "status_server_port: 18003" -Force -Encoding utf8
312-
if (Test-Path env:HTTPS_PROXY) {
313-
(Get-Content $InfraConfig) | Where-Object {
314-
$_ -notmatch "^proxy"
315-
} | Set-Content $InfraConfig
316-
317-
Add-Content -Path $InfraConfig -Value "proxy: $env:HTTPS_PROXY" -Force -Encoding utf8
318-
}
319-
320-
$customAttributes=@"
321-
{{.NRIA_CUSTOM_ATTRIBUTES}}
322-
"@
323-
324-
Add-Content -Path $InfraConfig -Value $customAttributes -Force -Encoding utf8
325-
326-
# Restart service only for fresh installs to pick up config changes
327-
# For upgrades, installer.ps1 already handles the restart
328-
if (-not $existingService) {
329-
$service = Get-Service -Name "newrelic-infra" -ErrorAction SilentlyContinue
330-
if ($service -and $service.Status -eq "Running") {
331-
Write-Host "Restarting service to apply configuration changes..."
332-
Restart-Service -Name "newrelic-infra" -Force
333-
Start-Sleep -Seconds 3
334-
Write-Host "Service restarted with updated configuration"
335-
}
317+
# MSI starts the service with a yamlgen-generated config (license_key only).
318+
# Rewrite config fresh and restart so all required settings take effect.
319+
Write-FreshConfig -Path $InfraConfig -LicenseKey $LICENSE_KEY -Region $NEW_RELIC_REGION -CustomAttrs $customAttributes
320+
Restart-Service -Name "newrelic-infra" -Force -ErrorAction SilentlyContinue
336321
}
337322
'
338323

0 commit comments

Comments
 (0)