Skip to content

Commit 81ea927

Browse files
committed
bob-l2: implement fetch-metadata service
1 parent 9a6fb97 commit 81ea927

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[Unit]
2+
Description=Fetch some environment variables from metadata service
3+
After=network.target network-setup.service
4+
Requires=network-setup.service
5+
6+
[Service]
7+
Type=oneshot
8+
ExecStart=/usr/bin/fetch-metadata.sh
9+
RemainAfterExit=yes
10+
StandardOutput=journal
11+
StandardError=journal
12+
13+
[Install]
14+
WantedBy=minimal.target
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[Unit]
2+
After=fetch-metadata.service
3+
Requires=fetch-metadata.service
4+
5+
[Service]
6+
EnvironmentFile=/etc/metadata.env
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[Unit]
2+
After=fetch-metadata.service
3+
Requires=fetch-metadata.service
4+
5+
[Service]
6+
EnvironmentFile=/etc/metadata.env
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/sh
2+
set -eu -o pipefail
3+
4+
# This script fetches couple of pre-defined keys from instance metadata server
5+
# and writes them to /etc/metadata.env in the format:
6+
# METADATA_{KEY}='{VALUE}'
7+
#
8+
# It also checks that received values do not contain newlines and conform to
9+
# ^[a-zA-Z0-9.,@:/_ -]*$
10+
11+
# TODO: we should have fallback for local development in QEMU
12+
13+
rm -f /etc/metadata.env # just in case
14+
touch /etc/metadata.env
15+
16+
METADATA_URL="http://169.254.169.254/computeMetadata/v1/instance/attributes/"
17+
fetch_metadata_value() {
18+
local key="$1"
19+
curl -s -H "Metadata-Flavor: Google" "${METADATA_URL}${key}"
20+
}
21+
22+
for key in \
23+
BOB_L2_BACKRUNS_IP \
24+
BOB_L2_TX_STREAM_IP \
25+
BOB_L2_OP_NODE_CIDR
26+
do
27+
value=$(fetch_metadata_value "$key")
28+
29+
if [ "$(echo "$value" | wc -l)" -ne 1 ]; then
30+
echo "Error: Value for $key contains newlines"
31+
exit 1
32+
fi
33+
34+
if echo "$value" | grep -qvE '^[a-zA-Z0-9.,@:/_ -]*$'; then
35+
echo "Error: Value for $key contains bad characters"
36+
exit 1
37+
fi
38+
39+
echo "METADATA_$key='$value'" >> /etc/metadata.env
40+
done

0 commit comments

Comments
 (0)