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