PrunOps
Covers all Deployment and maintainance Operations in a Ruby on Rails Application server:
- CONFIGURATION: Capistrano tasks to configure servers.
- DEPLOYMENT: Capistrano tasks to deploy your rails Apps.
- DIAGNOSIS: Capistrano diagnosis tools to guet your Apps status on real time.
- RELEASE: Rake tasks to manage and tag version number in your Apps (X.Y.Z).
- BACKUP: Backup policy for database and files in your Apps, using git as storage.
Installation
Add this line to your application's Gemfile:
gem 'prun-ops'And then execute:
$ bundle
Or install it yourself as:
$ gem install prun-ops
Usage: Day-to-day rake and capistrano tasks
Configure Capistrano
gem "capistrano-rails" is included as prun-ops requirement. Create basic files cap install
Capfile should include these requirements:
require 'capistrano/setup'
require 'capistrano/deploy'
require "capistrano/rvm"
require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require "capistrano/puma"
install_plugin Capistrano::Puma # Default puma tasks
install_plugin Capistrano::Puma::Systemd
require "#{File.dirname(__FILE__)}/config/application"
require 'capistrano/prun_ops'
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }In order to configure RVM and PUMA you have here the params:
set :rvm_ruby_version, File.read(".ruby-version").strip
set :rvm_custom_path, "/usr/share/rvm"
#PUMA:
# Run 'cap production puma:systemd:config' to setup teh linux service
# set :puma_threads, [4, 16]
# set :puma_workers, 0
set :puma_service_unit_type, "notify"
set :puma_access_log, "#{release_path}/log/puma.access.log"
set :puma_error_log, "#{release_path}/log/puma.error.log"
set :puma_bind, "tcp://0.0.0.0:3000"
# set :puma_conf, "#{shared_path}/puma.rb"
# set :puma_state, "#{shared_path}/tmp/pids/puma.state"
# set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
# set :puma_preload_app, trueNotice that you are adding all prun-ops tasks with the line require 'capistrano/prun_ops'
Your config/deploy/production.rb:
server "example.com", user: 'root', roles: %w{web app db} # , port: 2222
Note: Remember change this line in production.rb file: config.assets.compile = true
If you are accessing your server through a bastion you can define it:
bastion 'bastion.example.com', user: 'user'
Configuration
Main task is cap [stg] config
Secondary tasks:
-
cap [stg] ubuntu:prepareinitial ubuntu dependencies -
cap [stg] ruby:brightbox(DEPRECATED) install ruby on .ruby-version version from brightbox -
cap [stg] ruby:rvminstall ruby version manager -
cap [stg] ruby:install_rvm_project_versioninstalls ruby version in .ruby-version file of the project -
cap [stg] rails:prepare(DEPRECATED) install rails dependencies -
cap [stg] postgres:installinstall and configure postgres -
cap [stg] postgres:[start|stop|restart]start/stop postgres -
cap [stg] nginx:installinstall and configure nginx -
cap [stg] nginx:[start|stop|restart]start/stop nginx -
cap [stg] nodejs:installinstall node -
cap [stg] redis:installinstall redis server -
cap [stg] puma:installinstall puma server -
cap [stg] app:db_prepare(DEPRECATED) database first load -
cap [stg] nginx:certcreate SSL certificates with Let's Encrypt and confiture nginx -
cap [stg] nginx:ssl(DEPRECATED) configure nginx with SSL certificates
Considerations:
Deployment
Main task is cap [stg] deploy
Secondary tasks:
-
cap [stg] deploy:upload_linked_filesuploads configuration files defined as linked_files -
cap [stg] deploydeploy your app as usual -
cap [stg] deploy:restartrestart thin server of this application -
cap [stg] deploy:stopstop thin server -
cap [stg] deploy:startstart thin server -
cap [stg] deploy:db_createcreate database -
cap [stg] deploy:db_resetload schema and seeds for first DB setup -
cap [stg] deploy:db_seedseeds the database -
cap [stg] git:remove_repoRemoves repo (useful when repo_url changes)
Added the possibility of deploying from local repository. Add to deploy.rb or [stg].rb files:
before :deploy, "git:deploy_from_local_repo"
Take care to remove the previous repo if you are changing the :repo_url : cap [stg] git:remove_repo
Trouble shooting
-
In Rails 7 you have to create a new credentials/staging.key and file for each environment. This is just deleting credentials and runing again EDITOR="vim" rails credentials:edit --staging
-
If capistrano dos no find any command is the order of .basrc file, here is well explained
Backup
Backups/restore database and files in your Rails app.
Configure your 'config/applciation.rb':
# Backup directories
config.backup_dirs = %w{public/ckeditor_assets public/system}
# Backup repo
config.backup_repo = "git@github.com:example/backup.git"-
cap [stg] pull:data: downloads DDBB and file folders from the stage you need. -
cap [stg] pull:db: Hot backup, download and restore of the stage database -
cap [stg] pull:files: Hot restore of backup_files -
cap [stg] backup[TAG]: Commit a backup of DDBB and files to the git repo configured. "application-YYYYMMDD" tagged if no tag is provided. -
cap [stg] backup:restore[TAG]: Restore the last backup into the stage indicated, or tagged state if TAG is provided. -
rake backup |TAG|: Uploads backup to git store from local, tagging with date, or with TAG if provided. Useful to backup production stage. -
rake backup:restore |TAG|: Restore last backup copy, or tagged with TAG if provided.
TODO: Release
Release management
-
rake release |VERSION|push forward from dev-branch to master-branch and tag the commit with VERSION name. -
rake release:delete |VERSION|remove tag with VERSION name. -
rake git:ffmerge dev branch towards master branch without releasing (Deprecating, new version "rake tomaster[message]")
Diagnosis
Some capistrano commands useful to connect to server and help with the problem solving.
-
cap [stg] sshopen a ssh connection with server -
cap [stg] log_tail[LOG_FILENAME]tail all rails logs by default, or only one if LOG_FILENAME is provided -
cap [stg] log_patter[PATTERN]search a pattern in all logs -
cap [stg] copen a rails console with server -
cap [stg] dbconsoleopen a rails database console with server -
cap [stg] x[COMMAND]execute any command in server provided as COMMAND (i.e.: cap production x['free -m']) -
cap [stg] rake[TASK]execute any rake task in server provided as TASK (i.e.: cap production rake[db:version])
Monitoring
At this moment we are implementing NewRelic monitoring, including as dependency 'newrelic_rpm' gem. To configure yourproject you just need to create an account at NewRelic and follow the instructions (creating a 'config/newrelic.yml file with your license_key).
TODO: Configuration management
configure.yml: for variables with environment dependency
cap [stg] configure
cap [stg] configure:base: for packages, git, time ...
cap [stg] configure:ruby: ruby version
cap [stg] configure:pgsql
cap [stg] configure:nginx
cap [stg] configure:node
cap [stg] configure:newrelic
cap [stg] configure:jenkins
cap [stg] configure:wordpress
Contributing
- Fork it ( https://github.com/[my-github-username]/prun-ops/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
License
MIT License. Made by Lebrijo.com