Commit c52113ea authored by Yori Fournier's avatar Yori Fournier
Browse files

added build_step.sh and necessary files

parent 14f9c08d
......@@ -158,3 +158,33 @@ artifact-step4:
- image: hash
- runtime-parameters: hash
- archive: hash
# MiniKube
## Building the Image
The Image registry of Minikube is the docker registry of Minikube's docker
It can be accessed via:
```
eval $(minikube docker-env)
```
Then you are using minikube docker and can interact with all containers and images normally. (Be careful, on can break stuff!!)
## Persistant Volumes
There are two persistant volumes to be defined:
step-tree: contains all the artifacts volumes (folder like define above)
archives: contains all the archive volumes (folder named, structured and content after ArchGen + ArchDescr)
Before anything one need to mount this volumes into Minikube:
minkube mount <host-path>:<minikube-path>
Then one need to define the PersistantVolume
When submitting the Job one can claim the Volume (Important that way only one Job can access it at a time, no parallel Job possible, for that you need NFS share mounts)
#!/bin/env bash
function usage() {
echo "Usage: "
echo " build_step.sh -h/--help"
echo " build_step.sh <STEP-NAME> <COMMIT> [--path-to-dockerfile=<PATH-TO-DOCKERFILE>]"
}
# Process command line options
shortopts="h"
longopts="help,path-to-dockerfile:"
opts=$(getopt -o "$shortopts" -l "$longopts" -n "$(basename $0)" -- "$@")
if [ $? -eq 0 ]; then
#eval set -- ${opts}
eval "set -- ${opts}"
while [ $# -gt 0 ]; do
case "$1" in
( -- ) shift; break;;
( -h|--help ) usage; exit 2; shift; break;;
( --path-to-dockerfile ) PATH_TO_DOCKERFILE=$2; shift; break;;
esac
done
else
usage; exit 2;
fi
# set default value for PATH_TO_DOCKERFILE
if [ -z ${PATH_TO_DOCKERFILE} ];
then
PATH_TO_DOCKERFILE="."
fi;
# check input params
STEP_NAME=$1
COMMIT=$2
if [ -z ${STEP_NAME} ];
then
echo "Missing argument: STEP_NAME."
usage; exit 2;
fi;
if [ -z ${COMMIT} ];
then
echo "Missing argument: COMMIT."
usage; exit 2;
fi;
# check env
PIPE_ROOT=`pwd`
source ${PIPE_ROOT}/pipe-env.sh
if [ -z ${IMAGES_ARCH} ]
then
echo "ERROR: There is no image archive defined. Please define one via: export IMAGES_ARCH=<path-to-archive>"
exit 3;
fi;
if [ -z ${PIPELINE} ]
then
echo "ERROR: There is no pipeline defined. Please define one via: export PIPELINE=<path-to-steps-src>"
exit 3;
fi;
# load functions
source ${PIPE_ROOT}/src/setup.sh
source ${PIPE_ROOT}/src/build.sh
# START SCRIPT
# load minikube docker
load_minikube_docker
# check if the image already exists
image_exists $STEP_NAME $COMMIT
WHERE=$?
case ${WHERE} in
1) # build from repo
# check if repo is there
if [ ! -d ${PIPE_ROOT}/${PIPELINE}/${STEP_NAME} ];
then
echo "ERROR: The step ${STEP_NAME} is not setup."
exit 1
fi;
ABS_PATH_TO_DOCKERFILE=`realpath ${PIPE_ROOT}/${PIPELINE}/${STEP_NAME}/${PATH_TO_DOCKERFILE}`
BUILD_ARGS="$(cat ${ABS_PATH_TO_DOCKERFILE}/build-args.txt)"
# build
build_step ${STEP_NAME} ${COMMIT} ${ABS_PATH_TO_DOCKERFILE} ${BUILD_ARGS}
if [[ $? != 0 ]]; then exit $?; fi;
# save
save_image_to_archive ${STEP_NAME} ${COMMIT}
if [[ $? != 0 ]]; then exit $?; fi;
;;
2) # load from image archive
load_image_from_archive ${STEP_NAME} ${COMMIT}
if [[ $? != 0 ]]; then exit $?; fi;
;;
*) # it is in there do nothing
;;
esac;
exit 0
export PIPE_ROOT=`pwd`
export IMAGES_ARCH="images"
export PIPELINE="pipeline"
#!/bin/env bash
function load_minikube_docker() {
# The trick to "upload" the image to
# the minikube image-registry is to
# build the image with the minikube docker
#
# load the minikube docker environement
eval $(minikube docker-env)
return $?
}
function build_step() {
# build the step from docker build cmd
#
# args:
# STEP_NAME: the name used for the image
# COMMIT: the name used for the tag
# PATH_TO_DOCKERFILE: the full filename (incl. path) of the file to use for build
# BUILD_ENV_VARIABLES: the build variable str (incl. --build-args)
local STEP_NAME=$1; shift;
local COMMIT=$1; shift;
local PATH_TO_DOCKERFILE=$1; shift;
local BUILD_ENV_VARIABLES=$@;
# checkout the commit for the step
checkout_commit ${PIPE_ROOT} ${STEP_NAME} ${COMMIT}
if [[ $? != 0 ]]; then return $?; fi;
# check if dockerfile exists
if [ ! -e ${PATH_TO_DOCKERFILE}/Dockerfile ]
then
echo "ERROR: The Dockerfile at ${PATH_TO_DOCKERFILE} does not exists"
return 1
fi;
# build the image
docker build ${BUILD_ENV_VARIABLES} -t ${STEP_NAME}:${COMMIT} -t ${STEP_NAME}:latest ${PATH_TO_DOCKERFILE}
if [[ $? != 0 ]];
then
echo "ERROR: could not successfully build the image of ${STEP_NAME}. commit: ${COMMIT}"
echo "CMD: docker build ${BUILD_ENV_VARIABLES} -t ${STEP_NAME}:${COMMIT} -t ${STEP_NAME}:latest ${PATH_TO_DOCKERFILE}"
return 1
fi;
return 0
}
function save_image_to_archive() {
# save the image as tar in the IMAGES_ARCH directory
#
# args:
# STEP_NAME: the name used for the image
# COMMIT: the name used for the tag
#
# rmk:
# Requires IMAGES_ARCH (the path to the image archive directory) to be defined
local STEP_NAME=$1
local COMMIT=$2
docker save --output ${IMAGES_ARCH}/${STEP_NAME}_${COMMIT}.tar ${STEP_NAME}:${COMMIT}
return $?
}
function load_image_from_archive() {
# load and image from the archive
#
# args:
# STEP_NAME: image name
# COMMIT: the tag of the image (don't use latest)
#
# rmk:
# Requires IMAGES_ARCH (the path to the image archive directory) to be defined
if [[ -e ${IMAGES_ARCH}/${STEP_NAME}_${COMMIT}.tar ]]
then
docker load --input ${IMAGES_ARCH}/${STEP_NAME}_${COMMIT}.tar
if [[ $? != 0 ]]
then
echo "ERROR: Image ${IMAGES_ARCH}/${STEP_NAME}_${COMMIT}.tar failed to be loaded into the docker registry"
return $?
fi;
else
echo "ERROR: Image ${IMAGES_ARCH}/${STEP_NAME}_${COMMIT}.tar does not exist in the image archive: ${IMAGES_ARCH}"
return 1
fi;
return 0
}
function image_exists() {
# Check if the image was already build
#
# args:
# STEP_NAME: image name
# COMMIT: the tag of the image (don't use latest)
#
# rmk:
# Requires IMAGES_ARCH (the path to the image archive directory) to be defined
local STEP_NAME=$1
local COMMIT=$2
# check if the image is in the docker registry
docker image inspect ${STEP_NAME}:${COMMIT} &> /dev/null
if [[ $? != 0 ]];
then
# look if the image is in the image archive, then load it
if [[ -e ${IMAGES_ARCH}/${STEP_NAME}_${COMMIT}.tar ]]
then
echo "Image ${STEP_NAME}:${COMMIT} exists in the image archive."
return 2
else
echo "Image ${STEP_NAME}:${COMMIT} does not exists."
return 1
fi;
else
echo "Image ${STEP_NAME}:${COMMIT} exists and is loaded in image registry."
fi;
return 0
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment