#!/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
}