-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathrun-agent.ps1
95 lines (81 loc) · 2.71 KB
/
run-agent.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
$agentDist="C:/BuildAgent"
$agentScript="${agentDist}/bin/agent.bat"
$configDir="${agentDist}/conf"
$logDir="${agentDist}/logs"
function fixDns() {
if($Env:USE_CUSTOM_DNS) {
$nic = Get-NetAdapter
Set-DnsClientServerAddress -InterfaceIndex $nic.IfIndex -ServerAddresses ("$Env:USE_CUSTOM_DNS")
Write-Host "Updated interface $nic.IfIndex to use DNS Server $Env:USE_CUSTOM_DNS"
}
}
function configure($options) {
if ($options.length -eq 0) { return }
Write-Host "Run agent.bat configure ${options}"
&$agentScript configure $options
if ($LastExitCode -ne 0) { throw "Failed to update configuration $LastExitCode" }
Write-Host "File buildAgent.properties was updated"
}
function unquote($value) {
# Strip double quotes produced by docker-compose
return $value -Replace '""', '"'
}
function reconfigure() {
$options=@()
fixDns
if ($Env:SERVER_URL) {
$options += "--server-url"
$options += unquote $env:SERVER_URL
}
if ($Env:AGENT_TOKEN) {
$options += "--auth-token"
$options += unquote $env:AGENT_TOKEN
}
if ($Env:AGENT_NAME) {
$options += "--name"
$options += unquote $env:AGENT_NAME
}
configure $options
}
function prepare_conf() {
Write-Host "Will prepare agent configuration"
Get-ChildItem "${agentDist}/conf_dist" -Recurse | ForEach-Object {
if ($_.Name -eq "buildAgent.dist.properties") {
Move-Item $_.FullName "${configDir}/buildAgent.properties" -Force
} else {
Move-Item $_.FullName -Destination $configDir -Force
}
}
Write-Host "File buildAgent.properties was created"
}
if (Test-Path -Path $logDir) {
Get-ChildItem $logDir -Filter "*.pid" | ForEach-Object { Remove-Item $_.FullName -Force }
}
if (Test-Path -Path "${configDir}/buildAgent.properties") {
Write-Host "File buildAgent.properties was found in ${configDir}"
} else {
Write-Host "Will create a new buildAgent.properties using distribution"
if ($env:SERVER_URL) {
Write-Host "TeamCity URL: ${env:SERVER_URL}"
} else {
Write-Host "TeamCity URL was not specified, please use SERVER_URL environment variable for that."
exit 1
}
prepare_conf
reconfigure
}
&$agentScript start
if ($LastExitCode -ne 0) { throw "Failed to start TeamCity build agent: $LastExitCode" }
if (!(Test-Path -Path "${logDir}/teamcity-agent.log")) {
Write-Host -NoNewline "Waiting for TeamCity build agent start"
while(!(Test-Path -Path "${logDir}/teamcity-agent.log")) {
Write-Host -NoNewline "."
Start-Sleep -s 1
}
Write-Host ""
}
Trap {
&$agentScript stop force
exit 0
}
Get-Content -Path "${logDir}/teamcity-agent.log" -Wait