Blame view

docs/rails.md 3.72 KB
3a47d364c   Jianwei Han   Remove caches for...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  # Using Boxen with Rails Projects
  
  Boxen is designed to work extremely well with Rails.
  Let's break down some of the basics:
  
  ## Project Management
  
  We make a few assumptions about how you run Rails in development for ease and consistency:
  
  * You use a web server that listens on a socket
  * You use environment variables and something like [dotenv](https://github.com/bkeepers/dotenv) for managing secrets
  
  A typical Rails app that uses things like MySQL, Resque, and phantomjs
  might have a [project definition](../modules/projects/README.md) like so:
  
  ``` puppet
  class projects::rails_app {
    include phantomjs
  
    boxen::project { 'rails_app':
      ruby   => '1.9.3',
      mysql  => true,
      redis  => true,
      nginx  => true,
      source => 'username/rails_app'
    }
  }
  ```
  
  This does a few things for you:
  
  * Clones `https://github.com/username/rails_app.git` to `~/src/rails_app`
  * Ensures the default 1.9.3 version of Ruby is installed
  * Creates `~/src/rails_app/.ruby-version` with `1.9.3` in it
  * Ensures mysql is installed and running
  * Creates two mysql databases: `rails_app_test` and `rails_app_development`
  * Ensures redis is installed and running
  * Ensures nginx is installed and running
  * Copies the [template nginx config](../modules/projects/templates/shared/nginx.conf.erb) into the nginx config dir
  
  It won't necessarily do all of them in that order, but it will guarantee
  that if they run successfully they're all done in a correct order.
  
  See the section below for some handy configuration tips on how to configure
  your app best to work with Boxen.
  
  ## Configuration
  
  ### MySQL
  
  ``` yaml
  # config/database.yml
  
  <%
    socket = [
      ENV["BOXEN_MYSQL_SOCKET"],
      "/var/run/mysql5/mysqld.sock",
      "/tmp/mysql.sock"
    ].compact.detect { |f| File.exist? f }
  
    port = ENV["BOXEN_MYSQL_PORT"] || "3306"
  %>
  
  development:
    adapter: mysql2
    database: rails_app_development
    username: root
    password:
  <% if socket %>
    host: localhost
    socket: <%= socket %>
  <% else %>
    host: 127.0.0.1
    port: <%= port %>
  <% end %>
  
  test:
    adapter: mysql2
    database: rails_app_test
    username: root
    password:
  <% if socket %>
    host: localhost
    socket: <%= socket %>
  <% else %>
    host: 127.0.0.1
    port: <%= port %>
  <% end %>
  ```
  
  ### PostgreSQL
  
  ``` yaml
  # config/database.yml
  
  development:
    adapter: postgresql
    database: rails_app_development
    encoding: unicode
    port: <%= ENV["BOXEN_POSTGRESQL_PORT"] || 5432 %>
    host: localhost
  
  test:
    adapter: postgresql
    database: rails_app_test
    encoding: unicode
    port: <%= ENV["BOXEN_POSTGRESQL_PORT"] || 5432 %>
    host: localhost
  ```
  
  ### Redis
  
  ``` ruby
  # config/initializers/redis.rb
  
  $redis = Redis.new(ENV['BOXEN_REDIS_URL'] || 'redis://localhost:6379/')
  ```
  
  ### Elasticsearch
  
  ``` ruby
  # config/initializers/elasticsearch.rb
  
  Tire.configure do
    url (ENV['BOXEN_ELASTICSEARCH_URL'] || 'http://localhost:9200/')
  end
  ```
  
  ### MongoDB
  
  ``` yaml
  # config/mongo.yml
  
  development:
    host: 127.0.0.1
    port: <%= ENV['BOXEN_MONGODB_PORT'] || 27017 %>
    database: rails_app_development
  
  test:
    host: 127.0.0.1
    port: <%= ENV['BOXEN_MONGODB_PORT'] || 27017 %>
    database: rails_app_test
  ```
  
  ### Memcached
  
  ``` ruby
  # config/initializers/memcached.rb
  
  $memcached = Dalli::Client.new(
    ENV['BOXEN_MEMCACHED_URL'] || 'memcached://localhost:11211/'
  )
  ```
  
  ### Unicorn
  
  ``` ruby
  # config/unicorn.rb
  
  if ENV['RACK_ENV'] == 'development'
    worker_processes 1
    listen "#{ENV['BOXEN_SOCKET_DIR']}/rails_app", :backlog => 1024
    timeout 120
  end
  
  after_fork do |server, worker|
    ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
  end
  ```
  
  ``` shell
  # script/server
  
  #!/bin/sh
  
  set -e
  
  cd $(dirname "$0")/..
  : ${RAILS_ENV:=development}
  : ${RACK_ENV:=development}
  
  export RAILS_ENV RACK_ENV
  
  bin/unicorn_rails -E "$RAILS_ENV" -c config/unicorn.rb
  ```