-
Notifications
You must be signed in to change notification settings - Fork 4
/
deploy.sh
executable file
·192 lines (152 loc) · 5.02 KB
/
deploy.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
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
182
183
184
185
186
187
188
189
190
191
192
#!/bin/bash
set -e
bold=$(tput bold)
normal=$(tput sgr0)
underline=$(tput smul)
nounderline=$(tput rmul)
fail() {
echo -e "\n\nTry ${bold}'./deploy.sh --help'${normal} or ${bold}'./deploy.sh -h'${normal} for more information" >&2
exit 255
}
if [[ "$*" =~ (^|\ )(-h|-H|--help)($|\ ) ]]; then
echo "
${bold}NAME${normal}
${bold}deploy.sh${normal} - script to deploy sisoputnfrba's TP.
${bold}SYNOPSIS${normal}
${bold}deploy.sh${normal} [ ${bold}-t${normal}=${underline}target${nounderline} ] [ ${bold}-s${normal}=${underline}structure${nounderline} ] [ ${bold}-r${normal}=${underline}rule${nounderline} ] [ option=${underline}value${nounderline}... ] ${underline}repository${nounderline}
${bold}DESCRIPTION${normal}
The ${bold}deploy.sh${normal} utility is to ease the deploy process.
${bold}POSITIONAL ARGUMENTS${normal}
${bold}-t | --target${normal} Changes the directory where the script is executed. By default it will be the current directory.
${bold}-s | --structure${normal} Changes the path where the script should look for makefiles. By default it will be the current directory of each project.
${bold}-r | --rule${normal} Changes the makefile rule for building projects. By default it will be 'all'.
${bold}OPTIONS${normal}
${bold}-l | --lib${normal} Adds an external dependency to build and install.
${bold}-d | --dependency${normal} Adds an internal dependency to build and install from the repository.
${bold}-p | --project${normal} Adds a project to build from the repository.
${bold}-c | --config${normal} Replaces a variable in all configuration files containing it. The format is 'key=value'.
${bold}COMPATIBILITY${normal}
The repository must be in ${bold}sisoputnfrba${normal} organization and have makefiles to compile each project or dependency.
${bold}EXAMPLE${normal}
${bold}./deploy.sh${normal} ${bold}-l${normal}=mumuki/cspec ${bold}-d${normal}=sockets ${bold}-p${normal}=server ${bold}-p${normal}=client -c=IP_SERVER=192.168.0.32 ${underline}tp-2022-1c-example${nounderline}
"
exit 1
fi
TARGET=""
case $1 in
-t=*|--target=*)
case ${1#*=} in
/*) TARGET="${1#*=}" ;;
*) TARGET="$PWD/${1#*=}" ;;
esac
shift
;;
*)
;;
esac
STRUCTURE=""
case $1 in
-s=*|--structure=*)
STRUCTURE="${1#*=}"
shift
;;
*)
;;
esac
RULE="all"
case $1 in
-r=*|--rule=*)
RULE="${1#*=}"
shift
;;
*)
;;
esac
if [[ $# -lt 1 ]]; then
echo -e "\n\n${bold}No repository specified!${normal}" >&2
fail
fi
LIBRARIES=()
DEPENDENCIES=()
PROJECTS=()
CONFIGURATIONS=()
OPTIONS=("${@:1:$#-1}")
for i in "${OPTIONS[@]}"
do
case $i in
-l=*|--lib=*)
LIBRARIES+=("${i#*=}")
shift
;;
-d=*|--dependency=*)
DEPENDENCIES+=("${i#*=}")
shift
;;
-p=*|--project=*)
PROJECTS+=("${i#*=}")
shift
;;
-c=*|--config=*)
CONFIGURATIONS+=("${i#*=}")
shift
;;
*)
echo -e "\n\n${bold}Invalid option:${normal} ${i}" >&2
fail
;;
esac
done
REPONAME="$1"
if [[ $REPONAME != "tp"* ]]; then
echo -e "\n\n${bold}Invalid repository${normal}: $REPONAME" >&2
fail
fi
if [[ $TARGET ]]; then
echo -e "\n\n${bold}Changing directory:${normal} ${PWD} -> ${bold}$TARGET${normal}"
cd "$TARGET" || fail
fi
echo -e "\n\n${bold}Checking commons library is installed...${normal}\n\n"
# Refresh dynamic linker cache
sudo ldconfig > /dev/null
if sudo ldconfig -p | grep "libcommons.so" > /dev/null; then
echo -e "\n\n${bold}Commons library already installed${normal}"
else
echo -e "\n\n${bold}Installing commons library...${normal}\n\n"
rm -rf "so-commons-library"
git clone "https://github.com/sisoputnfrba/so-commons-library.git"
make -C "so-commons-library" install
fi
echo -e "\n\n${bold}Cloning external libraries...${normal}"
for i in "${LIBRARIES[@]}"
do
echo -e "\n\n${bold}Building ${i}${normal}\n\n"
rm -rf "${i#*\/}"
git clone "https://github.com/${i}.git"
make -C "${i#*\/}"
sudo make -C "${i#*\/}" install
done
echo -e "\n\n${bold}Cloning project repo...${normal}\n\n"
rm -rf "$REPONAME"
git clone "https://github.com/sisoputnfrba/${REPONAME}.git"
echo -e "\n\n${bold}Building dependencies${normal}..."
for i in "${DEPENDENCIES[@]}"
do
echo -e "\n\n${bold}Building ${i}${normal}\n\n"
make -C "$REPONAME/$i/$STRUCTURE" "$RULE"
sudo make -C "$REPONAME/$i/$STRUCTURE" install
done
echo -e "\n\n${bold}Building projects...${normal}"
for i in "${PROJECTS[@]}"
do
echo -e "\n\n${bold}Building ${i}${normal}\n\n"
make -C "$REPONAME/$i/$STRUCTURE" "$RULE"
done
echo -e "\n\n${bold}Replacing variables...${normal}"
for i in "${CONFIGURATIONS[@]}"
do
KEY="${i%=*}"
VALUE="${i#*=}"
echo -e "\n\nReplacing all ${bold}${KEY:?}${normal} values with ${bold}${VALUE:?}${normal}...\n\n"
grep -Rl "^\s*$KEY\s*=" "$REPONAME" | grep -E '\.config|\.cfg' | tee >(xargs -n1 sed -i "s|^\($KEY\s*=\).*|\1$VALUE|")
done
echo -e "\n\n${bold}Deploy done!${normal}\n\n"