-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpool-manager.sh
144 lines (134 loc) · 4.31 KB
/
pool-manager.sh
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
# ZFS variables
guid="00000000000000000000" # GUID of the pool this script should import/export
# Replication variables
replname="replicationtask-1234" # Name of the replication task this script should run
# PDU Variables
webhookurl="https://webhook.endpoint.local/enclosure-control" # Custom HTTP Endpoint to control disk enclosure
pdu1ip="192.168.0.1"
pdu1user="user1"
pdu1pwd="password1"
pdu1sct="1-4"
pdu2ip="192.168.0.2"
pdu2user="user2"
pdu2pwd="password2"
pdu2sct="1-4"
validate_job () {
echo "Waiting for job to finish..."
jobstate=$(midclt call core.get_jobs "[[\"id\",\"=\",$jobid]]" | jq -r ".[].state")
until [[ "$jobstate" == "SUCCESS" ]] || [[ "$jobstate" == "FAILED" ]]; do
case "$jobstate" in
SUCCESS|FAILED|RUNNING)
;;
*)
echo "Jobstate cannot be parsed: $jobstate"
return 1
;;
esac
sleep 5
jobstate=$(midclt call core.get_jobs "[[\"id\",\"=\",$jobid]]" | jq -r ".[].state")
done
unset jobid
case "$jobstate" in
SUCCESS)
echo "Task finished successfully."
return 0
;;
FAILED)
echo "Task failed."
return 1
;;
*)
echo "Jobstate cannot be parsed: $jobstate"
return 1
;;
esac
}
import_pool () {
echo "Starting import for pool with guid $guid"
# Check if pool already imported
importpoolid=$(midclt call pool.query | jq ".[] | select(.guid == \"$guid\").id")
if [[ -n "$importpoolid" ]]; then
echo "Pool is already imported (ID $importpoolid), skipping..."
else
# Import pool with specified guid
jobid=$(midclt call pool.import_pool "{\"guid\":\"$guid\"}")
echo "Current job id: $jobid"
# Wait until job completed
validate_job
fi
}
export_pool () {
echo "Export for pool with guid \"$guid\" started."
# Find pool to export and check if it's not empty
exportpoolid=$(midclt call pool.query | jq ".[] | select(.guid == \"$guid\").id")
if [[ -z "$exportpoolid" ]]; then
echo "Pool is not imported, skipping export."
else
echo "Proceeding with pool id $exportpoolid"
# Export pool with specified id
jobid=$(midclt call pool.export $exportpoolid '{"cascade":false,"destroy":false}')
echo "Current job id: $jobid"
# Wait until export completed
validate_job
fi
}
start_replication () {
echo "Searching for replication task..."
# Find replication task id by name
replid=$(midclt call replication.query | jq ".[] | select(.name == \"$replname\").id")
[[ -z "$replid" ]] && { echo "Replication task id could not be determined"; return 1; }
echo "Proceeding with replication task id $replid"
# Start replication task
jobid=$(midclt call replication.run $replid)
echo "Current job id: $jobid"
# Wait until replication completed
validate_job
}
start_disks () {
echo "Calling webhook to start disks."
curl -s -k --data "{\"host\":\"$pdu1ip\",\"user\":\"$pdu1user\",\"password\":\"$pdu1pwd\",\"action\":\"on\",\"sockets\":\"$pdu1sct\"}" --output "/dev/null" "$webhookurl"
curl -s -k --data "{\"host\":\"$pdu2ip\",\"user\":\"$pdu2user\",\"password\":\"$pdu2pwd\",\"action\":\"on\",\"sockets\":\"$pdu2sct\"}" --output "/dev/null" "$webhookurl"
echo "Disks spinning up, sleeping for 2 minutes..."
sleep 120
}
stop_disks () {
curl -s -k --data "{\"host\":\"$pdu1ip\",\"user\":\"$pdu1user\",\"password\":\"$pdu1pwd\",\"action\":\"off\",\"sockets\":\"$pdu1sct\"}" --output "/dev/null" "$webhookurl"
curl -s -k --data "{\"host\":\"$pdu2ip\",\"user\":\"$pdu2user\",\"password\":\"$pdu2pwd\",\"action\":\"off\",\"sockets\":\"$pdu2sct\"}" --output "/dev/null" "$webhookurl"
}
case "$1" in
start_disks)
start_disks
;;
import_pool)
import_pool
;;
start_replication)
start_replication
;;
export_pool)
export_pool
;;
stop_disks)
stop_disks
;;
backuptask)
start_disks
import_pool
start_replication
export_pool
stop_disks
;;
start_disks_and_import)
start_disks
import_pool
;;
export_and_stop_disks)
export_pool
stop_disks
;;
*)
echo "Please provide one of the following arguments to run this script:"
echo "start_disks, import_pool, start_replication, export_pool, stop_disks, backuptask, start_disks_and_import, stop_disks_and_export"
;;
esac