Skip to content

Commit

Permalink
feat: Added Azure site extension installation scripts (newrelic#2448)
Browse files Browse the repository at this point in the history
Co-authored-by: James Sumners <[email protected]>
  • Loading branch information
svetlanabrennan and jsumners-nr authored Sep 6, 2024
1 parent ae82760 commit a56c4e1
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cloud-tooling/README.md
Original file line number Diff line number Diff line change
@@ -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)

9 changes: 9 additions & 0 deletions cloud-tooling/azure-site-extension/Content/install.cmd
Original file line number Diff line number Diff line change
@@ -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%
110 changes: 110 additions & 0 deletions cloud-tooling/azure-site-extension/Content/install.ps1
Original file line number Diff line number Diff line change
@@ -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
42 changes: 42 additions & 0 deletions cloud-tooling/azure-site-extension/Content/uninstall.cmd
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a56c4e1

Please sign in to comment.