Browse Source

Add support for interfaces, safety checks, fix shellcheck errors

master
fnux 3 years ago
parent
commit
ee5d485deb
  1. 1
      README.md
  2. 66
      uncloud-init

1
README.md

@ -5,3 +5,4 @@ This tool takes care of initializing and maintaining virtual machines in
* Growing root partition/filesystem when the underlying disk is expanded.
* Fetch and deploy SSH keys.
* UP networks interfaces.

66
uncloud-init

@ -3,9 +3,19 @@
# Initialize an uncloud VM. This script depends on:
# curl grep getent (i.e. glibc) curl,dirname (i.e. coreutils)
# TODO: complete other tasks even if one fail.
# TODO: do not up ALL network interfaces.
# TODO: write configuration instead of manually setting interfaces up?
if [ "$(whoami)" != 'root' ]; then
echo "This script must be run as root." >&2
exit 1
fi
###
# TODO: handle command-line parameters.
ENABLE_NETWORKING=1
DEPLOY_SSH_AUTHORIZED_KEYS=1
OVERRIDE_EXISTING_SSH_AUTHORIZED_KEYS=0
GROW_ROOT_PARTITION=0
@ -14,8 +24,14 @@ SSH_USER=root
SSH_DAEMON_CONFIG=/etc/ssh/sshd_config
UNCLOUD_METADATA_SERVER=https://key.wf
if [ ! $ENABLE_NETWORKING ] && [ $DEPLOY_SSH_AUTHORIZED_KEYS ]; then
echo "SSH key deployment requires networking, please review uncloud-init \
configuration." >&2
exit 1
fi
###
# SSH key deployment logic.
# Internal logic.
deploy_ssh_authorized_keys () {
# Ensure SSHD configuration can be found.
@ -31,46 +47,66 @@ deploy_ssh_authorized_keys () {
fi
fi
# Get home directory of SSH_USER.
# Get home directory of SSH_USER. User might not exist or have a home
# directory.
homedir=$(getent passwd "$SSH_USER" | cut -d: -f6)
if [ $? != 0 ]; then
echo "Could not resolve user $SSH_USER." >&2
if [ "$homedir" = "" ]; then
echo "Could not resolve home directory of user $SSH_USER." >&2
exit 1
fi
# Fetch and deploy SSH keys from metadata server.
authorized_keys_file="$homedir/.ssh/authorized_keys"
mkdir -p $(dirname "$authorized_keys_file")
mkdir -p "$(dirname "$authorized_keys_file")"
if [ -f "$authorized_keys_file" ] \
&& if [ ! $OVERRIDE_EXISTING_SSH_AUTHORIZED_KEYS ];
&& [ ! $OVERRIDE_EXISTING_SSH_AUTHORIZED_KEYS ]; then
echo "Aborting SSH key deployement to not override existing $authorized_keys_file."
echo "You can change this behavior with the OVERRIDE_EXISTING_SSH_AUTHORIZED_KEYS flag."
return
fi
curl "$METDATA_SERVER/fnux" --output $authorized_keys_file
curl "$UNCLOUD_METADATA_SERVER/fnux" --output "$authorized_keys_file"
}
###
# Partition/filesystem growth logic.
grow_root_partition () {
# TODO
growpart_script='uncloud-init-growpart'
if [ -x "$(command -v "$growpart_script")" ]; then
# TODO: this command seems quite fragile...
# sh growpart -q /dev/vda 3 > /dev/null;
true # no-op
else
echo "Could not find or execute $growpart_script." >&2
exit 1
fi
}
up_network_interfaces () {
interfaces=$(ip -o link | cut -d: -f2 | grep -v '^ lo')
for i in $interfaces; do
ip link set dev "$i" up
done
}
###
# Entrypoint.
if [ $ENABLE_NETWORKING ]; then
routine='up main network interface'
echo "--- RUNNING $routine..."
up_network_interfaces
echo "--- DONE with $routine."
fi
if [ $DEPLOY_SSH_AUTHORIZED_KEYS ]; then
routine='SSH authorized_keys deployment routine'
echo "--- RUNNING $routine..."
deploy_ssh_authorized_keys()
deploy_ssh_authorized_keys
echo "--- DONE with $routine."
fi
if [ $GROW_ROOT_PARTITION ]: then
routine='SSH authorized_keys deployment routine'
if [ $GROW_ROOT_PARTITION ]; then
routine='growing root partition and filesystem'
echo "--- RUNNING $routine..."
grow_root_partition()
grow_root_partition
echo "--- DONE with $routine."
fi

Loading…
Cancel
Save