Skip to content

Commit

Permalink
Merge pull request #292 from code0-tech/272-configuration-settings
Browse files Browse the repository at this point in the history
Replace env configuration with file configuration
  • Loading branch information
Taucher2003 authored Sep 7, 2024
2 parents 7f4e85c + 03ade06 commit 6301f71
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 31 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,10 @@
*.njsproj
*.sln
*.sw?

################
##
## Configuration files
##
################
config/sagittarius.yml
13 changes: 3 additions & 10 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ include:
before_script:
- source ~/.asdf/asdf.sh
- bundle config build.pg --with-pg-config=$(asdf where postgres)/bin/pg_config
- cp tooling/pipeline/sagittarius.yml config/sagittarius.yml
- !reference [.bundle, before_script]
services:
- postgres:16.1
variables:
POSTGRES_DB: sagittarius_test
POSTGRES_USER: sagittarius
POSTGRES_PASSWORD: sagittarius
SAGITTARIUS_DATABASE_HOST: postgres
SAGITTARIUS_DATABASE_PORT: '5432'
RAILS_ENV: test

rubocop:
Expand Down Expand Up @@ -138,10 +137,7 @@ boot:production-mode:
POSTGRES_DB: sagittarius_production
POSTGRES_USER: sagittarius
POSTGRES_PASSWORD: sagittarius
SAGITTARIUS_DATABASE_HOST: postgres
SAGITTARIUS_DATABASE_PORT: '5432'
RAILS_ENV: production
RAILS_FORCE_SSL: 'false'
BUNDLE_WITHOUT: 'development:test'
script:
- '[[ "$EDITION" = "ee" ]] || rm -rf ee/'
Expand Down Expand Up @@ -169,10 +165,7 @@ boot:docker:
--name sagittarius
--network sagittarius_default
--network-alias sagittarius
-e SAGITTARIUS_DATABASE_HOST=postgresql
-e SAGITTARIUS_DATABASE_PORT=5432
-e SAGITTARIUS_REDIS_HOST=redis
-e SAGITTARIUS_REDIS_PORT=6379
-v $(pwd)/tooling/pipeline/sagittarius.yml:/rails/config/sagittarius.yml
-e SAGITTARIUS_DISABLE_ORCHESTRATOR=true
ghcr.io/code0-tech/sagittarius/ci-builds:${CI_COMMIT_SHA}-${EDITION} &
- >
Expand All @@ -188,7 +181,7 @@ boot:docker:
--retry-connrefused
http://sagittarius:3000/health/liveness
boot:docker-orchestrated:
.boot:docker-orchestrated:
stage: test
image: docker:27.1.1-dind
parallel:
Expand Down
4 changes: 3 additions & 1 deletion app/models/application_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ def self.current
records = ApplicationSetting.all
assert_settings_present!(records)

overrides = Sagittarius::Configuration.application_setting_overrides

records.each_with_object(ApplicationSettings.new) do |record, acc|
acc[record.setting] = record.value
acc[record.setting] = overrides.fetch(record.setting.to_sym, record.value)
end
end
end
18 changes: 9 additions & 9 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
require 'rails/all'
require_relative '../lib/sagittarius/utils'
require_relative '../lib/sagittarius/extensions'
require_relative '../lib/sagittarius/memoize'
require_relative '../lib/sagittarius/configuration'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Expand Down Expand Up @@ -68,16 +70,14 @@ class Application < Rails::Application
# Configure active job to use good_job
config.active_job.queue_adapter = :good_job

# Generated with 'bin/rails db:encryption:init'
# Use some random generated keys, production will override this with the environment variables
config.active_record.encryption.primary_key = ENV.fetch('SAGITTARIUS_DATABASE_ENCRYPTION_PRIMARY_KEY',
'YzaMv4bXYK84unYIQI4Ms4sV3ucbvWs0')
config.active_record.encryption.deterministic_key = ENV.fetch('SAGITTARIUS_DATABASE_ENCRYPTION_DETERMINISTIC_KEY',
'jgTaxTqzM15ved1S8HdXrqrjfCfF5R0h')
config.active_record.encryption.key_derivation_salt =
ENV.fetch('SAGITTARIUS_DATABASE_ENCRYPTION_KEY_DERIVATION_SALT', 'Z6zcLTgobXLYjXUslRsLMKxvXKq3j6DJ')
configuration = Sagittarius::Configuration.config
encryption_config = configuration[:rails][:db][:encryption]

config.secret_key_base = ENV.fetch('SAGITTARIUS_SECRET_KEY_BASE', 'MVMD6CtQwEWrQ28TdokQakbG2FG5abOn')
config.active_record.encryption.primary_key = encryption_config[:primary_key]
config.active_record.encryption.deterministic_key = encryption_config[:deterministic_key]
config.active_record.encryption.key_derivation_salt = encryption_config[:key_derivation_salt]

config.secret_key_base = configuration[:rails][:secret_key_base]

# Configuration for the application, engines, and railties goes here.
#
Expand Down
13 changes: 8 additions & 5 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@
# Configure Using Gemfile
# gem "pg"
#

<% db_config = Sagittarius::Configuration.config[:rails][:db] %>

default: &default
adapter: postgresql
encoding: unicode
username: <%= ENV.fetch("SAGITTARIUS_DATABASE_USERNAME") { "sagittarius" } %>
password: <%= ENV.fetch("SAGITTARIUS_DATABASE_PASSWORD") { "sagittarius" } %>
host: <%= ENV.fetch("SAGITTARIUS_DATABASE_HOST") { "localhost" } %>
port: <%= ENV.fetch("SAGITTARIUS_DATABASE_PORT") { 5433 } %>
username: <%= db_config[:username] %>
password: <%= db_config[:password] %>
host: <%= db_config[:host] %>
port: <%= db_config[:port] %>
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
pool: <%= Sagittarius::Configuration.config[:rails][:threads] %>

development:
<<: *default
Expand Down
3 changes: 2 additions & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
# config.assume_ssl = true

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
config.force_ssl = Sagittarius::Utils.to_boolean(ENV.fetch('RAILS_FORCE_SSL', nil), default: true)
config.force_ssl = Sagittarius::Utils.to_boolean(Sagittarius::Configuration.config[:rails][:web][:force_ssl],
default: true)

# Log to STDOUT by default
config.logger = ActiveSupport::Logger.new($stdout)
Expand Down
7 changes: 3 additions & 4 deletions config/puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
max_threads_count = ENV.fetch('RAILS_MAX_THREADS', 5)
min_threads_count = ENV.fetch('RAILS_MIN_THREADS') { max_threads_count }
threads min_threads_count, max_threads_count
thread_count = Sagittarius::Configuration.config[:rails][:threads]
threads thread_count, thread_count

# Specifies that the worker count should equal the number of processors in production.
if ENV['RAILS_ENV'] == 'production'
Expand All @@ -25,7 +24,7 @@
worker_timeout 3600 if ENV.fetch('RAILS_ENV', 'development') == 'development'

# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
port ENV.fetch('PORT', 3000)
port Sagittarius::Configuration.config[:rails][:web][:port]

# Specifies the `environment` that Puma will run in.
environment ENV.fetch('RAILS_ENV', 'development')
Expand Down
17 changes: 17 additions & 0 deletions config/sagittarius.example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
rails:
threads: 5
web:
port: 3000
force_ssl:
db:
host: localhost
port: 5433
username: sagittarius
password: sagittarius
encryption:
primary_key: YzaMv4bXYK84unYIQI4Ms4sV3ucbvWs0
deterministic_key: jgTaxTqzM15ved1S8HdXrqrjfCfF5R0h
key_derivation_salt: Z6zcLTgobXLYjXUslRsLMKxvXKq3j6DJ
secret_key_base: MVMD6CtQwEWrQ28TdokQakbG2FG5abOn

application_setting_overrides: {}
2 changes: 1 addition & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3.9'

services:
postgresql:
postgres:
image: postgres:16.1
environment:
POSTGRES_USER: "sagittarius"
Expand Down
45 changes: 45 additions & 0 deletions lib/sagittarius/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

module Sagittarius
class Configuration
extend Sagittarius::Memoize

def self.config
memoize(:config) do
file_config = YAML.safe_load_file(Rails.root.join('config/sagittarius.yml')).deep_symbolize_keys
defaults.deep_merge(file_config)
rescue Errno::ENOENT # config file does not exist
defaults
end
end

def self.application_setting_overrides
config[:application_setting_overrides]
end

def self.defaults
{
rails: {
threads: 5,
web: {
port: 3000,
force_ssl: nil,
},
db: {
host: 'localhost',
port: 5433,
username: 'sagittarius',
password: 'sagittarius',
encryption: {
primary_key: 'YzaMv4bXYK84unYIQI4Ms4sV3ucbvWs0',
deterministic_key: 'jgTaxTqzM15ved1S8HdXrqrjfCfF5R0h',
key_derivation_salt: 'Z6zcLTgobXLYjXUslRsLMKxvXKq3j6DJ',
},
},
secret_key_base: 'MVMD6CtQwEWrQ28TdokQakbG2FG5abOn',
},
application_setting_overrides: {},
}
end
end
end
33 changes: 33 additions & 0 deletions spec/lib/sagittarius/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Sagittarius::Configuration do
describe '.config' do
let(:yaml_config) do
<<~CONFIG
rails:
threads: 4
CONFIG
end

before do
allow(File).to receive(:open).and_call_original
allow(File).to receive(:open)
.with(Rails.root.join('config/sagittarius.yml'), instance_of(String))
.and_yield(yaml_config)
described_class.clear_memoize(:config)
end

it 'loads the config' do
expect(described_class.config).to include(rails: a_hash_including(threads: 4))
end
end

describe '.defaults' do
it 'matches the example config' do
example_config = YAML.safe_load_file(Rails.root.join('config/sagittarius.example.yml')).deep_symbolize_keys
expect(example_config).to eq(described_class.defaults)
end
end
end
6 changes: 6 additions & 0 deletions tooling/pipeline/sagittarius.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rails:
db:
host: postgres
port: 5432
web:
force_ssl: false

0 comments on commit 6301f71

Please sign in to comment.