forked from signalfx/signalfx-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.ps1
454 lines (399 loc) · 18 KB
/
install.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# The following comment block acts as usage for powershell scripts
# you can view it by passing the script as an argument to the cmdlet 'Get-Help'
# To view the paremeter documentation invoke Get-Help with the option '-Detailed'
# ex. PS C:\> Get-Help "<path to script>\install.ps1" -Detailed
<#
.SYNOPSIS
Installs the SignalFx Agent from the package repos.
.DESCRIPTION
Installs the SignalFx Agent from the package repos. If access_token is not
provided, it will be prompted for on the console. If you want to view full documentation
execute Get-Help with the parameter "-Full".
.PARAMETER access_token
The token used to send metric data to SignalFx.
.EXAMPLE
.\install.ps1 -access_token "ACCESSTOKEN"
.PARAMETER stage
(OPTIONAL) The package stage to install from ['test', 'beta', 'release']. Defaults to 'release'.
.EXAMPLE
.\install.ps1 -access_token "ACCESSTOKEN" -stage "test"
.PARAMETER ingest_url
(OPTIONAL) Base URL of the SignalFx ingest server. Defaults to 'https://ingest.signalfx.com'.
.EXAMPLE
.\install.ps1 -access_token "ACCESSTOKEN" -ingest_url "https://ingest.signalfx.com"
.PARAMETER api_url
(OPTIONAL) Base URL of the SignalFx API server. Defaults to 'https://api.signalfx.com'.
.EXAMPLE
.\install.ps1 -access_token "ACCESSTOKEN" -api_url "https://api.signalfx.com"
.PARAMETER insecure
(OPTIONAL) If true then certificates will not be checked when downloading resources. Defaults to '$false'.
.EXAMPLE
.\install.ps1 -access_token "ACCESSTOKEN" -insecure $true
.PARAMETER agent_version
(OPTIONAL) Specify a specific version of the agent to install. Defaults to the latest version available.
.EXAMPLE
.\install.ps1 -access_token "ACCESSTOKEN" -agent_version "4.0.0"
.PARAMETER format
(OPTIONAL) Specify the format of the SignalFxAgent package ('msi' or 'zip'). Defaults to 'msi'.
Specify 'zip' if the -agent_version parameter is also specified for versions older than TBD.
.EXAMPLE
.\install.ps1 -access_token "ACCESSTOKEN" -format "zip" -agent_version "4.0.0"
.PARAMETER msi_path
(OPTIONAL) Specify a local path to a SignalFxAgent MSI package to install instead of downloading the package.
If specified, the -agent_version, -stage, and -format parameters will be ignored.
.EXAMPLE
.\install.ps1 -access_token "ACCESSTOKEN" -msi_path "C:\SOME_FOLDER\SignalFxAgent-X.Y.Z-win64.msi"
#>
param (
[parameter(Mandatory=$true)]
[string]$access_token = "",
[ValidateSet('test','beta','release')]
[string]$stage = "release",
[string]$ingest_url = "https://ingest.signalfx.com",
[string]$api_url = "https://api.signalfx.com",
[bool]$insecure = $false,
[string]$agent_version = "",
[string]$msi_path = "",
[ValidateSet('msi','zip')]
[string]$format = "msi",
[bool]$UNIT_TEST = $false
)
$arch = "win64"
$signalfx_dl = "https://dl.signalfx.com"
$installation_path = "\Program Files"
$tempdir = "\tmp\SignalFx"
$program_data_path = "\ProgramData\SignalFxAgent"
$old_config_path = "\Program Files\SignalFx\SignalFxAgent\etc\signalfx\agent.yaml"
$config_path = "\ProgramData\SignalFxAgent\agent.yaml"
# check that we're not running with a restricted execution policy
function check_policy(){
$executionPolicy = (Get-ExecutionPolicy)
$executionRestricted = ($executionPolicy -eq "Restricted")
if ($executionRestricted){
throw @"
Your execution policy is $executionPolicy, this means you will not be able import or use any scripts including modules.
To fix this change you execution policy to something like RemoteSigned.
PS> Set-ExecutionPolicy RemoteSigned
For more information execute:
PS> Get-Help about_execution_policies
"@
}
}
# check if running as administrator
function check_if_admin(){
$identity = [Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()
If (-NOT $identity.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)){
return $false
}
return $true
}
# get latest package tag given a stage and format
function get_latest([string]$stage=$stage,[string]$format=$format) {
try {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$latest = (New-Object System.Net.WebClient).DownloadString("$signalfx_dl/windows/$stage/$format/latest/latest.txt").Trim()
}catch {
$err = $_.Exception.Message
$message = "
An error occurred while fetching the latest package version $signalfx_dl/windows/$stage/$format/latest/latest.txt
$err
"
throw "$message"
}
return $latest
}
# builds the filename for the package
function get_filename([string]$tag="",[string]$format=$format,[string]$arch=$arch){
$filename = "SignalFxAgent-$tag-$arch.$format"
return $filename
}
# builds the url for the package
function get_url([string]$stage="", [string]$format=$format, [string]$filename=""){
return "$signalfx_dl/windows/$stage/$format/$filename"
}
# download a file to a given destination
function download_file([string]$url, [string]$outputDir, [string]$fileName) {
try{
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
(New-Object System.Net.WebClient).DownloadFile($url, "$outputDir\$fileName")
} catch {
$err = $_.Exception.Message
$message = "
An error occurred while downloading $url
$err
"
throw "$message"
}
}
# ensure a file exists and raise an exception if it doesn't
function ensure_file_exists([string]$path="C:\"){
if (!(Test-Path -Path "$path")){
throw "Cannot find the path '$path'"
}
}
# unzip a file
function unzip_file($zipFile, $outputDir){
if (!(Test-Path -Path "$zipFile")){
throw "can't find zip file"
}
# found the following on https://www.howtogeek.com/tips/how-to-extract-zip-files-using-powershell/
$shell = new-object -com shell.application
foreach($item in ($shell.NameSpace($zipfile)).items()){
$shell.Namespace($outputDir).copyhere($item)
}
}
# verify a SignalFx access token
function verify_access_token([string]$access_token="", [string]$ingest_url=$INGEST_URL, [bool]$insecure=$INSECURE) {
if ($insecure) {
# turn off certificate validation
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} ;
}
$url = "$ingest_url/v2/event"
echo $url
try {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$resp = Invoke-WebRequest -Uri $url -Method POST -ContentType "application/json" -Headers @{"X-Sf-Token"="$access_token"} -Body "[]" -UseBasicParsing
} catch {
$err = $_.Exception.Message
$message = "
An error occurred while validating the access token
$err
"
throw "$message"
}
if (!($resp.StatusCode -Eq 200)) {
return $false
} else {
return $true
}
}
# create the signalfx directory if it doesn't exist
function create_signalfx_dir($installation_path=$installation_path){
if (!(Test-Path -Path "$installation_path\SignalFx")){
mkdir "$installation_path\SignalFx" -ErrorAction Ignore
}
}
# create directories in program data
function create_program_data() {
if (!(Test-Path -Path "$program_data_path")) {
mkdir "$program_data_path" -ErrorAction Ignore
}
}
# create the signalfx directory if it doesn't exist
function create_temp_dir($tempdir=$tempdir){
if ((Test-Path -Path "$tempdir")) {
Remove-Item -Recurse -Force "$tempdir"
}
mkdir "$tempdir" -ErrorAction Ignore
}
# copy etc from an existing installation in to the unzipped package
function copy_existing_etc([string]$installation_path=$installation_path, [string]$tempdir="") {
Remove-Item -Recurse -Force "$tempdir\SignalFxAgent\etc"
Copy-Item -Recurse -Force "$installation_path\SignalFx\SignalFxAgent\etc" "$tempdir\SignalFxAgent\etc"
}
# whether the agent service is running
function service_running() {
return (((Get-CimInstance -ClassName win32_service -Filter "Name = 'SignalFx Smart Agent'" | Select Name, State).State -Eq "Running") -Or ((Get-CimInstance -ClassName win32_service -Filter "Name = 'signalfx-agent'" | Select Name, State).State -Eq "Running"))
}
# whether the agent service is installed
function service_installed() {
return (((Get-CimInstance -ClassName win32_service -Filter "Name = 'SignalFx Smart Agent'" | Select Name, State).Name -Eq "SignalFx Smart Agent") -Or ((Get-CimInstance -ClassName win32_service -Filter "Name = 'signalfx-agent'" | Select Name, State).Name -Eq "signalfx-agent"))
}
# start the service if it's stopped
function start_service([string]$installation_path=$installation_path, [string]$config_path=$config_path) {
if (!(service_running)){
$agent_bin = Resolve-Path "$installation_path\SignalFx\SignalFxAgent\bin\signalfx-agent.exe"
& $agent_bin -service "start" -config "$config_path"
}
}
# stop the service if it's running
function stop_service([string]$installation_path=$installation_path, [string]$config_path=$config_path) {
if ((service_running)){
$agent_bin = Resolve-Path "$installation_path\SignalFx\SignalFxAgent\bin\signalfx-agent.exe"
& $agent_bin -service "stop" -config "$config_path"
}
}
# remove registry entries created by the agent service
function remove_agent_registry_entries() {
try
{
if (Test-Path "HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application\SignalFx Smart Agent"){
Remove-Item "HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application\SignalFx Smart Agent"
}
if (Test-Path "HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application\signalfx-agent"){
Remove-Item "HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application\signalfx-agent"
}
} catch {
$err = $_.Exception.Message
$message = "
unable to remove registry entries at HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application\SignalFx Smart Agent
$err
"
throw "$message"
}
}
# install the service if it's not already installed
function install_service([string]$installation_path=$installation_path, [string]$config_path=$config_path) {
if (!(service_installed)){
$agent_bin = Resolve-Path "$installation_path\SignalFx\SignalFxAgent\bin\signalfx-agent.exe"
& $agent_bin -service "install" -logEvents -config "$config_path"
}
}
# uninstall the service
function uninstall_service([string]$installation_path=$installation_path) {
if ((service_installed)){
stop_service -installation_path $installation_path -config_path $config_path
$agent_bin = Resolve-Path "$installation_path\SignalFx\SignalFxAgent\bin\signalfx-agent.exe"
& $agent_bin -service "uninstall" -logEvents
}
}
# uninstall the agent
function uninstall_agent($installation_path=$installation_path) {
if (Test-Path -Path "$installation_path\SignalFx\SignalFxAgent") {
echo "Uninstalling agent..."
# stop the agent and uninstall it as a service
uninstall_service -installation_path $installation_path
echo "- Done"
echo "Removing old agent..."
# if the \etc\signalfx directory is a symlink remove it before recursively deleting the rest
if (Test-Path -Path "$installation_path\SignalFx\SignalFxAgent\etc\signalfx"){
if ([bool]((Get-Item "$installation_path\SignalFx\SignalFxAgent\etc\signalfx" -Force -ea SilentlyContinue).Attributes -band [IO.FileAttributes]::ReparsePoint)){
cmd /c rmdir "$installation_path\SignalFx\SignalFxAgent\etc\signalfx"
}
}
Remove-Item -Recurse -Force "$installation_path\SignalFx\SignalFxAgent"
remove_agent_registry_entries
echo "- Done"
} else {
echo "No existing agent installation found!"
}
}
# download agent package from repo
function download_agent_package([string]$agent_version=$agent_version, [string]$tempdir=$tempdir, [string]$stage=$stage, [string]$arch=$arch, [string]$format=$format){
# get the filename to download
$filename = get_filename -tag $agent_version -format $format -arch $arch
echo $filename
# get url for file to download
$fileurl = get_url -stage $stage -format $format -filename $filename
echo "Downloading package..."
download_file -url $fileurl -outputDir $tempdir -filename $filename
ensure_file_exists "$tempdir\$filename"
echo "- $fileurl -> '$tempdir'"
if ($format -Eq "zip") {
echo "Extracting package..."
unzip_file "$tempdir\$filename" "$tempdir"
}
}
# check registry for the agent msi package
function msi_installed([string]$name="SignalFx Smart Agent") {
return (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where { $_.DisplayName -eq $name }) -ne $null
}
$ErrorActionPreference = 'Stop'; # stop on all errors
# check administrator status
echo 'Checking if running as Administrator...'
if (!(check_if_admin)){
throw 'You are not currently running this installation under an Administrator account. Installation aborted!'
} else {
echo '- Running as Administrator'
}
# check execution policy
echo 'Checking execution policy'
check_policy
# verify access token
echo 'Verifying Access Token...'
if (!(verify_access_token -access_token $access_token -ingest_url $ingest_url -insecure $insecure)) {
throw "Failed to authenticate access token please verify that your access token is correct"
} else {
echo '- Verified Access Token'
}
# set up signalfx directory under installation path
$signalfx_dir = create_signalfx_dir -installation_path $installation_path
# set up a temporary directory under signalfx directory
$tempdir = create_temp_dir -tempdir $tempdir
if ($msi_path -Eq "") {
# determine package version to fetch
if ($agent_version -Eq ""){
echo 'Determining latest release...'
$agent_version = get_latest -stage $stage -format $format
echo "- Latest release is $agent_version"
}
# download the agent package with the specified agent_version or latest
download_agent_package -agent_version $agent_version -tempdir $tempdir -stage $stage -arch $arch -format $format
} else {
$msi_path = Resolve-Path "$msi_path"
if (!(Test-Path -Path "$msi_path")) {
throw "$msi_path not found!"
}
}
mkdir "$tempdir\SignalFxAgent\etc\signalfx" -ErrorAction Ignore
# stage configurations
if (Test-Path -Path "$program_data_path") {
# copy existing program data into temp dir
Remove-Item -Recurse "$tempdir\SignalFxAgent\etc\signalfx"
Copy-Item -Recurse "$program_data_path" "$tempdir\SignalFxAgent\etc\signalfx"
} elseif (Test-Path -Path "$installation_path\SignalFx\SignalFxAgent\etc") {
# copy existing \etc directory
copy_existing_etc -installation_path $installation_path -tempdir $tempdir
} else {
# write the access token file
[System.IO.File]::WriteAllText("$tempdir\SignalFxAgent\etc\signalfx\token","$access_token",[System.Text.Encoding]::ASCII)
# write the ingest url file
[System.IO.File]::WriteAllText("$tempdir\SignalFxAgent\etc\signalfx\ingest_url","$ingest_url",[System.Text.Encoding]::ASCII)
# write the api url file
[System.IO.File]::WriteAllText("$tempdir\SignalFxAgent\etc\signalfx\api_url"," $api_url",[System.Text.Encoding]::ASCII)
}
# uninstall agent bundle if installed
if (!(msi_installed)) {
uninstall_agent -installation_path $installation_path
}
# create program data directory
create_program_data
# empty the program data directory
Remove-Item "$program_data_path\*" -Recurse -Force
# copy configs to program data
Copy-Item -Recurse "$tempdir\SignalFxAgent\etc\signalfx\*" "$program_data_path\"
# remove the packaged etc before copying agent dir into place
if (Test-Path -Path "$tempdir\SignalFxAgent\etc\*") {
Remove-Item "$tempdir\SignalFxAgent\etc\*" -Recurse -Force
}
if ($format -Eq "zip") {
echo "Copying agent files into place..."
# copy agent files into place
Copy-Item -Recurse "$tempdir\SignalFxAgent" "$installation_path\SignalFx\SignalFxAgent"
# create symlink in old config location to new config location for backwards compatability
cmd /c mklink /D "$installation_path\SignalFx\SignalFxAgent\etc\signalfx" "$program_data_path"
} else {
if ($msi_path -Eq "") {
$msi_path = get_filename -tag $agent_version -format $format -arch $arch
$msi_path = (Join-Path "$tempdir" "$msi_path")
}
echo "Installing $msi_path ..."
Start-Process msiexec.exe -Wait -ArgumentList "/qn /norestart /i $msi_path"
}
echo "- Done"
if (!(Test-Path -Path "$config_path") -And (Test-Path -Path "$installation_path\SignalFx\SignalFxAgent\etc\signalfx\agent.yaml")) {
echo "$config_path not found"
echo "Copying default agent.yaml to $config_path"
Copy-Item "$installation_path\SignalFx\SignalFxAgent\etc\signalfx\agent.yaml" "$config_path"
}
echo "Installing agent service..."
# be doubly sure we don't have previously existing registry entries
remove_agent_registry_entries
install_service -installation_path $installation_path -config_path $config_path
echo "- Done"
echo "Starting agent service..."
start_service -installation_path $installation_path -config_path $config_path
# wait for the service to start
$startTime = Get-Date
while (!(service_running)){
# timeout after 30 seconds
if ((New-TimeSpan -Start $startTime -End (Get-Date)).TotalSeconds -gt 60){
throw "Agent service is not running. Something went wrong durring the installation. Please rerun the installer"
}
# give windows a second to synchronize service status
Start-Sleep -Seconds 1
}
echo "- Started"
# remove the temporary directory
Remove-Item -Recurse -Force "$tempdir"
echo "Installation Complete!"