summaryrefslogtreecommitdiff
path: root/playbooks.d/seaweedfs-volume/playbook.bash
blob: 8485ba0a5b1d1b892f9d4372353f5cfe80a4e2b5 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env bash

BASHTARD_PLAYBOOK_VARS[$BASHTARD_PLAYBOOK.dc]="required"
BASHTARD_PLAYBOOK_VARS[$BASHTARD_PLAYBOOK.ip]="required"
BASHTARD_PLAYBOOK_VARS[$BASHTARD_PLAYBOOK.rack]="required"

playbook_add() {
	local buffer

	# Make sure seaweedfs is installed
	"$BASHTARD_BIN" add seaweedfs || true

	# Generate systemd unit files
	case "${BASHTARD_PLATFORM[init]}" in
		systemd)
			cat <<-EOF > "$(config "fs.etcdir")/systemd/system/seaweedfs-volume@.service"
			[Unit]
			Description=SeaweedFS Volume
			After=network.target

			[Service]
			Type=simple
			User=root
			Group=root

			ExecStart=$(config "fs.bindir")/weed volume -options="$(config "fs.etcdir")/seaweedfs/volume-%i.conf"
			WorkingDirectory=/var/lib/seaweedfs/volume/%i
			SyslogIdentifier=seaweedfs

			[Install]
			WantedBy=multi-user.target
			EOF
			;;
		*) die "NYI"
	esac

	# Perform initial configuration
	playbook_sync

	# Start services
	while read -r volume
	do
		systemctl enable --now "seaweedfs-volume@$volume"
	done < <(config_subkeys "$BASHTARD_PLAYBOOK.volumes")
}

playbook_sync() {
	local buffer
	local conf
	local peers=()
	local hash

	buffer="$(tmpfile)"

	# Add all registered seaweedfs-master nodes as peers
	while read -r host
	do
		peers+=("[$(config_for "$(basename "$host")" "seaweedfs-master.ip")]:$(config_for "$(basename "$host")" "seaweedfs-master.port" "9333")")
	done < <(grep -FHl "seaweedfs-master" "$BASHTARD_ETCDIR/registry.d"/*)

	while read -r volume
	do
		conf="$(config "fs.etcdir")/seaweedfs/volume-$volume.conf"
		hash="$(file_hash "$conf")"

		info "$BASHTARD_PLAYBOOK/sync/$volume" "Updating $conf"

		# Generate config file
		file_template "volume.conf" \
			dc="$(config "$BASHTARD_PLAYBOOK.dc")" \
			dir="/var/lib/seaweedfs/volume/$volume" \
			disk="$(config "$BASHTARD_PLAYBOOK.volume.$volume.disk" "hdd")" \
			max="$(config "$BASHTARD_PLAYBOOK.volume.$volume.max" "0")" \
			free_space="$(config "$BASHTARD_PLAYBOOK.free-space" "10GiB")" \
			filesize_limit="$(config "$BASHTARD_PLAYBOOK.filesize-limit" "256")" \
			ip="$(config "$BASHTARD_PLAYBOOK.ip")" \
			mserver="$(join_args "${peers[@]}")" \
			port="$(config "$BASHTARD_PLAYBOOK.volume.$volume.port" "8080")" \
			rack="$(config "$BASHTARD_PLAYBOOK.rack")" \
			> "$buffer"

		# Write config file
		[[ "$(file_hash "$buffer")" == "$hash" ]] && continue
		info "$BASHTARD_PLAYBOOK/sync/$volume" "Configuration file changed"
		mv -- "$buffer" "$conf"

		# Restart volume server
		info "$BASHTARD_PLAYBOOK/sync/$volume" "Restarting volume $volume"
		[[ "$BASHTARD_ACTION" == "sync" ]] && systemctl restart "seaweedfs-volume@$volume"
	done < <(config_subkeys "$BASHTARD_PLAYBOOK.volumes")
}

playbook_del() {
	# Stop services
	while read -r volume
	do
		systemctl disable --now "seaweedfs-volume@$volume"
	done < <(config_subkeys "$BASHTARD_PLAYBOOK.volumes")

	# Remove systemd unit file
	rm -fr -- "$(config "fs.etcdir")/systemd/system/seaweedfs-volume@.service"

	# Remove configuration files
	rm -fr -- "$(config "fs.etcdir")/seaweedfs/volume.conf"
}