From 62b24b6713cb2d27574a33634d2d19813294a64e Mon Sep 17 00:00:00 2001 From: David Kebler Date: Fri, 15 Jan 2021 07:41:34 -0800 Subject: [PATCH] builds custom environment file from a cfg file and uses docker env-file option to use in yaml template file which uses make-env-file script that sets defaults against the cfg settings. --- .gitignore | 1 + docker-compose.yml | 19 +++++++++++++++++++ editors/example.cfg | 8 ++++++++ make-env-file | 31 +++++++++++++++++++++++++++++++ restart | 5 +++++ start | 6 ++++++ stop | 6 ++++++ 7 files changed, 76 insertions(+) create mode 100644 .gitignore create mode 100644 docker-compose.yml create mode 100644 editors/example.cfg create mode 100755 make-env-file create mode 100755 restart create mode 100755 start create mode 100755 stop diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..03bd412 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.env diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..1b8efa4 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,19 @@ +version: "2.1" +services: + code-server: + image: ghcr.io/linuxserver/code-server + container_name: ${Name} + environment: + - PUID=${PUID} + - PGID=${PGID} + - TZ=${TZ} + - PASSWORD=${Password} + # in container files directory + # - USER_DATA_DIR=/config/workspace + volumes: + - ${Config}:/config + - ${Files}:/config/workspace + + ports: + - ${Port}:8443 + restart: unless-stopped diff --git a/editors/example.cfg b/editors/example.cfg new file mode 100644 index 0000000..a981e52 --- /dev/null +++ b/editors/example.cfg @@ -0,0 +1,8 @@ +#!/bin/bash +# Name=containername +# USER=jim +# TZ=America/Los_Angles +# Password=jimshoe +Parent_Folder=/mnt/data +Files=/mnt/data/test-files +Port=4321 # should be changed to avoid conflict with other containers diff --git a/make-env-file b/make-env-file new file mode 100755 index 0000000..4f246fa --- /dev/null +++ b/make-env-file @@ -0,0 +1,31 @@ +#!/bin/bash + +is_p_int() { [ "$1" ] && [ -z "${1//[0-9]}" ] ;} + +declare -A settings + +[[ ! $(getent group docker) ]] && echo no docker group on machine, exiting && exit +[[ ! $1 ]] && echo "no editor set, usage make " && exit +SDIR=$(cd $(dirname $(readlink -f "$0")) >/dev/null 2>&1 ; pwd -P) +ENV_FILE=$SDIR/editors/${1}.cfg +# source any environment for particular site +# shopt -s extglob +# ENV_FILE=$(ls ${FILE}* 2> /dev/null | grep -v '.off$\|.example$\|.tmpl$\|.template$') +[[ -f $ENV_FILE ]] && source $ENV_FILE || echo WARNING no settings file $ENV_FILE, using defaults +PUID=$(id -u $USER 2> /dev/null) +[[ ! $PUID ]] && echo no user $USER, exiting && exit +[[ ! $(getent group docker | grep ${USER}) ]] && echo $USER not in docker group, exiting && exit +__ce_PUID=${PUID:-$([[ ! $(is_p_int PUID) ]] && echo $(id -u ))} +__ce_PGID=${PGID:-$(getent group docker | cut -d: -f3)} +ename=$(basename "${1%.*}") +__ce_Name=${Name:-$ename-files-editor} +__ce_TZ=${TZ:-"America/Los_Angeles"} +__ce_Password=${Password:-password} +Parent_Folder=${Parent_Folder:-${HOME}/.local/share/code-edit} +__ce_Files=${Files:-${Parent_Folder}/$ename/files} +mkdir -p ${__ce_Files} +__ce_Config=${Config:-${Parent_Folder}/$ename/config} +mkdir -p ${__ce_Config} +__ce_Port=${Port:-8443} + +echo "$( set -o posix ; set | grep __ce_ | sed 's/__ce_//')" > $SDIR/editors/docker.$ename.env diff --git a/restart b/restart new file mode 100755 index 0000000..dc75c64 --- /dev/null +++ b/restart @@ -0,0 +1,5 @@ +#!/bin/bash +[[ ! $1 ]] && echo "no editor set, usage restart " && exit +SDIR=$(cd $(dirname $(readlink -f "$0")) >/dev/null 2>&1 ; pwd -P) +source $SDIR/stop $1 +source $SDIR/start $1 diff --git a/start b/start new file mode 100755 index 0000000..890b8a2 --- /dev/null +++ b/start @@ -0,0 +1,6 @@ +#!/bin/bash +[[ ! $1 ]] && echo "no editor set, usage start " && exit +SDIR=$(cd $(dirname $(readlink -f "$0")) >/dev/null 2>&1 ; pwd -P) +env_file=$SDIR/editors/docker.${1}.env +[[ ! -f $env_file ]] && $($SDIR/make-env-file $1) +docker-compose --env-file $SDIR/editors/docker.${1}.env up -d diff --git a/stop b/stop new file mode 100755 index 0000000..68fb53e --- /dev/null +++ b/stop @@ -0,0 +1,6 @@ +#!/bin/bash +[[ ! $1 ]] && echo "no editor set, usage start " && exit +SDIR=$(cd $(dirname $(readlink -f "$0")) >/dev/null 2>&1 ; pwd -P) +env_file=$SDIR/editors/docker.${1}.env +[[ ! -f $env_file ]] && $($SDIR/make-env-file $1) +docker-compose --env-file $SDIR/editors/docker.${1}.env down