-
Notifications
You must be signed in to change notification settings - Fork 0
/
zdsp
executable file
·144 lines (114 loc) · 3.5 KB
/
zdsp
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
#!/usr/bin/env bash
config=$1
ZDSP_BASE_DIR="$( cd "$( dirname "$0" )" && pwd )"
ZDSP_TASK_DIR=$ZDSP_BASE_DIR/tasks
ZDSP_CONFIG_DIR=$ZDSP_BASE_DIR/configs
read -r -d '' ZDSP_GLOBAL_FUNC <<'FUNC'
zdsp_success () { echo -e "\x1B[00;32m${1:-Success}\x1B[00m"; };
zdsp_error () { echo -e "\x1B[00;31mError: $1\x1B[00m"; };
zdsp_fatal () { echo -e "\x1B[00;31mFatal Error: $1\x1B[00m"; exit 1; };
zdsp_info () { echo -e "\x1B[00;34m$1\x1B[00m"; };
FUNC
eval $ZDSP_GLOBAL_FUNC
usage() {
echo "Usage: $0 configname"
echo "Available configurations:"
for f in $(find $ZDSP_CONFIG_DIR -type f); do
if [ $(echo $f | grep -v "__init__") ]; then
echo $f | sed "s#$ZDSP_CONFIG_DIR/##g"
fi
done
}
run_task() {
vars=$(echo "$2" | sed -e "s#export ##g")
c=$ZDSP_GLOBAL_FUNC;
c+=$'\n'
c+=ZDSP_TASK_DIR=$ZDSP_TASK_DIR;
c+=$'\n'
c+=ZDSP_CONFIG_DIR=$ZDSP_CONFIG_DIR;
c+=$'\n'
c+=ZDSP_BASE_DIR=$ZDSP_BASE_DIR;
c+=$'\n'
c+=$2;
c+=$'\n'
c+=$(cat $1)
c+=$'\n'
bash -c "$c"
}
if [ -z $config ]; then
usage
exit 1
fi
provision() {
config=$1
if [ ! -f $ZDSP_CONFIG_DIR/$config ]; then
zdsp_fatal "Config $config does not exist. To create it, add the file $ZDSP_CONFIG_DIR/$config."
fi
zdsp_info "Running config $config"
zdsp_info "Checking tasks"
while read -r task; do
if [[ $task == export* ]]; then
continue
fi
echo $task
if [[ $task == provision:* ]]; then
continue
fi
if [ -d $ZDSP_TASK_DIR/$task ]; then
if [ ! -f $ZDSP_TASK_DIR/$task/do.sh ]; then
zdsp_fatal "task file $ZDSP_TASK_DIR/$task/do.sh not found"
fi
elif [ ! -f $ZDSP_TASK_DIR/$task ]; then
zdsp_fatal "task file $ZDSP_TASK_DIR/$task not found"
fi
done < $ZDSP_CONFIG_DIR/$config
zdsp_info "Running tasks"
num=1
base=""
configs=""
for comp in `echo $config | sed -e 's#/# #g'`; do
base=$base$comp
if [ -f $ZDSP_CONFIG_DIR/$base/__init__ ]; then
configs="$configs $ZDSP_CONFIG_DIR/$base/__init__"
else
if [ -f $ZDSP_CONFIG_DIR/$base ]; then
configs="$configs $ZDSP_CONFIG_DIR/$base"
fi
fi
base=$base/
done
script_vars=''
while read -r task
do
if [[ $task == export* ]]; then
script_vars+="$task;"
continue
fi
if [[ $task == provision:* ]]; then
provision `echo $task | sed "s/^provision://g" `
fi
zdsp_info "◆ Starting task $num: $task"
if [ -f $ZDSP_TASK_DIR/$task ]; then
scriptfile=$ZDSP_TASK_DIR/$task
else
scriptfile=$ZDSP_TASK_DIR/$task/do.sh
fi
run_task $scriptfile "$script_vars"
if [ $? != 0 ]; then
zdsp_error "task $task failed. Rolling back."
for task in `tail -r $ZDSP_CONFIG_DIR/$config | tail -n $num`; do
if [ -f $ZDSP_TASK_DIR/$task/undo.sh ]; then
zdsp_info "undo $task"
bash $ZDSP_TASK_DIR/$task/undo.sh
fi
done
zdsp_error "Provisioning of $config failed, because the task $task dir not finish successfully."
exit 1
else
zdsp_success "✓ Task $task: Success"
fi
let num=num+1
done < <(cat $configs)
}
provision $config
zdsp_info "$config is now ready."