From 5ffc6974f04674a3027d20f7c3a6002ff4ebc959 Mon Sep 17 00:00:00 2001 From: magicfixin Date: Wed, 23 Jul 2025 23:42:41 +0200 Subject: [PATCH] Improve installation process --- .gitignore | 9 +++- Gemfile | 20 ++++++--- Gemfile.lock | 16 +++++-- README.md | 74 ++++++++++++++++++++++--------- bin/fixinme.service.dist | 16 +++++++ config/application.rb.dist | 2 +- config/database.yml.dist | 20 ++++----- config/environments/production.rb | 2 +- 8 files changed, 115 insertions(+), 44 deletions(-) create mode 100644 bin/fixinme.service.dist diff --git a/.gitignore b/.gitignore index 5c7cc02..eba4eea 100644 --- a/.gitignore +++ b/.gitignore @@ -3,10 +3,15 @@ /.cache /.gem -# Ignore master key for decrypting credentials and more. +# Ignore service startup scripts. +/bin/fixinme.service + +# Ignore: +# * master key for decrypting credentials and encrypted credentials, +# * custom app, database and server settings (based on *.dist templates). /config/application.rb +/config/credentials.yml.enc /config/database.yml -/config/initializers/secret_token.rb /config/master.key /config/puma.rb diff --git a/Gemfile b/Gemfile index 77000db..e743124 100644 --- a/Gemfile +++ b/Gemfile @@ -1,20 +1,28 @@ source "https://rubygems.org" -ruby '>= 3.3.0' +# The requirement for the Ruby version comes from Rails gem "rails", "~> 7.2.2" gem "sprockets-rails" -gem "mysql2", "~> 0.5" gem "puma", "~> 6.0" gem "sassc-rails" # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] +# TODO: select db gems automatically based on config/database.yml:adapter +group :mysql, optional: true do + gem "mysql2", "~> 0.5" +end +group :postgresql, optional: true do + gem "pg", "~> 1.5" +end +group :sqlite, optional: true do + gem "sqlite3", "~> 2.7" +end + gem "devise" -gem 'importmap-rails' -# turborails >= 2.0.0 required with npm v8.0.0 with support for [autofocus] -# attribute in turbo-streams -gem 'turbo-rails', '> 1.5.0' +gem "importmap-rails" +gem "turbo-rails", "~> 2.0" group :development, :test do gem "byebug" diff --git a/Gemfile.lock b/Gemfile.lock index 857357e..64b9d11 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -164,6 +164,7 @@ GEM nokogiri (1.18.8-x86_64-linux-musl) racc (~> 1.4) orm_adapter (0.5.0) + pg (1.5.9) pp (0.6.2) prettyprint prettyprint (0.2.0) @@ -245,6 +246,14 @@ GEM actionpack (>= 6.1) activesupport (>= 6.1) sprockets (>= 3.0.0) + sqlite3 (2.7.3-aarch64-linux-gnu) + sqlite3 (2.7.3-aarch64-linux-musl) + sqlite3 (2.7.3-arm-linux-gnu) + sqlite3 (2.7.3-arm-linux-musl) + sqlite3 (2.7.3-arm64-darwin) + sqlite3 (2.7.3-x86_64-darwin) + sqlite3 (2.7.3-x86_64-linux-gnu) + sqlite3 (2.7.3-x86_64-linux-musl) stringio (3.1.7) thor (1.3.2) tilt (2.6.0) @@ -287,17 +296,16 @@ DEPENDENCIES devise importmap-rails mysql2 (~> 0.5) + pg (~> 1.5) puma (~> 6.0) rails (~> 7.2.2) sassc-rails selenium-webdriver sprockets-rails - turbo-rails (> 1.5.0) + sqlite3 (~> 2.7) + turbo-rails (~> 2.0) tzinfo-data web-console -RUBY VERSION - ruby 3.3.7p123 - BUNDLED WITH 2.6.3 diff --git a/README.md b/README.md index d2420f5..efd0aca 100644 --- a/README.md +++ b/README.md @@ -4,47 +4,70 @@ README Quantified self -Software requirements ---------------------- +Installation +------------ + +The steps described in this section are for preparing a production installation. +For possible modifications to this procedure to configure the development +environment, see the _Contributing_ section below. + +### Requirements * Server side: - * Ruby version: developed on Ruby 3.x - * database with: - * recursive Common Table Expressions (CTE) support, e.g. - MySQL >= 8.0, MariaDB >= 10.2.2 - * decimal type with precision of at least 30 (not sure if SQLite3 - supports this) + * Ruby interpreter, depending on the version of Rails used (see _Gemfile_), + * https://guides.rubyonrails.org/upgrading_ruby_on_rails.html#ruby-versions + * database (e.g. MySQL >= 8.0) supporting: + * recursive Common Table Expressions (CTE) for SELECT/UPDATE/DELETE, + * MariaDB does not support CTE for UPDATE/DELETE + (https://jira.mariadb.org/browse/MDEV-18511) + * decimal datatype with precision of at least 30, + * SQLite3 _flexible typing_ decimal will work, but precision + will be limited to 16, making it practical mostly for testing + purposes * for testing: browser as specified in _Client side_ requirements * Client side: - * browser supporting below requirements (e.g. Firefox >= 121): + * browser (e.g. Firefox >= 121) supporting: * [`import maps`](https://caniuse.com/import-maps) (required by `importmap-rails` gem >= 2.0) * CSS [`:has()` pseudo-class](https://caniuse.com/css-has) +### Gems -Installation ------------- +On systems where development tools and libraries are not installed by default +(such as Ubuntu), you should install them before proceeding with Ruby gems. +Select the database client library according to the database engine you are +planning to use: + + sudo apt install build-essential libyaml-dev libmysqlclient-dev git clone https://gitea.michalczyk.pro/fixin.me/fixin.me.git - bundle config set --local path '.gem' + cd fixin.me + bundle config --local frozen true + bundle config --local path .gem + +Select which database engine gem to install (mysql, postgresql, sqlite): + + bundle config --local with mysql bundle install +### Configuration -Configuration -------------- +Customize application settings (starting below `SETUP` comment) appropriately: cp -a config/application.rb.dist config/application.rb -Modify configuration settings below `SETUP` comment appropriately. +Create `secret_key_base`. It will be automatically generated on first +`credentials:edit`, so it's enough to run command below, save the file and exit +editor: + bundle exec rails credentials:edit -Database --------- +### Database Grant database user and privileges: > mysql -p - mysql> create user fixinme@localhost identified by ''; + mysql> create user fixinme@localhost identified by 'Some-password1%'; mysql> grant all privileges on fixinme.* to fixinme@localhost; mysql> flush privileges; @@ -62,7 +85,7 @@ Running ### Standalone Rails server + Apache proxy -Copy Puma config template: +Customize Puma config template: cp -a config/puma.rb.dist config/puma.rb @@ -70,10 +93,21 @@ and specify server IP/port, either with `port` or `bind`, e.g.: bind 'tcp://0.0.0.0:3000' -Run server +#### (option 1) Start server manually bundle exec rails s -e production +#### (option 2) Start server as systemd service + +Customize service template, setting at least `User` and `WorkingDirectory`: + + sudo cp bin/fixinme.service.dist /etc/systemd/system/fixinme.service + + sudo systemctl daemon-reload + sudo systemctl enable fixin.service + sudo systemctl start fixin.service + + sudo systemctl status fixin.service ### Apache mod_passenger diff --git a/bin/fixinme.service.dist b/bin/fixinme.service.dist new file mode 100644 index 0000000..84fdfb5 --- /dev/null +++ b/bin/fixinme.service.dist @@ -0,0 +1,16 @@ +[Unit] +Description=fixin.me Rails Application +After=network.target + +[Service] +Type=simple +User=USER +WorkingDirectory=PATH_TO_APP_DIRECTORY +Environment="RAILS_ENV=production" +Environment="RAILS_SERVE_STATIC_FILES=true" +ExecStart=/bin/bash -lc 'bundle exec rails s -e production' +Restart=always +RestartSec=5 + +[Install] +WantedBy=multi-user.target diff --git a/config/application.rb.dist b/config/application.rb.dist index d28d914..f1be478 100644 --- a/config/application.rb.dist +++ b/config/application.rb.dist @@ -47,7 +47,7 @@ module FixinMe # List of hosts this app is available at. # https://guides.rubyonrails.org/configuring.html#actiondispatch-hostauthorization - config.hosts << 'localhost' + config.hosts += ['localhost', 'example.com', IPAddr.new('1.2.3.4/32')] # Email address of admin account config.admin = 'admin@localhost' diff --git a/config/database.yml.dist b/config/database.yml.dist index 206381a..5d079fe 100644 --- a/config/database.yml.dist +++ b/config/database.yml.dist @@ -2,7 +2,7 @@ # in your source code, provide the password or a full connection URL as an # environment variable when you boot the app. For example: # -# DATABASE_PASSWORD="some-password" +# DATABASE_PASSWORD="Some-password1%" # # or # @@ -29,22 +29,22 @@ default: &default collation: utf8mb4_0900_as_ci pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: fixinme - password: + password: Some-password1% socket: /run/mysqld/mysqld.sock production: <<: *default - database: fixinme_production + database: fixinme -# Unless you're planning on developing the application, you can skip/remove +# Unless you're planning on developing the application, you can skip # configurations for development and test databases altogether. -development: - <<: *default - database: fixinme_dev +#development: +# <<: *default +# database: fixinme_dev # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. -test: - <<: *default - database: fixinme_test +#test: +# <<: *default +# database: fixinme_test diff --git a/config/environments/production.rb b/config/environments/production.rb index 7516c7e..12d3542 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -28,7 +28,7 @@ Rails.application.configure do # config.assets.css_compressor = :sass # Do not fallback to assets pipeline if a precompiled asset is missed. - config.assets.compile = false + config.assets.compile = true # Enable serving of images, stylesheets, and JavaScripts from an asset server. # config.asset_host = "http://assets.example.com"