Skip to content

Commit

Permalink
entrypoint cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jonocodes committed May 7, 2024
1 parent 58915ca commit e49551b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 70 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
docker-up: ## start docker stack in foreground
docker compose up --build --abort-on-container-exit

# TODO: add user
# TODO: make recipes or perhaps use go-task?
# add user
# start local?
# make local dirs
# setup local db
# run tests
# install local requirements
# push production image

# or evaluate go-task perhaps

help: ## get help
@grep -E '^[a-zA-Z_-]+:.*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
- MYSQL_USER=cypht
- MYSQL_PASSWORD=cypht_password
cypht:
# image: sailfrog/cypht-docker:latest
build:
context: .
dockerfile: ./docker/Dockerfile
Expand Down
10 changes: 2 additions & 8 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
FROM php:7.4.33-fpm-alpine

# ENV CYPHT_DEST "/usr/local/share/cypht"

# WORKDIR "/var/www"

# TODO: change this to /app for simplification?
WORKDIR "/usr/local/share/cypht"

RUN set -e \
Expand All @@ -28,14 +23,13 @@ COPY docker/nginx.conf /etc/nginx/nginx.conf
COPY docker/supervisord.conf /etc/supervisord.conf
COPY composer.* .

# TODO: probably dont want to run update here since it modifies composer.lock
# TODO: probably dont want to run update here since it modifies composer.lock and its inconsistant with INSTALL
RUN composer update && composer self-update --2 && composer install
# RUN composer install

COPY . .
COPY .env.example .env

EXPOSE 80 443
EXPOSE 80

HEALTHCHECK CMD curl --fail http://localhost || exit 1

Expand Down
57 changes: 15 additions & 42 deletions docker/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,9 @@
set -e

APP_DIR=/usr/local/share/cypht
cd ${APP_DIR}


# TODO: validate env var values here, perhaps in php



# Wait for database to be ready then setup tables for sessions, authentication, and settings as needed
${APP_DIR}/scripts/setup_database.php

${APP_DIR}/scripts/setup_system.sh

#
# Additional tasks based on the newly-configured settings
#
# TODO: validate env var values here, perhaps in php or in Hm_Site_Config_File()

# TODO: source these defaults from an .env file or some other place?
USER_CONFIG_TYPE="${USER_CONFIG_TYPE:-file}"
Expand All @@ -25,46 +14,30 @@ ATTACHMENT_DIR="${ATTACHMENT_DIR:-/var/lib/hm3/attachments}"
APP_DATA_DIR="${APP_DATA_DIR:-/var/lib/hm3/app_data}"


if [ "${USER_CONFIG_TYPE}" = "file" ]
then
chown www-data:www-data ${USER_SETTINGS_DIR}
fi

chown www-data:www-data ${ATTACHMENT_DIR}
chown -R www-data:www-data /var/lib/nginx
chown www-data:www-data ${APP_DATA_DIR}
# Wait for database to be ready then setup tables for sessions, authentication, and settings as needed
./scripts/setup_database.php

./scripts/setup_system.sh

# Generate the run-time configuration
#
cd $APP_DIR
php ./scripts/config_gen.php

#
# Enable the program in the web-server
#
rm -r /var/www
ln -s ${APP_DIR}/site /var/www

# Enable the program in the web-server

# TODO: should a user be created if USER_CONFIG_TYPE=file ?

if [[ "${USER_CONFIG_TYPE}" = "DB" && -n "${AUTH_USERNAME}" ]]
if [ "${USER_CONFIG_TYPE}" = "file" ]
then
php ./scripts/create_account.php ${AUTH_USERNAME} ${AUTH_PASSWORD}
chown www-data:www-data ${USER_SETTINGS_DIR}
fi

chown www-data:www-data ${ATTACHMENT_DIR}
chown -R www-data:www-data /var/lib/nginx
chown www-data:www-data ${APP_DATA_DIR}

#
# Close out tasks
#

# now that we're definitely done writing configuration, let's clear out the relevant environment variables (so that stray "phpinfo()" calls don't leak secrets from our code)
#for e in "${envs[@]}"; do
# unset "$e"
#done
rm -r /var/www
ln -s $(pwd)/site /var/www

# Start supervisord and services
# Start services
/usr/bin/supervisord -c /etc/supervisord.conf

exec "$@"
# exec "$@" # TODO: what is this for?
18 changes: 2 additions & 16 deletions scripts/setup_database.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
#!/usr/bin/env php

<?php

define('APP_PATH', dirname(dirname(__FILE__)).'/');
define('VENDOR_PATH', APP_PATH.'vendor/');

/* get the framework */
require VENDOR_PATH.'autoload.php';
require APP_PATH.'lib/framework.php';


/* get config object */
$config = new Hm_Site_Config_File();

// $session_type = getenv('SESSION_TYPE') ?: 'PHP';
// $auth_type = getenv('AUTH_TYPE') ?: 'DB';
// $user_config_type = getenv('USER_CONFIG_TYPE') ?: 'file';
// $db_driver = getenv('DB_DRIVER') ?: 'mysql';
// $db_name = getenv('DB_NAME') ?: 'cypht_db';
// $db_user = getenv('DB_USER');
// $db_pass = getenv('DB_PASS');
// $db_driver = getenv('DB_DRIVER') ?: 'mysql';
// $db_host = getenv('DB_HOST') ?: '127.0.0.1';
// $db_socket = getenv('DB_SOCKET') ?: '/tmp/temp_cypht.sqlite';

$session_type = $config->get('session_type');
$auth_type = $config->get('auth_type');
$user_config_type = $config->get('user_config_type');
Expand Down Expand Up @@ -50,7 +37,7 @@
// $conn = new pdo("{$db_driver}:host={$db_host};dbname={$db_name}", $db_user, $db_pass);

if ($db_driver == 'sqlite') {
// TODO: sqlite should be handled by connection. not manually done here.
// TODO: sqlite should be handled by connect(). not manually done here.
$conn = new pdo("{$db_driver}:{$db_socket}");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
Expand Down Expand Up @@ -93,7 +80,6 @@
}

try {
// TODO: figure out why this is not working for sqlite
$rows = $conn->exec($stmt);
printf("{$stmt}\n");
} catch (PDOException $e) {
Expand Down
11 changes: 9 additions & 2 deletions scripts/setup_system.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@ mkdir -p ${ATTACHMENT_DIR}
echo "Creating directory for application data ${APP_DATA_DIR}"
mkdir -p ${APP_DATA_DIR}

# TODO: should a user be created if USER_CONFIG_TYPE=file ?
if [[ "${USER_CONFIG_TYPE}" = "DB" && -n "${AUTH_USERNAME}" ]]
then
php ./scripts/create_account.php ${AUTH_USERNAME} ${AUTH_PASSWORD}
fi

# TODO: move this here from docker-entrypoint. I think it depends on the module system?
#

# TODO: move this here from docker-entrypoint. I think it depends on the module system? works in docker, but not local
#
# Generate the run-time configuration
#
# php ${SCRIPT_DIR}/../scripts/config_gen.php
# php ./scripts/config_gen.php

0 comments on commit e49551b

Please sign in to comment.