Inizio dockerizzazione

This commit is contained in:
2022-08-29 23:20:16 +02:00
parent b348b3a83d
commit 3d22f4cc6a
851 changed files with 1117 additions and 904 deletions
+11
View File
@@ -0,0 +1,11 @@
#!/bin/bash
# Logs in the local docker to ECR so it can push and pull images.
# Works for both aws cli versions (1 and 2 )
IS_V2=`aws --version | grep -P "^aws-cli/2" | wc -l`
if [[ $IS_V2 -eq "1" ]]; then
aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin 299952237278.dkr.ecr.eu-west-1.amazonaws.com
else
$(aws ecr get-login --no-include-email)
fi
+116
View File
@@ -0,0 +1,116 @@
#!/bin/bash
######
# This script builds the docker image locally
# It expects docker to be logged in to ECR (see aws-login)
set -e
VERBOSE=0
DIR=`dirname $(realpath "${BASH_SOURCE[0]}")`
OLDDIR=`pwd`
cd "$DIR"
source "$DIR/../config"
source "$DIR/libs.sh"
usage() {
echo "Usage: $0 [-h] [-v] [-t: version] [image type, see below] " 1>&2;
echo "This script builds framework docker images, the following options are available:" 1>&2;
echo " -h: This message" 1>&2;
echo " Image types (at least one, otherwise the script does nothing)" 1>&2;
echo " -a: All, same as ub" 1>&2;
echo " -u: Unit" 1>&2;
echo " -b: Base" 1>&2;
echo " -d: Debug" 1>&2;
echo " -t: Project version, this will be used as base for the docker image tag (default: local)" 1>&2;
echo " -v: Verbose" 1>&2;
exit 1;
}
BASE_MODE=0
UNIT_MODE=0
DEBUG_MODE=0
TAG="local"
while getopts "t:hvaubd" o; do
case "${o}" in
b)
BASE_MODE=1
;;
u)
UNIT_MODE=1
;;
d)
DEBUG_MODE=1
;;
a)
BASE_MODE=1
UNIT_MODE=1
;;
t)
TAG=${OPTARG}
;;
v)
VERBOSE=1
;;
h)
usage
;;
*)
printerr "Unsupported option ${o}"
usage
;;
esac
done
cd ".."
# STANDARD MODE
if [[ $BASE_MODE -gt 0 ]]; then
DOCKERFILE=`mktemp -t dockerfile.XXXXX`
msg "Generating base dockerfile '$DOCKERFILE'"
OPTS="b"
if [[ $VERBOSE -eq 1 ]]; then
OPTS+="v"
fi
$DIR/generateDockerfile.sh -$OPTS -o $DOCKERFILE
msg "Building project image $TAG"
docker build -t $PROJECT_REPOSITORY/$PROJECT_GROUP/$PROJECT_BUILDNAME:$TAG -f $DOCKERFILE .
fi
# UNIT MODE
if [[ $UNIT_MODE -gt 0 ]]; then
DOCKERFILE=`mktemp -t dockerfile.XXXXX`
msg "Generating unit dockerfile '$DOCKERFILE'"
OPTS="u"
if [[ $VERBOSE -eq 1 ]]; then
OPTS+="v"
fi
$DIR/generateDockerfile.sh -$OPTS -o $DOCKERFILE
msg "Building project image $TAG"
docker build -t $PROJECT_REPOSITORY/$PROJECT_GROUP/$PROJECT_BUILDNAME:$TAG-unit -f $DOCKERFILE .
fi
# DEBUG MODE
if [[ $DEBUG_MODE -gt 0 ]]; then
DOCKERFILE=`mktemp -t dockerfile.XXXXX`
msg "Generating debug dockerfile '$DOCKERFILE'"
OPTS="d"
if [[ $VERBOSE -eq 1 ]]; then
OPTS+="v"
fi
$DIR/generateDockerfile.sh -$OPTS -o $DOCKERFILE
msg "Building project image $TAG"
docker build -t $PROJECT_REPOSITORY/$PROJECT_GROUP/$PROJECT_BUILDNAME:$TAG-debug -f $DOCKERFILE .
fi
+117
View File
@@ -0,0 +1,117 @@
#!/bin/bash
######
# This script builds the docker image locally
# It expects docker to be logged in to ECR (see aws-login)
set -e
VERBOSE=0
DIR=`dirname $(realpath "${BASH_SOURCE[0]}")`
OLDDIR=`pwd`
cd "$DIR"
source "$DIR/../config"
source "$DIR/libs.sh"
usage() {
echo "Usage: $0 [-h] [-v] [-t: version] [action(s), see below]" 1>&2;
echo "This script builds framework docker images, the following options are available:" 1>&2;
echo " -h: This message" 1>&2;
echo " Actions" 1>&2;
echo " -u: Unit tests locally" 1>&2;
echo " -d: Run debug vesion locally" 1>&2;
echo " -p: Publish (requires a version)" 1>&2;
echo " -t: Software version, this will be used for the docker tag (default: local)" 1>&2;
echo " -v: Verbose" 1>&2;
exit 1;
}
UNIT_MODE=0
DEBUG_MODE=0
PUBLISH_MODE=0
SELECTED_MODES=0
TAG="local"
while getopts "t:hvudp" o; do
case "${o}" in
u)
UNIT_MODE=1
SELECTED_MODES+=1
;;
d)
DEBUG_MODE=1
SELECTED_MODES+=1
;;
p)
PUBLISH_MODE=1
SELECTED_MODES+=1
;;
t)
TAG=${OPTARG}
;;
v)
VERBOSE=1
;;
h)
usage
;;
*)
printerr "Unsupported option ${o}"
usage
;;
esac
done
if [[ $SELECTED_MODES -ne 1 ]]; then
printerr "Select only one mode"
usage
fi
if [[ $UNIT_MODE -eq 1 ]]; then
BUILDOPT+="u"
UNITOPT=""
if [[ $VERBOSE -gt 0 ]]; then
BUILDOPT+="v"
UNITOPT="-v"
fi
msg "running ./build.sh -$BUILDOPT -t $TAG"
./build.sh -$BUILDOPT -t $TAG
./run-unit.sh $UNITOPT -t $TAG
fi
if [[ $PUBLISH_MODE -eq 1 ]]; then
BUILDOPT+="bu"
VERBOPT=""
if [[ $VERBOSE -gt 0 ]]; then
BUILDOPT+="v"
VERBOPT="-v"
fi
msg "running ./build.sh -$BUILDOPT -t $TAG"
./build.sh -$BUILDOPT -t $TAG
./run-unit.sh $VERBOPT -t $TAG
./publish.sh $VERBOPT -t $TAG
fi
if [[ $DEBUG_MODE -eq 1 ]]; then
BUILDOPT+="d"
if [[ $VERBOSE -gt 0 ]]; then
BUILDOPT+="v"
fi
msg "running ./build.sh -$BUILDOPT -t $TAG"
./build.sh -$BUILDOPT -t $TAG
OUT_FOLDER="/home/$PROJECT_AUTHOR/$PROJECT_BUILDNAME/local"
mkdir -p $OUT_FOLDER
./generateComposeFolder.sh -L -o "$OUT_FOLDER" -t $TAG
cd "$OUT_FOLDER"
docker-compose up -d
fi
+93
View File
@@ -0,0 +1,93 @@
#!/bin/bash
######
# Generates dockerfiles
# It expects docker to be logged in to ECR (see aws-login)
set -e
VERBOSE=0
DIR=`dirname $(realpath "${BASH_SOURCE[0]}")`
OLDDIR=`pwd`
cd "$DIR"
source "$DIR/../config"
source "$DIR/libs.sh"
usage() {
echo "Usage: $0 [-h] [-v] [-dub] [-i: input, default] -o: output " 1>&2;
echo "This script builds framework docker images, the following options are available:" 1>&2;
echo " -h: This message" 1>&2;
echo " Environment" 1>&2;
echo " -l: Local" 1>&2;
echo " -L: Local Live" 1>&2;
echo " -s: Test" 1>&2;
echo " -p: Production" 1>&2;
echo " -i: Config Input File" 1>&2;
echo " -o: Output directory" 1>&2;
echo " -t: Project version, this will be used as base for the docker image tag (default: local)" 1>&2;
echo " -v: Verbose" 1>&2;
exit 1;
}
STARTING_FILE=""
CFGFILE="$DIR/../config"
OUT_DIRECTORY=""
SELECTED_MODES=0
while getopts "i:o:t:hvlLsp" o; do
case "${o}" in
l)
STARTING_FILE="$DIR/../cicd/docker-compose/local.yml"
SELECTED_MODES+=1
;;
L)
STARTING_FILE="$DIR/../cicd/docker-compose/local-live.yml"
SELECTED_MODES+=1
;;
s)
STARTING_FILE="$DIR/../cicd/docker-compose/test.yml"
SELECTED_MODES+=1
;;
p)
STARTING_FILE="$DIR/../cicd/docker-compose/production.yml"
SELECTED_MODES+=1
;;
i)
CFGFILE="${OLDDIR}/${OPTARG}"
;;
o)
OUT_DIRECTORY="${OPTARG}"
;;
t)
TAG=${OPTARG}
;;
v)
VERBOSE=1
;;
h)
usage
;;
*)
printerr "Unsupported option ${o}"
usage
;;
esac
done
if [[ $SELECTED_MODES -ne 1 ]]; then
printerr "Invalid mode"
usage
fi
if [[ $OUT_DIRECTORY == "" ]]; then
printerr "An output directory must be configured"
usage
fi
cp -r "$DIR/../cicd/environments" "$OUT_DIRECTORY/environments"
buildTemplate -f "$CFGFILE" -t "$STARTING_FILE" -o "$OUT_DIRECTORY/docker-compose.yml" --add TAG_NAME=$TAG
+101
View File
@@ -0,0 +1,101 @@
#!/bin/bash
######
# Generates dockerfiles
# It expects docker to be logged in to ECR (see aws-login)
set -e
VERBOSE=0
DIR=`dirname $(realpath "${BASH_SOURCE[0]}")`
OLDDIR=`pwd`
cd "$DIR"
source "$DIR/../config"
source "$DIR/libs.sh"
usage() {
echo "Usage: $0 [-h] [-v] [-dub] [-i: input, default] -o: output " 1>&2;
echo "This script builds framework docker images, the following options are available:" 1>&2;
echo " -h: This message" 1>&2;
echo " Environment" 1>&2;
echo " -d: Debug" 1>&2;
echo " -u: Unit" 1>&2;
echo " -b: Base" 1>&2;
echo " -i: Config Input File" 1>&2;
echo " -o: Output file" 1>&2;
echo " -v: Verbose" 1>&2;
exit 1;
}
OUTFILE=""
CFGFILE="../config"
SELECTED_MODES=0
DEBUG_MODE=0
UNIT_MODE=0
BASE_MODE=0
while getopts "i:o:hvdub" o; do
case "${o}" in
d)
DEBUG_MODE=1
SELECTED_MODES+=1
;;
u)
UNIT_MODE=1
SELECTED_MODES+=1
;;
b)
BASE_MODE=1
SELECTED_MODES+=1
;;
i)
if [[ "$OPTARG" != "${OPTARG#/}" ]]; then
CFGFILE="${OPTARG}"
else
CFGFILE="${OLDDIR}/${OPTARG}"
fi
;;
o)
if [[ "$OPTARG" != "${OPTARG#/}" ]]; then
OUTFILE="${OPTARG}"
else
OUTFILE="${OLDDIR}/${OPTARG}"
fi
;;
v)
VERBOSE=1
;;
h)
usage
;;
*)
printerr "Unsupported option ${o}"
usage
;;
esac
done
if [[ $SELECTED_MODES -ne 1 ]]; then
printerr "Invalid mode"
usage
fi
if [[ $OUTFILE == "" ]]; then
printerr "Missing outfile"
usage
fi
STARTING_FILE="../cicd/dockerfiles/build.dockerfile"
if [[ $DEBUG_MODE -eq 1 ]]; then
STARTING_FILE="../cicd/dockerfiles/debug.dockerfile"
fi
if [[ $UNIT_MODE -eq 1 ]]; then
STARTING_FILE="../cicd/dockerfiles/test.dockerfile"
fi
msg "Using source file $STARTING_FILE"
buildTemplate -f "$CFGFILE" -t "$STARTING_FILE" -o "$OUTFILE"
+74
View File
@@ -0,0 +1,74 @@
#!/bin/bash
# Shared utility methods
function printerr() {
echo -e "\e[31m[ERROR]\e[0m $1"
}
function msg() {
if [[ $VERBOSE -gt 0 ]]; then
echo "$1"
fi
}
function buildTemplate() {
local CONFIG_FILE=""
local SEPARATOR="§"
declare -A ADDITIONALS
while [[ "$#" -gt 0 ]]
do
case $1 in
-f|--input-file)
CONFIG_FILE="$2"
;;
-t|--template)
local SOURCE_FILE="$2"
;;
-o|--output)
local OUTPUT_FILE="$2"
;;
-s|--separator)
SEPARATOR="$2"
;;
--add)
arrIN=(${2//=/ })
ADDITIONALS[${arrIN[0]}]=${arrIN[1]}
;;
esac
shift
done
if [[ -z $CONFIG_FILE ]]; then
printerr "Impossible to call ${FUNCNAME} without passing a config file file"
exit 1
else
source "$CONFIG_FILE"
fi
if [[ -z $OUTPUT_FILE ]]; then
printerr "Impossible to call ${FUNCNAME} without passing an output file"
exit 1
fi
if [[ -z $SOURCE_FILE ]]; then
printerr "Impossible to call ${FUNCNAME} without passing a template file"
exit 1
fi
TMPFILE=`mktemp`
cp "$SOURCE_FILE" "$OUTPUT_FILE"
for config_key in "${!CONFIG[@]}"; do
cp "$OUTPUT_FILE" "$TMPFILE"
config_value="${CONFIG[$config_key]}"
cat "$TMPFILE" | sed -e "s~${SEPARATOR}${config_key}${SEPARATOR}~${config_value}~g" > "$OUTPUT_FILE"
done
for config_key in "${!ADDITIONALS[@]}"; do
cp "$OUTPUT_FILE" "$TMPFILE"
config_value="${ADDITIONALS[$config_key]}"
cat "$TMPFILE" | sed -e "s~${SEPARATOR}${config_key}${SEPARATOR}~${config_value}~g" > "$OUTPUT_FILE"
done
}
+52
View File
@@ -0,0 +1,52 @@
#!/bin/bash
######
# This script is used to publish images to ECR.
# It exepcts images to be already built on the local docker
# It expects docker to be logged in to ECR (see aws-login)
set -e
VERBOSE=0
DIR=`dirname $(realpath "${BASH_SOURCE[0]}")`
OLDDIR=`pwd`
cd "$DIR"
source "$DIR/../config"
source "$DIR/libs.sh"
usage() {
echo "Usage: $0 [-h] [-v] [-t: version]" 1>&2;
echo "This script pushes framework docker images, the following options are available:" 1>&2;
echo " -h: This message" 1>&2;
echo " -t: Framework version, this will be used as base for the docker image tag (default: local)" 1>&2;
echo " -v: Verbose" 1>&2;
exit 1;
}
TAG=""
while getopts "t:hv" o; do
case "${o}" in
t)
TAG=${OPTARG}
;;
v)
VERBOSE=1
;;
h)
usage
;;
*)
printerr "Unsupported option ${o}"
usage
;;
esac
done
if [[ $TAG == "" ]]; then
printerr "Tag (-t) is required"
fi
msg "Pushing project version $TAG"
docker push $PROJECT_REPOSITORY/$PROJECT_GROUP/$PROJECT_BUILDNAME:$TAG
+47
View File
@@ -0,0 +1,47 @@
#!/bin/bash
######
# This script runs unit tests and fails if unit tests don't succeed
# It expects docker to be logged in to ECR (see aws-login)
set -e
VERBOSE=0
DIR=`dirname $(realpath "${BASH_SOURCE[0]}")`
OLDDIR=`pwd`
cd "$DIR"
source "$DIR/../config"
source "$DIR/libs.sh"
usage() {
echo "Usage: $0 [-h] [-v] [-t: version] [image type, see below] " 1>&2;
echo "This script runs unit test for docker images, the image must exist:" 1>&2;
echo " -h: This message" 1>&2;
echo " -t: Framework version, this will be used as base for the docker image tag (default: local)" 1>&2;
echo " -v: Verbose" 1>&2;
exit 1;
}
TAG="local"
while getopts "t:hvaAjJmub" o; do
case "${o}" in
t)
TAG=${OPTARG}
;;
v)
VERBOSE=1
;;
h)
usage
;;
*)
printerr "Unsupported option ${o}"
usage
;;
esac
done
msg "Checking unit tests for version $TAG"
docker run $PROJECT_REPOSITORY/$PROJECT_GROUP/$PROJECT_BUILDNAME:$TAG-unit -c /test-phpframework/phpunit.xml