#!/usr/bin/env bash BASHTARD_PLAYBOOK_VARS[$BASHTARD_PLAYBOOK.entry.host]="required" BASHTARD_PLAYBOOK_VARS[$BASHTARD_PLAYBOOK.internal-ip]="required" playbook_add() { local host local role local token host="$(config "$BASHTARD_PLAYBOOK.entry.host")" role="$(config "$BASHTARD_PLAYBOOK.role" "agent")" token="$(config "$BASHTARD_PLAYBOOK.entry.token" "")" pkg install curl # If token is not supplied manually, attempt to fetch it from the entry # host over ssh if [[ -z "$token" ]] then # Dealing with an IPv6 address, strip away the brackets if [[ "${host:0:1}" == "[" && "${host: -1}" == "]" ]] then host="${host:1:-1}" fi info "$BASHTARD_PLAYBOOK/add" "Attempting to fetch token from $host" token="$(ssh "$host" -- cat /var/lib/rancher/k3s/server/token)" fi if [[ -z "$token" ]] then emerg "$BASHTARD_PLAYBOOK/add" "No entry token found, set one with bashtard var -s $BASHTARD_PLAYBOOK.entry.token " return 1 fi info "$BASHTARD_PLAYBOOK/add" "Writing config.yaml for k3s" mkdir -pv -- /etc/rancher/k3s { cat <<-EOF node-name: "${BASHTARD_PLATFORM[fqdn]}" node-ip: "$(config "$BASHTARD_PLAYBOOK.internal-ip" "127.0.0.1")" EOF if [[ -n "$(config "$BASHTARD_PLAYBOOK.flannel-iface" "")" ]] then printf "flannel-iface: \"%s\"\n" "$(config "$BASHTARD_PLAYBOOK.flannel-iface")" fi if [[ -n "$(config "$BASHTARD_PLAYBOOK.external-ip" "")" ]] then printf "node-external-ip: \"%s\"\n" "$(config "$BASHTARD_PLAYBOOK.external-ip")" fi if [[ "$role" == "server" ]] then info "$BASHTARD_PLAYBOOK/add" "Node is a server, adding master configuration" cat <<-EOF bind-address: "$(config "$BASHTARD_PLAYBOOK.bind-address" "0.0.0.0")" cluster-cidr: "$(config "$BASHTARD_PLAYBOOK.cluster-cidr" "172.19.0.0/16")" cluster-domain: "$(config "$BASHTARD_PLAYBOOK.cluster-domain" "cluster.local")" flannel-backend: wireguard-native flannel-ipv6-masq: true service-cidr: "$(config "$BASHTARD_PLAYBOOK.service-cidr" "172.20.0.0/16")" service-node-port-range: "$(config "$BASHTARD_PLAYBOOK.service-node-port-min" "30000")-$(config "$BASHTARD_PLAYBOOK.service-node-port-max" "32767")" disable: - traefik EOF fi printf "\n" printf "server: %s\n" "https://$(config "$BASHTARD_PLAYBOOK.entry.host"):$(config "$BASHTARD_PLAYBOOK.entry.port" "6443")" printf "token: %s\n" "$token" } > "$(config "fs.etcdir")/rancher/k3s/config.yaml" info "$BASHTARD_PLAYBOOK/add" "Installing k3s" curl -sfL https://get.k3s.io | sh -s - "$role" } playbook_sync() { :; } playbook_del() { local role role="$(config "$BASHTARD_PLAYBOOK.role" "agent")" case "$role" in server) /usr/local/bin/k3s-uninstall.sh ;; agent) /usr/local/bin/k3s-agent-uninstall.sh ;; esac }