|
| 1 | +## capistrano_recipes |
| 2 | + |
| 3 | +Bunch of capistrano recipes |
| 4 | + |
| 5 | +GitHub: https://github.com/rubydev/capistrano_recipes |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +Add `capistrano_recipes` to your application's Gemfile: |
| 10 | + |
| 11 | + gem 'capistrano_recipes', :git => 'git://github.com/rubydev/capistrano_recipes.git' |
| 12 | + |
| 13 | +And then install the bundle: |
| 14 | + |
| 15 | + $ bundle |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +First, initialize capistrano: |
| 20 | + |
| 21 | + capify . |
| 22 | + |
| 23 | +This will create `./Capfile ` and `./config/deploy.rb`. Edit `./config/deploy.rb` and load recipes required |
| 24 | +for your application: |
| 25 | + |
| 26 | + require "capistrano_recipes/nginx" |
| 27 | + require "capistrano_recipes/mysql" |
| 28 | + require "capistrano_recipes/host" |
| 29 | + |
| 30 | +Then run `cap host:setup deploy:initial` to make your first deploy. Use `cap deploy:migrations` for next deploys. |
| 31 | + |
| 32 | +### Example of rails app deployment |
| 33 | + |
| 34 | +`config/deploy.rb`: |
| 35 | + |
| 36 | + default_run_options[:pty] = true # Must be set for the password prompt to work |
| 37 | + set :use_sudo, false # Don't use sudo (deploy user must have very limited permissions in system) |
| 38 | + |
| 39 | + set :default_stage, "staging" # Deploy on staging server by default |
| 40 | + |
| 41 | + # Configure capistrano deploy strategy |
| 42 | + set :repository, '.' |
| 43 | + set :deploy_via, :copy |
| 44 | + set :copy_exclude, [".git", "coverage", "results", "tmp", "public/system", "builder"] |
| 45 | + |
| 46 | + set :root_user, "user" # User with root privileges on server. You will need him only for host:setup |
| 47 | + # and *:setup_host tasks. This will not be used during deploy, |
| 48 | + # deploy:migrations and other common tasks. |
| 49 | + set :user, "app_name" # Deployer user |
| 50 | + set :group, "app_name" # Deployer group |
| 51 | + set :user_home_path, "/home/www" # Deployer home on target system (used in host:setup when creating new user) |
| 52 | + set(:deploy_to) { "#{user_home_path}/#{application}" } # Path where to deploy your application |
| 53 | + |
| 54 | + role(:web) { domain } # Your HTTP server, Apache/etc |
| 55 | + role(:app) { domain } # This may be the same as your `Web` server |
| 56 | + role(:db, :primary => true) { domain } # This is where Rails migrations will run |
| 57 | + |
| 58 | + require "capistrano_colors" # Colorized output in console (gem install capistrano_colors) |
| 59 | + require "capistrano/ext/multistage" # Enable multistage deployment |
| 60 | + require "capistrano_recipes/nginx" # Use nginx as web-server |
| 61 | + require "capistrano_recipes/mysql" # Use mysql as db-server |
| 62 | + require "capistrano_recipes/host" # Require host:setup task |
| 63 | + require "capistrano_recipes/monit" # Use monit as monitoring system |
| 64 | + require "capistrano_recipes/ruby/thin" # Use thing as app-server |
| 65 | + require "capistrano_recipes/ruby/rails" # Load rails-specific recipes |
| 66 | + require "bundler/capistrano" # Use bundler on server |
| 67 | + load "deploy/assets" # Use rails assets pipeline |
| 68 | + |
| 69 | +`config/deploy/production.rb` |
| 70 | + |
| 71 | + set :application, "app_name" # Application name for production |
| 72 | + # (it's used in some configs and paths) |
| 73 | + set :application_domain, "app_domain.com" # Domain for production server |
| 74 | + set :domain, "192.168.1.1" # Server address where to deploy production |
| 75 | + |
| 76 | +`config/deploy/staging.rb` |
| 77 | + |
| 78 | + set :application, "app_name_test" # Application name for staging server |
| 79 | + set :application_domain, "qa.app_domain.com" # Domain for staging server |
| 80 | + set :domain, "192.168.1.1" # Server address where to deploy staging |
| 81 | + |
| 82 | +Then execute `cap host:setup deploy:initial nginx:enable` to create user and config files and perform initial deploy, |
| 83 | +then symlink `/etc/nginx/sites-available/app_name.conf` to `/etc/nginx/sites-enabled/app_name.conf`. |
| 84 | +On next deploy just run `cap deploy:migrations` to deploy new application version. |
| 85 | + |
| 86 | +`cap host:setup` will perform: |
| 87 | + |
| 88 | + * `host:create_user` - creates `:user` on target system (if doesn't exist yet) |
| 89 | + * `host:ssh_copy_id` - adds `~/.ssh/id_rsa.pub` from local machine to `authorized_keys` on target machine for `:user` |
| 90 | + * Then it performs `*:setup_host` for all loaded recipes: |
| 91 | + * `nginx:setup_host` - creates nginx config file in `/etc/nginx/sites-available/app_name.conf`, grants user |
| 92 | + permissions to modify it. Creates `#{deploy_to}/shared/logs/nginx` directory and grants nginx permissions to |
| 93 | + write there. |
| 94 | + * `mysql:setup_host` - creates mysql user with random password grants him administrative permissions for application |
| 95 | + database (`:database_name`). Then creates `:database_config_template` in `#{deploy_to}/shared/config/database.yml`, |
| 96 | + grants it 440 permissions (only user and group can read it). It will be symlinked to |
| 97 | + `#{deploy_to}/current/config/database.yml` on each deploy. |
| 98 | + * `monit:setup_host` - creates monit config file for thin in `/etc/monit/conf.d/app_name-thin`, grants user |
| 99 | + permissions to modify it. |
| 100 | + * `thin:setup_host` - creates directory for thin sockets (if `:thin_socket` is set), creates thin config file in |
| 101 | + `#{deploy_to}/shared/config/thin.yml` |
| 102 | + |
| 103 | +## Config templates |
| 104 | + |
| 105 | +Capistrano-scrip uses ERB to parse nginx/monit/thin/etc templates. You can see default config templates at github: |
| 106 | +https://github.com/rubydev/capistrano_recipes/tree/master/templates |
| 107 | + |
| 108 | +If you don't like any of this, you can replace it with you own: |
| 109 | + |
| 110 | +* Put your template in `config/deploy/templates/#{template_name}` |
| 111 | +* Or change `*_template` variable value to reflect your template path: `set :monit_config_template, 'deploy/monit.erb'` |
| 112 | + |
| 113 | +## Contributing |
| 114 | + |
| 115 | +1. Fork |
| 116 | +2. Create your feature branch (`git checkout -b my_branch`) |
| 117 | +3. Commit your changes (`git commit -am "my cool feature"`) |
| 118 | +4. Push to the branch (`git push origin my_branch`) |
| 119 | +5. Create new Pull Request |
| 120 | + |
| 121 | +### Editing documentation |
| 122 | + |
| 123 | +1. Install dependencies: `bundle install` |
| 124 | +2. Run yard server: `bundle exec yard server -r` |
| 125 | +3. Edit documentation |
| 126 | +4. Preview your changes at http://localhost:8808/ |
| 127 | +5. Commit: `git commit -am "Updated documentation for ..." |
0 commit comments