-
Notifications
You must be signed in to change notification settings - Fork 3
/
osp-workshop-common
104 lines (86 loc) · 2.57 KB
/
osp-workshop-common
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
SCENARIO_NUM=$(ls -d $WORKDIR/roles/scenario* | sed 's/.*scenario\([0-9]*\)/\1/g' | sort -n | tail -n1)
DEFAULT_BACKUP_NAME=backup
debug=false
ansible_params=""
backup_name=$DEFAULT_BACKUP_NAME
private_key=""
WORKSHOP_MESSAGE_FILE=/tmp/workshop_message
function prepare_scenario() {
check_and_get_inventory
rm -f $WORKSHOP_MESSAGE_FILE
echo "Preparing scenario $1 ... please wait."
if [ "$debug" == "true" ]; then
$ansible_playbook $WORKDIR/playbooks/workarounds.yml
$ansible_playbook $WORKDIR/playbooks/scenario.yml -e scenario=$1
ansible_run_ecode=$?
else
$ansible_playbook $WORKDIR/playbooks/workarounds.yml
$ansible_playbook $WORKDIR/playbooks/scenario.yml -e scenario=$1 > /dev/null
ansible_run_ecode=$?
fi
if [ $ansible_run_ecode -eq 0 ]; then
echo "Scenario $1 is ready."
else
echo "Scenario has failed!" >&2
exit 3
fi
if [ -e $WORKSHOP_MESSAGE_FILE ]; then
echo
echo
cat $WORKSHOP_MESSAGE_FILE
echo
echo
fi
}
function do_snapshot() {
check_and_get_inventory
set -e
$ansible_playbook $WORKDIR/playbooks/snapshot.yml -e backup_name=$backup_name -e snapshot_action=$1
echo
# Check that everything is fine
if virsh list --all | tail -n+3 | grep -q paused; then
echo "Some VMs got stuck in paused state, trying to fix it ..." 1>&2
for vm in $(virsh list --all | awk '/paused/{ print $ 2}'); do
virsh reset $vm
virsh resume $vm
done
if virsh list --all | tail -n+3 | head -n-1 | grep -v running | grep -q "-"; then
echo "Unable to resume some VMs, check libvirt" 1>&2
exit 2
fi
fi
$ansible_playbook $WORKDIR/playbooks/sync.yml
echo "$1 operation using backup name $backup_name was successful"
}
function main() {
if [ "x$1" == "x" ]; then
echo "Missing action!"
echo
usage
fi
case "$1" in
"scenario")
if [ "x$2" == "x" ]; then
echo "Missing scenario number!"
echo
usage
fi
if [ $2 -lt 1 -o $2 -gt $SCENARIO_NUM ]; then
echo "Wrong scenario number!"
echo
usage
fi
prepare_scenario $2
;;
"backup")
# The backup action is called snapshot
do_snapshot snapshot
;;
"restore")
do_snapshot revert
;;
*)
usage
;;
esac
}