Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: Added Azure site extension installation scripts #2448

Merged
Merged
Show file tree
Hide file tree
Changes from 19 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%
113 changes: 113 additions & 0 deletions cloud-tooling/azure-site-extension/Content/install.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Define paths
$extensionModulesPath = "$PSScriptRoot\node_modules"
$appRootPath = "$env:HOME\site\wwwroot"
$userModulesPath = "$appRootPath\node_modules"

# Define the path to the node_modules directory and the package to check
$UserNodeModulesPath = "$env:HOME"
$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 $UserNodeModulesPath | 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 "$env:HOME\site\wwwroot" 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
7 changes: 7 additions & 0 deletions cloud-tooling/azure-site-extension/Content/uninstall.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
:: Copyright 2024 New Relic Corporation. All rights reserved.
:: SPDX-License-Identifier: Apache-2.0

SET NEW_RELIC_FOLDER="%HOME%\node_modules/newrelic"
IF EXIST %NEW_RELIC_FOLDER% (
npm uninstall newrelic
)
107 changes: 107 additions & 0 deletions cloud-tooling/azure-site-extension/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
## Testing

How to test the site extension in Azure.

1. Create web app in Azure.
- Home > Create a resource > Web App > Create
- Select the "Node Team Sandbox" subscription.
- Select or create a new resource group.
- Name your application. Uncheck "unique default hostname".
- Select how you want to publish your web app (code, container or static web app).
- Select a runtime stack.
- Select "Windows" as the operating system.
- Leave pricing on the "Free F1" option.
- Leave zone redundancy to "disabled".
- Review + create > Create. (No need to setup other things like database, deployment, networking...etc)

2. Deploy web app
- Deploy your web application to Azure. There are several ways you can do that: manual (uploading a zip file containing your application files), Azure CLI, VS Code Extensions, local git, ftp/ftps and github actions.
- Make sure your deployed app has a `web.config` file that looks something like this otherwise you will get a "You do not have permission to view this directory or page" error:
<details>
<?xml version="1.0" encoding="utf-8"?>
<!--
This configuration file is required if iisnode is used to run node processes behind
IIS or IIS Express. For more information, visit:

https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
-->

<configuration>
<system.webServer>
<!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
<webSocket enabled="false" />
<handlers>
<!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module -->
<add name="iisnode" path="index.js" verb="*" modules="iisnode"/>
</handlers>

<rewrite>
<rules>
<!-- Do not interfere with requests for node-inspector debugging -->
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^index.js\/debug[\/]?" />
</rule>

<!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
<rule name="StaticContent">
<action type="Rewrite" url="public{PATH_INFO}"/>
</rule>

<!-- All other URLs are mapped to the node.js site entry point -->
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="index.js"/>
</rule>
</rules>
</rewrite>

<!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
<security>
<requestFiltering>
<hiddenSegments>
<remove segment="bin"/>
</hiddenSegments>
</requestFiltering>
</security>

<!-- Make sure error responses are left untouched -->
<httpErrors existingResponse="PassThrough" />

<!--
You can control how Node is hosted within IIS using the following options:
* watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
* node_env: will be propagated to node as NODE_ENV environment variable
* debuggingEnabled - controls whether the built-in debugger is enabled

See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
-->
<!--<iisnode watchedFiles="web.config;*.js"/>-->
</system.webServer>
</configuration>
- Note: update the path in the web.config file to match your entry file.
</details>

3. Install site extension
- You can do this in two different ways:
- Using CLI: Development Tools > Extensions > Add > Search for site extension
- Using Kudu: Development Tools > Advanced Tools > Go > Site Extensions > Gallery + Search for site extension
4. Add environment variables
- Select Web App > Settings > Environment variables
- Add `NEW_RELIC_LICENSE_KEY`, `NEW_RELIC_LOG_ENABLED` set to `true` and `NEW_RELIC_LOG_LEVEL` set to `trace`.
- Click "Apply"
5. Restart application
- Overview > Restart

6. Send traffic to your application. You should see a `newrelic_agent.log` created in the `/site/wwwroot` directory and data should be flowing to your NR1 account.

### Using Kudu to view:
- Site extension logs:
- Development Tools > Advanced Tools > Go > Debug console > Powershell > SiteExtensions > Select the site extension > install.log (download the file or click on pencil icon to see it in the console)
- New Relic logs:
- Development Tools > Advanced Tools > Go > Debug console > Powershell > site > wwwroot > newrelic_agent.log (download the file or click on pencil icon to see it in the console)
- Environment variables, app settings, system info, connection strings, path, headers, server variables
- Development Tools > Advanced Tools > Go > Environment
- Additional log files:
- Development Tools > Advanced Tools > Go > Debug console > Powershell > LogFiles