summaryrefslogtreecommitdiff
path: root/playbook.bash
blob: 49b6ee8f064e9f3d3a99901f3e22a31e722ccfb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env bash

playbook_add() {
	pkg install curl nfs-common open-iscsi

	case "${BASHTARD_PLATFORM[key]}" in
		linux-debian_gnu_linux|linux-ubuntu)
			info "$BASHTARD_PLAYBOOK" "Configure package manager repositories"
			curl https://baltocdn.com/helm/signing.asc | gpg --dearmor > /usr/share/keyrings/helm.gpg
			printf "deb [arch=%s signed-by=%s] %s all main\n" \
				"$(dpkg --print-architecture)" \
				"/usr/share/keyrings/helm.gpg" \
				"https://baltocdn.com/helm/stable/debian/" \
				> "/etc/apt/sources.list.d/$BASHTARD_PLAYBOOK.list"
			apt update
			;;
	esac

	info "$BASHTARD_PLAYBOOK" "Installing additional programs"
	pkg install helm

	info "$BASHTARD_PLAYBOOK" "Writing config.yaml for k3s"
	mkdir -pv -- /etc/rancher/k3s
	cat <<-EOF > /etc/rancher/k3s/config.yaml
	node-name: "${BASHTARD_PLATFORM[fqdn]}"
	node-ip: "$(config "$BASHTARD_PLAYBOOK.node-ip" "$(config "bashtard.ssh.host")")"
	cluster-cidr: "$(config "$BASHTARD_PLAYBOOK.cluster-cidr" "172.19.0.0/16")"
	service-cidr: "$(config "$BASHTARD_PLAYBOOK.service-cidr" "172.20.0.0/16")"
	cluster-domain: "$(config "$BASHTARD_PLAYBOOK.cluster-domain" "cluster.local")"
	service-node-port-range: "$(config "$BASHTARD_PLAYBOOK.service-node-port-min" "30000")-$(config "$BASHTARD_PLAYBOOK.service-node-port-max" "32767")"
	EOF

	info "$BASHTARD_PLAYBOOK" "Installing k3s"
	curl -sfL https://get.k3s.io | sh -s - server --cluster-init  # I hate this

	notice "$BASHTARD_PLAYBOOK" "Creating data directories"
	mkdir -pv -- "$(playbook_path "data")/manifests.d"

	notice "$BASHTARD_PLAYBOOK" "Waiting for node to become available"
	{ grep -q -m 1 "${BASHTARD_PLATFORM[fqdn]}[[:space:]]\+Ready"; kill $!; } < <(k3s kubectl get node -w)

	playbook_sync
}

playbook_sync() {
	local data
	local kubeconfig
	local manifest_prefix
	local values

	data="$(playbook_path "data")"
	kubeconfig="$(config "fs.etcdir")/rancher/k3s/k3s.yaml"
	manifest_prefix="$(config "$BASHTARD_PLAYBOOK.manifest-prefix" "")"

	notice "$BASHTARD_PLAYBOOK/manifests" "Applying manifests.d"
	kubectl --kubeconfig "$kubeconfig" apply --recursive --filename "$data/manifests.d/$manifest_prefix"
}

playbook_del() {
	/usr/local/bin/k3s-uninstall.sh
}