diff --git a/.gitignore b/.gitignore index 4d97c5e..7af1d09 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ bin/cron/ var/ +systems/* + + +script/clone \ No newline at end of file diff --git a/.profile b/.profile index b1ff785..049dcfd 100644 --- a/.profile +++ b/.profile @@ -1,10 +1,11 @@ -# set GIT branch in bash +# GIT branch in bash START parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' } PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ ' +# GIT branch in bash END -# ssh setup +# SSH Setup START SSH_ENV="$HOME/.ssh/agent-environment" function start_agent { echo "Initialising new SSH agent..." @@ -25,3 +26,4 @@ if [ -f "$SSH_ENV" ]; then else start_agent fi +# SSH Setup END \ No newline at end of file diff --git a/.systems b/.systems new file mode 100644 index 0000000..7f27cc6 --- /dev/null +++ b/.systems @@ -0,0 +1,9 @@ +# Systems configuration +# configure the systems for the dev-env with the following pattern: +# +# = +# +# LIKE CRON - THE FILE HAS TO END WITH AN EMPTY LINE +# +backend weedkeeper/backend ssh://git@docker.local:222/flo/weedkeeper-backend.git +frontend weedkeeper/frontend ssh://git@docker.local:222/flo/weedkeeper-frontend.git diff --git a/README.md b/README.md new file mode 100644 index 0000000..860d447 --- /dev/null +++ b/README.md @@ -0,0 +1,53 @@ +Folgende Kommandos müssen abgebildet werden: + +befehle: +denv + - init -> call script + - update -> call script + + - build -> dcom + - stop -> dcom + - down -> dcom + - up -> dcom + +drun + - docker exec + - examples: + drun composer install # Executes composer install in container associated with current directory + drun weedkeeper-backend composer install # Executes composer install in weedkeeper-backend container + +dcom + - build + - stop + - down + - up + - examples: + dcom build # Executes docker compose build with docker-compose file located in docker/ + dcom weedkeeper-backend build # Executes docker compose build with docker-compose file located in weedkeeper-backend/docker/ + + +dev-env: + bin: + denv + drun + dcom + script: + init + clone + update + + docker: + build + stop + down + up -d + + +system: + .env + script + init + update + firstRun + docker + docker-compose.yml \ No newline at end of file diff --git a/bin/dcom b/bin/dcom new file mode 100644 index 0000000..df5ce05 --- /dev/null +++ b/bin/dcom @@ -0,0 +1,53 @@ +#!/bin/bash +ENV_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd ) +source $ENV_DIR/bin/denv_msg + +dcom() { + if [ $# -lt 1 ]; then + denv_error_msg "Verwendung: dcom [system] " + return 1 + fi + + local SYSTEMS_CONFIG="$ENV_DIR/.systems" + + SYSTEM_IDENTIFIER="$1" + SYSTEM_PATH=$(grep "^$SYSTEM_IDENTIFIER" "$SYSTEMS_CONFIG" | cut -d' ' -f2) + + if [ -z "$SYSTEM_PATH" ]; then # First Parameter is not a valid system identifier + SYSTEMS_DIRECTORY=$(realpath "$ENV_DIR/systems") + CURRENT_DIRECTORY=$(realpath $(pwd)) + + # We currently are in a subdirectory of dev-env/systems + if [[ "$CURRENT_DIRECTORY" == "$SYSTEMS_DIRECTORY"* ]]; then + ASSUMED_SYSTEM_PATH=$(realpath --relative-to="$SYSTEMS_DIRECTORY" "$CURRENT_DIRECTORY") + SYSTEM_PATH=$(grep "$ASSUMED_SYSTEM_PATH" "$SYSTEMS_CONFIG" | cut -d' ' -f2) + + if [ -z "$SYSTEM_PATH" ]; then # You are NOT in a configured system directory (though in a subdirectory of dev-env/systems). + denv_error_msg "$CURRENT_DIRECTORY is not a configured dev-env system path.\nPlease register the system in $ENV_DIR/.systems, navigate to a registered system, or provide a system identifer as the first parameter of dcom." + return 1 + else # You are in a configured system directory + SYSTEM_IDENTIFIER=$(grep "$ASSUMED_SYSTEM_PATH" "$SYSTEMS_CONFIG" | cut -d' ' -f1) + fi + else + denv_error_msg "$CURRENT_DIRECTORY is not a subdirectory of $ENV_DIR/systems.\nPlease navigate to a configured system, or provide a system identifer as the first parameter of dcom." + return 1 + fi + + else # First Parameter is a valid system identifier + shift # Remove first Parameter + fi + + SYSTEM_PATH="$ENV_DIR/systems/$SYSTEM_PATH" + DOCKER_COMPOSE_COMMAND="$@" + DOCKER_COMPOSE_FILE="$SYSTEM_PATH/docker/docker-compose.yml" + + if [[ ! -f "$DOCKER_COMPOSE_FILE" ]]; then + denv_error_msg "$DOCKER_COMPOSE_FILE was not found. Have you run init yet?" + return 1 + fi + + denv_info_msg "System: $SYSTEM_IDENTIFIER" + denv_info_msg "Command: $DOCKER_COMPOSE_COMMAND" + + docker compose -f "$DOCKER_COMPOSE_FILE" $DOCKER_COMPOSE_COMMAND +} \ No newline at end of file diff --git a/bin/denv b/bin/denv index 675c2b0..5d9e5e6 100644 --- a/bin/denv +++ b/bin/denv @@ -1,14 +1,8 @@ #!/bin/bash - ENV_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/../" &> /dev/null && pwd ) denv() { - if [ ! -z $1 ] - then - if [[ "$1" == "setup" ]]; then - echo "SETUP IS ONLY ALLOWED ONCE AFTER CLOING THE ENVIRONMENT!!!" - exit 0 - fi + if [ ! -z $1 ] then $ENV_DIR/$1 else cd $ENV_DIR diff --git a/bin/denv_msg b/bin/denv_msg new file mode 100644 index 0000000..4403c3a --- /dev/null +++ b/bin/denv_msg @@ -0,0 +1,25 @@ +#!/bin/bash +Black='\033[0;30m' +Red='\033[0;31m' +Green='\033[0;32m' +Yellow='\033[0;33m' +Blue='\033[0;34m' +Purple='\033[0;35m' +Cyan='\033[0;36m' +White='\033[0;37m' + +denv_error_msg () { + echo -e "${Red}$@${White}" +} + +denv_success_msg () { + echo -e "${Green}$@${White}" +} + +denv_info_msg () { + echo -e "${Cyan}$@${White}" +} + +denv_echo_msg () { + echo -e "${White}$@" +} \ No newline at end of file diff --git a/bin/git b/bin/git index feb31d1..8560656 100644 --- a/bin/git +++ b/bin/git @@ -6,6 +6,3 @@ acp() { git push } -co() { - git checkout $1 -} diff --git a/bin/script/foreachSystem b/bin/script/foreachSystem deleted file mode 100755 index 4bc4f98..0000000 --- a/bin/script/foreachSystem +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/bash - -COMMAND="$@" -ENV_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/../.." &> /dev/null && pwd ) - -source ${ENV_DIR}/bin/dexec - -if [ $# -lt 1 ]; then - echo "Verwendung: foreachSystem up -d" - return 1 -fi - - -for SYSTEM in $ENV_DIR/systems/*/*/ ; do - relative_path="${SYSTEM#*/systems/}" - relative_path="${relative_path%/}" - - typePart="${relative_path%/*}" # before "/" eg. backend / frontend - systemPart="${relative_path##*/}" # after "/" eg. bee / auth / etc - SYSTEM_CODE="$systemPart-$typePart" - DENV_COMMAND="dexec" - - if [[ "$COMMAND" == "init" ]]; then - $ENV_DIR/systems/$typePart/$systemPart/bin/script/init - elif [[ "$COMMAND" == "update" ]]; then - $ENV_DIR/systems/$typePart/$systemPart/bin/script/update - else - $DENV_COMMAND $SYSTEM_CODE $COMMAND - fi -done diff --git a/bin/script/pull b/bin/script/pull deleted file mode 100755 index 884fda5..0000000 --- a/bin/script/pull +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -EXEC_DIR=$(pwd) - -echo "Execute git pull in each system" - -for SYSTEM in $EXEC_DIR/systems/*/*/ ; do - cd $SYSTEM - git pull - cd $EXEC_DIR -done diff --git a/docker/docker-compose-mac.yml.dist b/docker/docker-compose-mac.yml.dist deleted file mode 100644 index bf5e33a..0000000 --- a/docker/docker-compose-mac.yml.dist +++ /dev/null @@ -1,27 +0,0 @@ -networks: - template: - name: template - -services: - traefik: - image: traefik:v3.0 - networks: - - template - command: - - "--providers.docker=true" - - "--entrypoints.web.address=:80" - - "--entrypoints.web.http.redirections.entrypoint.to=websecure" - - "--entrypoints.web.http.redirections.entrypoint.scheme=https" - - "--entrypoints.websecure.address=:443" - - "--certificatesresolvers.le.acme.tlschallenge=true" - - "--certificatesresolvers.le.acme.email=florian_feistel@outlook.de" - - "--certificatesresolvers.le.acme.storage=/letsencrypt/acme.json" - ports: - - "80:80" - - "443:443" - - "8080:8080" - volumes: - - "./letsencrypt:/letsencrypt" - - "/var/run/docker.sock:/var/run/docker.sock" - labels: - - "traefik.http.routers.traefik.rule=Host(`traefik.template.local`)" diff --git a/init b/init index 4c67f44..06333ed 100755 --- a/init +++ b/init @@ -1,33 +1,17 @@ #!/bin/bash - -SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) - -source $SCRIPT_DIR/bin/denv -source $SCRIPT_DIR/bin/dexec -source $SCRIPT_DIR/bin/drun - -EXIT=0 +ENV_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) # Check docker-compose.yml file -if [ ! -f "$SCRIPT_DIR/docker/docker-compose.yml" ] +if [ ! -f "$ENV_DIR/docker/docker-compose.yml" ] then - cp "$SCRIPT_DIR/docker/docker-compose.yml.dist" "$SCRIPT_DIR/docker/docker-compose.yml" - EXIT=1 + cp "$ENV_DIR/docker/docker-compose.yml.dist" "$ENV_DIR/docker/docker-compose.yml" fi -# Check docker-compose-mac.yml file -if [ ! -f "$SCRIPT_DIR/docker/docker-compose-mac.yml" ] -then - cp "$SCRIPT_DIR/docker/docker-compose-mac.yml.dist" "$SCRIPT_DIR/docker/docker-compose-mac.yml" - EXIT=1 -fi +# Update .profile +$ENV_DIR/script/update_profile +# Clone all systems. +$ENV_DIR/script/clone -if [ $EXIT -eq 1 ] -then - echo "docker-compose files created, please change variables and call init again" - exit 1 -fi - -denvexec up -d -$SCRIPT_DIR/bin/script/foreachSystem init +# Run init for all systems +./script/foreachSystem init diff --git a/script/foreachSystem b/script/foreachSystem new file mode 100755 index 0000000..d0602bf --- /dev/null +++ b/script/foreachSystem @@ -0,0 +1,33 @@ +#!/bin/bash +COMMAND="$@" +ENV_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd ) +CONFIG_FILE="$ENV_DIR/.systems" + +source $ENV_DIR/bin/denv_msg + +# Check if .systems configuration exists +if [ ! -f "$CONFIG_FILE" ]; then + denv_error_msg "$CONFIG_FILE not found." + exit 1 +fi + +# Iterate through all lines in .systems configuration +while IFS=' ' read -r IDENTIFIER DIRECTORY GIT; do + # Skip comments and emtpy lines + if [[ "$IDENTIFIER" =~ ^#.* ]] || [ -z "$IDENTIFIER" ]; then + continue + fi + + denv_info_msg "System $IDENTIFIER" + SYSTEM_SCRIPT_DIRECTORY=$(realpath "systems/$DIRECTORY/bin/script") + COMMAND_PATH="$SYSTEM_SCRIPT_DIRECTORY/$COMMAND"; + + # Check if system directory already exists + if [ ! -f "$COMMAND_PATH" ]; then + denv_error_msg "$COMMAND_PATH not found." + exit 1 + else + denv_echo_msg "Executing $COMMAND_PATH..." + $COMMAND_PATH + fi +done < "$CONFIG_FILE" diff --git a/script/update_profile b/script/update_profile new file mode 100755 index 0000000..7c4d5ea --- /dev/null +++ b/script/update_profile @@ -0,0 +1,34 @@ +#!/bin/bash +ENV_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd ) + +PROFILE_FILE="$HOME/.profile" +TEMPLATE_FILE="$ENV_DIR/.profile" + +START_MARKER="#DEV ENV SETUP START" +END_MARKER="#DEV ENV SETUP END" + + +# Check if template file exists +if [ ! -f "$TEMPLATE_FILE" ]; then + echo "[DEV-ENV]: Template file '$TEMPLATE_FILE' does not exist. Skip updating .profile." + exit 1 +fi + +TEMPLATE_CONTENT=$(<"$TEMPLATE_FILE") + +echo "[DEV-ENV]: Update $PROFILE_FILE" + +# Check if start marker already exists in profile file +if grep -q "$START_MARKER" "$PROFILE_FILE"; then + echo "[DEV-ENV]: Section already exists. Update contents..." + + # Remove contents between start and end marker + sed -i "/$START_MARKER/,/$END_MARKER/d" "$PROFILE_FILE" +else + echo "[DEV-ENV]: Section does not exists. Adding contents..." +fi + +# Add contents +echo -e "\n$START_MARKER\n$TEMPLATE_CONTENT\n$END_MARKER" >> "$PROFILE_FILE" + +echo "[DEV-ENV]: Successfully updated $PROFILE_FILE" diff --git a/setup b/setup deleted file mode 100755 index e5ebc32..0000000 --- a/setup +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash - -# THIS SCRIPT NEEDS TO BE EXECUTED FIRST AFTER CLONING THE ENVIRONMENT AND SHALL NEVER BE EXECUTED AGAIN -# IT SETS UP THE ENVIRONMENT IN THE PROFILE TO ENABLE CORE COMMANDS LIKE denv drun dexec - -# MAC -if [[ "$OSTYPE" == "darwin"* ]]; then - home="$HOME" - -# LINUX -elif [[ "$OSTYPE" == "linux-gnu" ]]; then - home="$HOME" - -else - echo "Dieses Skript wird auf deinem Gerät nicht unterstützt" - exit 1 -fi - -ENV_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) - -echo "Setup dev-env..." - -echo "Adding sourcing of environment commands to .profile" -echo "" >> ~/.profile -echo "# denv commands setup" >> ~/.profile -echo "source $ENV_DIR/bin/denv" >> ~/.profile -echo "source $ENV_DIR/bin/drun" >> ~/.profile -echo "source $ENV_DIR/bin/dexec" >> ~/.profile -echo "source $ENV_DIR/bin/git" >> ~/.profile - -echo "appending dev-env .profile to .profile" -echo "" >> ~/.profile -cat .profile >> ~/.profile diff --git a/systems/.gitignore b/systems/.gitignore deleted file mode 100644 index d6b7ef3..0000000 --- a/systems/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore