From ce5f843b32a6a64063b9f99d5288b8a279a38aef Mon Sep 17 00:00:00 2001 From: casjay Date: Wed, 12 Oct 2022 21:15:50 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=97=83=EF=B8=8F=20Committing=20everything?= =?UTF-8?q?=20that=20changed=20=F0=9F=97=83=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 6 +- bin/entrypoint-deno.sh | 188 +++++++++++++++++++++++++++++++---------- data/sample.ts | 10 --- 3 files changed, 148 insertions(+), 56 deletions(-) delete mode 100644 data/sample.ts diff --git a/Dockerfile b/Dockerfile index e04a6b1..3bd097e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,8 +17,10 @@ ENV TZ="$TIMEZONE" \ RUN set -ex; \ rm -Rf "/etc/apk/repositories"; \ + mkdir -p "/usr/local/share/template-files/data/htdocs/www" && \ apt-get update && apt-get upgrade -yy && apt-get install -yy \ - unzip + unzip && \ + git clone -q "https://github.com/casjay-templates/bunjs" "/usr/local/share/template-files/data/htdocs/www" COPY ./bin/. /usr/local/bin/ COPY ./data/. /usr/local/share/template-files/data/ @@ -26,7 +28,7 @@ COPY ./config/. /usr/local/share/template-files/config/ RUN chmod -Rf 755 /usr/local/bin/get-deno.sh && \ /usr/local/bin/get-deno.sh && \ - rm -Rf /usr/local/bin/get-deno.sh /tmp/* /bin/.gitkeep /config /data /var/lib/apt/lists/* + rm -Rf /usr/local/bin/get-deno.sh /tmp/* /bin/.gitkeep /config /data /var/lib/apt/lists/* /usr/local/share/template-files/data/htdocs/www/.git FROM scratch diff --git a/bin/entrypoint-deno.sh b/bin/entrypoint-deno.sh index 03f211a..a4f0d05 100755 --- a/bin/entrypoint-deno.sh +++ b/bin/entrypoint-deno.sh @@ -1,81 +1,181 @@ #!/usr/bin/env bash +# shellcheck shell=bash # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -##@Version : 202207101005-git -# @Author : Jason Hempstead -# @Contact : jason@casjaysdev.com -# @License : WTFPL -# @ReadME : entrypoint-deno.sh --help -# @Copyright : Copyright: (c) 2022 Jason Hempstead, Casjays Developments -# @Created : Sunday, Jul 10, 2022 10:05 EDT -# @File : entrypoint-deno.sh -# @Description : -# @TODO : -# @Other : -# @Resource : -# @sudo/root : no +##@Version : 202210121546-git +# @@Author : Jason Hempstead +# @@Contact : jason@casjaysdev.com +# @@License : WTFPL +# @@ReadME : entrypoint-deno.sh --help +# @@Copyright : Copyright: (c) 2022 Jason Hempstead, Casjays Developments +# @@Created : Wednesday, Oct 12, 2022 15:46 EDT +# @@File : entrypoint-deno.sh +# @@Description : +# @@Changelog : New script +# @@TODO : Better documentation +# @@Other : +# @@Resource : +# @@Terminal App : no +# @@sudo/root : no +# @@Template : other/docker-entrypoint # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Set bash options [ -n "$DEBUG" ] && set -x # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - APPNAME="$(basename "$0" 2>/dev/null)" -VERSION="202207101005-git" +VERSION="202210121546-git" HOME="${USER_HOME:-$HOME}" USER="${SUDO_USER:-$USER}" RUN_USER="${SUDO_USER:-$USER}" -SRC_DIR="${BASH_SOURCE%/*}" -# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -' -__exec_bash() { [ $# -ne 0 ] && exec "${*:-bash -l}" || exec /bin/bash -l; } -__find() { ls -A "$*" 2>/dev/null; } +SCRIPT_SRC_DIR="${BASH_SOURCE%/*}" # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -DATA_DIR="$(__find /data/ 2>/dev/null | grep '^' || false)" -CONFIG_DIR="$(__find /config/ 2>/dev/null | grep '^' || false)" +# Set functions +__find() { ls -A "$*" 2>/dev/null || return 10; } +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +__exec_command() { + local cmd="${*:-/bin/bash -l}" + local exitCode=0 + echo "Executing command: $cmd" + eval "$cmd" || exitCode=10 + [ "$exitCode" = 0 ] || exitCode=10 + return ${exitCode:-$?} +} +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +# Functions +__heath_check() { + local status=0 + #curl -q -LSsf -o /dev/null -s -w "200" "http://localhost/server-health" || status=$(($status + 1)) + echo "$(uname -s) $(uname -m) is running" + return ${status:-$?} +} +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +# Define default variables - don not change these export TZ="${TZ:-America/New_York}" +export LOCAL_BIN_DIR="${LOCAL_BIN_DIR:-/usr/local/bin}" export HOSTNAME="${HOSTNAME:-casjaysdev-bin}" +export SSL="${SSL:-false}" +export SSL_DIR="${SSL_DIR:-/config/ssl}" +export SSL_CA="${SSL_CA:-$SSL_DIR/ca.crt}" +export SSL_KEY="${SSL_KEY:-$SSL_DIR/server.key}" +export SSL_CERT="${SSL_CERT:-$SSL_DIR/server.crt}" # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -if [[ -n "${TZ}" ]]; then - echo "${TZ}" >/etc/timezone +# Additional variables and variable overrides +export HTTP_PORT="${HTTP_PORT:-80}" +export HTTPS_PORT="${HTTPS_PORT:-443}" +export SERVICE_PORT="${SERVICE_PORT:-}" +export DEFAULT_CONF_DIR="${DEFAULT_CONF_DIR:-/usr/local/share/template-files/config/defaults}" +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +# import variables from file +[ -f "/root/env.sh" ] && . "/root/env.sh" +[ -f "/config/env.sh" ] && "/config/env.sh" +[ -f "/config/.env.sh" ] && . "/config/.env.sh" +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +# Set timezone +[ -n "${TZ}" ] && echo "${TZ}" >"/etc/timezone" +[ -f "/usr/share/zoneinfo/${TZ}" ] && ln -sf "/usr/share/zoneinfo/${TZ}" "/etc/localtime" +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +# Set hostname +if [ -n "${HOSTNAME}" ]; then + echo "${HOSTNAME}" >"/etc/hostname" + echo "127.0.0.1 ${HOSTNAME} localhost ${HOSTNAME}.local" >"/etc/hosts" fi # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -if [[ -f "/usr/share/zoneinfo/${TZ}" ]]; then - ln -sf "/usr/share/zoneinfo/${TZ}" "/etc/localtime" +# Delete any gitkeep files +[ -d "/data" ] && rm -Rf "/data/.gitkeep" "/data"/*/*.gitkeep +[ -d "/config" ] && rm -Rf "/config/.gitkeep" "/data"/*/*.gitkeep +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +# Setup bin directory +if [ -d "/config/bin" ]; then + for bin in /config/bin/*; do + name="$(basename "$bin")" + ln -sf "$bin" "/usr/local/bin/$name" + done fi # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -if [[ -n "${HOSTNAME}" ]]; then - echo "${HOSTNAME}" >/etc/hostname - echo "127.0.0.1 $HOSTNAME localhost" >/etc/hosts +# Create directories +[ -d "/etc/ssl" ] || mkdir -p "/etc/ssl" +[ -d "/usr/local/bin" ] && rm -Rf "/usr/local/bin/.gitkeep" +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +if [ "$SSL" = "true" ] || [ "$SSL" = "yes" ]; then + if [ -f "/config/ssl/server.crt" ] && [ -f "/config/ssl/server.key" ]; then + export SSL="true" + if [ -n "$SSL_CA" ] && [ -f "$SSL_CA" ]; then + mkdir -p "/etc/ssl/certs" + cat "$SSL_CA" >>"/etc/ssl/certs/ca-certificates.crt" + fi + else + [ -d "$SSL_DIR" ] || mkdir -p "$SSL_DIR" + create-ssl-cert + fi + type update-ca-certificates &>/dev/null && update-ca-certificates fi # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -[[ -f "/config/.env" ]] && source /config/.env +[ -f "$SSL_CA" ] && cp -Rfv "$SSL_CA" "/etc/ssl/ca.crt" +[ -f "$SSL_KEY" ] && cp -Rfv "$SSL_KEY" "/etc/ssl/server.key" +[ -f "$SSL_CERT" ] && cp -Rfv "$SSL_CERT" "/etc/ssl/server.crt" +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +# Create default config +if [ ! -e "/config/$APPNAME" ] && [ -e "$DEFAULT_CONF_DIR/$APPNAME" ]; then + cp -Rf "$DEFAULT_CONF_DIR/$APPNAME" "/config/$APPNAME" +fi +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +# Setup deno +mkdir -p "/data/htdocs/www" && cd "/data/htdocs/www" || exit 10 +if [ -z "$1" ] && [ -z "$(ls -A "/data/htdocs/www"/* 2>/dev/null)" ]; then + cp -Rf "/usr/local/share/template-files/data/htdocs/www/." "/data/htdocs/www/" + RUN_SCRIPT="src/index.ts" +fi +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +# Additional commands -if [ -z "$1" ] && [ -z "$(ls -A "/data/htdocs/www" 2>/dev/null)" ]; then - FRESH_INSTALL="true" - deno run -A -r https://fresh.deno "/data/htdocs/www" -fi # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - case "$1" in ---help) +--help) # Help message + echo 'Docker container for '$APPNAME'' echo "Usage: $APPNAME [healthcheck, bash, command]" - exit + echo "Failed command will have exit code 10" + echo "" + exit ${exitCode:-$?} ;; -healthcheck) - type -P deno &>/dev/null && echo 'OK' || exit 1 + +healthcheck) # Docker healthcheck + __heath_check || exitCode=10 + exit ${exitCode:-$?} ;; -sh | bash | shell | /bin/sh | /bin/bash) + +*/bin/sh | */bin/bash | bash | shell | sh) # Launch shell shift 1 - __exec_bash "$@" + __exec_command "${@:-/bin/bash}" + exit ${exitCode:-$?} ;; deno) shift 1 - deno run --allow-net "${@:-/data/htdocs/www/sample.ts}" + deno "$@" ;; -*) - if [ "$FRESH_INSTALL" = "true" ]; then - deno --allow-all task start + +*) # Execute primary command + if [ $# -eq 0 ]; then + if [ -n "$RUN_SCRIPT" ]; then + START_SCRIPT="$RUN_SCRIPT" + elif [ -f "src/index.ts" ]; then + RUN_SCRIPT="index.ts" + elif [ -f "index.ts" ]; then + RUN_SCRIPT="index.ts" + elif [ -f "app.ts" ]; then + RUN_SCRIPT="app.ts" + elif [ -f "server.ts" ]; then + RUN_SCRIPT="server.ts" + fi + deno --allow-all task start || + deno run --watch --allow-all "${@:-}" + exit ${exitCode:-$?} else - deno run --watch --allow-all "${@:-/data/htdocs/www/sample.ts}" + __exec_command "$@" + exitCode=$? fi ;; esac # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -#end +# end of entrypoint +exit ${exitCode:-$?} +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/data/sample.ts b/data/sample.ts deleted file mode 100644 index e860653..0000000 --- a/data/sample.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { serve } from 'https://deno.land/std@0.77.0/http/server.ts'; - -const PORT = 1993; -const s = serve(`0.0.0.0:${PORT}`); -const body = new TextEncoder().encode('Hello World\n'); - -console.log(`Server started on port ${PORT}`); -for await (const req of s) { - req.respond({ body }); -}