diff options
Diffstat (limited to 'playbooks.d/seaweedfs-volume/playbook.bash')
-rw-r--r-- | playbooks.d/seaweedfs-volume/playbook.bash | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/playbooks.d/seaweedfs-volume/playbook.bash b/playbooks.d/seaweedfs-volume/playbook.bash new file mode 100644 index 0000000..875a2cd --- /dev/null +++ b/playbooks.d/seaweedfs-volume/playbook.bash @@ -0,0 +1,106 @@ +#!/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.volumes.$volume.disk" "hdd")" \ + filesize_limit="$(config "$BASHTARD_PLAYBOOK.filesize-limit" "256")" \ + free_space="$(config "$BASHTARD_PLAYBOOK.free-space" "10GiB")" \ + ip="$(config "$BASHTARD_PLAYBOOK.ip")" \ + max="$(config "$BASHTARD_PLAYBOOK.volumes.$volume.max" "0")" \ + mserver="$(join_args "${peers[@]}")" \ + port_grpc="$(config "$BASHTARD_PLAYBOOK.volumes.$volume.port.grpc" "17080")" \ + port_http="$(config "$BASHTARD_PLAYBOOK.volumes.$volume.port.http" "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" +} |