Skip to content

Commit 4d57d9c

Browse files
committed
bob-l2: implement fetch-metadata service
1 parent c5a5e54 commit 4d57d9c

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
rm -f /etc/metadata.env # just in case
12+
touch /etc/metadata.env
13+
14+
METADATA_URL="http://169.254.169.254/computeMetadata/v1/instance/attributes/"
15+
fetch_metadata_value() {
16+
local key="$1"
17+
curl -s -H "Metadata-Flavor: Google" "${METADATA_URL}${key}"
18+
}
19+
20+
for key in \
21+
BOB_L2_OP_NODE_CIDR \
22+
BOB_L2_BACKRUNS_IP \
23+
BOB_L2_TX_STREAM_IP
24+
do
25+
value=$(fetch_metadata_value "$key")
26+
27+
if [ "$(echo "$value" | wc -l)" -ne 1 ]; then
28+
echo "Error: Value for $key contains newlines"
29+
exit 1
30+
fi
31+
32+
if echo "$value" | grep -qvE '^[a-zA-Z0-9.,@:/_ -]*$'; then
33+
echo "Error: Value for $key contains bad characters"
34+
exit 1
35+
fi
36+
37+
echo "METADATA_$key='$value'" >> /etc/metadata.env
38+
done

0 commit comments

Comments
 (0)