~charmers/charms/precise/rails/trunk

9.1.1 by Pavel Pachkovskij
rack v2
1
# Overview
2
3
Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. This Charm will deploy Ruby on Rails, Sinatra or any other Rack application and connect it to supported services.
4
5
# Usage
6
7
To deploy this charm you will need at a minimum: a cloud environment, working Juju installation and a successful bootstrap. Once bootstrapped, deploy Rack charm and all required services.
8
9
## Ruby on Rails example
10
11
Configure your application, for example:
12
13
**sample-rails.yml**
14
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
15
    sample-rails:
16
      repo: https://github.com/pavelpachkovskij/sample-rails
9.1.1 by Pavel Pachkovskij
rack v2
17
18
Deploy Rack application:
19
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
20
    juju deploy rack sample-rails --config sample-rails.yml
9.1.1 by Pavel Pachkovskij
rack v2
21
22
Deploy and relate database
23
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
24
    juju deploy postgresql
25
    juju add-relation postgresql:db sample-rails
9.1.1 by Pavel Pachkovskij
rack v2
26
27
Now you can run migrations:
28
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
29
    juju ssh sample-rails/0 run rake db:migrate
9.1.1 by Pavel Pachkovskij
rack v2
30
31
Seed database
32
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
33
    juju ssh sample-rails/0 run rake db:seed
9.1.1 by Pavel Pachkovskij
rack v2
34
35
And finally expose the Rack service:
36
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
37
    juju expose rack
9.1.1 by Pavel Pachkovskij
rack v2
38
39
Find the Rack instance's public URL from
40
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
41
    juju status
9.1.1 by Pavel Pachkovskij
rack v2
42
43
### MySQL setup
44
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
45
    juju deploy mysql
46
    juju add-relation mysql rack
9.1.1 by Pavel Pachkovskij
rack v2
47
48
## Sinatra example
49
50
Configure your application, for example html2haml
51
52
**html2haml.yml**
53
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
54
    html2haml:
55
      repo: https://github.com/twilson63/html2haml.git
9.1.1 by Pavel Pachkovskij
rack v2
56
57
Deploy your Rack service
58
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
59
    juju deploy rack html2haml --config html2haml.yml
9.1.1 by Pavel Pachkovskij
rack v2
60
61
Expose Rack service:
62
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
63
    juju expose html2haml
9.1.1 by Pavel Pachkovskij
rack v2
64
65
## Source code updates
66
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
67
    juju set <service_name> revision=<revision>
9.1.1 by Pavel Pachkovskij
rack v2
68
69
## Executing commands
70
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
71
    juju ssh <unit_name> run <command>
9.1.1 by Pavel Pachkovskij
rack v2
72
73
## Restart application
74
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
75
    juju ssh <unit_name> sudo restart rack
9.1.1 by Pavel Pachkovskij
rack v2
76
77
## Foreman integration
78
79
You can add Procfile to your application and Rack to start additional processes or replace default application server:
80
81
Example Procfile:
82
83
    web: bundle exec unicorn -p $PORT
84
    watcher: bundle exec rake watch
85
86
## Specifying a Ruby Version
87
88
You can use the ruby keyword of your app's Gemfile to specify a particular version of Ruby.
89
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
90
    source "https://rubygems.org"
91
    ruby "1.9.3"
9.1.1 by Pavel Pachkovskij
rack v2
92
93
# Horizontal scaling
94
95
Juju makes it easy to scale your Rack application. You can simply deploy any supported load balancer, add relation and launch any number of application instances.
96
97
## HAProxy
98
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
99
    juju deploy rack rack --config rack.yml
100
    juju deploy haproxy
101
    juju add-relation haproxy rack
102
    juju expose haproxy
103
    juju add-unit rack -n 2
9.1.1 by Pavel Pachkovskij
rack v2
104
105
## Apache2
106
107
Apache2 is harder to start with, but it provides more flexibility with configuration options.
108
Here is a quick example of using Apache2 as a load balancer with your rack application:
109
110
Deploy Rack application
111
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
112
    juju deploy rack --config rack.yml
9.1.1 by Pavel Pachkovskij
rack v2
113
114
You have to enable mod_proxy_balancer and mod_proxy_http modules in your Apache2 config:
115
116
**apache2.yml** example
117
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
118
    apache2:
119
      enable_modules: proxy_balancer proxy_http
9.1.1 by Pavel Pachkovskij
rack v2
120
121
Deploy Apache2
122
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
123
    juju deploy apache2 --config apache2.yml
9.1.1 by Pavel Pachkovskij
rack v2
124
125
Create balancer relation between Apache2 and Rack application
126
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
127
    juju add-relation apache2:balancer rack
9.1.1 by Pavel Pachkovskij
rack v2
128
129
Apache2 charm expects a template to be passed in. Example of vhost that will balance all traffic over your application instances:
130
131
**vhost.tmpl**
132
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
133
    <VirtualHost *:80>
134
      ServerName rack
135
      ProxyPass / balancer://rack/ lbmethod=byrequests stickysession=BALANCEID
136
      ProxyPassReverse / balancer://rack/
137
    </VirtualHost>
9.1.1 by Pavel Pachkovskij
rack v2
138
139
Update Apache2 service config with this template
140
141
juju set apache2 "vhost_http_template=$(base64 < vhost.tmpl)"
142
143
Expose Apache2 service
144
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
145
    juju expose apache2
9.1.1 by Pavel Pachkovskij
rack v2
146
147
# Logging with Logstash
148
149
You can add logstash service to collect information from application's logs and Kibana application to visualize this data.
150
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
151
    juju deploy kibana
152
    juju deploy logstash-indexer
153
    juju add-relation kibana logstash-indexer:rest
9.1.1 by Pavel Pachkovskij
rack v2
154
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
155
    juju deploy logstash-agent
156
    juju add-relation logstash-agent logstash-indexer
157
    juju add-relation logstash-agent rack
158
    juju set logstash-agent CustomLogFile="['/var/www/rack/current/log/*.log']" CustomLogType="rack"
159
    juju expose kibana
9.1.1 by Pavel Pachkovskij
rack v2
160
161
# Monitoring with Nagios and NRPE
162
163
You can can perform HTTP checks with Nagios. To do this deploy Nagios and relate it to your Rack application:
164
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
165
    juju deploy nagios
166
    juju add-relation rack nagios
9.1.1 by Pavel Pachkovskij
rack v2
167
168
Additionally you can perform disk, mem, and swap checks with NRPE extension:
169
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
170
    juju deploy nrpe
171
    juju add-relation rack nrpe
172
    juju add-relation nrpe nagios
9.1.1 by Pavel Pachkovskij
rack v2
173
174
# MongoDB relation
175
176
Deploy MonogDB service and relate it to Rack application:
1 by Pavel Pachkovskij
release rack revision 1
177
178
    juju deploy mongodb
179
    juju add-relation mongodb rack
180
9.1.1 by Pavel Pachkovskij
rack v2
181
Rack charm will set environment variables which you can use to configure your Mongodb adapter.
182
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
183
    MONGODB_URL   => mongodb://host:port/database
9.1.1 by Pavel Pachkovskij
rack v2
184
185
## Mongoid 2.x
186
187
Your mongoid.yml should look like:
188
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
189
    production:
190
      uri: <%= ENV['MONGODB_URL'] %>
9.1.1 by Pavel Pachkovskij
rack v2
191
192
## Mongoid 3.x
193
194
Your mongoid.yml should look like:
195
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
196
    production:
197
      sessions:
198
        default:
199
          uri: <%= ENV['MONGODB_URL'] %>
9.1.1 by Pavel Pachkovskij
rack v2
200
201
In both cases you can set additional options specified by Mongoid.
202
203
# Memcached relation
204
205
Deploy Memcached service and relate it to Rack application:
206
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
207
    juju deploy memcached
208
    juju add-relation memcached rack
9.1.1 by Pavel Pachkovskij
rack v2
209
210
Rack charm will set environment variables which you can use to configure your Memcache adapter. [Dalli](https://github.com/mperham/dalli) use those variables by default.
211
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
212
    MEMCACHE_PASSWORD    => xxxxxxxxxxxx
213
    MEMCACHE_SERVERS     => instance.hostname.net
214
    MEMCACHE_USERNAME    => xxxxxxxxxxxx
9.1.1 by Pavel Pachkovskij
rack v2
215
216
# Redis relation
217
218
Deploy Redis service and relate it to Rack application:
219
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
220
    juju deploy redis-master
221
    juju add-relation redis-master:redis-master rack
9.1.1 by Pavel Pachkovskij
rack v2
222
223
Rack charm will set environment variables which you can use to configure your Redis adapter.
224
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
225
    REDIS_URL   => redis://username:password@my.host:6389
9.1.1 by Pavel Pachkovskij
rack v2
226
227
For example you can configure Redis adapter in config/initializers/redis.rb
228
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
229
    uri = URI.parse(ENV["REDIS_URL"])
230
    REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
9.1.1 by Pavel Pachkovskij
rack v2
231
232
# Known issues
233
234
## Rack application didn't start because assets were not compiled
235
236
To be able to compile assets before you've joined database relation you have to disable initialize_on_precompile option in application.rb:
237
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
238
    config.assets.initialize_on_precompile = false
9.1.1 by Pavel Pachkovskij
rack v2
239
240
If you can't do this you still can join database and compile assets manually:
241
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
242
    juju ssh rack/0 run rake assets:precompile
9.1.1 by Pavel Pachkovskij
rack v2
243
244
Then restart Rack service (while you have to replace 'rack/0' with your application name, e.g. 'sample-rails/0', 'sudo restart rack' is a valid command to restart any deployed application):
245
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
246
    juju ssh rack/0 sudo restart rack
9.1.1 by Pavel Pachkovskij
rack v2
247
248
# Configuration
249
250
## Deploy from Git
251
252
Sample Git config:
253
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
254
    rack:
255
      repo: <repository_url>
256
      revision: <revision_number>
9.1.1 by Pavel Pachkovskij
rack v2
257
258
To deploy from private repo via SSH add 'deploy_key' option:
259
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
260
    deploy_key: <private_key>
9.1.1 by Pavel Pachkovskij
rack v2
261
262
## Deploy from SVN
263
264
Sample SVN config:
265
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
266
    rack:
267
      scm_provider: svn
268
      repo: <repository_url>
269
      revision: <revision_number>
270
      svn_username: <username>
271
      svn_password: <password>
9.1.1 by Pavel Pachkovskij
rack v2
272
273
## Install extra packages
274
275
Specify list of packages separated by spaces:
276
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
277
    extra_packages: 'libsqlite3++-dev libmagick++-dev'
9.1.1 by Pavel Pachkovskij
rack v2
278
279
## Set ENV variables
280
281
You can set ENV variables, which will be available within all processes defined in a Procfile:
282
9.1.4 by Pavel Pachkovskij
fix code formatting in README.md
283
    env: 'AWS_ACCESS_KEY_ID=aws_access_key_id AWS_SECRET_ACCESS_KEY=aws_secret_access_key'