Parent: [ebe4b9] (diff)

Child: [c21875] (diff)

Download this file

functions.sh    254 lines (214 with data), 7.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
#!/usr/bin/env bash
COL_DEFAULT='\e[0;00m'
COL_BLUE='\e[0;96m'
COL_GREEN='\e[0;92m'
COL_RED='\e[41m'
DOCKER="echo docker run --rm --dns=192.168.0.11"
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=192.168.0.11"
else
echo "Not able to execute docker"
exit -1
fi
fi
DEV_DOCKER_IMAGE="c2netproject.eu/runtime/java/dev: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
}
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-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} != "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"
# 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=".*" ${componentSrc} ${COMPONENTS_HOME}/
sudo rm -rf ${ComponentPath}/code/src
sudo mv ${ComponentPath}/code/module ${ComponentPath}/code/src
echoBlue "$ComponentName Copied to Components Bundle"
# Special case for task executor
elif [ ${prepare} = "copy-src-import-alg" ]
then
# check if directory exists
origin=$PWD
cd ${componentSrc}/resources/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"
# 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 .
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
zip --exclude '.*' -mqr ${BUNDLE_ZIP} ./components/${ComponentName}
echoBlue "$ComponentName removed form Components Folder"
echoBlue "$ComponentName zipped to Bundle $BUNDLE_ZIP"
fi
}