Skip to content

Commit

Permalink
feat: add basic environment creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Michele Mondelli committed Apr 29, 2023
1 parent 5f09000 commit 37ef02a
Show file tree
Hide file tree
Showing 18 changed files with 551 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .bmeme/build/bin/app
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

set -e
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
source "${SCRIPT_DIR}/../common/lib.sh"

for var in "$@"
do
PARAMS="$PARAMS ${var}"
done

if [[ -t 1 ]] ; then OPTS=""; else OPTS="-T"; fi

docker-compose ps ${CONTAINER} | grep -q Up | true
ret=${PIPESTATUS[1]}
if [ $ret -eq 1 ]; then
echo -e "${CR}Error${CN} - It seems that the ${CONTAINER} container is not running"
exit 1
fi

docker-compose exec -u $(id -u):$(id -g) ${OPTS} ${CONTAINER} /bin/bash -c "${PARAMS}"
31 changes: 31 additions & 0 deletions .bmeme/build/bin/c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

set -e
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
source "${SCRIPT_DIR}/../common/lib.sh"

PWD=$(pwd)
while [[ "$PWD" != "" && ! -e "$PWD/.bmeme" ]]; do
PWD=${PWD%/*}
done

FOUND=$PWD

if [[ "${FOUND}" = "" ]]; then
echo -e "${CR}Error${CN} - Unable to find parent bmeme project!"
exit -1
fi

ORIG_DIR=$(pwd)
REL_DIR=${ORIG_DIR#"$FOUND"}

set -e
QUOTEDCMD=()
for token in "$@"; do
QUOTEDCMD+=($(printf "%q" "$token"))
done
CMD="${QUOTEDCMD[*]}"

pushd ${FOUND} > /dev/null
${APP_BIN} cd ".${REL_DIR}" \; ${CMD}
popd > /dev/null
21 changes: 21 additions & 0 deletions .bmeme/build/commands/docker/create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

set -e

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
source "${SCRIPT_DIR}/../../common/lib.sh"

if [[ "$1" == "descr" ]]; then
echo -n "Create and start docker environment"
exit 0
fi

if [[ "$1" == "run" ]]; then
echo -e "============ ${CB}Creating${CN} docker environment ..."
echo "${LOGPREFIX} CMD docker-compose build --pull --build-arg FIX_UID=\"\$(id -u)\" --build-arg FIX_GID=\"\$(id -g)\"" >> ${LOGFILE}
docker-compose build --pull --build-arg FIX_UID="$(id -u)" --build-arg FIX_GID="$(id -g)"
echo ""
echo -e "============ ${CB}Starting${CN} docker environment ..."
echo "${LOGPREFIX} CMD docker-compose up -d" >> ${LOGFILE}
docker-compose up -d
fi
16 changes: 16 additions & 0 deletions .bmeme/build/commands/docker/destroy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

set -e

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
source "${SCRIPT_DIR}/../../common/lib.sh"

if [[ "$1" == "descr" ]]; then
echo -n "Clean docker environment"
exit 0
fi

if [[ "$1" == "run" ]]; then
echo -e "============ ${CB}Cleaning${CN} docker environment ..."
docker-compose down -v
fi
16 changes: 16 additions & 0 deletions .bmeme/build/commands/docker/logs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

set -e

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
source "${SCRIPT_DIR}/../../common/lib.sh"

if [[ "$1" == "descr" ]]; then
echo -n "Show docker container logs"
exit 0
fi

if [[ "$1" == "run" ]]; then
echo -e "============ ${CB}Showing${CN} docker container logs ..."
docker-compose logs -f
fi
17 changes: 17 additions & 0 deletions .bmeme/build/commands/docker/shell.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

set -e

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
source "${SCRIPT_DIR}/../../common/lib.sh"

if [[ "$1" == "descr" ]]; then
echo -n "Run a shell inside the docker container"
exit 0
fi

if [[ "$1" == "run" ]]; then
echo -e "============ ${CB}Running${CN} a shell inside the docker container ..."
echo "${LOGPREFIX} CMD ${C_BIN} bash" >> ${LOGFILE}
${C_BIN} bash
fi
16 changes: 16 additions & 0 deletions .bmeme/build/commands/docker/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

set -e

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
source "${SCRIPT_DIR}/../../common/lib.sh"

if [[ "$1" == "descr" ]]; then
echo -n "Start docker environment"
exit 0
fi

if [[ "$1" == "run" ]]; then
echo -e "============ ${CB}Starting${CN} docker environment ..."
docker-compose start
fi
16 changes: 16 additions & 0 deletions .bmeme/build/commands/docker/status.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

set -e

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
source "${SCRIPT_DIR}/../../common/lib.sh"

if [[ "$1" == "descr" ]]; then
echo -n "Check docker environment status"
exit 0
fi

if [[ "$1" == "run" ]]; then
echo -e "============ ${CB}Checking${CN} docker environment status ..."
docker-compose ps
fi
16 changes: 16 additions & 0 deletions .bmeme/build/commands/docker/stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

set -e

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
source "${SCRIPT_DIR}/../../common/lib.sh"

if [[ "$1" == "descr" ]]; then
echo -n "Stop docker environment"
exit 0
fi

if [[ "$1" == "run" ]]; then
echo -e "============ ${CB}Stopping${CN} docker environment ..."
docker-compose stop
fi
21 changes: 21 additions & 0 deletions .bmeme/build/commands/requirements.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

set -e

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
source "${SCRIPT_DIR}/../common/lib.sh"

if [[ "$1" == "descr" ]]; then
echo -n "Check system requirements"
exit 0
fi

if [[ "$1" == "run" ]]; then
echo ""
echo -e "Checking ${CG}system requirements${CN} ..."
echo ""
echo "${LOGPREFIX} CMD docker --version" >> ${LOGFILE}
docker --version 2>&1 >> ${LOGFILE}
echo "${LOGPREFIX} CMD docker-compose --version" >> ${LOGFILE}
docker-compose --version 2>&1 >> ${LOGFILE}
fi
59 changes: 59 additions & 0 deletions .bmeme/build/common/lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bash

set -e

CN="\\033[0m"
CR="\\033[91m"
CG="\\033[92m"
CY="\\033[93m"
CB="\\033[94m"
CT="\\033[37m"
TB="\\033[1m"

LIB_SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
BASE_PRJ_DIR=$(realpath "${LIB_SCRIPT_DIR}/../../..")
if test -f "${BASE_PRJ_DIR}/.env"; then
source "${BASE_PRJ_DIR}/.env"
fi
APP_DIR="${BASE_PRJ_DIR}/app"

CONTAINER="app"

BUILD_DIR="${BASE_PRJ_DIR}/.bmeme/build"
LOCAL_PATH="${BUILD_DIR}/bin"
C_BIN="${LOCAL_PATH}/c"
APP_BIN="${LOCAL_PATH}/app"

LOGDIR="${BASE_PRJ_DIR}/.bmeme/build/log"
LOGFILE="${LOGDIR}/configure.log"
LOGPREFIX=">>>> "

# Load custom configuration
if test -f "${BUILD_DIR}/config"; then
source "${BUILD_DIR}/config"
fi

log_begin() {
echo "" >>${LOGFILE}
echo "${LOGPREFIX} SCRIPT EXECUTED" >>${LOGFILE}
echo "${LOGPREFIX} Bin: ${0}" >>${LOGFILE}
echo "${LOGPREFIX} Params: ${@}" >>${LOGFILE}
echo "${LOGPREFIX} Time: $(date +'%Y-%m-%d %H:%M:%S')" >>${LOGFILE}
echo "" >>${LOGFILE}
}

log_end() {
echo "" >>${LOGFILE}
echo "${LOGPREFIX} SCRIPT TERMINATED" >>${LOGFILE}
echo "${LOGPREFIX} Time: $(date +'%Y-%m-%d %H:%M:%S')" >>${LOGFILE}
echo "" >>${LOGFILE}
}

show_cmd_error() {
echo -e "${TB}${CR}ERROR${CN}"
echo ""
echo -e "See ${CB}logfile${CN} for details: "
echo -e "\t${CY}tail -f ${LOGFILE}${CN}"
echo ""
exit 1
}
Loading

0 comments on commit 37ef02a

Please sign in to comment.