symfony-kube-skeletor/init-app
2023-06-19 14:28:01 +02:00

142 lines
3.4 KiB
Bash
Executable File

#!/bin/sh
#
# Initialize a new symfony app
#
DOCKERFILES_ROOT="misc/images"
KUSTOMIZE_DIR="misc/k8s/kustomization"
SKELL_GIT_URL="https://forge.cadoles.com/CadolesKube/symfony-kube-skeletor.git"
run_form() {
local conf_dest="${1}"
echo "Welcome !"
echo
read -p "What is your application name (myapp) ? " appName
local appName=${appName:-myapp}
read -p "Where do you want to start your versions ? (0.0.1) : " initVersion
local initVersion=${initVersion:-0.0.1}
read -p "List your standard git branches (stable staging dev) : " initBranches
local initBranches=${initBranches:-"stable staging dev"}
local branches=""
for br in ${initBranches}; do
branches="${branches}\"${br}\", "
done
read -p "Docker registry address (reg.cadoles.com) : " dockerRegistry
local dockerRegistry=${dockerRegistry:-reg.cadoles.com}
cat <<__EOF__ > ${conf_dest}
{
"app": {
"name": "${appName}",
"init_version": "${initVersion}",
"branches": [ ${branches::-2} ]
},
"registry": {
"address": "${dockerRegistry}"
}
}
__EOF__
}
create_docker_secret() {
local conf="${1}"
local registry=$(jq -cr ".registry.address" ${conf})
docker login ${registry}
mkdir -p ${KUSTOMIZE_DIR}/base/secrets/dockerconfig
mkdir -p ${KUSTOMIZE_DIR}/overlays/dev/secrets/dockerconfig
docker --config ${KUSTOMIZE_DIR}/base/secrets/dockerconfig login ${registry}
mv ${KUSTOMIZE_DIR}/base/secrets/dockerconfig/config.json ${KUSTOMIZE_DIR}/base/secrets/dockerconfig/.dockerconfigjson
cp ${KUSTOMIZE_DIR}/base/secrets/dockerconfig/.dockerconfigjson ${KUSTOMIZE_DIR}/overlays/dev/secrets/dockerconfig/.dockerconfigjson
}
init_app() {
local conf="${1}"
local appDest="${2}/${appName}"
jq empty ${conf}
if [ ${?} -ne 0 ]; then
echo "Invalid application configuration ${conf}"
return 3
fi
if [ ! -d ${appDest} ]; then
mkdir -p ${appDest}
else
echo "Error ${appDest} allready exists"
return 2
fi
git clone --branch "feat/automation" ${SKELL_GIT_URL} ${appDest}
cd ${appDest}
rm -rf .git
echo "Setting Dockerfiles"
dirs=$(find ${DOCKERFILES_ROOT}/* -type d -name 'myapplication-*')
for d in ${dirs}; do
mv ${d} $(echo ${d} | sed "s/myapplication/${appName}/g")
done
echo "Setting Kustomization directories"
dirs=$(find ${KUSTOMIZE_DIR}/* -type d -name 'myapplication-*')
for d in ${dirs}; do
mv ${d} $(echo ${d} | sed "s/myapplication/${appName}/g")
done
echo "Setting Kustomization files"
dirs=$(find ${KUSTOMIZE_DIR}/* -type f -name 'myapplication-*')
for d in ${dirs}; do
mv ${d} $(echo ${d} | sed "s/myapplication/${appName}/g")
done
grep -rl "myapplication" ${KUSTOMIZE_DIR} | xargs sed -i "s/myapplication/${appName}/g"
echo "Setting Skaffold configuration"
sed -i "s/myapplication/${appName}/g" ${appDest}/skaffold.yaml
echo "Setting docker registry secrets"
create_docker_secret ${conf}
cd -
}
main() {
local cleanup="0"
if [ -n "${1}" ]; then
app_conf="$(pwd)/${1}"
else
cleanup="1"
app_conf=$(mktemp)
run_form "${app_conf}"
cat ${app_conf}
fi
local appName=$(jq -cr ".app.name" ${app_conf})
appName=${appName,,}
local appDest=${2:-"/tmp/${appName}"}
init_app ${app_conf} ${appDest}
if [ "${cleanup}" -eq 1 ]; then
rm -rf ${app_conf}
fi
}
CONFIG=""
DEST="/tmp"
while getopts "c:d:" option
do
case $option in
c) CONFIG=${OPTARG} ;;
d) DEST=${OPTARG} ;;
esac
done
main "${CONFIG}" "${DEST}"