-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathinstall.sh
executable file
·276 lines (240 loc) · 8.04 KB
/
install.sh
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
#!/bin/bash
set -e
set -o pipefail
#
# Creates a 'semaphore-agent' systemd service.
# If it already exists, it will be overriden.
# SEMAPHORE_AGENT_START controls whether the service will be started as well.
#
create_systemd_service() {
SYSTEMD_SERVICE=$(cat <<-END
[Unit]
Description=Semaphore agent
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=$SEMAPHORE_AGENT_SYSTEMD_RESTART
RestartSec=$SEMAPHORE_AGENT_SYSTEMD_RESTART_SEC
User=$SEMAPHORE_AGENT_INSTALLATION_USER
WorkingDirectory=$AGENT_INSTALLATION_DIRECTORY
ExecStart=$AGENT_INSTALLATION_DIRECTORY/agent start --config-file $AGENT_CONFIG_PATH
Environment=SEMAPHORE_AGENT_LOG_FILE_PATH=$AGENT_INSTALLATION_DIRECTORY/agent.log
[Install]
WantedBy=multi-user.target
END
)
SYSTEMD_PATH=/etc/systemd/system
SERVICE_NAME=semaphore-agent
SYSTEMD_SERVICE_PATH=$SYSTEMD_PATH/$SERVICE_NAME.service
echo "Creating $SYSTEMD_SERVICE_PATH..."
if [[ -f "$SYSTEMD_SERVICE_PATH" ]]; then
echo "systemd service already exists at $SYSTEMD_SERVICE_PATH. Overriding it..."
echo "$SYSTEMD_SERVICE" > $SYSTEMD_SERVICE_PATH
systemctl daemon-reload
if [[ "$SEMAPHORE_AGENT_START" == "false" ]]; then
echo "Not restarting agent."
else
echo "Restarting semaphore-agent service..."
systemctl restart semaphore-agent
fi
else
echo "$SYSTEMD_SERVICE" > $SYSTEMD_SERVICE_PATH
if [[ "$SEMAPHORE_AGENT_START" == "false" ]]; then
echo "Not starting agent."
else
echo "Starting semaphore-agent service..."
systemctl start semaphore-agent
fi
fi
}
#
# Creates a 'com.semaphoreci.agent' launchd daemon, at /Library/LaunchDaemons.
# If it already exists, it will be overriden. SEMAPHORE_AGENT_START controls
# whether the daemon will be started as well.
#
create_launchd_daemon() {
LAUNCHD_DAEMON_LABEL=com.semaphoreci.agent
LAUNCHD_DAEMON=$(cat <<-END
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>$LAUNCHD_DAEMON_LABEL</string>
<key>ProgramArguments</key>
<array>
<string>$AGENT_INSTALLATION_DIRECTORY/agent</string>
<string>start</string>
<string>--config-file</string>
<string>$AGENT_CONFIG_PATH</string>
</array>
<key>EnvironmentVariables</key>
<dict>
<key>SEMAPHORE_AGENT_LOG_FILE_PATH</key>
<string>$AGENT_INSTALLATION_DIRECTORY/agent.log</string>
</dict>
<key>RunAtLoad</key>
<false/>
<key>KeepAlive</key>
<dict>
<key>Crashed</key>
<true/>
</dict>
<key>UserName</key>
<string>$SEMAPHORE_AGENT_INSTALLATION_USER</string>
<key>WorkingDirectory</key>
<string>$AGENT_INSTALLATION_DIRECTORY</string>
</dict>
</plist>
END
)
LAUNCHD_PATH=/Library/LaunchDaemons
LAUNCHD_DAEMON_PATH=$LAUNCHD_PATH/$LAUNCHD_DAEMON_LABEL.plist
echo "Creating $LAUNCHD_DAEMON_PATH..."
if [[ -f "$LAUNCHD_DAEMON_PATH" ]]; then
echo "launchd daemon already exists at $LAUNCHD_DAEMON_PATH. Overriding it..."
echo "$LAUNCHD_DAEMON" > $LAUNCHD_DAEMON_PATH
if [[ "$SEMAPHORE_AGENT_START" == "false" ]]; then
echo "Not starting/restarting $LAUNCHD_DAEMON_LABEL."
else
echo "Bootout $LAUNCHD_DAEMON_LABEL..."
launchctl bootout system $LAUNCHD_DAEMON_PATH || true
echo "Bootstrap $LAUNCHD_DAEMON_LABEL..."
launchctl bootstrap system $LAUNCHD_DAEMON_PATH
echo "Kickstart $LAUNCHD_DAEMON_LABEL..."
launchctl kickstart -k system/com.semaphoreci.agent
fi
else
echo "$LAUNCHD_DAEMON" > $LAUNCHD_DAEMON_PATH
if [[ "$SEMAPHORE_AGENT_START" == "false" ]]; then
echo "Not starting $LAUNCHD_DAEMON_LABEL."
else
echo "Bootstrap $LAUNCHD_DAEMON_LABEL service..."
launchctl bootstrap system $LAUNCHD_DAEMON_PATH
echo "Kickstart $LAUNCHD_DAEMON_LABEL service..."
launchctl kickstart -k system/com.semaphoreci.agent
fi
fi
}
# Find the toolbox URL based on operating system (linux/darwin) and architecture.
# It also considers SEMAPHORE_TOOLBOX_VERSION. If not set, it uses the latest version.
find_toolbox_url() {
local os=$(echo $DIST | tr '[:upper:]' '[:lower:]')
local tarball_name="self-hosted-${os}.tar"
if [[ ${ARCH} =~ "arm" || ${ARCH} == "aarch64" ]]; then
tarball_name="self-hosted-${os}-arm.tar"
fi
if [[ -z "${SEMAPHORE_TOOLBOX_VERSION}" ]]; then
echo "https://github.com/semaphoreci/toolbox/releases/latest/download/${tarball_name}"
else
echo "https://github.com/semaphoreci/toolbox/releases/download/${SEMAPHORE_TOOLBOX_VERSION}/${tarball_name}"
fi
}
#
# Main script
#
DIST=$(uname)
ARCH=$(uname -m)
AGENT_INSTALLATION_DIRECTORY="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
if [[ "$EUID" -ne 0 ]]; then
echo "Please run with sudo."
exit 1
fi
if [[ -z $SEMAPHORE_ENDPOINT ]]; then
if [[ -z $SEMAPHORE_ORGANIZATION ]]; then
read -p "Enter organization: " SEMAPHORE_ORGANIZATION
if [[ -z $SEMAPHORE_ORGANIZATION ]]; then
echo "Organization cannot be empty."
exit 1
fi
fi
SEMAPHORE_ENDPOINT="$SEMAPHORE_ORGANIZATION.semaphoreci.com"
fi
if [[ -z $SEMAPHORE_REGISTRATION_TOKEN ]]; then
read -p "Enter registration token: " SEMAPHORE_REGISTRATION_TOKEN
if [[ -z $SEMAPHORE_REGISTRATION_TOKEN ]]; then
echo "Registration token cannot be empty."
exit 1
fi
fi
if [[ -z $SEMAPHORE_AGENT_INSTALLATION_USER ]]; then
LOGGED_IN_USER=$(logname)
read -p "Enter user [$LOGGED_IN_USER]: " SEMAPHORE_AGENT_INSTALLATION_USER
SEMAPHORE_AGENT_INSTALLATION_USER="${SEMAPHORE_AGENT_INSTALLATION_USER:=$LOGGED_IN_USER}"
fi
if ! id "$SEMAPHORE_AGENT_INSTALLATION_USER" &>/dev/null; then
echo "User $SEMAPHORE_AGENT_INSTALLATION_USER does not exist. Exiting..."
exit 1
fi
#
# Download toolbox
#
USER_HOME_DIRECTORY=$(sudo -u $SEMAPHORE_AGENT_INSTALLATION_USER -H bash -c 'echo $HOME')
TOOLBOX_DIRECTORY="$USER_HOME_DIRECTORY/.toolbox"
if [[ -d "$TOOLBOX_DIRECTORY" ]]; then
echo "Toolbox was already installed at $TOOLBOX_DIRECTORY. Overriding it..."
rm -rf "$TOOLBOX_DIRECTORY"
fi
toolbox_url=$(find_toolbox_url)
echo "Downloading toolbox from ${toolbox_url}..."
curl -sL ${toolbox_url} -o toolbox.tar
tar -xf toolbox.tar
mv toolbox $TOOLBOX_DIRECTORY
case $DIST in
Darwin)
sudo chown -R $SEMAPHORE_AGENT_INSTALLATION_USER $TOOLBOX_DIRECTORY
;;
Linux)
sudo chown -R $SEMAPHORE_AGENT_INSTALLATION_USER:$SEMAPHORE_AGENT_INSTALLATION_USER $TOOLBOX_DIRECTORY
;;
esac
sudo -u $SEMAPHORE_AGENT_INSTALLATION_USER -H bash $TOOLBOX_DIRECTORY/install-toolbox
echo "source ~/.toolbox/toolbox" >> $USER_HOME_DIRECTORY/.bash_profile
rm toolbox.tar
#
# Create agent config
#
SEMAPHORE_AGENT_DISCONNECT_AFTER_JOB=${SEMAPHORE_AGENT_DISCONNECT_AFTER_JOB:-false}
SEMAPHORE_AGENT_DISCONNECT_AFTER_IDLE_TIMEOUT=${SEMAPHORE_AGENT_DISCONNECT_AFTER_IDLE_TIMEOUT:-0}
AGENT_CONFIG=$(cat <<-END
endpoint: "$SEMAPHORE_ENDPOINT"
token: "$SEMAPHORE_REGISTRATION_TOKEN"
no-https: false
shutdown-hook-path: "$SEMAPHORE_AGENT_SHUTDOWN_HOOK"
disconnect-after-job: $SEMAPHORE_AGENT_DISCONNECT_AFTER_JOB
disconnect-after-idle-timeout: $SEMAPHORE_AGENT_DISCONNECT_AFTER_IDLE_TIMEOUT
env-vars: []
files: []
fail-on-missing-files: false
END
)
AGENT_CONFIG_PATH="$AGENT_INSTALLATION_DIRECTORY/config.yaml"
echo "Creating agent config file at $AGENT_CONFIG_PATH..."
echo "$AGENT_CONFIG" > $AGENT_CONFIG_PATH
case $DIST in
Darwin)
sudo chown $SEMAPHORE_AGENT_INSTALLATION_USER $AGENT_CONFIG_PATH
;;
Linux)
sudo chown $SEMAPHORE_AGENT_INSTALLATION_USER:$SEMAPHORE_AGENT_INSTALLATION_USER $AGENT_CONFIG_PATH
;;
esac
SEMAPHORE_AGENT_SYSTEMD_RESTART=${SEMAPHORE_AGENT_SYSTEMD_RESTART:-always}
SEMAPHORE_AGENT_SYSTEMD_RESTART_SEC=${SEMAPHORE_AGENT_SYSTEMD_RESTART_SEC:-60}
#
# Check if we can use some kind of service manager to run the agent.
# We use systemd for Linux, and launchd for MacOS.
#
case $DIST in
Darwin)
create_launchd_daemon
;;
Linux)
create_systemd_service
;;
*)
echo "$DIST is not supported. You can still start the agent with '$AGENT_INSTALLATION_DIRECTORY/agent start --config $AGENT_CONFIG_PATH'."
;;
esac
echo "Done."