Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: Added Azure site extension installation scripts #2448

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
bcfb43a
wip extension scripts
svetlanabrennan Aug 9, 2024
d08187b
Add spec files
svetlanabrennan Aug 9, 2024
e4e18dc
Add version file
svetlanabrennan Aug 9, 2024
4f33dc3
update install script
svetlanabrennan Aug 19, 2024
60d40c2
refactor script and add update site extension version script
svetlanabrennan Aug 19, 2024
47cc522
Delete cloud-tooling/azure-site-extension/Content/.gitignore
svetlanabrennan Aug 19, 2024
52fb75c
Delete cloud-tooling/azure-site-extension/Content/NewRelic.Azure.Webs…
svetlanabrennan Aug 19, 2024
0a590e4
Delete cloud-tooling/azure-site-extension/Content/publish.sh
svetlanabrennan Aug 19, 2024
c599b6c
delete site extension and version files
svetlanabrennan Aug 19, 2024
247af7c
Remove application host files
svetlanabrennan Aug 19, 2024
0a6d499
delete gitignore and site extension file
svetlanabrennan Aug 19, 2024
9205318
fix lint
svetlanabrennan Aug 19, 2024
0db8d2f
refactor install scripts
svetlanabrennan Aug 21, 2024
8af6880
Add comments to install file
svetlanabrennan Aug 21, 2024
4e7b7db
update npm install in catch block
svetlanabrennan Aug 22, 2024
6528e56
Add readme to cloud tooling directory
svetlanabrennan Aug 22, 2024
899ffcc
Update cloud-tooling/README.md
svetlanabrennan Aug 22, 2024
4fbb5dd
Add testing to readme
svetlanabrennan Aug 23, 2024
f5e1b0c
Add another step to testing
svetlanabrennan Aug 23, 2024
b69583c
remove testing readme, update uninstall and refactor env vars in inst…
svetlanabrennan Aug 30, 2024
0286e8a
Update uninstall script to remove empty @ directories
svetlanabrennan Sep 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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