|
| 1 | +# Capistrano 2 PostgreSQL |
| 2 | + |
| 3 | +**Note: this plugin works only with Capistrano 2.** Plase check the capistrano |
| 4 | +gem version you're using before installing this gem: |
| 5 | +`$ bundle show | grep capistrano` |
| 6 | + |
| 7 | +Plugin for Capistrano 3 coming soon. |
| 8 | + |
| 9 | +### About |
| 10 | + |
| 11 | +Capistrano 2 PostgreSQL plugin abstracts and speeds up common administration |
| 12 | +tasks for PostgreSQL when deploying rails applications. |
| 13 | + |
| 14 | +Here are the specific things this plugin does for your rails app capistrano |
| 15 | +deployment process: |
| 16 | + |
| 17 | +* creates a new PostgreSQL database and database user on the server |
| 18 | +* generates and populates `database.yml` file with the right data on the server |
| 19 | + (no need to ssh to the server and do this manually!) |
| 20 | +* no config necessary (or it's kept to a minimum) |
| 21 | + |
| 22 | +### Installation |
| 23 | + |
| 24 | +Put the following in your application's `Gemfile`: |
| 25 | + |
| 26 | + group :development do |
| 27 | + gem 'capistrano2-postgresql', require: false |
| 28 | + end |
| 29 | + |
| 30 | +Install the gem with: |
| 31 | + |
| 32 | + $ bundle install |
| 33 | + |
| 34 | +### Standard usage |
| 35 | + |
| 36 | +If you're deploying a standard rails app, all that you need to do is put |
| 37 | +the following in `config/deploy.rb` file: |
| 38 | + |
| 39 | + require "capistrano-postgresql" |
| 40 | + |
| 41 | +Easy, right? |
| 42 | + |
| 43 | +Check below to see what happens in the background. |
| 44 | + |
| 45 | +### How it works |
| 46 | + |
| 47 | +The following tasks run automatically after `cap deploy:setup`: |
| 48 | + |
| 49 | +* `postgresql:create_database` creates a postgresql user and a database for |
| 50 | + your app. Password for the user is automatically generated and used in the |
| 51 | + next step. |
| 52 | + |
| 53 | +* `postgresql:setup` it creates a `database.yml` file and copies it to |
| 54 | + `#{shared_path}/config/database.yml` on the server. |
| 55 | + |
| 56 | +The following task runs automatically after `cap deploy:cold` and each |
| 57 | +`cap deploy`: |
| 58 | + |
| 59 | +* `postgresql:symlink` ensures the `database.yml` from the previous step is |
| 60 | + always symlinked to `config/database.yml` for new app release. |
| 61 | + |
| 62 | +The above tasks are all you need for getting rails app to work with PostgreSQL. |
| 63 | + |
| 64 | +### Gotchas |
| 65 | + |
| 66 | +Be sure to remove `database.yml` from your application's version control. |
| 67 | + |
| 68 | +### Configuration |
| 69 | + |
| 70 | +This plugin should just work with no configuration whatsoever. However, |
| 71 | +customization is possible. Here's the list of options and the defaults for each |
| 72 | +option: |
| 73 | + |
| 74 | +* `set :postgresql_user`<br/> |
| 75 | +Name of the database user. Defaults to `"#{capistrano_user}_#{application}"` |
| 76 | +which is most likely something like `deploy_myapp`. |
| 77 | + |
| 78 | +* `set :postgresql_database`<br/> |
| 79 | +Name of the database for your app. Defaults to `#{application}`. |
| 80 | + |
| 81 | +* `set :postgresql_password`<br/> |
| 82 | +Password for the database user. By default this option is not set and |
| 83 | +**new random password** is generated each time you create a new database. |
| 84 | +If you set this option to `"some_secure_password"` - that will be the db |
| 85 | +password. Keep in mind that having a hardcoded password in `deploy.rb` (or |
| 86 | +anywhere in version control) is a bad practice. |
| 87 | +I recommend sticking to the default and generating a new secure and random |
| 88 | +password each time a database is generated. That way you don't have to worry |
| 89 | +about it or try to remember it. |
| 90 | + |
| 91 | +* `set :postgresql_ask_for_password`, default `false`<br/> |
| 92 | +Set this option to `true` if you want to be prompted for the password when |
| 93 | +database user is created. This is safer than setting the password via |
| 94 | +`postgresql_password`, but the downside is you have to choose and remember |
| 95 | +yet another fricking password.<br/> |
| 96 | +`postgresql_password` option has precedence. If it is set, |
| 97 | +`postgresql_ask_for_password` is ignored. |
| 98 | + |
| 99 | +* `set :postgresql_default_tasks`<br/> |
| 100 | +This task determines whether capistrano tasks from this plugin are executed |
| 101 | +automatically during capistrano deploy process. Defaults to `true`. Tasks that |
| 102 | +are run automatically are: `postgresql:create_database`, `postgresql:setup` and |
| 103 | +`postgresql:symlink`. |
| 104 | + |
| 105 | +`database.yml` template-only settings: |
| 106 | + |
| 107 | +* `set :postgresql_pool`<br/> |
| 108 | +Pool config in `database.yml` template. Defaults to `5`. |
| 109 | + |
| 110 | +* `set :postgresql_host`<br/> |
| 111 | +`hostname` config in `database.yml` template. Defaults to `localhost`. |
| 112 | + |
| 113 | +* `set :postgresql_encoding`<br/> |
| 114 | +`encogind` config in `database.yml` template. Defaults to `unicode`. |
| 115 | + |
| 116 | +### Customizing the `database.yml` template |
| 117 | + |
| 118 | +This is the default `database.yml` template that gets copied to the capistrano |
| 119 | +shared directory on the server: |
| 120 | + |
| 121 | +```yml |
| 122 | +production: |
| 123 | + adapter: postgresql |
| 124 | + encoding: <%= postgresql_encoding %> |
| 125 | + database: <%= postgresql_database %> |
| 126 | + pool: <%= postgresql_pool %> |
| 127 | + username: <%= postgresql_user %> |
| 128 | + password: '<%= postgresql_password %>' |
| 129 | + host: <%= postgresql_host %> |
| 130 | +``` |
| 131 | +
|
| 132 | +If for any reason you want to edit or tweak this template, you can copy it to |
| 133 | +`config/deploy/templates/postgresql.yml.erb` with this command: |
| 134 | + |
| 135 | + rails g capistrano:postgresql:template |
| 136 | + |
| 137 | +After you edit this newly created file in your repo, it will be used as a |
| 138 | +template for `database.yml` on the server. |
| 139 | + |
| 140 | +You can configure the template location. For example: |
| 141 | +`set :templates_path, "config"` and the template will be copied to |
| 142 | +`config/postgresql.yml.erb`. |
| 143 | + |
| 144 | +### Contributing and bug reports |
| 145 | + |
| 146 | +Contributions and improvements are very welcome. Just open a pull request and |
| 147 | +I'll look it up shortly. |
| 148 | + |
| 149 | +If something is not working for you, or you find a bug please report it. |
| 150 | + |
| 151 | +### Thanks |
| 152 | + |
| 153 | +Here are other plugins and people this project was based upon: |
| 154 | + |
| 155 | +* [Matt Bridges](https://github.com/mattdbridges) - capistrano postgresql tasks |
| 156 | +from this plugin are heavily based on his |
| 157 | +[capistrano-recipes repo](https://github.com/mattdbridges/capistrano-recipes). |
| 158 | + |
| 159 | +* [Kalys Osmonom](https://github.com/kalys) - his |
| 160 | +[capistrano-nginx-unicorn](https://github.com/kalys/capistrano-nginx-unicorn) |
| 161 | +gem structure was an inspiration for this plugin. A lot of the features were |
| 162 | +directly copied from his project (example: `database.yml` template generator). |
| 163 | + |
| 164 | +### License |
| 165 | + |
| 166 | +[MIT](LICENSE.md) |
0 commit comments