Skip to content

Home Information - Your home, your data, organized.

Python

Added script to generate env file for running.

Anthony Cassandra65083cc575a7
diff --git a/Dockerfile b/Dockerfile
index 95245dac..5d954785 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -25,7 +25,9 @@ COPY src /src
EXPOSE 8000
-VOLUME /data
-RUN mkdir -p /data
+VOLUME /data/database
+RUN mkdir -p /data/database
+VOLUME /data/media
+RUN mkdir -p /data/media
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf" ]
diff --git a/Makefile b/Makefile
index 1f51ede7..5d6eb39c 100644
--- a/Makefile
+++ b/Makefile
@@ -11,8 +11,12 @@ docker-build: Dockerfile
--label "build-date=$(NOW_DATE)" \
--tag hi:$(HI_VERSION) .
-docker-run: Dockerfile flutter-webapp-build
+docker-run: .private/env/local.sh Dockerfile
./run_container.sh
docker-stop:
docker stop hi
+
+local-env: .private/env/local.sh
+ ./packaging/env-generate.py
+
diff --git a/packaging/env-generate.py b/packaging/env-generate.py
new file mode 100755
index 00000000..89744f37
--- /dev/null
+++ b/packaging/env-generate.py
@@ -0,0 +1,234 @@
+#!/usr/bin/env python3
+
+import os
+import secrets
+import stat
+import shutil
+import string
+
+from dataclasses import dataclass
+
+
+@dataclass
+class EmailSettings:
+ email_address : str
+ password : str
+ host : str
+ port : str
+ use_tls : bool
+ use_ssl : bool
+
+
+class HiEnvironmentGenerator:
+
+ SECRETS_DIRECTORY = '.private/env'
+
+ DEFAULT_SETTINGS = {
+ 'DJANGO_SETTINGS_MODULE': 'hi.settings.local',
+ 'HI_SUPPRESS_AUTHENTICATION':'true',
+ 'HI_REDIS_HOST': '127.0.0.1',
+ 'HI_REDIS_PORT': '6379',
+ 'HI_REDIS_KEY_PREFIX': 'local',
+ 'HI_EMAIL_SUBJECT_PREFIX': '',
+ 'HI_DB_PATH': '/data/database', # Docker container location
+ 'HI_MEDIA_PATH': '/data/media', # Docker container location
+ }
+
+ def __init__( self, env_name = 'local' ):
+ self._env_name = env_name
+
+ self._settings_map = dict()
+ self._settings_map.update( self.DEFAULT_SETTINGS )
+
+ self._destination_filename = os.path.join( self.SECRETS_DIRECTORY, f'{self._env_name}.sh' )
+ return
+
+ def setup_secrets_directory(self):
+ if not os.path.exists( self.SECRETS_DIRECTORY ):
+ print( f'Creating directory: {self.SECRETS_DIRECTORY}' )
+ os.makedirs( self.SECRETS_DIRECTORY, exist_ok = True )
+ os.chmod( self.SECRETS_DIRECTORY, stat.S_IRWXU ) # Read/write/execute for user only
+ return
+
+ def check_existing_env_file(self):
+
+ if os.path.exists( self._destination_filename ):
+ print( f'WARNING: {self._destination_filename} already exists.' )
+ overwrite = input( 'Do you want to overwrite it? (y/n): ').strip().lower()
+ if overwrite not in ['yes', 'y']:
+ print( 'Env file generation cancelled.' )
+ exit(1)
+
+ backup_filename = f'{self._destination_filename}.BAK'
+ print(f'Creating backup: {backup_filename}')
+ shutil.copy2(self._destination_filename, backup_filename)
+ return
+
+ def generate_env_file( self ):
+
+ self.setup_secrets_directory()
+ self.check_existing_env_file()
+
+ email_settings = self.get_email_settings()
+
+ django_admin_email = email_settings.email_address
+ django_admin_password = self.generate_memorable_password()
+
+ from_email = email_settings.email_address
+ server_email = email_settings.email_address
+
+ self._settings_map['DJANGO_SECRET_KEY'] = self.generate_secret_key()
+ self._settings_map['DJANGO_SUPERUSER_EMAIL'] = django_admin_email
+ self._settings_map['DJANGO_SUPERUSER_PASSWORD'] = django_admin_password
+ self._settings_map['HI_DEFAULT_FROM_EMAIL'] = from_email
+ self._settings_map['HI_SERVER_EMAIL'] = server_email
+ self._settings_map['HI_EMAIL_HOST'] = email_settings.host
+ self._settings_map['HI_EMAIL_PORT'] = email_settings.port
+ self._settings_map['HI_EMAIL_HOST_USER'] = email_settings.email_address
+ self._settings_map['HI_EMAIL_HOST_PASSWORD'] = email_settings.password
+ self._settings_map['HI_EMAIL_USE_TLS'] = str(email_settings.use_tls)
+ self._settings_map['HI_EMAIL_USE_SSL'] = str(email_settings.use_ssl)
+
+ self.write_file()
+
+ print( '\nYour Django admin credentials:' )
+ print( f' Email: {django_admin_email}' )
+ print( f' Password: {django_admin_password}\n' )
+ return
+
+ def get_email_settings(self):
+
+ print( 'We need email settings to set up the Django configuration file.' )
+
+ email_address = input('Enter your email address: ').strip()
+ password = input('Enter your email password: ').strip()
+
+ domain = email_address.split('@')[-1]
+
+ settings = None
+ if domain in self.COMMON_EMAIL_PROVIDER_SETTINGS:
+ use_predefined = input( f'Used predefined settings for {domain}? (y/n) ' )
+ if use_predefined.strip().lower() == 'y':
+ settings = self.COMMON_EMAIL_PROVIDER_SETTINGS[domain]
+ else:
+ print( f'Unknown email provider: {domain}.' )
+
+ if not settings:
+ print( 'Please provide SMTP settings.' )
+ settings = {
+ 'host': input('Enter SMTP host: ').strip(),
+ 'port': input('Enter SMTP port: ').strip(),
+ 'use_tls': input('Use TLS (Y/n): ').strip().lower() != 'n',
+ 'use_ssl': input('Use SSL (y/N): ').strip().lower() == 'y',
+ }
+
+ return EmailSettings(
+ email_address = email_address,
+ password = password,
+ host = settings['host'],
+ port = settings['port'],
+ use_tls = settings['use_tls'],
+ use_ssl = settings['use_ssl'],
+ )
+
+ def generate_memorable_password( self, num_words = 2, separator = "-" ):
+
+ words = [
+ 'apple', 'banana', 'cherry', 'delta', 'eagle', 'falcon', 'grape',
+ 'hunter', 'island', 'joker', 'kitten', 'lemon', 'melon', 'ninja', 'ocean'
+ ]
+
+ chosen_words = [ secrets.choice(words) for _ in range(num_words) ]
+ random_number = str( secrets.randbelow( 100 )) # A number between 0-99
+ chosen_words.append( str(random_number) )
+
+ password = separator.join(chosen_words)
+ return password
+
+ def write_file( self ):
+ with open( self._destination_filename, 'w' ) as fh:
+ for name, value in self._settings_map.items():
+ fh.write( f'export {name}="{value}"\n' )
+ continue
+ print( f'File created: {self._destination_filename}' )
+ return
+
+ def generate_secret_key( self, length : int = 50 ):
+ chars = string.ascii_letters + string.digits + string.punctuation
+ return ''.join(secrets.choice(chars) for _ in range(length))
+
+ COMMON_EMAIL_PROVIDER_SETTINGS = {
+ 'gmail.com': {
+ 'host': 'smtp.gmail.com',
+ 'port': '587',
+ 'use_tls': True,
+ 'use_ssl': False,
+ },
+ 'yahoo.com': {
+ 'host': 'smtp.mail.yahoo.com',
+ 'port': '465',
+ 'use_tls': False,
+ 'use_ssl': True,
+ },
+ 'outlook.com': {
+ 'host': 'smtp.office365.com',
+ 'port': '587',
+ 'use_tls': True,
+ 'use_ssl': False,
+ },
+ 'hotmail.com': {
+ 'host': 'smtp.office365.com',
+ 'port': '587',
+ 'use_tls': True,
+ 'use_ssl': False,
+ },
+ 'icloud.com': {
+ 'host': 'smtp.mail.me.com',
+ 'port': '587',
+ 'use_tls': True,
+ 'use_ssl': False,
+ },
+ 'aol.com': {
+ 'host': 'smtp.aol.com',
+ 'port': '587',
+ 'use_tls': True,
+ 'use_ssl': False,
+ },
+ 'zoho.com': {
+ 'host': 'smtp.zoho.com',
+ 'port': '587',
+ 'use_tls': True,
+ 'use_ssl': False,
+ },
+ 'protonmail.com': {
+ 'host': '127.0.0.1', # Requires the ProtonMail Bridge app
+ 'port': '1025', # Default for ProtonMail Bridge
+ 'use_tls': True,
+ 'use_ssl': False,
+ },
+ 'yandex.com': {
+ 'host': 'smtp.yandex.com',
+ 'port': '465',
+ 'use_tls': False,
+ 'use_ssl': True,
+ },
+ 'fastmail.com': {
+ 'host': 'smtp.fastmail.com',
+ 'port': '465',
+ 'use_tls': False,
+ 'use_ssl': True,
+ },
+ # Mail.com
+ 'mail.com': {
+ 'host': 'smtp.mail.com',
+ 'port': '587',
+ 'use_tls': True,
+ 'use_ssl': False,
+ },
+ }
+
+
+if __name__ == "__main__":
+
+ generator = HiEnvironmentGenerator()
+ generator.generate_env_file()
diff --git a/run_container.sh b/run_container.sh
index eb66baec..096775f7 100755
--- a/run_container.sh
+++ b/run_container.sh
@@ -1,12 +1,13 @@
#!/bin/bash
-ENV_VAR_FILE=".private/env/stage.sh"
-HI_DATA_DIR="/var/hi/data"
-EXTERNAL_PORT=8000
+ENV_VAR_FILE=".private/env/local.sh"
+DATABASE_DATA_DIR="/var/hi/database"
+MEDIA_DATA_DIR="/var/hi/media"
+EXTERNAL_PORT=9411
BACKGROUND_FLAGS=""
usage() {
- echo "Usage: $0 [-env ENV_VAR_FILE] [-dir HI_DATA_DIR] [-port EXTERNAL_PORT] [-bg]"
+ echo "Usage: $0 [-env ENV_VAR_FILE] [-db DATABASE_DATA_DIR] [-media MEDIA_DATA_DIR] [-port EXTERNAL_PORT] [-bg]"
exit 1
}
@@ -16,11 +17,15 @@ while [[ $# -gt 0 ]]; do
ENV_VAR_FILE="$2"
shift 2
;;
- -dir)
- HI_DATA_DIR="$2"
+ -db)
+ DATABASE_DATA_DIR="$2"
shift 2
- ;;
- -port)
+ ;;
+ -media)
+ MEDIA_DATA_DIR="$2"
+ shift 2
+ ;;
+ -port)
EXTERNAL_PORT="$2"
shift 2
;;
@@ -39,16 +44,28 @@ if [[ ! -f "$ENV_VAR_FILE" ]]; then
exit 1
fi
-if [[ -d "$HI_DATA_DIR" ]]; then
- if [[ ! -w "$HI_DATA_DIR" ]]; then
- echo "Error: Directory '$HI_DATA_DIR' exists but is not writable."
+if [[ -d "$DATABASE_DATA_DIR" ]]; then
+ if [[ ! -w "$DATABASE_DATA_DIR" ]]; then
+ echo "Error: Directory '$DATABASE_DATA_DIR' exists but is not writable."
+ exit 1
+ fi
+else
+ echo "Creating directory '$DATABASE_DATA_DIR'..."
+ mkdir -p "$DATABASE_DATA_DIR"
+ sudo chown -R "$USER:$USER" "$DATABASE_DATA_DIR"
+ chmod -R 775 "$DATABASE_DATA_DIR"
+fi
+
+if [[ -d "$MEDIA_DATA_DIR" ]]; then
+ if [[ ! -w "$MEDIA_DATA_DIR" ]]; then
+ echo "Error: Directory '$MEDIA_DATA_DIR' exists but is not writable."
exit 1
fi
else
- echo "Creating directory '$HI_DATA_DIR'..."
- mkdir -p "$HI_DATA_DIR"
- sudo chown -R "$USER:$USER" "$HI_DATA_DIR"
- chmod -R 775 "$HI_DATA_DIR"
+ echo "Creating directory '$MEDIA_DATA_DIR'..."
+ mkdir -p "$MEDIA_DATA_DIR"
+ sudo chown -R "$USER:$USER" "$MEDIA_DATA_DIR"
+ chmod -R 775 "$MEDIA_DATA_DIR"
fi
if ! docker image inspect hi > /dev/null 2>&1; then
@@ -59,5 +76,6 @@ fi
docker run $BACKGROUND_FLAGS \
--name hi \
--env-file "$ENV_VAR_FILE" \
- -v "$HI_DATA_DIR:/data" \
+ -v "$DATABASE_DATA_DIR:/data/database" \
+ -v "$MEDIA_DATA_DIR:/data/media" \
-p "$EXTERNAL_PORT:8000" hi
diff --git a/src/hi/settings/local.py b/src/hi/settings/local.py
new file mode 100644
index 00000000..af3ad4a5
--- /dev/null
+++ b/src/hi/settings/local.py
@@ -0,0 +1,43 @@
+# -*- coding: utf-8 -*-
+from .base import *
+
+# SECURITY WARNING: don't run with debug turned on in production!
+DEBUG = False
+
+STATICFILES_STORAGE = 'pipeline.storage.PipelineManifestStorage'
+STATIC_ROOT = '/src/static'
+
+LOGGING = {
+ 'version': 1,
+ 'disable_existing_loggers': False,
+ 'formatters': {
+ 'verbose': {
+ 'format': '{levelname} {module} {process:d} {message}',
+ 'style': '{',
+ },
+ 'simple': {
+ 'format': '{levelname} {message}',
+ 'style': '{',
+ },
+ },
+ 'handlers': {
+ 'console': {
+ 'class': 'logging.StreamHandler',
+ 'formatter': 'verbose'
+ },
+ },
+ 'loggers': {
+ 'django': {
+ 'handlers': ['console'],
+ 'level': 'ERROR',
+ },
+ 'django.db.backends': {
+ 'handlers': ['console'],
+ 'level': 'ERROR',
+ },
+ 'hi': {
+ 'handlers': ['console' ],
+ 'level': 'INFO',
+ },
+ },
+}