summaryrefslogtreecommitdiff
path: root/playbooks.d/seaweedfs-master/playbook.bash
blob: 34e46c1a0541b106933094c70318dfdd9b389a55 (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
#!/usr/bin/env bash

BASHTARD_PLAYBOOK_VARS[$BASHTARD_PLAYBOOK.ip]="required"

playbook_add() {
	local buffer

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

	# Create directories used by seaweedfs
	mkdir -pv -- "/var/lib/seaweedfs/master"

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

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

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

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

	# Perform initial configuration
	playbook_sync

	# Start the service
	svc enable seaweedfs-master
	svc start seaweedfs-master
}

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

	buffer="$(tmpfile)"
	conf="$(config "fs.etcdir")/seaweedfs/master.conf"
	hash="$(file_hash "$conf")"

	# Add all registered seaweedfs-master nodes as peers
	while read -r host
	do
		# Except this node itself
		[[ "$(basename "$host")" == "${BASHTARD_PLATFORM[fqdn]}" ]] && continue

		peers+=("$(config_for "$(basename "$host")" "$BASHTARD_PLAYBOOK.ip")")
	done < <(grep -FHl "$BASHTARD_PLAYBOOK" "$BASHTARD_ETCDIR/registry.d"/*)

	# Generate config file
	file_template "master.conf" \
		ip="$(config "$BASHTARD_PLAYBOOK.ip")" \
		port="$(config "$BASHTARD_PLAYBOOK.port" "9333")" \
		peers="$(join_args "${peers[@]}")" \
		mdir="$(config "$BASHTARD_PLAYBOOK.mdir" "/var/lib/seaweedfs/master/mdir")" \
		replication="$(config "$BASHTARD_PLAYBOOK.replication" "000")" \
		volume_size="$(config "$BASHTARD_PLAYBOOK.volume-size" "1024")" \
		> "$buffer"

	[[ "$(file_hash "$buffer")" == "$hash" ]] && return

	mv -- "$buffer" "$conf"

	[[ "$BASHTARD_ACTION" == "add" ]] && return

	# Reload service
	svc restart seaweedfs-master
}

playbook_del() {
	# Stop service
	svc stop seaweedfs-master
	svc disable seaweedfs-master

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

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