diff options
Diffstat (limited to '')
-rwxr-xr-x | bin/compose | 112 | ||||
-rwxr-xr-x | bin/psql (renamed from psql) | 23 | ||||
-rwxr-xr-x | bin/setup_env | 62 |
3 files changed, 183 insertions, 14 deletions
diff --git a/bin/compose b/bin/compose new file mode 100755 index 0000000..20c9992 --- /dev/null +++ b/bin/compose @@ -0,0 +1,112 @@ +#!/bin/sh +### CRIMSON --- A simple PHP framework. +### Copyright © 2024 Freya Murphy <contact@freyacat.org> +### +### This file is part of CRIMSON. +### +### CRIMSON is free software; you can redistribute it and/or modify it +### under the terms of the GNU General Public License as published by +### the Free Software Foundation; either version 3 of the License, or (at +### your option) any later version. +### +### CRIMSON is distributed in the hope that it will be useful, but +### WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +### GNU General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with CRIMSON. If not, see <http://www.gnu.org/licenses/>. + +# `compose` +# This script will provide a docker compose interface with all the CRIMSON +# environment setup. This script may request root privilages since it needs all +# docker containers to run with user 1000:1000. If you are not user 1000:1000, +# root privlage is required to create volume folders with uid/gid 1000. + +# Make sure errors fail to avoid nasal demons +set -e + +# ========================================================= PERMISSION CHECK == +# Make sure we are either root or user 1000:1000. This is required for crimson. + +args="$@" +command="$0 $args" + +uid=$(id -u) +gid=$(id -g) + +if [[ $uid -eq 1000 ]] && [[ $gid -eq 1000 ]]; then + true # all set +elif [[ $uid -eq 0 ]] && [[ $gid -eq 0 ]]; then + true # all set +else + # root required (1000:1000 may not exist) + exec sudo -E $command +fi + +# ========================================================= LOAD ENVIRONMENT == +# We need to load the environment variables provided by crimson and the user +# making the project. +SCRIPT_DIR="$(dirname "$0")" +source "$SCRIPT_DIR/setup_env" + +# ================================================================ BOOTSTRAP == +# Choose which docker compose files are to be loaded, and setup which arguemnts +# to pass into docker compose. + +docker_args="" + +# add env files to docker args + +docker_env_args=$(echo "$ENV_FILES" | tr ':' '\n' | sed 's/^/--env-file /' | tr '\n' ' ') +docker_args="$docker_args $docker_env_args" + +# get list of env files for docker containers + +docker_env_files="[$(echo "$ENV_FILES" | tr ':' "\n" | sed 's/.*/"&"/' | tr '\n' ',')]" + +# add compose files to docker args + +function include_docker { + local name src dest bool + name="docker-compose.$1.yml" + src="$CRIMSON_ROOT/docker/$name" + dest="/tmp/crimson/docker/$name" + + bool="$2" + if [[ "$bool" == "true" ]]; then + + mkdir -p "$(dirname "$dest")" + sed "s#DOCKER_ENV_FILES#$docker_env_files#" "$src" > "$dest" + + docker_args="$docker_args -f $dest" + fi +} + +include_docker "base" "true" +include_docker "db" "$POSTGRES_ENABLED" +include_docker "api" "$API_ENABLED" + +# set project name + +docker_args="$docker_args -p $PROJECT_NAME" + +# source check + +if [ ! -d "$PROJECT_SOURCE" ]; then + printf "fatal: cannot find PROJECT_SOURCE: '$PROJECT_SOURCE'\n" > /dev/stderr + exit 1 +fi + +# data check + +if [ ! -d "$PROJECT_DATA" ]; then + mkdir -p "$PROJECT_DATA" + mkdir -p "$PROJECT_DATA/schemas" + mkdir -p "$PROJECT_DATA/crimson" + chown -R 1000:1000 "$PROJECT_DATA" +fi + +# run docker compose + +exec -a docker -- docker compose $docker_args "$@" @@ -25,21 +25,16 @@ # database at times. This makes it nicer enter it since you dont have to type # the full command. :) -# ================================================================ CONSTANTS == -# ROOT: This is the folder the crimson project is located in. ROOT is used to -# load the crimson environment. We need this since that is where POSTGRES_USER -# and POSTGRES_DB are stored. -# CALL_ROOT: This is the folder that the user who called `compose` is currently -# in. For crimson to work this must be the folder that your project using -# crimson is. This is because crimson loads `.env` here to load any user -# specified environment. `.env` is needed in cause you override POSTGRES_USER -# and/or POSTGRES_DB. -ROOT="$(dirname "$0")" -CALL_ROOT="$(pwd)" +# Make sure errors fail to avoid nasal demons +set -e + +# ========================================================= LOAD ENVIRONMENT == +# We need to load the environment variables provided by crimson and the user +# making the project. +SCRIPT_DIR="$(dirname "$0")" +source "$SCRIPT_DIR/setup_env" # ================================================================ BOOTSTRAP == # Load `base.env` and `.env`, then launch psql in docker. -source "$ROOT/base.env" -source "$CALL_ROOT/.env" -$ROOT/compose exec postgres psql -U "${POSTGRES_USER}" "${POSTGRES_DB}" "$@" +$SCRIPT_DIR/compose exec postgres psql -U "${POSTGRES_USER}" "${POSTGRES_DB}" "$@" diff --git a/bin/setup_env b/bin/setup_env new file mode 100755 index 0000000..4a3049c --- /dev/null +++ b/bin/setup_env @@ -0,0 +1,62 @@ +#!/bin/sh +### CRIMSON --- A simple PHP framework. +### Copyright © 2024 Freya Murphy <contact@freyacat.org> +### +### This file is part of CRIMSON. +### +### CRIMSON is free software; you can redistribute it and/or modify it +### under the terms of the GNU General Public License as published by +### the Free Software Foundation; either version 3 of the License, or (at +### your option) any later version. +### +### CRIMSON is distributed in the hope that it will be useful, but +### WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +### GNU General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with CRIMSON. If not, see <http://www.gnu.org/licenses/>. + +# `setup_env` +# This script sets up the environment for crimson by loading all required .env +# files. + +# Make sure errors fail to avoid nasal demons +set -e + +# ================================================================ CONSTANTS == +# CRIMSON_ROOT: This is the folder the crimson project is located in. This +# variable is needed since it is used to load any files located in crimson, +# including base.env, and docker compose files. +# PROJECT_ROOT: This is the folder that the user who called any crimson script +# is currently located in. For crimson to work this must be the folder that +# your project checkout is in. This is because crimson loads `.env` here to +# load any user specified environment. +# PROJECT_DATA: Where docker persistent data will be stored. +# PROEJCT_SOURCE: Where user project code is located. +# ENV_FILES: A colon seperated list of all .env files loaded. +export CRIMSON_ROOT="$(realpath "$(dirname "$(dirname "$0")")")" +export PROJECT_ROOT="$(realpath "$(pwd)")" +export PROJECT_DATA="${PROJECT_ROOT}/data" +export PROJECT_SOURCE="${PROJECT_ROOT}/src" +ENV_FILES="" + +# ================================================================ BOOTSTRAP == +# Load `base.env` and `.env` +function load_env { + local file seperator + file="$1" + seperator=":" + if [ -f "$file" ]; then + # dont add seperator at the start + if [ -z "$ENV_FILES" ]; then + seperator="" + fi + # load file + source "$file" + ENV_FILES="${ENV_FILES}${seperator}${file}" + fi +} + +load_env "$CRIMSON_ROOT/base.env" +load_env "$PROJECT_ROOT/.env" |