From 69c6b35d17aaf7f0d248497f7579e3023e1e29ee Mon Sep 17 00:00:00 2001 From: Philippe Caseiro Date: Thu, 15 Jun 2023 12:20:28 +0200 Subject: [PATCH] feat(initApp): adding new script to start a project the "clean way" --- init-app | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100755 init-app diff --git a/init-app b/init-app new file mode 100755 index 0000000..c19e80b --- /dev/null +++ b/init-app @@ -0,0 +1,142 @@ +#!/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}" \ No newline at end of file