Parent: [be733d] (diff)

Download this file

functions.sh    328 lines (280 with data), 10.4 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env bash
COL_DEFAULT='\e[0;00m'
COL_BLUE='\e[0;96m'
COL_GREEN='\e[0;92m'
COL_RED='\e[41m'
#DNS="--dns=192.168.0.11"
DNS=""
DOCKER="docker run --rm ${DNS}"
if $(groups|grep -qvw docker); then
# We are not in the docker group, checking if we are sudoers
sudo -n true
if [ $? == 0 ]; then
DOCKER="sudo docker run --rm ${DNS}"
else
echo "Not able to execute docker"
exit -1
fi
fi
DEV_DOCKER_IMAGE="c2netproject.eu/runtime/java/dev:1_0_0"
MARIA_DOCKER_IMAGE="eslap.cloud/runtime/mariadb:1_0_0"
NPMINSTALL="
cd /tmp/component
while [ ! -f node_modules/.bin/coffee ]; do
[ -d node_modules ] && rm -rf node_modules
npm -s install --production
done
"
function echoError() {
echo
echo -e ${COL_RED}"✘ $1"${COL_DEFAULT}
echo
}
function echoGreen() {
echo -e ${COL_GREEN}$1${COL_DEFAULT}
}
function echoBlue() {
echo -e ${COL_BLUE}$1${COL_DEFAULT}
}
function createBlobBundle() {
name=$1
hash=$2
cat <<EOF >Manifest.json
{
"spec" : "http://eslap.cloud/manifest/blob/1_0_0",
"name" : "${name}",
"hash" : "${hash}"
}
EOF
}
## Adds a component to the bundle
# $1: Component absolute path
# $2: Action to perform before copying to bundle folder:
# Options:
# "skip-copy-src": ignores copying to source
# "copy-src": common case, copies the component as is
# "copy-src-pubsub": special case for component pub/sub.
# "copy-src-ucp": special case for UCP
# "unzip-ucp": special case for UCP as it is embedded in the c2net-test service
# "copy-src-import-alg": special case to import algorithms and build julia image
# "copy-src-dkms": special case for dkms
#
# $3: Action to perform to build component
# Options:
# "build-node": builds component using NODE
# "build-java": builds component using JAVA
# "skip-build": skips building component
#
# $4: Action to perform after packing the component.
# Options:
# "zip-bundle": zips the component and leaves the sources in the bundle folder
# "zip-bundle-remove": zips the component and removes the sources from the bundle folder
#
##
function addComponentToBuild() {
#todo check vars num, print usage
componentFolder=${1}
prepare=${2}
build=${3}
pack=${4}
# Validate component git folder
if [ ! -d "$componentFolder" ]; then
echoError "Folder $componentFolder not found"
exit -1
fi
# Validate prepare actions
if ( [ ${prepare} != "skip-copy-src" ] && [ ${prepare} != "copy-src" ] \
&& [ ${prepare} != "copy-src" ] && [ ${prepare} != "copy-src-pubsub" ] \
&& [ ${prepare} != "copy-src-ucp" ] && [ ${prepare} != "unzip-ucp" ] \
&& [ ${prepare} != "copy-all" ] && [ ${prepare} != "copy-src-import-alg-julia" ] \
&& [ ${prepare} != "copy-src-import-alg" ] && [ ${prepare} != "copy-src-dkms" ] )
then
echoError "Invalid prepare action $prepare"
exit -1
fi
# Validate build actions
if ([ ${build} != "build-node" ] && [ ${build} != "build-java" ] && [ ${build} != "build-mariadb" ] && [ ${build} != "skip-build" ])
then
echoError "Invalid build action $build"
exit -1
fi
# Validate pack actions
if ([ ${pack} != "zip-bundle" ] && [ ${pack} != "zip-bundle-remove" ])
then
echoError "Invalid pack action $pack"
exit -1
fi
# Add component to components with the actions
components=("${components[@]}" "$componentFolder $prepare $build $pack")
echoGreen "✔ $componentFolder"
}
function initBundle() {
echoGreen "\nCreating Bundle..."
BUNDLE_ZIP=${1-"module_bundle"}
SERVICE_HOME=${2}
COMPONENTS_HOME=${SERVICE_HOME}/bundle/components
SDK_HOME=${3}
cd ${SERVICE_HOME}/bundle
rm ${BUNDLE_ZIP}.zip || true
zip -q ${BUNDLE_ZIP} ./service/Manifest.json ./deployment/Manifest.json
echo "Service and Deployment manifests added to Bundle."
}
function prepareComponent() {
componentSrc=${1}
prepare=${2}
ComponentName="${componentSrc##*/}"
ComponentPath=${COMPONENTS_HOME}/${ComponentName}
echo
echoGreen "$ComponentName Building component"
echo
# Component not to be copied into Service Folder
if [ ${prepare} = "skip-copy-src" ]
then
echoBlue "$ComponentName Ignoring copying component"
# Copy component into Service Folder
elif [ ${prepare} = "copy-src" ]
then
sudo rm -rf ${ComponentPath} || true
rsync -avq --include="Manifest.json" --include="code" --exclude="node_modules" --exclude="test*" --exclude=".*" ${componentSrc} ${COMPONENTS_HOME}/
echoBlue "$ComponentName Copied to Components Bundle"
# Copy ALL in component, to avoid to recompile again
elif [ ${prepare} = "copy-all" ]
then
sudo rm -rf ${ComponentPath} || true
rsync -avq --include="Manifest.json" --include="code" --exclude="test*" --exclude=".*" ${componentSrc} ${COMPONENTS_HOME}/
echoBlue "$ComponentName Copied to Components Bundle with node_modules"
# Special case for pubsub, todo change this
elif [ ${prepare} = "copy-src-pubsub" ]
then
sudo rm -rf ${ComponentPath} || true
mkdir ${ComponentPath}
rsync -avq --include="Manifest.json" --include="code" --exclude="node_modules" --exclude="test*" --exclude=".*" ${componentSrc}/pubsubService/components/pubsub/* ${ComponentPath}/.
echoBlue "$ComponentName Copied to Components Folder"
# Special case for ucp, todo change this
elif [ ${prepare} = "unzip-ucp" ]
then
sudo rm -rf ${ComponentPath} || true
unzip ${SERVICE_HOME}/files/ucp-tpm.zip -d ${ComponentPath}
echoBlue "$ComponentName Unzipped to Components Folder"
# Special case for dkms, todo change this
elif [ ${prepare} = "copy-src-dkms" ]
then
sudo rm -rf ${ComponentPath} || true
mkdir ${ComponentPath}
rsync -avq --include="Manifest.json" --include="code" --exclude="node_modules" --exclude="test*" --exclude="module" --exclude=".*" ${componentSrc} ${COMPONENTS_HOME}/
echoBlue "$ComponentName Copied to Components Bundle"
# Special case for task executor
elif [ ${prepare} = "copy-src-import-alg-julia" ]
then
if [ ! -d "$componentSrc/scripts" ]; then
echoError "Folder NOT FOUND $componentSrc/scripts "
exit 1
fi
origin=$PWD
echoGreen "$ComponentName Copying algorithms"
cd ${componentSrc}/scripts
./import_algorithms.sh
echoGreen "$ComponentName Building Julia image "
cd ${componentSrc}/scripts
./build_julia_image.sh
echoBlue "$ComponentName Optimization Algorithms imported"
cd ${origin}
sudo rm -rf ${ComponentPath} || true
rsync -avq --include="Manifest.json" --include="code" --exclude="node_modules" --exclude="test*" --exclude=".*" --exclude="resources" ${componentSrc} ${COMPONENTS_HOME}/
echoBlue "$ComponentName Copied to Components Bundle"
# Special case for task executor
elif [ ${prepare} = "copy-src-import-alg" ]
then
if [ ! -d "$componentSrc/scripts" ]; then
echoError "Folder NOT FOUND $componentSrc/scripts "
exit 1
fi
origin=$PWD
cd ${componentSrc}/scripts
./import_algorithms.sh
echoBlue "$ComponentName Optimization Algorithms imported"
cd ${origin}
sudo rm -rf ${ComponentPath} || true
rsync -avq --include="Manifest.json" --include="code" --exclude="node_modules" --exclude="test*" --exclude=".*" --exclude="resources" ${componentSrc} ${COMPONENTS_HOME}/
echoBlue "$ComponentName Copied to Components Bundle"
fi
}
function buildComponent() {
componentSrc=${1}
build=${3}
ComponentName="${componentSrc##*/}"
ComponentPath=${COMPONENTS_HOME}/${ComponentName}
# Node component
if [ ${build} = "build-node" ]
then
echoBlue "$ComponentName Compiling NODE... "
cd ${ComponentPath}/code/src/
sudo rm -rf ./node_modules || true
${DOCKER} -v $PWD:/tmp/component --entrypoint=bash "$DEV_DOCKER_IMAGE" -c "$NPMINSTALL"
echoBlue "$ComponentName compiled"
# mariadb wrapper component
elif [ ${build} = "build-mariadb" ]
then
echoBlue "$ComponentName Compiling MARIADB... "
cd ${ComponentPath}/code/src/
sudo rm -rf ./node_modules || true
${DOCKER} -v $PWD:/tmp/component --entrypoint=bash "$MARIA_DOCKER_IMAGE" -c "$NPMINSTALL"
echoBlue "$ComponentName compiled"
# Java component
elif [ ${build} = "build-java" ]
then
echoBlue "$ComponentName Compiling JAVA... "
mkdir -p ${ComponentPath}/code/src/build/classes
cat <<EOF >${ComponentPath}/code/src/build/build.sh
JAVALIB="/eslap/java-runtime-lib"
for i in \$(find \$JAVALIB -name "*.jar");do CLASSPATH="\$i:\$CLASSPATH";done
cd /tmp/component
for i in \$(find lib -name "*.jar");do CLASSPATH="\$i:\$CLASSPATH";done
export CLASSPATH
javac -d build/classes \$(find src -name "*.java")
EOF
${DOCKER} -v ${ComponentPath}/code/src:/tmp/component --entrypoint=bash "$DEV_DOCKER_IMAGE" -c '. /tmp/component/build/build.sh'
rm ${ComponentPath}/code/src/build/build.sh
# cd ${ComponentPath}/code/src/build
# cp -rf ../lib .
mkdir ${ComponentPath}/code/tmp
rsync -av --exclude="build.sh" --exclude=".*" ${ComponentPath}/code/src/build/* ${ComponentPath}/code/tmp
rm -rf ${ComponentPath}/code/src
mv ${ComponentPath}/code/tmp ${ComponentPath}/code/src
echoBlue "$ComponentName Compiled"
# No need to build component
elif [ ${build} = "skip-build" ]
then
echoBlue "$ComponentName SKIP compile step"
fi
}
function validateComponent() {
componentSrc=${1}
pack=${4}
ComponentName="${componentSrc##*/}"
ComponentPath=${COMPONENTS_HOME}/${ComponentName}
DIR=$PWD
cd ${SDK_HOME}/tools
echoBlue "$ComponentName Starting Manifests syntax..."
${SDK_HOME}/tools/manifest-validator.sh ${ComponentPath}
echoBlue "$ComponentName Manifests syntax finished"
cd ${DIR}
}
function packComponent() {
componentSrc=${1}
pack=${4}
ComponentName="${componentSrc##*/}"
ComponentPath=${COMPONENTS_HOME}/${ComponentName}
# Creating bundle
cd ${SERVICE_HOME}/bundle
if [ ${pack} = "zip-bundle" ]
then
zip --exclude '.*' -qr ${BUNDLE_ZIP} ./components/${ComponentName}
echoBlue "$ComponentName zipped to Bundle $BUNDLE_ZIP"
elif [ ${pack} = "zip-bundle-remove" ]
then
sudo zip --exclude '.*' -mqr ${BUNDLE_ZIP} ./components/${ComponentName}
echoBlue "$ComponentName removed from Components Folder"
echoBlue "$ComponentName zipped to Bundle $BUNDLE_ZIP"
fi
}