#!/bin/bash
set -e
## Note:
## To check if the k3s installation has been finished
## issue the "systemctl status k3sinstall.service" command.
## To finish the installation you must reboot!
## Once booted you can check the node with:
## "kubectl get nodes"
## For more check out:
## "https://documentation.suse.com/trd/kubernetes/pdf/kubernetes_ri_k3s-slemicro_color_en.pdf"
## Enable network
# combustion: network
## Post output on stdout
exec > >(exec tee -a /dev/tty0) 2>&1
## Add password for root user
## SUSE documentation recommends openssl passwd -6, mkpasswd --method=sha-512 works as well
## The default password that is set here is: linux
ROOT_USER_PASSWORD='$6$NWOL.CUVsuDtWZhb$rs8JeaNfx/ZL.OvQPYugAbXAUA67va.geqLa96zNo8no/PqxvFDICB.Gb2BFlIZoTx095qHSkRevKIyy1/AKj.'
SSH_ROOT_PUBLIC_KEY=ssh_key.pub
SSH_USER_PUBLIC_KEY=ssh_key.pub
USER_REQUIRED_PACKAGES='patterns-microos-cockpit cockpit bash-completion'
CREATE_NORMAL_USER=user ## Add the username here to create a user, leave empty to skip creating one
NORMAL_USER_PASSWORD='$6$NWOL.CUVsuDtWZhb$rs8JeaNfx/ZL.OvQPYugAbXAUA67va.geqLa96zNo8no/PqxvFDICB.Gb2BFlIZoTx095qHSkRevKIyy1/AKj.'
## K3s configuration
## You can set the Kubernetes version to be installed by setting INSTALL_K3S_VERSION in the [service] below
## the following way: Environment="INSTALL_K3S_VERSION=$[Version of K3s to download from Github]"
## Will attempt to download from the stable channel if not specified.
INSTALL_K3S_UPSTREAM=true ## Set to false if you want to use the openSUSE rpm, also add the package name to USER_REQUIRED_PACKAGES
MASTER_NODE_ADDR='172.168.255.104' ## The ip or FQDN of the first node
MASTER_NODE_K3S_TOKEN='K106bcc041130fd90b367680868839a458aae9b6f1b2deee6e2308a3ff330bd4b51::server:fda85366efe11d4c31b4bfdccedc2994' ## You can find this on the master/CP node: /var/lib/rancher/k3s/server/node-token
NODE_HOSTNAME="tanis02" ## If you want to add additional nodes to a cluster you must set the hostname or nodes will not be able to join
## Set hostname
echo $NODE_HOSTNAME > /etc/hostname
## Mount /var and /home so user can be created smoothly
if [ "$CREATE_NORMAL_USER" ]
then
mount /var && mount /home
fi
## Set root password
echo root:$ROOT_USER_PASSWORD | chpasswd -e
## Add ssh public key as authorized key for the root user
mkdir -pm700 /root/.ssh/
cat $SSH_ROOT_PUBLIC_KEY >> /root/.ssh/authorized_keys
## User creation
if [ "$CREATE_NORMAL_USER" ]
then
echo "User creation is requested, creating user."
useradd -m $CREATE_NORMAL_USER -s /bin/bash -g users
echo $CREATE_NORMAL_USER:$NORMAL_USER_PASSWORD | chpasswd -e
echo $CREATE_NORMAL_USER "ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/adminusers
mkdir -pm700 /home/$CREATE_NORMAL_USER/.ssh/
chown -R $CREATE_NORMAL_USER:users /home/$CREATE_NORMAL_USER/.ssh/
cat $SSH_USER_PUBLIC_KEY >> /home/$CREATE_NORMAL_USER/.ssh/authorized_keys
echo "Requested user has been created, requested password has been set."
else
echo "No user will be created"
fi
## Install required packages
if [ "$USER_REQUIRED_PACKAGES" ]
then
zypper ref && zypper --non-interactive install $USER_REQUIRED_PACKAGES
fi
if $INSTALL_K3S_UPSTREAM; then
## Download and install the latest k3s installer
curl -L --output k3s_installer.sh https://get.k3s.io && install -m755 k3s_installer.sh /usr/bin/
## Create a systemd unit that installs k3s if not installed yet
cat <<-EOF > /etc/systemd/system/install-rancher-k3s-worker.service
[Unit]
Description=Run K3s installer
Wants=network-online.target
After=network.target network-online.target
ConditionPathExists=/usr/bin/k3s_installer.sh
ConditionPathExists=!/usr/local/bin/k3s
[Service]
Type=forking
TimeoutStartSec=120
Environment="K3S_URL=https://$MASTER_NODE_ADDR:6443"
Environment="K3S_TOKEN=$MASTER_NODE_K3S_TOKEN"
Environment="K3S_KUBECONFIG_MODE=644"
ExecStart=/usr/bin/k3s_installer.sh
RemainAfterExit=yes
KillMode=process
[Install]
WantedBy=multi-user.target
EOF
fi
## Enable services
systemctl enable cockpit.socket
systemctl enable sshd
systemctl enable install-rancher-k3s-worker.service
## Unmount var and home
if [ "$CREATE_NORMAL_USER" ]
then
umount /var && umount /home
fi
echo "Configured with Combustion" > /etc/issue.d/combustion