crimson/compose

123 lines
3.8 KiB
Text
Raw Normal View History

2024-12-23 15:39:16 +00:00
#!/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
# ================================================================ CONSTANTS ==
# ROOT: This is the folder the crimson project is located in. ROOT is used to
# access docker-compose files, base.env, and other crimson files.
# 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 for $DATA and $SOURCE. Read base.env
# for more information.
ROOT="$(realpath "$(dirname "$0")")"
CALL_ROOT="$(pwd)"
# ================================================================ FUNCTIONS ==
# add_arg - adds arguments to the docker compose command to be run
# include_env - loads a .env file
# include - adds the docker compose file to be included called
# docker-compose.<$1>.yml
docker_args=""
function add_arg {
docker_args="$docker_args $@"
}
function include_env {
local file
file="$1"
if [ -f "$file" ]; then
source "$file"
add_arg --env-file $file
fi
}
function include {
local file
if [ "$2" = "true" ]; then
file="$ROOT/docker/docker-compose.$1.yml"
add_arg -f $file
fi
}
# ================================================================ BOOTSTRAP ==
# Enter the crimson project directory, load all .env files, and pick which
# docker-compose.*.yml files are requested. Then make the docker volumes here
# with the correct permissions. If we let docker do it, it will make them owned
# by root (thanks) and break our containers.
cd "$ROOT"
# get docker file includes
include_env "$ROOT/base.env"
include_env "$CALL_ROOT/.env"
include "base" "true"
include "db" "$POSTGRES_ENABLED"
include "api" "$API_ENABLED"
# assert SOURCE and DATA are set
if [ -z "$SOURCE" ]; then
printf "fatal: SOURCE is not set. See '$ROOT/base.env'\n"
exit 1
fi
if [ -z "$DATA" ]; then
printf "fatal: DATA is not set. See '$ROOT/base.env'\n"
exit 1
fi
# preset perms (postgres will crash if not)
if [ ! -d "$DATA" ]; then
mkdir -p "$DATA"
mkdir -p "$DATA/crimson"
mkdir -p "$DATA/schemas"
chown -R 1000:1000 "$DATA"
fi
# run docker compose
exec -a docker -- docker compose -p $PROJECT_NAME $docker_args "$@"