forked from DeterminateSystems/macos-ephemeral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-without-nix.sh
201 lines (164 loc) · 6.12 KB
/
setup-without-nix.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
#!/bin/sh
set -eux
set -o pipefail
(
date
ls /Volumes || true
ls /Volumes/CONFIG || true
echo "%admin ALL = NOPASSWD: ALL" > /etc/sudoers.d/passwordless
while ! ping -c1 github.com; do
sleep 1
done
if [ "$(uname -m)" = "arm64" ]; then
jobset=nixpkgs-unstable-aarch64-darwin
arch=aarch64-darwin
else
jobset=trunk
arch=x86_64-darwin
fi
cd ~root
while [ ! -d /Volumes/CONFIG ]; do
echo "Waiting for /Volumes/CONFIG to exist ..."
sleep 1
done
if [ ! -f /Volumes/CONFIG/buildkite-agent/sshkey ]; then
mkdir -p "$(dirname /Volumes/CONFIG/buildkite-agent/sshkey)" || true
echo "Waiting a second in case the config volume shows up"
sleep 5
fi
if [ ! -f /Volumes/CONFIG/buildkite-agent/sshkey ]; then
mkdir -p "$(dirname /Volumes/CONFIG/buildkite-agent/sshkey)" || true
ssh-keygen -t ed25519 -f /Volumes/CONFIG/buildkite-agent/sshkey -N ""
fi
# install xcode (for git) ugh
# inspired by https://gist.github.com/mokagio/b974620ee8dcf5c0671f
# and yes, this file is required, or else the command line tools don't show up in softwareupdate -l
touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
softwareupdate -i "$(softwareupdate -l | grep "\*.*Command Line" | tail -n 1 | sed 's/^[^C]* //')"
if ! git --help &>/dev/null; then
# Didn't find command line tools first time, try again?
softwareupdate -i "$(softwareupdate -l | grep "\*.*Command Line" | tail -n 1 | sed 's/^[^C]* //')"
fi
if ! pgrep -qf "tailscaled"; then
# tailscale
curl -L -o tailscale "https://hydra.nixos.org/job/nixpkgs/$jobset/tailscale.$arch/latest/download/1/out/bin/tailscale"
curl -L -o tailscaled "https://hydra.nixos.org/job/nixpkgs/$jobset/tailscale.$arch/latest/download/1/out/bin/tailscaled"
chmod +x ./tailscale{,d}
mkdir -p /usr/local/bin/
mv ./tailscaled /usr/local/bin/tailscaled
mv ./tailscale /usr/local/bin/tailscale
cat <<EOF > /Library/LaunchDaemons/com.tailscale.tailscaled.plist
<?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>com.tailscale.tailscaled</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/tailscaled</string>
<string>-state</string>
<string>mem:</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/var/log/tailscaled.log</string>
<key>StandardOutPath</key>
<string>/var/log/tailscaled.log</string>
</dict>
</plist>
EOF
launchctl load /Library/LaunchDaemons/com.tailscale.tailscaled.plist
launchctl start /Library/LaunchDaemons/com.tailscale.tailscaled.plist || true
cat <<EOF > /Library/LaunchDaemons/com.tailscale.tailscale-auth.plist
<?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>com.tailscale.tailscale-auth</string>
<key>ProgramArguments</key>
<array>
<string>/bin/sh</string>
<string>-xc</string>
<string>sleep 5 ; /usr/local/bin/tailscale up --accept-routes --auth-key file:/var/root/tailscale.token && (while true; do sleep 2073600; done)</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardErrorPath</key>
<string>/var/log/tailscale-auth.log</string>
<key>StandardOutPath</key>
<string>/var/log/tailscale-auth.log</string>
</dict>
</plist>
EOF
launchctl load /Library/LaunchDaemons/com.tailscale.tailscale-auth.plist
launchctl start /Library/LaunchDaemons/com.tailscale.tailscale-auth.plist || true
fi
if ! pgrep -qf "buildkite-agent"; then
# buildkite-agent
curl -sL https://raw.githubusercontent.com/buildkite/agent/main/install.sh -o install-buildkite.sh
HOME=/tmp/buildkite-agent-staging bash ./install-buildkite.sh
mv /tmp/buildkite-agent-staging/.buildkite-agent /var/lib/buildkite-agent
cat <<EOF > /var/lib/buildkite-agent/buildkite-agent.cfg
token="$(cat /Volumes/CONFIG/buildkite.token)"
name="%hostname-%n"
spawn=1
disconnect-after-job=true
meta-data="queue=bootstrap,mac=1,nix=0,system=$arch"
build-path="/var/lib/buildkite-agent/builds"
hooks-path="/var/lib/buildkite-agent/hooks"
tags-from-host=true
EOF
cp /Volumes/CONFIG/buildkite-agent/sshkey ~ephemeraladmin/.ssh/id_ed25519
cp /Volumes/CONFIG/buildkite-agent/sshkey.pub ~ephemeraladmin/.ssh/id_ed25519.pub
chmod 600 ~ephemeraladmin/.ssh/id_ed25519
chown ephemeraladmin:staff \
~ephemeraladmin/.ssh/id_ed25519 \
~ephemeraladmin/.ssh/id_ed25519.pub
mkdir -p /var/lib/buildkite-agent/hooks
cat <<'EOF' > /var/lib/buildkite-agent/hooks/agent-shutdown
#!/bin/sh
while sleep 1; do
for machine in bonk{,-{1,2,3,4,5}}; do
curl -X POST --connect-timeout 1 -v "http://$machine/erase-self"
sleep 1
done
done
EOF
chmod +x /var/lib/buildkite-agent/hooks/agent-shutdown
chown -R ephemeraladmin:staff /var/lib/buildkite-agent
touch /var/log/buildkite-agent.log
chown ephemeraladmin:staff /var/log/buildkite-agent.log
cat <<EOF > /Library/LaunchDaemons/com.buildkite.buildkite-agent.plist
<?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>com.buildkite.buildkite-agent</string>
<key>ProgramArguments</key>
<array>
<string>/var/lib/buildkite-agent/bin/buildkite-agent</string>
<string>start</string>
<string>--config</string>
<string>/var/lib/buildkite-agent/buildkite-agent.cfg</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/var/log/buildkite-agent.log</string>
<key>StandardOutPath</key>
<string>/var/log/buildkite-agent.log</string>
<key>UserName</key>
<string>ephemeraladmin</string>
</dict>
</plist>
EOF
launchctl load /Library/LaunchDaemons/com.buildkite.buildkite-agent.plist
launchctl start /Library/LaunchDaemons/com.buildkite.buildkite-agent.plist || true
fi
) 2>&1 | tee -a /var/log/mosyle-bootstrap-script.log