Last active
November 20, 2018 10:01
-
-
Save strvmarv/f4e0714c995242bdb54d9481c0449aed to your computer and use it in GitHub Desktop.
Stackify_Agent_Windows_Azure_CloudServices_Scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
REM Skip if running locally/emulated | |
if "%EMULATED%"=="true" goto :EOF | |
REM Temp folder overrides | |
echo STACKIFYTEMP is %STACKIFYTEMP% | |
echo TMP is %TMP% | |
echo TEMP is %TEMP% | |
if "%STACKIFYTEMP%"=="" ( | |
echo Skipping TMP/TEMP assignment as STACKIFYTEMP does not exist | |
) else ( | |
echo Assigning TMP/TEMP to STACKIFYTEMP - %STACKIFYTEMP% | |
set TMPOLD=%TMP% | |
set TEMPOLD=%TEMP% | |
set TMP=%STACKIFYTEMP% | |
set TEMP=%STACKIFYTEMP% | |
) | |
REM Execute installation via powershell | |
powershell -command "Set-ExecutionPolicy Unrestricted" | |
powershell -File .\InstallAgent.ps1 %DOWNLOADURL% %ACTIVATIONKEY% "%ENVIRONMENT%" %ENABLEDEVPROFILER% %RESTARTIIS% | |
REM Reset TEMP folder overrides | |
if "%TMPOLD%"=="" ( | |
echo Not resetting TMP as TMPOLD does not exist | |
) else ( | |
echo Resetting TMP to TMPOLD - %TMPOLD% | |
set TMP=%TMPOLD% | |
) | |
if "%TEMPOLD%"=="" ( | |
echo Not resetting TEMP as TEMPOLD does not exist | |
) else ( | |
echo Resetting TEMP to TEMPOLD - %TEMPOLD% | |
set TEMP=%TEMPOLD% | |
) | |
REM Reset our vars | |
SET STACKIFYTEMP= | |
SET TMPOLD= | |
SET TEMPOLD= | |
echo STACKIFYTEMP is %STACKIFYTEMP% | |
echo TMP is %TMP% | |
echo TEMP is %TEMP% | |
:EOF | |
EXIT %errorlevel% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add-Type -AssemblyName System.Net.Http | |
## FUNCTIONS | |
function Retry-Command { | |
[CmdletBinding()] | |
Param( | |
[Parameter(Position=0, Mandatory=$true)] | |
[scriptblock]$ScriptBlock, | |
[Parameter(Position=1, Mandatory=$false)] | |
[int]$Maximum = 5, | |
[Parameter(Position=1, Mandatory=$false)] | |
[int]$SleepSeconds = 5 | |
) | |
Begin { | |
$cnt = 0 | |
} | |
Process { | |
do { | |
$cnt++ | |
try { | |
Write-Host "Retry-Command - executing block on count $cnt || " | |
$ScriptBlock.Invoke() | |
return | |
} catch { | |
Write-Error $_.Exception.InnerException.Message -ErrorAction Continue | |
Start-Sleep -s $SleepSeconds * $cnt | |
} | |
} while ($cnt -lt $Maximum) | |
# Throw an error after $Maximum unsuccessful invocations. Doesn't need | |
# a condition, since the function returns upon successful invocation. | |
throw 'Execution failed.' | |
} | |
} | |
function DownloadFile { | |
Param( | |
[Parameter(Position=0,mandatory=$true)] | |
[string]$Source, | |
[Parameter(Position=1,mandatory=$true)] | |
[string]$Destination | |
) | |
if (($Source -as [System.URI]).AbsoluteURI -ne $null) | |
{ | |
Write-Host "DownloadFile - Downloading $Source to $destination || " | |
$handler = New-Object System.Net.Http.HttpClientHandler | |
$client = New-Object System.Net.Http.HttpClient($handler) | |
$client.Timeout = New-Object System.TimeSpan(0, 60, 0) | |
$cancelTokenSource = [System.Threading.CancellationTokenSource]::new() | |
$responseMsg = $client.GetAsync([System.Uri]::new($Source), $cancelTokenSource.Token) | |
$responseMsg.Wait() | |
if (!$responseMsg.IsCanceled) | |
{ | |
$response = $responseMsg.Result | |
if ($response.IsSuccessStatusCode) | |
{ | |
$downloadedFileStream = [System.IO.FileStream]::new($Destination, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write) | |
$copyStreamOp = $response.Content.CopyToAsync($downloadedFileStream) | |
$copyStreamOp.Wait() | |
$downloadedFileStream.Close() | |
if ($copyStreamOp.Exception -ne $null) | |
{ | |
throw $copyStreamOp.Exception | |
} | |
Write-Host "DownloadFile - Download of $Source to $destination completed || " | |
} | |
else | |
{ | |
Write-Host "DownloadFile - Download of $Source to $destination was not successful || " | |
} | |
} | |
else | |
{ | |
Write-Host "DownloadFile - Download of $Source to $destination was canceled || " | |
} | |
} | |
else | |
{ | |
throw "DownloadFile - Cannot download from $Source || " | |
} | |
} | |
## PROCESS | |
$args >> output.txt | |
$serviceName = 'StackifyMonitoringService' # Used to check if agent is already installed - IT IS RE-ENTRANT | |
$key = $args[0] # Stackify ApiKey | |
$env = $args[1] # Stackify Environment | |
$enableProfiler = 1 # Enable Stackify Profiler | |
$attachAll = 0 # Enable Stackify Profiler for Worker Roles (non-web) | |
$restartIIS = 1 # Enable Restart of IIS after Profiler installation - IMPORTANT | |
$url = "http://s1.stackify.com/Account/AgentDownload" # Where to download the installer from | |
$file = "$pwd\StackifyInstall.exe" # Where to download the installer - "present working directory" | |
Retry-Command -ScriptBlock { | |
Write-Host "InstallAgent - starting || " | |
# Only run this if StackifyMonitoringService does not exist | |
If (-not (Get-Service $serviceName -ErrorAction SilentlyContinue)) { | |
Write-Host "InstallAgent - needs to run || " | |
# Download installer | |
DownloadFile $url $file | |
# Wait | |
Start-Sleep -s 15 | |
# Run installer | |
& $file /s /v"ACTIVATIONKEY=$key ENVIRONMENT=\`"$env\`" ENABLEPROFILER=$enableProfiler ATTACHALL=$attachAll RESTARTIIS=$restartIIS /qn /l*v .\Log.txt" /debuglog | |
} | |
else | |
{ | |
Write-Host "InstallAgent - does not need to run || " | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ECHO OFF | |
REM logwrap.cmd calls passed in batch file, redirecting all output to the StartupLog.txt log file. | |
ECHO [%date% %time%] == START logwrap.cmd ============================================== >> "%TEMP%\StartupLog.txt" 2>&1 | |
ECHO [%date% %time%] Running %1 >> "%TEMP%\StartupLog.txt" 2>&1 | |
REM Call the child command batch file, redirecting all output to the StartupLog.txt log file. | |
START /B /WAIT %1 >> "%TEMP%\StartupLog.txt" 2>&1 | |
REM Log the completion of child command. | |
ECHO [%date% %time%] Done >> "%TEMP%\StartupLog.txt" 2>&1 | |
IF %ERRORLEVEL% EQU 0 ( | |
REM No errors occurred. Exit logwrap.cmd normally. | |
ECHO [%date% %time%] == END logwrap.cmd ================================================ >> "%TEMP%\StartupLog.txt" 2>&1 | |
ECHO. >> "%TEMP%\StartupLog.txt" 2>&1 | |
EXIT /B 0 | |
) ELSE ( | |
REM Log the error. | |
ECHO [%date% %time%] An error occurred. The ERRORLEVEL = %ERRORLEVEL%. >> "%TEMP%\StartupLog.txt" 2>&1 | |
ECHO [%date% %time%] == END logwrap.cmd ================================================ >> "%TEMP%\StartupLog.txt" 2>&1 | |
ECHO. >> "%TEMP%\StartupLog.txt" 2>&1 | |
EXIT /B %ERRORLEVEL% | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
<ConfigurationSettings> | |
<Setting name="Stackify.ApiKey" value="[Your API/Activation Key Here]" /> | |
<Setting name="Stackify.Environment" value="[Your Environment Here]" /> | |
</ConfigurationSettings> | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
<ConfigurationSettings> | |
<Setting name="Stackify.ApiKey" /> | |
<Setting name="Stackify.Environment" /> | |
</ConfigurationSettings> | |
... | |
<LocalResources> | |
<LocalStorage name="StackifyTemp" sizeInMB="1024" cleanOnRoleRecycle="false" /> | |
</LocalResources> | |
<Startup> | |
<Task commandLine="logwrap.cmd InstallAgent.cmd" executionContext="elevated" taskType="simple"> | |
<Environment> | |
<Variable name="ACTIVATIONKEY"> | |
<RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/ConfigurationSettings/ConfigurationSetting[@name='Stackify.ApiKey']/@value" /> | |
</Variable> | |
<Variable name="ENVIRONMENT"> | |
<RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/ConfigurationSettings/ConfigurationSetting[@name='Stackify.Environment']/@value" /> | |
</Variable> | |
<Variable name="STACKIFYTEMP"> | |
<RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/LocalResources/LocalResource[@name='StackifyTemp']/@path" /> | |
</Variable> | |
</Environment> | |
</Task> | |
</Startup> | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
powershell -File .\InstallAgent.ps1 %DOWNLOADURL% %ACTIVATIONKEY% "%ENVIRONMENT%" %ENABLEDEVPROFILER% %RESTARTIIS%
InstallAgent.cmd, line 25 has a minor bug. It calls the Powershell script with
%DOWNLOADURL%
as the first argument, where the Powershell script expects the API key as the first argument. It only works because%DOWNLOADURL%
is never instantiated, so it's effectively whitespace and ignored.Additionally, that line adds two arguments that are then just ignored by the Powershell script:
%ENABLEDEVPROFILER%
and%RESTARTIIS%
: