mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-10 01:01:39 -05:00
Implement production environment
This commit is contained in:
parent
1476816418
commit
ba40b7d284
11 changed files with 115 additions and 89 deletions
2
.github/workflows/build_and_push.yml
vendored
2
.github/workflows/build_and_push.yml
vendored
|
|
@ -40,7 +40,7 @@ jobs:
|
||||||
uses: docker/build-push-action@v2
|
uses: docker/build-push-action@v2
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
file: ./Dockerfile
|
file: ./docker/Dockerfile
|
||||||
push: true
|
push: true
|
||||||
tags: freikin/dawarich:latest,freikin/dawarich:${{ github.event.inputs.branch || github.ref_name }}
|
tags: freikin/dawarich:latest,freikin/dawarich:${{ github.event.inputs.branch || github.ref_name }}
|
||||||
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6
|
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6
|
||||||
|
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
echo "Environment: $RAILS_ENV"
|
|
||||||
|
|
||||||
# set env var defaults
|
|
||||||
DATABASE_HOST=${DATABASE_HOST:-"dawarich_db"}
|
|
||||||
DATABASE_PORT=${DATABASE_PORT:-5432}
|
|
||||||
DATABASE_USER=${DATABASE_USER:-"postgres"}
|
|
||||||
DATABASE_PASSWORD=${DATABASE_PASSWORD:-"password"}
|
|
||||||
DATABASE_NAME=${DATABASE_NAME:-"dawarich_development"}
|
|
||||||
|
|
||||||
# Wait for the database to be ready
|
|
||||||
until nc -zv $DATABASE_HOST ${DATABASE_PORT:-5432}; do
|
|
||||||
echo "Waiting for PostgreSQL to be ready..."
|
|
||||||
sleep 1
|
|
||||||
done
|
|
||||||
|
|
||||||
# run passed commands
|
|
||||||
bundle exec ${@}
|
|
||||||
|
|
@ -32,17 +32,17 @@ RUN gem install bundler --version "$BUNDLE_VERSION" \
|
||||||
# Navigate to app directory
|
# Navigate to app directory
|
||||||
WORKDIR $APP_PATH
|
WORKDIR $APP_PATH
|
||||||
|
|
||||||
COPY Gemfile Gemfile.lock vendor .ruby-version ./
|
COPY ../Gemfile ../Gemfile.lock ../vendor ../.ruby-version ./
|
||||||
|
|
||||||
# Install missing gems
|
# Install missing gems
|
||||||
RUN bundle config set --local path 'vendor/bundle' \
|
RUN bundle config set --local path 'vendor/bundle' \
|
||||||
&& bundle install --jobs 20 --retry 5
|
&& bundle install --jobs 20 --retry 5
|
||||||
|
|
||||||
COPY . ./
|
COPY ../. ./
|
||||||
|
|
||||||
# Copy entrypoint scripts and grant execution permissions
|
# Copy entrypoint scripts and grant execution permissions
|
||||||
COPY ./dev-docker-entrypoint.sh /usr/local/bin/dev-entrypoint.sh
|
COPY ./docker/prod-docker-entrypoint.sh /usr/local/bin/entrypoint.sh
|
||||||
RUN chmod +x /usr/local/bin/dev-entrypoint.sh
|
RUN chmod +x /usr/local/bin/entrypoint.sh
|
||||||
|
|
||||||
EXPOSE $RAILS_PORT
|
EXPOSE $RAILS_PORT
|
||||||
|
|
||||||
|
|
@ -24,8 +24,8 @@ until nc -zv $DATABASE_HOST ${DATABASE_PORT:-5432}; do
|
||||||
done
|
done
|
||||||
|
|
||||||
# Install gems
|
# Install gems
|
||||||
gem update --system 3.5.7
|
gem update --system 3.6.2
|
||||||
gem install bundler --version '2.5.9'
|
gem install bundler --version '2.5.21'
|
||||||
|
|
||||||
# Create the database
|
# Create the database
|
||||||
if [ "$(psql "postgres://$DATABASE_USERNAME:$DATABASE_PASSWORD@$DATABASE_HOST:$DATABASE_PORT" -XtAc "SELECT 1 FROM pg_database WHERE datname='$DATABASE_NAME'")" = '1' ]; then
|
if [ "$(psql "postgres://$DATABASE_USERNAME:$DATABASE_PASSWORD@$DATABASE_HOST:$DATABASE_PORT" -XtAc "SELECT 1 FROM pg_database WHERE datname='$DATABASE_NAME'")" = '1' ]; then
|
||||||
|
|
@ -37,7 +37,7 @@ fi
|
||||||
|
|
||||||
# Run database migrations
|
# Run database migrations
|
||||||
echo "PostgreSQL is ready. Running database migrations..."
|
echo "PostgreSQL is ready. Running database migrations..."
|
||||||
bundle exec rails db:prepare
|
bundle exec rails db:migrate
|
||||||
|
|
||||||
# Run data migrations
|
# Run data migrations
|
||||||
echo "Running DATA migrations..."
|
echo "Running DATA migrations..."
|
||||||
|
|
@ -2,7 +2,7 @@ networks:
|
||||||
dawarich:
|
dawarich:
|
||||||
services:
|
services:
|
||||||
dawarich_redis:
|
dawarich_redis:
|
||||||
image: redis:7.0-alpine
|
image: redis:7.4-alpine
|
||||||
container_name: dawarich_redis
|
container_name: dawarich_redis
|
||||||
command: redis-server
|
command: redis-server
|
||||||
networks:
|
networks:
|
||||||
|
|
@ -17,7 +17,7 @@ services:
|
||||||
start_period: 30s
|
start_period: 30s
|
||||||
timeout: 10s
|
timeout: 10s
|
||||||
dawarich_db:
|
dawarich_db:
|
||||||
image: postgres:14.2-alpine
|
image: postgres:17-alpine
|
||||||
container_name: dawarich_db
|
container_name: dawarich_db
|
||||||
volumes:
|
volumes:
|
||||||
- db_data:/var/lib/postgresql/data
|
- db_data:/var/lib/postgresql/data
|
||||||
|
|
@ -27,15 +27,16 @@ services:
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: postgres
|
POSTGRES_USER: postgres
|
||||||
POSTGRES_PASSWORD: password
|
POSTGRES_PASSWORD: password
|
||||||
|
POSTGRES_DB: dawarich_production
|
||||||
restart: always
|
restart: always
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: [ "CMD-SHELL", "pg_isready -U postgres -d dawarich_development" ]
|
test: [ "CMD", "pg_isready", "-U", "postgres" ]
|
||||||
interval: 10s
|
interval: 10s
|
||||||
retries: 5
|
retries: 5
|
||||||
start_period: 30s
|
start_period: 30s
|
||||||
timeout: 10s
|
timeout: 10s
|
||||||
dawarich_app:
|
dawarich_app:
|
||||||
image: freikin/dawarich:latest
|
image: dawarich:prod
|
||||||
container_name: dawarich_app
|
container_name: dawarich_app
|
||||||
volumes:
|
volumes:
|
||||||
- gem_cache:/usr/local/bundle/gems_app
|
- gem_cache:/usr/local/bundle/gems_app
|
||||||
|
|
@ -48,16 +49,17 @@ services:
|
||||||
# - 9394:9394 # Prometheus exporter, uncomment if needed
|
# - 9394:9394 # Prometheus exporter, uncomment if needed
|
||||||
stdin_open: true
|
stdin_open: true
|
||||||
tty: true
|
tty: true
|
||||||
entrypoint: dev-entrypoint.sh
|
entrypoint: entrypoint.sh
|
||||||
command: ['bin/dev']
|
command: ['bin/dev']
|
||||||
restart: on-failure
|
restart: on-failure
|
||||||
environment:
|
environment:
|
||||||
RAILS_ENV: production
|
RAILS_ENV: production
|
||||||
REDIS_URL: redis://dawarich_redis:6379/0
|
REDIS_URL: redis://dawarich_redis:6379/0
|
||||||
DATABASE_HOST: dawarich_db
|
DATABASE_HOST: dawarich_db
|
||||||
|
DATABASE_PORT: 5432
|
||||||
DATABASE_USERNAME: postgres
|
DATABASE_USERNAME: postgres
|
||||||
DATABASE_PASSWORD: password
|
DATABASE_PASSWORD: password
|
||||||
DATABASE_NAME: dawarich_development
|
DATABASE_NAME: dawarich_production
|
||||||
MIN_MINUTES_SPENT_IN_CITY: 60
|
MIN_MINUTES_SPENT_IN_CITY: 60
|
||||||
APPLICATION_HOST: localhost
|
APPLICATION_HOST: localhost
|
||||||
APPLICATION_HOSTS: localhost
|
APPLICATION_HOSTS: localhost
|
||||||
|
|
@ -69,6 +71,8 @@ services:
|
||||||
PROMETHEUS_EXPORTER_ENABLED: false
|
PROMETHEUS_EXPORTER_ENABLED: false
|
||||||
PROMETHEUS_EXPORTER_HOST: 0.0.0.0
|
PROMETHEUS_EXPORTER_HOST: 0.0.0.0
|
||||||
PROMETHEUS_EXPORTER_PORT: 9394
|
PROMETHEUS_EXPORTER_PORT: 9394
|
||||||
|
SECRET_KEY_BASE: 1234567890
|
||||||
|
RAILS_LOG_TO_STDOUT: "true"
|
||||||
logging:
|
logging:
|
||||||
driver: "json-file"
|
driver: "json-file"
|
||||||
options:
|
options:
|
||||||
|
|
@ -93,7 +97,7 @@ services:
|
||||||
cpus: '0.50' # Limit CPU usage to 50% of one core
|
cpus: '0.50' # Limit CPU usage to 50% of one core
|
||||||
memory: '2G' # Limit memory usage to 2GB
|
memory: '2G' # Limit memory usage to 2GB
|
||||||
dawarich_sidekiq:
|
dawarich_sidekiq:
|
||||||
image: freikin/dawarich:latest
|
image: dawarich:prod
|
||||||
container_name: dawarich_sidekiq
|
container_name: dawarich_sidekiq
|
||||||
volumes:
|
volumes:
|
||||||
- gem_cache:/usr/local/bundle/gems_sidekiq
|
- gem_cache:/usr/local/bundle/gems_sidekiq
|
||||||
|
|
@ -103,16 +107,17 @@ services:
|
||||||
- dawarich
|
- dawarich
|
||||||
stdin_open: true
|
stdin_open: true
|
||||||
tty: true
|
tty: true
|
||||||
entrypoint: dev-entrypoint.sh
|
entrypoint: entrypoint.sh
|
||||||
command: ['sidekiq']
|
command: ['sidekiq']
|
||||||
restart: on-failure
|
restart: on-failure
|
||||||
environment:
|
environment:
|
||||||
RAILS_ENV: production
|
RAILS_ENV: production
|
||||||
REDIS_URL: redis://dawarich_redis:6379/0
|
REDIS_URL: redis://dawarich_redis:6379/0
|
||||||
DATABASE_HOST: dawarich_db
|
DATABASE_HOST: dawarich_db
|
||||||
|
DATABASE_PORT: 5432
|
||||||
DATABASE_USERNAME: postgres
|
DATABASE_USERNAME: postgres
|
||||||
DATABASE_PASSWORD: password
|
DATABASE_PASSWORD: password
|
||||||
DATABASE_NAME: dawarich_development
|
DATABASE_NAME: dawarich_production
|
||||||
APPLICATION_HOST: localhost
|
APPLICATION_HOST: localhost
|
||||||
APPLICATION_HOSTS: localhost
|
APPLICATION_HOSTS: localhost
|
||||||
BACKGROUND_PROCESSING_CONCURRENCY: 10
|
BACKGROUND_PROCESSING_CONCURRENCY: 10
|
||||||
93
docker/prod-docker-entrypoint.sh
Normal file
93
docker/prod-docker-entrypoint.sh
Normal file
|
|
@ -0,0 +1,93 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
unset BUNDLE_PATH
|
||||||
|
unset BUNDLE_BIN
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "Environment: $RAILS_ENV"
|
||||||
|
|
||||||
|
# Parse DATABASE_URL if present, otherwise use individual variables
|
||||||
|
if [ -n "$DATABASE_URL" ]; then
|
||||||
|
# Extract components from DATABASE_URL
|
||||||
|
DATABASE_HOST=$(echo $DATABASE_URL | awk -F[@/] '{print $4}')
|
||||||
|
DATABASE_PORT=$(echo $DATABASE_URL | awk -F[@/:] '{print $5}')
|
||||||
|
DATABASE_USERNAME=$(echo $DATABASE_URL | awk -F[:/@] '{print $4}')
|
||||||
|
DATABASE_PASSWORD=$(echo $DATABASE_URL | awk -F[:/@] '{print $5}')
|
||||||
|
DATABASE_NAME=$(echo $DATABASE_URL | awk -F[@/] '{print $5}')
|
||||||
|
else
|
||||||
|
# Use existing environment variables
|
||||||
|
DATABASE_HOST=${DATABASE_HOST:-dawarich_db}
|
||||||
|
DATABASE_PORT=${DATABASE_PORT:-5432}
|
||||||
|
DATABASE_USERNAME=${DATABASE_USERNAME:-postgres}
|
||||||
|
DATABASE_PASSWORD=${DATABASE_PASSWORD:-password}
|
||||||
|
DATABASE_NAME=${DATABASE_NAME:-dawarich_production}
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Function to test database connection
|
||||||
|
test_db_connection() {
|
||||||
|
echo "Testing connection to PostgreSQL..."
|
||||||
|
echo "Host: $DATABASE_HOST"
|
||||||
|
echo "Port: $DATABASE_PORT"
|
||||||
|
echo "Username: $DATABASE_USERNAME"
|
||||||
|
echo "Database: postgres (default database)"
|
||||||
|
|
||||||
|
if PGPASSWORD=$DATABASE_PASSWORD psql -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USERNAME" -d postgres -c "SELECT 1" > /dev/null 2>&1; then
|
||||||
|
echo "✅ Successfully connected to PostgreSQL!"
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
echo "❌ Failed to connect to PostgreSQL"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Try to connect to PostgreSQL, with a timeout
|
||||||
|
max_attempts=30
|
||||||
|
attempt=1
|
||||||
|
|
||||||
|
while ! test_db_connection; do
|
||||||
|
if [ $attempt -ge $max_attempts ]; then
|
||||||
|
echo "Failed to connect to PostgreSQL after $max_attempts attempts. Exiting."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Attempt $attempt of $max_attempts. Waiting 2 seconds before retry..."
|
||||||
|
attempt=$((attempt + 1))
|
||||||
|
sleep 2
|
||||||
|
done
|
||||||
|
|
||||||
|
# Remove pre-existing puma/passenger server.pid
|
||||||
|
rm -f $APP_PATH/tmp/pids/server.pid
|
||||||
|
|
||||||
|
# Install gems
|
||||||
|
gem update --system 3.6.2
|
||||||
|
gem install bundler --version '2.5.21'
|
||||||
|
|
||||||
|
# Create the database if it doesn't exist
|
||||||
|
if PGPASSWORD=$DATABASE_PASSWORD psql -h "$DATABASE_HOST" -p "$DATABASE_PORT" -U "$DATABASE_USERNAME" -c "SELECT 1 FROM pg_database WHERE datname='$DATABASE_NAME'" | grep -q 1; then
|
||||||
|
echo "Database $DATABASE_NAME already exists, skipping creation..."
|
||||||
|
else
|
||||||
|
echo "Creating database $DATABASE_NAME..."
|
||||||
|
bundle exec rails db:create
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Run database migrations
|
||||||
|
echo "PostgreSQL is ready. Running database migrations..."
|
||||||
|
bundle exec rails db:migrate
|
||||||
|
|
||||||
|
# Run data migrations
|
||||||
|
echo "Running DATA migrations..."
|
||||||
|
bundle exec rake data:migrate
|
||||||
|
|
||||||
|
# Run seeds
|
||||||
|
echo "Running seeds..."
|
||||||
|
bundle exec rake db:seed
|
||||||
|
|
||||||
|
# Precompile assets
|
||||||
|
if [ "$RAILS_ENV" = "production" ]; then
|
||||||
|
echo "Precompiling assets..."
|
||||||
|
bundle exec rake assets:precompile
|
||||||
|
fi
|
||||||
|
|
||||||
|
# run passed commands
|
||||||
|
bundle exec ${@}
|
||||||
|
|
@ -1,51 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
|
|
||||||
unset BUNDLE_PATH
|
|
||||||
unset BUNDLE_BIN
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
echo "Environment: $RAILS_ENV"
|
|
||||||
|
|
||||||
# set env var defaults
|
|
||||||
DATABASE_HOST=${DATABASE_HOST:-"dawarich_db"}
|
|
||||||
DATABASE_PORT=${DATABASE_PORT:-5432}
|
|
||||||
DATABASE_USERNAME=${DATABASE_USERNAME:-"postgres"}
|
|
||||||
DATABASE_PASSWORD=${DATABASE_PASSWORD:-"password"}
|
|
||||||
DATABASE_NAME=${DATABASE_NAME:-"dawarich_production"}
|
|
||||||
|
|
||||||
# Remove pre-existing puma/passenger server.pid
|
|
||||||
rm -f $APP_PATH/tmp/pids/server.pid
|
|
||||||
|
|
||||||
# Wait for the database to be ready
|
|
||||||
until nc -zv $DATABASE_HOST ${DATABASE_PORT:-5432}; do
|
|
||||||
echo "Waiting for PostgreSQL to be ready..."
|
|
||||||
sleep 1
|
|
||||||
done
|
|
||||||
|
|
||||||
# Install gems
|
|
||||||
gem update --system 3.5.23
|
|
||||||
gem install bundler --version '2.5.21'
|
|
||||||
|
|
||||||
# Create the database
|
|
||||||
if [ "$(psql "postgres://$DATABASE_USERNAME:$DATABASE_PASSWORD@$DATABASE_HOST:$DATABASE_PORT" -XtAc "SELECT 1 FROM pg_database WHERE datname='$DATABASE_NAME'")" = '1' ]; then
|
|
||||||
echo "Database $DATABASE_NAME already exists, skipping creation..."
|
|
||||||
else
|
|
||||||
echo "Creating database $DATABASE_NAME..."
|
|
||||||
bundle exec rails db:create
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Run database migrations
|
|
||||||
echo "PostgreSQL is ready. Running database migrations..."
|
|
||||||
bundle exec rails db:prepare
|
|
||||||
|
|
||||||
# Run data migrations
|
|
||||||
echo "Running DATA migrations..."
|
|
||||||
bundle exec rake data:migrate
|
|
||||||
|
|
||||||
# Run seeds
|
|
||||||
echo "Running seeds..."
|
|
||||||
bundle exec rake db:seed
|
|
||||||
|
|
||||||
# run passed commands
|
|
||||||
bundle exec ${@}
|
|
||||||
Loading…
Reference in a new issue