Skip to content

Commit

Permalink
add config and ver params to windows installer
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Krause committed Jun 2, 2022
1 parent 0852e4f commit ca65419
Showing 1 changed file with 169 additions and 0 deletions.
169 changes: 169 additions & 0 deletions install/install_windows.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
New-Module -name circonus-install -ScriptBlock {
$usage = "Circonus Unified Agent Install Help
Usage
install --key <apikey>
Options
[--key] Circonus API key/token
[--config] Absolute path to a configuration file
[--ver] CUA version number
[--help] This message
Note: Provide an authorized app for the key or ensure api
key/token has adequate privileges (default app state:allow)"
$installpath = "${env:systemdrive}\Program Files\Circonus\Circonus-Unified-Agent"
$name = "circonus-unified-agent"
$repo = "circonus-labs/${name}"
$releases = "https://api.github.com/repos/${repo}/releases"
$zip = "${name}.zip"

function New-Location {
if (!(Test-Path $installpath)) {
New-Item -ItemType Directory -Force -Path $installpath
}
else {
return
}
}

function Get-Latest-Release {
Write-Host "Determining latest release..."
$tag = (Invoke-WebRequest $releases -UseBasicParsing | ConvertFrom-Json)[0].tag_name
$tagrawv = $tag.substring(1)
$download = "https://github.com/${repo}/releases/download/${tag}/circonus-unified-agent_${tagrawv}_windows_x86_64.zip"
return $download
}

function Get-Release {
param ($tag)
Write-Host "Generating URL for release $tag"
$tagrawv = $tag.substring(1)
$download = "https://github.com/${repo}/releases/download/${tag}/circonus-unified-agent_${tagrawv}_windows_x86_64.zip"
return $download
}

function Get-Package {
param ($downloadpath)
Write-Host "Downloading Package..."
Invoke-WebRequest $downloadpath -Out "${env:temp}\${zip}"
}

function Expand-Package {
Write-Host "Expanding archive and installing..."
Expand-Archive -Path "${env:temp}\${zip}" -DestinationPath "${env:systemdrive}\Program Files\Circonus\Circonus-Unified-Agent"
}

function Enable-Service {
Write-Host ".........."
& "${installpath}\sbin\${name}d.exe" --service install --config "${installpath}\etc\circonus-unified-agent.conf"
Set-Service -Name circonus-unified-agent -StartupType Automatic
}

function Set-Config-Key {
param ($token)
$file = "${installpath}\etc\circonus-unified-agent.conf"
(Get-Content $file) -replace ' api_token = ""', " api_token = `"${token}`"" | Set-Content $file
}


function Set-Config-Default {
Write-Host "Copying config..."
Move-Item -Path "${installpath}\etc\example-circonus-unified-agent_windows.conf" -Destination "${installpath}\etc\circonus-unified-agent.conf"
}

function Set-Config-With-Config {
param ($configfile)
Write-Host "Copying config..."
Copy-Item -Path "${configfile}" -Destination "${installpath}\etc\circonus-unified-agent.conf"
}

function Cleanup {
Write-Host "Cleaning up..."
Remove-Item -Path "${env:temp}\${zip}" -Force
}

function Start-Service {
Write-Host "Starting service..."
Set-Service -Name circonus-unified-agent -Status Running -PassThru
}

function Install-Project {
<#
.SYNOPSIS
Install Circonus Unified Agent for Windows
.DESCRIPTION
Install Circonus Unified Agent for Windows
.EXAMPLE
iex; install -key <your key here>
#>
param (
# Circonus API Key
[Parameter()]
[string]$key,
[string]$config,
[string]$ver,
# display help
[bool]$help
)
if ($help) {
Write-Host $usage
return
}
if ($key -eq "" -And $config -eq "" ) {
Write-Host "Circonus API Key or Config file path is required."
Write-Host $usage
return
}
if ((Test-Path -Path "${installpath}\sbin\circonus-unified-agentd.exe" -PathType Leaf)) {
Write-Host "Circonus-Unified-Agent is already installed."
return
}
if ([Environment]::Is64BitProcess -ne [Environment]::Is64BitOperatingSystem) {
Write-Host "Circonus-Unified-Agent is only supported on 64-Bit Windows releases."
return
}

# Create the install directory
New-Location
# Determine the latest release

$release = ""
if ($ver -ne "") {
$release = Get-Release($ver)
}
else {
$release = Get-Latest-Release
}


# Fetch the latest CUA version zip file
Get-Package($release)
# Unarchive the zip file into their proper location
Expand-Package
# Set the service up
Enable-Service

# Setup the configuration file
if ($config -ne "" ) {
Set-Config-With-Config($config)
}
else {
Set-Config-Default
}

if ($key -ne "") {
# Replace the api key if not set
Set-Config-Key($key)
}

# Cleanup tmp dir
Cleanup
# Start the service
Start-Service
}
Set-Alias install -value Install-Project
Export-ModuleMember -function 'Install-Project' -alias 'install'
}

0 comments on commit ca65419

Please sign in to comment.