Skip to content

Commit 9432c13

Browse files
author
Joe K
authored
Merge pull request #126 from norvellj/main
Add set-hostname-imds service
2 parents 49a9f1b + 6eeb41c commit 9432c13

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

amazon-ec2-net-utils.spec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ make install DESTDIR=%{buildroot} PREFIX=/usr
3232
/usr/lib/systemd/system/policy-routes@.service
3333
/usr/lib/systemd/system/refresh-policy-routes@.service
3434
/usr/lib/systemd/system/refresh-policy-routes@.timer
35+
/usr/lib/systemd/system/set-hostname-imds.service
3536

3637
/usr/lib/udev/rules.d/99-vpc-policy-routes.rules
3738
%{_bindir}/setup-policy-routes
39+
%{_bindir}/set-hostname-imds
3840
%dir %{_datarootdir}/amazon-ec2-net-utils
3941
%{_datarootdir}/amazon-ec2-net-utils/lib.sh
4042

bin/set-hostname-imds.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
#
3+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"). You may
6+
# not use this file except in compliance with the License. A copy of the
7+
# License is located at
8+
#
9+
# http://aws.amazon.com/apache2.0/
10+
#
11+
# or in the "license" file accompanying this file. This file is distributed
12+
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
# express or implied. See the License for the specific language governing
14+
# permissions and limitations under the License.
15+
16+
set -eCo pipefail
17+
18+
libdir=${LIBDIR_OVERRIDE:-/usr/share/amazon-ec2-net-utils}
19+
20+
. "${libdir}/lib.sh"
21+
22+
if [ -s /etc/hostname ]; then
23+
info "Static hostname is already set - not modifying existing hostname"
24+
exit 0
25+
fi
26+
27+
hostname=$(get_imds local-hostname)
28+
29+
if [ -n "$hostname" ]; then
30+
info "Setting hostname to ${hostname} retrieved from IMDS"
31+
hostnamectl hostname ${hostname}
32+
else
33+
error "Unable to retrieve hostname from IMDS - aborting"
34+
exit 1
35+
fi

lib/lib.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ get_token() {
7171
while [ "$(date +%s)" -lt $deadline ]; do
7272
for ep in "${imds_endpoints[@]}"; do
7373
set +e
74-
imds_token=$(make_token_request "$ep" "$intf")
74+
if [ -n "$intf" ]; then
75+
imds_token=$(make_token_request "$ep" "$intf")
76+
fi
7577

7678
if [ -z "$imds_token" ]; then
7779
imds_token=$(make_token_request "$ep")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[Unit]
2+
Description=Sets the local hostname defined in IMDS
3+
After=network-online.target
4+
Requires=network-online.target
5+
6+
[Service]
7+
Type=oneshot
8+
ExecStart=/usr/bin/set-hostname-imds
9+
StandardOutput=journal+console
10+
StandardError=journal+console
11+
12+
[Install]
13+
WantedBy=multi-user.target

0 commit comments

Comments
 (0)