From bc25880c0dc82a6eb34209e8b1692aa2b7eaffec Mon Sep 17 00:00:00 2001 From: pvincent Date: Wed, 26 Jun 2024 00:10:46 +0400 Subject: [PATCH] production step1 --- app/extra/toto.rb | 16 ------------- bin/prod | 38 ++++++++++++++++++++++++++++++ config/application.rb | 10 ++++++-- config/database.yml | 9 +++++-- config/environments/development.rb | 19 +++++---------- config/environments/production.rb | 15 ++++++------ 6 files changed, 66 insertions(+), 41 deletions(-) delete mode 100644 app/extra/toto.rb create mode 100755 bin/prod diff --git a/app/extra/toto.rb b/app/extra/toto.rb deleted file mode 100644 index 2b1b968..0000000 --- a/app/extra/toto.rb +++ /dev/null @@ -1,16 +0,0 @@ -module Extra - # Toto est fluo - class Toto - def ok; end - - # this is a barman - def bar(ahaha, bzazaza, crezre) - puts "#{ahaha}#{bzazaza}#{crezre}" - end - - def candel - bar('a', 'aezeaz') - Toto - end - end -end diff --git a/bin/prod b/bin/prod new file mode 100755 index 0000000..b987ed2 --- /dev/null +++ b/bin/prod @@ -0,0 +1,38 @@ +#!/usr/bin/bash +set -Eeuo pipefail +SECRET_KEY_BASE=TOTO_EST_FLUO + +## FUNCTIONS + +function check_sudo_user { + if [[ $(id -u) -eq 0 ]] || groups | grep -q sudo; then + sudo --validate --prompt='sudo privilege required for further processing: ' + else + >&2 echo 'current user not in the `sudo` group, aborted!' + exit 10 + fi +} + +function fetch_latest_version { + sudo rm -rf /opt/egr + sudo cp -r /home/pvincent/easy-going-rails /opt/egr + sudo rm -rf /opt/egr/tmp/* + sudo chown -R pvincent:pvincent /opt/egr +} + +function bundle_and_populate { + cd /opt/egr + bundle install + DATABASE_HOST=ct1.lxd RAILS_ENV=production SECRET_KEY_BASE=$SECRET_KEY_BASE rails db:migrate + DATABASE_HOST=ct1.lxd RAILS_ENV=production SECRET_KEY_BASE=$SECRET_KEY_BASE rails assets:precompile +} + +function systemd_restart { + DATABASE_HOST=ct1.lxd RAILS_ENV=production SECRET_KEY_BASE=$SECRET_KEY_BASE rails server --port 7000 +} + +## MAIN +check_sudo_user +fetch_latest_version +bundle_and_populate +systemd_restart \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index b17647a..a5cde27 100644 --- a/config/application.rb +++ b/config/application.rb @@ -15,7 +15,7 @@ module EasyGoingRails # Please, add to the `ignore` list any other `lib` subdirectories that do # not contain `.rb` files, or that should not be reloaded or eager loaded. # Common ones are `templates`, `generators`, or `middleware`, for example. - config.autoload_lib(ignore: %w[assets tasks]) + config.autoload_lib(ignore: %w[assets tasks formatters hot_constants monkey_patches]) # main application title defined from current module name, see module above config.application_title = module_parent.to_s.titleize @@ -27,6 +27,12 @@ module EasyGoingRails # # config.time_zone = "Central Time (US & Canada)" # config.eager_load_paths << Rails.root.join("extras") - # + + # Customized Semantic Logger + config.rails_semantic_logger.semantic = false + config.rails_semantic_logger.started = true + config.rails_semantic_logger.processing = true + config.rails_semantic_logger.rendered = true + config.rails_semantic_logger.add_file_appender = false end end diff --git a/config/database.yml b/config/database.yml index 9e34cc7..2a010a0 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,5 +1,4 @@ --- - default: &default host: <%= ENV.fetch("DATABASE_HOST") { 'localhost' } %> database: &db_name <%= ENV.fetch("DATABASE_NAME") { 'easy-going-rails' } %> @@ -12,6 +11,12 @@ default: &default development: <<: *default +production: + <<: *default + database: &db_prod easy-going-rails_prod + user: *db_prod + password: *db_prod + test: <<: *default - database: easy-going-rails_test \ No newline at end of file + database: easy-going-rails_test diff --git a/config/environments/development.rb b/config/environments/development.rb index 79b2932..7e93d2a 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -74,19 +74,6 @@ Rails.application.configure do # rubocop:disable Metrics/BlockLength # Raise error when a before_action's only/except options reference missing actions config.action_controller.raise_on_missing_callback_actions = true - # Customized Semantic Logger - config.rails_semantic_logger.semantic = false - config.rails_semantic_logger.started = true - config.rails_semantic_logger.processing = true - config.rails_semantic_logger.rendered = true - config.rails_semantic_logger.add_file_appender = false - - require Rails.root.join('lib', 'formatters', 'ansi_formatter') - formatter = AnsiFormatter.new - config.semantic_logger.add_appender(io: $stdout, - formatter:, - filter: ->(log) { !formatter.reject(log) }) - routes.default_url_options[:port] = ARGV[1] # ie: Procfile.dev --port PORT routes.default_url_options[:host] = '127.0.0.1' @@ -94,4 +81,10 @@ Rails.application.configure do # rubocop:disable Metrics/BlockLength require Rails.root.join('lib', 'hot_constants', 'live_constants') LiveConstants.reload! end + + require Rails.root.join('lib', 'formatters', 'ansi_formatter') + formatter = AnsiFormatter.new + config.semantic_logger.add_appender(io: $stdout, + formatter:, + filter: ->(log) { !formatter.reject(log) }) end diff --git a/config/environments/production.rb b/config/environments/production.rb index 46c2bde..0bec7aa 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -1,4 +1,4 @@ -require "active_support/core_ext/integer/time" +require 'active_support/core_ext/integer/time' Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. @@ -49,20 +49,19 @@ Rails.application.configure do # config.assume_ssl = true # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. - config.force_ssl = true + config.force_ssl = false - # Log to STDOUT by default - config.logger = ActiveSupport::Logger.new(STDOUT) - .tap { |logger| logger.formatter = ::Logger::Formatter.new } - .then { |logger| ActiveSupport::TaggedLogging.new(logger) } + # Semantic Logger in syslog only! + require Rails.root.join('lib', 'formatters', 'basic_formatter') + config.semantic_logger.add_appender(appender: :syslog, formatter: BasicFormatter.new) # Prepend all log lines with the following tags. - config.log_tags = [ :request_id ] + # config.log_tags = [:request_id] # Info include generic and useful information about system operation, but avoids logging too much # information to avoid inadvertent exposure of personally identifiable information (PII). If you # want to log everything, set the level to "debug". - config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info") + config.log_level = ENV.fetch('RAILS_LOG_LEVEL', 'info') # Use a different cache store in production. # config.cache_store = :mem_cache_store