Skip to content

Commit ed08216

Browse files
authored
Merge pull request #3 from hoverinc/INFRA-4354
[INFRA-4354] create startup script after zone is picked
2 parents d160f17 + 051fb0b commit ed08216

1 file changed

Lines changed: 56 additions & 76 deletions

File tree

action.sh

Lines changed: 56 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -193,82 +193,13 @@ function start_vm {
193193

194194
echo "The new GCE VM will be ${VM_ID}"
195195

196-
startup_script="
197-
# Create a systemd service in charge of shutting down the machine once the workflow has finished
198-
cat <<-EOF > /etc/systemd/system/shutdown.sh
199-
#!/bin/sh
200-
sleep ${shutdown_timeout}
201-
gcloud compute instances delete $VM_ID --zone=$machine_zone --quiet
202-
EOF
203-
204-
cat <<-EOF > /etc/systemd/system/shutdown.service
205-
[Unit]
206-
Description=Shutdown service
207-
[Service]
208-
ExecStart=/etc/systemd/system/shutdown.sh
209-
[Install]
210-
WantedBy=multi-user.target
211-
EOF
212-
213-
chmod +x /etc/systemd/system/shutdown.sh
214-
systemctl daemon-reload
215-
systemctl enable shutdown.service
216-
217-
cat <<-EOF > /usr/bin/gce_runner_shutdown.sh
218-
#!/bin/sh
219-
echo \"✅ Self deleting $VM_ID in ${machine_zone} in ${shutdown_timeout} seconds ...\"
220-
# We tear down the machine by starting the systemd service that was registered by the startup script
221-
systemctl start shutdown.service
222-
EOF
223-
224-
# Install driver if this is a deeplearning image is specified
225-
if [ \"${image_project}\" == \"deeplearning-platform-release\" ]; then
226-
sudo /opt/deeplearning/install-driver.sh
227-
fi
228-
229-
# See: https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/running-scripts-before-or-after-a-job
230-
echo "ACTIONS_RUNNER_HOOK_JOB_COMPLETED=/usr/bin/gce_runner_shutdown.sh" >.env
231-
gcloud compute instances add-labels ${VM_ID} --zone=${machine_zone} --labels=gh_ready=0 && \\
232-
RUNNER_ALLOW_RUNASROOT=1 ./config.sh --url https://github.com/${GITHUB_REPOSITORY} --token ${RUNNER_TOKEN} --labels ${VM_ID} --unattended ${ephemeral_flag} --disableupdate && \\
233-
./svc.sh install && \\
234-
./svc.sh start && \\
235-
gcloud compute instances add-labels ${VM_ID} --zone=${machine_zone} --labels=gh_ready=1
236-
# 3 days represents the max workflow runtime. This will shutdown the instance if everything else fails.
237-
nohup sh -c \"sleep 3d && gcloud --quiet compute instances delete ${VM_ID} --zone=${machine_zone}\" > /dev/null &
238-
"
239-
240-
if $actions_preinstalled ; then
241-
echo "✅ Startup script won't install GitHub Actions (pre-installed)"
242-
startup_script="#!/bin/bash
243-
cd /actions-runner
244-
$startup_script"
245-
else
246-
if [[ "$runner_ver" = "latest" ]]; then
247-
latest_ver=$(curl -sL https://api.github.com/repos/actions/runner/releases/latest | jq -r '.tag_name' | sed -e 's/^v//')
248-
runner_ver="$latest_ver"
249-
echo "✅ runner_ver=latest is specified. v$latest_ver is detected as the latest version."
250-
if [[ -z "$latest_ver" || "null" == "$latest_ver" ]]; then
251-
echo "❌ could not retrieve the latest version of a runner"
252-
exit 2
253-
fi
254-
fi
255-
echo "✅ Startup script will install GitHub Actions v$runner_ver"
256-
if $arm ; then
257-
startup_script="#!/bin/bash
258-
mkdir /actions-runner
259-
cd /actions-runner
260-
curl -o actions-runner-linux-arm64-${runner_ver}.tar.gz -L https://github.com/actions/runner/releases/download/v${runner_ver}/actions-runner-linux-arm64-${runner_ver}.tar.gz
261-
tar xzf ./actions-runner-linux-arm64-${runner_ver}.tar.gz
262-
./bin/installdependencies.sh && \\
263-
$startup_script"
264-
else
265-
startup_script="#!/bin/bash
266-
mkdir /actions-runner
267-
cd /actions-runner
268-
curl -o actions-runner-linux-x64-${runner_ver}.tar.gz -L https://github.com/actions/runner/releases/download/v${runner_ver}/actions-runner-linux-x64-${runner_ver}.tar.gz
269-
tar xzf ./actions-runner-linux-x64-${runner_ver}.tar.gz
270-
./bin/installdependencies.sh && \\
271-
$startup_script"
196+
if [[ "$runner_ver" = "latest" ]]; then
197+
latest_ver=$(curl -sL https://api.github.com/repos/actions/runner/releases/latest | jq -r '.tag_name' | sed -e 's/^v//')
198+
runner_ver="$latest_ver"
199+
echo "✅ runner_ver=latest is specified. v$latest_ver is detected as the latest version."
200+
if [[ -z "$latest_ver" || "null" == "$latest_ver" ]]; then
201+
echo "❌ could not retrieve the latest version of a runner"
202+
exit 2
272203
fi
273204
fi
274205

@@ -326,6 +257,55 @@ function start_vm {
326257
zones=$(gcloud compute accelerator-types list --verbosity=error --filter="name=${accel_only} AND zone:us-*" --format="value(zone)" | shuf)
327258
for zone in $zones; do
328259
machine_zone=$zone
260+
startup_script="
261+
#!/bin/bash
262+
mkdir /actions-runner
263+
cd /actions-runner
264+
curl -o actions-runner-linux-x64-${runner_ver}.tar.gz -L https://github.com/actions/runner/releases/download/v${runner_ver}actions-runner-linux-x64-${runner_ver}.tar.gz
265+
tar xzf ./actions-runner-linux-x64-${runner_ver}.tar.gz
266+
./bin/installdependencies.sh && \\
267+
# Create a systemd service in charge of shutting down the machine once the workflow has finished
268+
cat <<-EOF > /etc/systemd/system/shutdown.sh
269+
#!/bin/sh
270+
sleep ${shutdown_timeout}
271+
gcloud compute instances delete $VM_ID --zone=$machine_zone --quiet
272+
EOF
273+
274+
cat <<-EOF > /etc/systemd/system/shutdown.service
275+
[Unit]
276+
Description=Shutdown service
277+
[Service]
278+
ExecStart=/etc/systemd/system/shutdown.sh
279+
[Install]
280+
WantedBy=multi-user.target
281+
EOF
282+
283+
chmod +x /etc/systemd/system/shutdown.sh
284+
systemctl daemon-reload
285+
systemctl enable shutdown.service
286+
287+
cat <<-EOF > /usr/bin/gce_runner_shutdown.sh
288+
#!/bin/sh
289+
echo \"✅ Self deleting $VM_ID in ${machine_zone} in ${shutdown_timeout} seconds ...\"
290+
# We tear down the machine by starting the systemd service that was registered by the startup script
291+
systemctl start shutdown.service
292+
EOF
293+
294+
# Install driver if this is a deeplearning image is specified
295+
if [ \"${image_project}\" == \"deeplearning-platform-release\" ]; then
296+
sudo /opt/deeplearning/install-driver.sh
297+
fi
298+
299+
# See: https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/running-scripts-before-or-after-a-job
300+
echo "ACTIONS_RUNNER_HOOK_JOB_COMPLETED=/usr/bin/gce_runner_shutdown.sh" >.env
301+
gcloud compute instances add-labels ${VM_ID} --zone=${machine_zone} --labels=gh_ready=0 && \\
302+
RUNNER_ALLOW_RUNASROOT=1 ./config.sh --url https://github.com/${GITHUB_REPOSITORY} --token ${RUNNER_TOKEN} --labels ${VM_ID} --unattended ${ephemeral_flag} --disableupdate && \\
303+
./svc.sh install && \\
304+
./svc.sh start && \\
305+
gcloud compute instances add-labels ${VM_ID} --zone=${machine_zone} --labels=gh_ready=1
306+
# 3 days represents the max workflow runtime. This will shutdown the instance if everything else fails.
307+
nohup sh -c \"sleep 3d && gcloud --quiet compute instances delete ${VM_ID} --zone=${machine_zone}\" > /dev/null &
308+
"
329309
create_vm
330310
[[ $? -eq 0 ]] && break
331311
done

0 commit comments

Comments
 (0)