#!/bin/bash MIAOU_BASEDIR=$(readlink -f "$(dirname "$0")/..") # shellcheck source=/dev/null . "$MIAOU_BASEDIR/lib/functions.sh" readonly MIAOU_BASEDIR miaou_init EXPANDED_CONF="$MIAOU_CONFIGDIR/miaou.expanded.yaml" NEW_GROUP=lxd readonly NEW_GROUP EXPANDED_CONF on_exit() { if [ -n "${1:-}" ]; then echowarn "Aborted by $1" elif [ "${status:-}" -ne 0 ]; then echowarn "Failure (status $status)" fi } function prepare_lxd { local PREFIX="lxd:prepare" # test group lxd assign to current user if ! groups | grep -q lxd; then echo "define lxd and assign to user <$USER>" sudo groupadd --force "$NEW_GROUP" sudo usermod --append --groups "$NEW_GROUP" "$(whoami)" realcommand="$MIAOU_BASEDIR/lib/install.sh" sg "$NEW_GROUP" -c "EMAIL=$valid_email $realcommand SESSION_RELOAD_REQUIRED $TARGET" set +e disable_all_signals sudo su - "$(whoami)" kill -9 "$PPID" # no further processing because exec has been called! else echo "user <$USER> already belongs to group !" fi sudo /opt/miaou-bash/tools/idem_apt_install lxd btrfs-progs sudo tee /etc/systemd/system/lxd-containers-restart-on-failure.service &>/dev/null </dev/null; then echo "bridge down, so initialization will use default preseed..." if [[ $(printenv container) == 'lxc' ]]; then echo "--------------------------------" echo "nested configuration applying..." echo "--------------------------------" cat < found implies it has been already initialized!" fi set_alias 'sameuser' "exec @ARG1@ -- su --whitelist-environment container,container_hostname - $(whoami)" set_alias 'login' 'exec @ARGS@ --mode interactive -- /bin/bash -c $@${user:-root} - exec su --whitelist-environment container,container_hostname - ' set_alias 'll' 'list -c ns4mDN' # test environment container hostname local env_container_hostname env_container_hostname=$(sg $NEW_GROUP -c 'lxc profile get default environment.container_hostname') if [[ -z "$env_container_hostname" ]]; then env_container_hostname=$(hostname -s) if env | grep -q container_hostname; then local previous_container_hostname previous_container_hostname=$(env | grep container_hostname | cut -d '=' -f2) env_container_hostname="$previous_container_hostname $env_container_hostname" fi echo -n "set environment container_hostname to <$env_container_hostname> ... " sg $NEW_GROUP -c "lxc profile set default environment.container_hostname \"$env_container_hostname\"" PREFIX="" echoinfo DONE else echo "environment container_hostname <$env_container_hostname> already defined!" fi if ! grep -q "root:$(id -u):1" /etc/subuid; then echo -n "saving subuid, subgid permissions for <$(whoami)> ..." printf "root:$(id -u):1\n" | sudo tee -a /etc/subuid /etc/subgid &>/dev/null PREFIX="" echoinfo DONE else echo "subuid, subgid allowing <$(whoami)> already done!" fi if [[ ! -d "$HOME/LXD/SHARED" ]]; then echo -n "$HOME/LXD/SHARED creating ... " mkdir "$HOME/LXD/SHARED" -p PREFIX="" echoinfo DONE else echo "folder <$HOME/LXD/SHARED> already created!" fi if [[ ! -d "$HOME/LXD/BACKUP" ]]; then echo -n "$HOME/LXD/BACKUP creating ... " mkdir "$HOME/LXD/BACKUP" -p PREFIX="" echoinfo DONE else echo "folder <$HOME/LXD/BACKUP> already created!" fi } function set_alias { local name="$1" local command="$2" if ! lxc alias list -f csv | grep -q "^$name,"; then echo -n "defining new lxc alias <$name> ..." lxc alias add "$name" "$command" PREFIX="" echoinfo DONE else echo "lxc alias <$name> already defined!" fi } function miaou_evalfrombashrc() { local PREFIX="miaou:bashrc" output=$( /opt/miaou-bash/tools/append_or_replace \ "^eval \"\\$\(.*miaou/lib/install.sh shellenv\)\"$" \ "eval \"\$($MIAOU_BASEDIR/lib/install.sh shellenv)\"" \ "$HOME/.bashrc" ) if [[ "$output" == "appended" ]]; then echo "new path <$MIAOU_BASEDIR> created!" SESSION_RELOAD_REQUIRED=true else echo "path <$MIAOU_BASEDIR> already loaded!" fi } function ask_target() { PS3='Choose miaou target purpose: ' foods=("Dev" "Beta" "Prod") select ans in "${foods[@]}"; do builtin echo "${ans^^}" break done } function check_credential { local PREFIX="check:credential" check_yaml_defined_value /etc/miaou/defaults.yaml 'credential.username' && check_yaml_defined_value /etc/miaou/defaults.yaml 'credential.shadow' && check_yaml_defined_value /etc/miaou/defaults.yaml 'credential.email' } function check_target() { case "${TARGET^^}" in DEV) ;; BETA) ;; PROD) ;; *) if [[ -f /etc/miaou/defaults.yaml ]]; then # load already defined target in expanded conf TARGET=$(grep -Es "^target:" /etc/miaou/defaults.yaml | cut -d ' ' -f2) else TARGET=$(ask_target) fi ;; esac TARGET=${TARGET,,} # downcase return 0 } function miaou_configfiles() { local PREFIX="miaou:config" if [[ ! -d /etc/miaou ]]; then echo -n "configuration initializing ..." sudo mkdir -p /etc/miaou sudo chown "$USER" /etc/miaou PREFIX="" echoinfo DONE fi if [[ ! -f /etc/miaou/defaults.yaml ]]; then echo -n "building /etc/miaou/defaults.yaml for the first time..." shadow_passwd=$(sudo grep "$CURRENT_USER" /etc/shadow | cut -d ':' -f2) env current_user="$CURRENT_USER" shadow_passwd="$shadow_passwd" valid_email="$valid_email" tera -e --env-key env --env-only -t "$MIAOU_BASEDIR/templates/etc/defaults.yaml.j2" -o /etc/miaou/defaults.yaml >/dev/null yq ".target=\"$TARGET\"" /etc/miaou/defaults.yaml -i PREFIX="" echoinfo DONE fi if [[ ! -f /etc/miaou/miaou.yaml ]]; then echo -n "building /etc/miaou/miaou.yaml for the first time..." cp "$MIAOU_BASEDIR/templates/etc/miaou.yaml.j2" /etc/miaou/miaou.yaml PREFIX="" echoinfo DONE fi PREVIOUS_TARGET="" echo "expanded configuration stored in <$MIAOU_CONFIGDIR>!" [[ -f "$EXPANDED_CONF" ]] && PREVIOUS_TARGET=$(grep -Es "^target:" "$EXPANDED_CONF" | cut -d ' ' -f2) if [[ "$PREVIOUS_TARGET" != "$TARGET" ]]; then if [[ -z "$PREVIOUS_TARGET" ]]; then echo "new target defined <$TARGET>" else echowarnn "TARGET has changed from <$PREVIOUS_TARGET> to <$TARGET>, do you agree?" if askConfirmation N; then echowarn "removing previous settings, please restart to apply changes" rm "$MIAOU_CONFIGDIR" -rf else echoerr "TARGET not accepted, exit" exit 102 fi fi yq ".target=\"$TARGET\"" /etc/miaou/defaults.yaml -i else echo "target <$TARGET> already defined!" fi } function opt_link() { if [[ $MIAOU_BASEDIR != '/opt/miaou' ]]; then if [[ -L '/opt/miaou' && -d '/opt/miaou' && $(readlink /opt/miaou) == "$MIAOU_BASEDIR" ]]; then echo "symbolic link /opt/miaou already set up!" else sudo rm -f /opt/miaou sudo ln -s "$MIAOU_BASEDIR" /opt/miaou echo "symbolic link /opt/miaou successfully defined!" fi else echo "real path /opt/miaou already set up!" fi } function miaou_resolver() { local PREFIX="miaou:resolver" bridge=$(ip addr show lxdbr0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1) gateway=$(ip route | grep default | cut -d' ' -f3) if command -v nmcli &>/dev/null; then if [[ ! -f /etc/NetworkManager/dispatcher.d/50-miaou-resolver ]]; then echo -n "use NetworkManager dispatcher to deal with LXD bridge automatically..." sudo cp "$MIAOU_BASEDIR/templates/network-manager/50-miaou-resolver" /etc/NetworkManager/dispatcher.d/ sudo chmod +x /etc/NetworkManager/dispatcher.d/50-miaou-resolver ACTIVE_CONNECTION=$(nmcli -g NAME connection show --active | head -n1) sudo nmcli connection up "$ACTIVE_CONNECTION" &>/dev/null PREFIX="" echoinfo DONE else echo "miaou-resolver in NetworkManager dispatcher already initialized!" fi fi if sudo systemctl is-enabled systemd-resolved.service --quiet &>/dev/null; then echo -n "disabling systemd-resolved..." sudo systemctl stop systemd-resolved.service --quiet sudo systemctl disable systemd-resolved.service --quiet PREFIX="" echoinfo DONE fi if ! grep -q "nameserver $bridge" /etc/resolv.conf; then echo -n "customize resolv.conf from scratch with bridge=<$bridge>, gatewary=<$gateway> ..." sudo tee /etc/resolv.conf &>/dev/null </dev/null <! Next time you type \`exit\`, this session will emit a kill signal!" else echoinfo "successful installation" fi fi