-
Notifications
You must be signed in to change notification settings - Fork 9
/
install.ps1
97 lines (83 loc) · 3.11 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
$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = "Stop"
$InstallationDirectory = $PSScriptRoot
#
# Assert required variables are set
#
if (Test-Path env:SemaphoreEndpoint) {
$SemaphoreEndpoint = $env:SemaphoreEndpoint
} else {
if (-not (Test-Path env:SemaphoreOrganization)) {
Write-Warning 'Either $env:SemaphoreOrganization or $env:SemaphoreEndpoint needs to be specified. Exiting...'
Exit 1
}
$SemaphoreEndpoint = "$env:SemaphoreOrganization.semaphoreci.com"
Write-Warning "`$env:SemaphoreEndpoint not set, using '$SemaphoreEndpoint'"
}
if (-not (Test-Path env:SemaphoreRegistrationToken)) {
Write-Warning 'Registration token cannot be empty, set $env:SemaphoreRegistrationToken. Exiting...'
Exit 1
}
#
# Set defaults in case some variables are not set
#
if (Test-Path env:SemaphoreAgentDisconnectAfterJob) {
$DisconnectAfterJob = $env:SemaphoreAgentDisconnectAfterJob
} else {
$DisconnectAfterJob = "false"
}
if (Test-Path env:SemaphoreAgentDisconnectAfterIdleTimeout) {
$DisconnectAfterIdleTimeout = $env:SemaphoreAgentDisconnectAfterIdleTimeout
} else {
$DisconnectAfterIdleTimeout = 0
}
#
# Download and unpack toolbox
#
$ToolboxDirectory = Join-Path $HOME ".toolbox"
$InstallScriptPath = Join-Path $ToolboxDirectory "install-toolbox.ps1"
Write-Output "> Toolbox will be installed at $ToolboxDirectory."
if (Test-Path $ToolboxDirectory) {
Write-Output "> Toolbox already installed at $ToolboxDirectory. Overriding it..."
Remove-Item -Path $ToolboxDirectory -Force -Recurse
}
if (Test-Path env:SemaphoreToolboxVersion) {
Write-Output "> Downloading and unpacking $env:SemaphoreToolboxVersion toolbox..."
Invoke-WebRequest "https://github.com/semaphoreci/toolbox/releases/download/$env:SemaphoreToolboxVersion/self-hosted-windows.tar" -OutFile toolbox.tar
} else {
Write-Output '> $env:SemaphoreToolboxVersion is not set. Downloading and unpacking latest toolbox...'
Invoke-WebRequest "https://github.com/semaphoreci/toolbox/releases/latest/download/self-hosted-windows.tar" -OutFile toolbox.tar
}
tar.exe -xf toolbox.tar -C $HOME
Rename-Item "$HOME\toolbox" $ToolboxDirectory
Remove-Item toolbox.tar -Force
#
# Install toolbox
#
Write-Output "> Installing toolbox..."
& $InstallScriptPath
#
# Create agent config in current directory
#
$AgentConfig = @"
endpoint: "$SemaphoreEndpoint"
token: "$env:SemaphoreRegistrationToken"
no-https: false
shutdown-hook-path: "$env:SemaphoreAgentShutdownHook"
disconnect-after-job: $DisconnectAfterJob
disconnect-after-idle-timeout: $DisconnectAfterIdleTimeout
env-vars: []
files: []
fail-on-missing-files: false
"@
$AgentConfigPath = Join-Path $InstallationDirectory "config.yaml"
if (Test-Path $AgentConfigPath) {
Write-Output "> Agent configuration file already exists in $AgentConfigPath. Overriding it..."
Remove-Item -Path $AgentConfigPath -Force -Recurse
}
New-Item -ItemType File -Path $AgentConfigPath > $null
Set-Content -Path $AgentConfigPath -Value $AgentConfig
Write-Output "> Successfully installed the agent in $InstallationDirectory."
Write-Output "
Start the agent with: $InstallationDirectory\agent.exe start --config-file $AgentConfigPath
"