-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise
More file actions
executable file
·181 lines (164 loc) · 3.47 KB
/
exercise
File metadata and controls
executable file
·181 lines (164 loc) · 3.47 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/bash
set -e
# Options to be setted for the script
base_dir=$(cd `dirname "$0"` && pwd -P)
template_proj_dir="$base_dir/templates/proj"
template_data_dir="$base_dir/templates/data"
prefix="exercise"
host_name="namenode"
host_home="~/"
editor="vim"
function create {
templates=$(ls $template_proj_dir)
found=false
for i in $templates; do
if [ "$i" == "$1" ]; then
found=true
cp -r "$template_proj_dir/$1" "$ex_dir"
if [ -f "$ex_dir/patch.sh" ]; then
"$ex_dir/patch.sh" $ex_name $ex_dir
rm "$ex_dir/patch.sh"
fi
break
fi
done
if [ $found == "false" ]; then
echo "usage: $0 ex_name create template_name"
echo " - please specify a valid exercise template"
echo " - Templates $templates"
fi
}
function data {
if [ $# -lt 2 ]; then
echo "usage: $0 ex_number data template out_name"
return
fi
templates=$(ls $template_data_dir)
found=false
for i in $templates; do
if [ "$i" == "$1" ]; then
found=true
cp "$template_data_dir/$1" "$ex_dir/"$ex_name"_data/$2"
break
fi
done
if [ $found == "false" ]; then
echo "The $1 template is not valid."
echo " - Templates $templates"
fi
}
function delete {
rm -rf $ex_dir
}
function clean {
cd $ex_dir
mvn clean
}
function doc {
doc="$ex_dir/README.md"
if [ -f $doc ]; then
if [ "$1" == "-s" ]; then
cat $doc
return
fi
$editor "$doc"
return
fi
cat <<EOF > $doc
# Exercise $ex_number
## Description
EOF
cat <&0 >> $doc
}
function build {
cd $ex_dir
mvn package
}
function deploy {
if [ "$1" == "--scp" ]; then
ssh namenode "if [ -d $host_home/$ex_name ]; then rm -rf $host_home/$ex_name; fi"
scp -r "$ex_dir" "$host_name:$host_home/$ex_name"
return
fi
args="--progress"
if [ -f "$ex_dir/files.exclude" ]; then
args="$args --exclude-from $ex_dir/files.exclude"
fi
rsync $args -r "$ex_dir" "$host_name:$host_home/"
}
function usage {
echo "This script should be used to automate the generation and"
echo "the testing of the exercises"
echo ""
echo "The normal usage is $0 ex_number type [command]"
echo " - where type is \"hadoop\" or \"spark\""
echo " - command can be one of the listed commands"
command
}
function commands {
echo "create"
echo "data"
echo "delete"
echo "build"
echo "deploy"
echo "bd (build and deploy)"
echo "clean"
echo "doc"
echo "cd"
}
if [ $# -lt 1 ]; then
usage
exit 1
fi
if [ "$1" == "list" ]; then
ls -l "$prefix*"
exit 1
fi
ex_number=$1
ex_name="$prefix$1"
ex_dir="$base_dir/$ex_name"
shift
if [ $# -lt 1 ]; then
echo "On exercise $ex_name you can execute the following commands"
commands
exit 0
fi
cmd=$1
shift
case "$cmd" in
create)
create $@
;;
data)
data $@
;;
delete)
delete $@
;;
build)
build $@
;;
deploy)
deploy $@
;;
bd)
build $@
deploy $@
;;
clean)
clean
;;
doc)
doc
;;
cd)
cd $ex_name"
echo "type exit to return to the previous shell"
PS1="EXERCISE:$ex_name > bash
;;
*)
echo "On exercise $ex_name you can execute the following commands"
commands
exit 0
esac
exit 1