Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion runner.vm/files/usr/lib/systemd/system/gcpnetwork.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ Before=network-online.target
OnFailure=poweroff.target

[Service]
Type=simple
Type=oneshot
ExecStart=/usr/local/sbin/network.sh
RemainAfterExit=yes
StandardOutput=journal+console
StandardError=journal+console

Expand Down
2 changes: 1 addition & 1 deletion runner.vm/files/usr/lib/systemd/system/runner.service
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ OnFailure=poweroff.target
OnSuccess=poweroff.target

[Service]
Type=simple
Type=oneshot
ExecStart=/usr/local/bin/runner.sh
User=enf
WorkingDirectory=~
Expand Down
10 changes: 6 additions & 4 deletions runner.vm/files/usr/local/bin/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ GITHUB_RUNNER_REG_URL=$(${CURL} "http://metadata.google.internal/computeMetadata
GITHUB_RUNNER_TOKEN=$(${CURL} "http://metadata.google.internal/computeMetadata/v1/instance/attributes/runnerToken" -H "Metadata-Flavor: Google")
GITHUB_RUNNER_LABEL=$(${CURL} "http://metadata.google.internal/computeMetadata/v1/instance/attributes/runnerLabel" -H "Metadata-Flavor: Google")

RUNNER_RELEASE_URL=$(${CURL} https://api.github.com/repos/actions/runner/releases | \
jq -r 'first(.[] | select(.prerelease == false)) | .assets[] | if .name | test("actions-runner-linux-x64-[0-9.]+.tar") then .browser_download_url else empty end')
${CURL} "${RUNNER_RELEASE_URL}" | tar zx
RUNNER_RELEASE_URL=$(${CURL} https://api.github.com/repos/actions/runner/releases/latest | \
jq -e -r '.assets[] | if .name | test("actions-runner-linux-x64-[0-9.]+.tar") then .browser_download_url else empty end')
${CURL} -O "${RUNNER_RELEASE_URL}"
Comment on lines +10 to +12

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The jq -e flag is a new trick for me, nice.

Should we quote these variable assignments to protect against BASH code injection?

RUNNER_RELEASE_URL=$(...)

...would become:

RUNNER_RELEASE_URL="$(...)"

As a point of interest, I believe the jq query if-then snippet...

if .name | test("actions-runner-linux-x64-[0-9.]+.tar") then .browser_download_url else empty end

...could be simplified to this.

.name | test("actions-runner-linux-x64-[0-9.]+.tar") | .browser_download_url // empty

I haven't tried it, not certain.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we quote these variable assignments to protect against BASH code injection

Yeah, probably prudent to do so just for best practices purposes.

Not sure if I exactly understand your suggestion. It's easy enough to try -- no token needed or anything. If you find a more concise way I'd certainly be interested.

$ curl -s https://api.github.com/repos/actions/runner/releases/latest | jq -e -r '.assets[] | .name | test("actions-runner-linux-x64-[0-9.]+.tar") | .browser_download_url'
jq: error (at <stdin>:1242): Cannot index boolean with string "browser_download_url"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind on the jq thing, I see the if...else block behaves slightly different than I had thought when I wrote that comment. My mistake.

tar xf *.tar.*
rm *.tar.*

./config.sh --url ${GITHUB_RUNNER_REG_URL} --token ${GITHUB_RUNNER_TOKEN} --ephemeral --disableupdate --unattended --labels ${GITHUB_RUNNER_LABEL}
./config.sh --url "${GITHUB_RUNNER_REG_URL}" --token "${GITHUB_RUNNER_TOKEN}" --ephemeral --disableupdate --unattended --labels "${GITHUB_RUNNER_LABEL}"
./run.sh
6 changes: 6 additions & 0 deletions runner.vm/files/usr/local/sbin/network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ sysctl -q -w net.ipv6.conf.eth0.disable_ipv6=1
ip addr add 169.254.0.0/16 dev eth0
ip link set eth0 up

tries=100
until eval '(( $(< /sys/class/net/eth0/carrier) ))'; do
(( tries-- > 0 )) || exit 1
sleep 0.1
done

Comment on lines +22 to +27

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop only verifies that an Ethernet cable is connected...not that the network interface is actually functional. Here are two suggestions from ChatGPT to check that a network interface is functional in a BASH script, both of which I tried in my terminal.

The first starts a service dependent on network-online.target from the NetworkManager-wait-online.service. It waits until this target is reached, then returns.

systemd-run --wait --quiet --service-type=oneshot --property="After=network-online.target" /bin/true

This second example is simpler, it reads a file like the one that you have.

cat /sys/class/net/eth0/operstate

We would want this command to return up.

From ChatGPT:

/sys/class/net/eth0/operstate is a sysfs file that shows the operational state of the network interface eth0. The possible values for this file are unknown, notpresent, down, lowerlayerdown, testing, dormant, up. The operstate file reflects the current state of the network interface based on various factors, including the physical state of the interface, the state of the underlying protocol stack, and the administrative state of the interface.

/sys/class/net/eth0/carrier is another sysfs file that indicates the physical state of the network interface eth0. This file shows whether the link is up or down and is used to determine if the network cable is connected to the interface. The possible values for this file are 0 and 1, where 0 indicates that the link is down and 1 indicates that the link is up.

In summary, operstate reflects the overall state of the network interface, while carrier indicates the physical state of the link.

echo $(get_metadata name) > /proc/sys/kernel/hostname

IP=$(get_metadata network-interfaces/0/ip)
Expand Down