Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node): Configuration revamp (HostOS integration) #3390

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
import tarfile
import tempfile

ALLOWED_UNDECLARED_DEPENDENCIES = {
"ic-os/components/misc/fetch-property.sh": {
# fetch-property.sh checks existence of metrics.sh
"/opt/ic/bin/metrics.sh",
}
}
ALLOWED_UNDECLARED_DEPENDENCIES = {}

# Check file patterns /opt/ic/...
COMPONENT_FILE_PATTERN = r"/opt/ic/[^\s'\"},)]+"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Description=Set up hostname
Before=systemd-networkd.service
DefaultDependencies=no
After=bootstrap-ic-node.service
After=update-config.service
Wants=update-config.service

[Install]
WantedBy=multi-user.target
Expand Down
Copy link
Member Author

Choose a reason for hiding this comment

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

guestos/setup-hostname.sh will be deleted when GuestOS moves to the config revamp and we will used the consolidated, top level setup-hostname.sh

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

set -ex

# Read the network config variables from file. The file must be of the form
# "key=value" for each line with a specific set of keys permissible (see
# code below).
#
# Arguments:
# - $1: Name of the file to be read.
function read_variables() {
# Read limited set of keys. Be extra-careful quoting values as it could
# otherwise lead to executing arbitrary shell code!
while IFS="=" read -r key value; do
case "$key" in
"hostname") hostname="${value}" ;;
esac
done <"$1"
}

if [ -e /boot/config/network.conf ]; then
cat /boot/config/network.conf
read_variables /boot/config/network.conf
hostname="${hostname:-blank}"
else
hostname="unnamed"
fi

echo "${hostname}" >/run/ic-node/etc/hostname
mount --bind /run/ic-node/etc/hostname /etc/hostname
restorecon -v /etc/hostname
hostname "${hostname}"
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Before=systemd-networkd.service
After=systemd-tmpfiles-setup.service
Wants=dev-ipmi0.device
After=dev-ipmi0.device
After=update-config.service
Wants=update-config.service

[Service]
Type=oneshot
Expand Down
108 changes: 0 additions & 108 deletions ic-os/components/early-boot/setup-hostname/hostos/setup-hostname.sh

This file was deleted.

122 changes: 94 additions & 28 deletions ic-os/components/early-boot/setup-hostname/setup-hostname.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,98 @@
#!/bin/bash

set -ex

# Read the network config variables from file. The file must be of the form
# "key=value" for each line with a specific set of keys permissible (see
# code below).
#
# Arguments:
# - $1: Name of the file to be read.
function read_variables() {
# Read limited set of keys. Be extra-careful quoting values as it could
# otherwise lead to executing arbitrary shell code!
while IFS="=" read -r key value; do
case "$key" in
"hostname") hostname="${value}" ;;
esac
done <"$1"
set -e

# Set the transient or persistent hostname.

source /opt/ic/bin/logging.sh
source /opt/ic/bin/metrics.sh
source /opt/ic/bin/config.sh

SCRIPT="$(basename $0)[$$]"

# Get keyword arguments
for argument in "${@}"; do
case ${argument} in
-f=* | --file=*)
FILE="${argument#*=}"
shift
;;
-h | --help)
echo 'Usage:
Set Transient Or Persistent Hostname

Arguments:
-f=, --file= optional: specify the file containing the node-id (Default: /boot/config/node-id)
-h, --help show this help message and exit
-t=, --type= mandatory: specify the node type (Examples: host, guest, boundary...)
'
exit 1
;;
-t=* | --type=*)
TYPE="${argument#*=}"
shift
;;
*)
echo "Error: Argument is not supported."
exit 1
;;
esac
done

# Set arguments if undefined
FILE="${FILE:=/boot/config/node-id}"

function validate_arguments() {
if [ "${FILE}" == "" -o "${TYPE}" == "" ]; then
$0 --help
fi
}

function read_config_variables() {
mgmt_mac=$(get_config_value '.icos_settings.mgmt_mac')
mgmt_mac=${mgmt_mac//:/} # Remove colons from mgmt_mac
config_hostname=$(get_config_value '.guestos_settings.guestos_dev_settings.hostname')
}

function construct_hostname() {
if [ "${config_hostname}" != "" ] && [ "${config_hostname}" != "null" ]; then
HOSTNAME=${config_hostname}
write_log "Using manually configured hostname: ${HOSTNAME}"
elif [[ -r ${FILE} && $(cat ${FILE}) != "" ]]; then
HOSTNAME=$(echo ${TYPE}-${mgmt_mac}-$(cat ${FILE}))
write_log "Using hostname: ${HOSTNAME}"
write_metric "setup_hostname" \
"1" \
"Hostname" \
"gauge"
else
HOSTNAME=$(echo ${TYPE}-${mgmt_mac})
write_log "Using hostname: ${HOSTNAME}"
write_metric "setup_hostname" \
"0" \
"Hostname" \
"gauge"
fi
}

function setup_hostname() {
if [ "$(mount | grep '/etc/hostname')" ]; then
umount /etc/hostname
fi

if [ -d /run/ic-node/etc ]; then
echo "${HOSTNAME}" >/run/ic-node/etc/hostname
mount --bind /run/ic-node/etc/hostname /etc/hostname
restorecon -v /etc/hostname
hostname "${HOSTNAME}"
fi
}

function main() {
validate_arguments
read_config_variables
construct_hostname
setup_hostname
}

if [ -e /boot/config/network.conf ]; then
cat /boot/config/network.conf
read_variables /boot/config/network.conf
hostname="${hostname:-blank}"
else
hostname="unnamed"
fi

echo "${hostname}" >/run/ic-node/etc/hostname
mount --bind /run/ic-node/etc/hostname /etc/hostname
restorecon -v /etc/hostname
hostname "${hostname}"
main
4 changes: 2 additions & 2 deletions ic-os/components/guestos.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ component_files = {
# early-boot
Label("early-boot/relabel-machine-id/guestos/relabel-machine-id.sh"): "/opt/ic/bin/relabel-machine-id.sh",
Label("early-boot/relabel-machine-id/relabel-machine-id.service"): "/etc/systemd/system/relabel-machine-id.service",
Label("early-boot/setup-hostname/setup-hostname.sh"): "/opt/ic/bin/setup-hostname.sh",
Label("early-boot/setup-hostname/setup-hostname.service"): "/etc/systemd/system/setup-hostname.service",
Label("early-boot/setup-hostname/guestos/setup-hostname.sh"): "/opt/ic/bin/setup-hostname.sh",
Label("early-boot/setup-hostname/guestos/setup-hostname.service"): "/etc/systemd/system/setup-hostname.service",
Label("early-boot/setup-hostname/hostname-empty"): "/etc/hostname",
Label("early-boot/save-machine-id/save-machine-id.sh"): "/opt/ic/bin/save-machine-id.sh",
Label("early-boot/save-machine-id/save-machine-id.service"): "/etc/systemd/system/save-machine-id.service",
Expand Down
Loading
Loading