diff --git a/cloud-tooling/README.md b/cloud-tooling/README.md new file mode 100644 index 0000000000..c406d05dee --- /dev/null +++ b/cloud-tooling/README.md @@ -0,0 +1,9 @@ +# Node Agent Cloud Tooling + +This repository contains various cloud based tools for the New Relic Node Agent (e.g. Azure Site Extensions). + +## Getting Started +Please refer to the README of any of the cloud based tools: + +- [Azure Site Extensions](./azure-site-extension/README.md) + diff --git a/cloud-tooling/azure-site-extension/Content/install.cmd b/cloud-tooling/azure-site-extension/Content/install.cmd new file mode 100644 index 0000000000..bdca190568 --- /dev/null +++ b/cloud-tooling/azure-site-extension/Content/install.cmd @@ -0,0 +1,9 @@ +:: Copyright 2024 New Relic Corporation. All rights reserved. +:: SPDX-License-Identifier: Apache-2.0 + +@echo off +REM Call the PowerShell script +powershell.exe -ExecutionPolicy Bypass -File .\install.ps1 + +REM Echo the exit code +echo %ERRORLEVEL% diff --git a/cloud-tooling/azure-site-extension/Content/install.ps1 b/cloud-tooling/azure-site-extension/Content/install.ps1 new file mode 100644 index 0000000000..47641c602e --- /dev/null +++ b/cloud-tooling/azure-site-extension/Content/install.ps1 @@ -0,0 +1,110 @@ +# Define the path to the node_modules directory and the package to check +$extensionModulesPath = "$PSScriptRoot\node_modules" +$appRootPath = "$env:HOME\site\wwwroot" +$userModulesPath = "$appRootPath\node_modules" +$packageName = "newrelic" + +WriteToInstallLog "Explicitly adding node to path" +$env:PATH = "C:\Program Files\Nodejs;" + $env:PATH + +function WriteToInstallLog($output) +{ + $logPath = (Split-Path -Parent $PSCommandPath) + "\install.log" + Write-Output "[$(Get-Date)] -- $output" | Out-File -FilePath $logPath -Append +} + +function Check-Version { + WriteToInstallLog "Checking installed version..." + + # Get installed version using npm list + $installedVersionOutput = & npm ls $packageName --prefix "$env:HOME" | Select-String -Pattern "$packageName@(\S+)" + + if ($installedVersionOutput) { + $UserVersion = $installedVersionOutput.Matches.Groups[1].Value + } else { + $UserVersion = "" + } + + WriteToInstallLog "Installed version is: $installedVersionOutput" + WriteToInstallLog "User version: $UserVersion" + + # Check if user package exists + if ($UserVersion -eq "") { + WriteToInstallLog "User package not found. Running install.ps1..." + Copy-NodeModules -sourcePath $extensionModulesPath -destinationPath $userModulesPath + exit $LASTEXITCODE + } else { + WriteToInstallLog "Installed version: $UserVersion" + WriteToInstallLog "Getting latest version from npm..." + + $LatestVersion = npm show $packageName version + WriteToInstallLog "Latest version: $LatestVersion" + + # Check if user package version matches the latest version + if ($UserVersion -ne $LatestVersion) { + WriteToInstallLog "Installed version ($UserVersion) does not match latest version ($LatestVersion). Running install.ps1..." + Copy-NodeModules -sourcePath $extensionModulesPath -destinationPath $userModulesPath + exit $LASTEXITCODE + } else { + WriteToInstallLog "Installed version ($UserVersion) matches the latest version ($LatestVersion). Skipping install.ps1..." + exit 0 + } + } +} + +# Function to move contents from extension's node_modules to user's node_modules +function Copy-NodeModules { + param ( + [string]$sourcePath, + [string]$destinationPath + ) + + try { + WriteToInstallLog "Start executing install.ps1" + + # Check if extension's node_module directory exists + if (Test-Path -Path $sourcePath) { + WriteToInstallLog "Source path exists: $sourcePath" + + # Check if user's node_modules directory exists and create if it doesn't + if (-not (Test-Path -Path $destinationPath)) { + WriteToInstallLog "Destination path does not exist: $destinationPath" + WriteToInstallLog "Creating destination directory..." + WriteToInstallLog -ItemType Directory -Path $destinationPath + } + + # Move node_modules from extension's node_modules directory to users + WriteToInstallLog "Moving node_modules from $sourcePath to $destinationPath..." + Move-Item -Path "$sourcePath\*" -Destination $destinationPath -Force + + WriteToInstallLog "Copy complete." + WriteToInstallLog "End executing install.ps1." + WriteToInstallLog "-----------------------------" + exit $LASTEXITCODE + } else { + WriteToInstallLog "Source path does not exist: $sourcePath. Skipping copy." + } + } catch { + $errorMessage = $_.Exception.Message + $errorLine = $_.InvocationInfo.ScriptLineNumber + WriteToInstallLog "Error at line $errorLine : $errorMessage" + + # Install node agent using npm + WriteToInstallLog "Executing npm install newrelic@latest" + npm install --prefix $appRootPath newrelic + + # Check if the installation was successful + if ($LASTEXITCODE -ne 0) { + WriteToInstallLog "npm install failed with exit code $LASTEXITCODE" + } else { + WriteToInstallLog "npm install completed successfully" + } + + WriteToInstallLog "End executing install.ps1." + WriteToInstallLog "-----------------------------" + exit 1 + } +} + +# Call the function +Check-Version diff --git a/cloud-tooling/azure-site-extension/Content/uninstall.cmd b/cloud-tooling/azure-site-extension/Content/uninstall.cmd new file mode 100644 index 0000000000..3e6f2a7a86 --- /dev/null +++ b/cloud-tooling/azure-site-extension/Content/uninstall.cmd @@ -0,0 +1,42 @@ +:: Copyright 2024 New Relic Corporation. All rights reserved. +:: SPDX-License-Identifier: Apache-2.0 + +@echo off +setlocal enabledelayedexpansion + +SET ROOT_DIR=%HOME%\site\wwwroot +SET NODE_MODULES=%ROOT_DIR%\node_modules + +REM Uninstall newrelic if it exists +SET NEW_RELIC_FOLDER="%NODE_MODULES%\newrelic" +IF EXIST %NEW_RELIC_FOLDER% ( + echo Uninstalling newrelic... + cd "%ROOT_DIR%" + call npm uninstall newrelic --save + IF !ERRORLEVEL! NEQ 0 ( + echo Failed to uninstall newrelic + exit /b 1 + ) ELSE ( + echo Successfully uninstalled newrelic + ) +) ELSE ( + echo newrelic package not found in node_modules +) + +REM Loop through directories starting with @ in node_modules +FOR /D %%G IN ("%NODE_MODULES%\@*") DO ( + SET "DIR_EMPTY=1" + FOR /F %%A IN ('dir /a /b "%%G" 2^>nul') DO SET "DIR_EMPTY=0" + IF !DIR_EMPTY!==1 ( + echo Removing empty directory: %%G + rmdir /s /q "%%G" + IF !ERRORLEVEL! NEQ 0 ( + echo Failed to remove directory: %%G + exit /b 1 + ) + ) +) + +echo Script completed successfully +endlocal +exit /b 0