~charmers/charms/precise/rails/trunk

« back to all changes in this revision

Viewing changes to README.md

  • Committer: Mark Mims
  • Date: 2013-08-29 17:47:43 UTC
  • mfrom: (9.1.5 rack)
  • Revision ID: mark.mims@canonical.com-20130829174743-ltmqllm9nkbfzqpa
merging ~pavel-pachkovskij/charms/precise/rack/trunk as per https://code.launchpad.net/~pavel-pachkovskij/charms/precise/rack/trunk/+merge/179721

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
## Overview
2
 
 
3
 
Rack applications subordinate [Juju Charm](http://jujucharms.com/). Works only when related to appropriate web-server.
4
 
 
5
 
## Usage
6
 
 
7
 
You can use any of available web servers, which provides rack container. Currently available:
8
 
 
9
 
- [apache2-passenger](http://jujucharms.com/charms/precise/apache2-passenger)
10
 
- [nginx-passenger](http://jujucharms.com/charms/precise/nginx-passenger)
11
 
 
12
 
All examples will use **apache2-passenger**, but you can choose any.
13
 
 
14
 
### Rails 3 example
15
 
 
16
 
1. Deploy web-server
17
 
 
18
 
        juju deploy apache2-passenger
19
 
 
20
 
2. Deploy Rack application (see configuration section below)
21
 
 
22
 
        juju deploy rack --config myapp.yml
23
 
 
24
 
3. Relate them
25
 
 
26
 
        juju add-relation rack apache2-passenger
27
 
 
28
 
4. Deploy and relate database
29
 
 
30
 
        juju deploy mysql
31
 
        juju add-relation mysql rack
32
 
 
33
 
5. Open the stack up to the outside world.
34
 
 
35
 
        juju expose apache2-passenger
36
 
 
37
 
6. Find the apache2-passenger instance's public URL from
38
 
 
39
 
        juju status
40
 
 
41
 
#### PostgreSQL setup
42
 
 
43
 
On a step 4 run
 
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
 
 
15
    sample-rails:
 
16
      repo: https://github.com/pavelpachkovskij/sample-rails
 
17
 
 
18
Deploy Rack application:
 
19
 
 
20
    juju deploy rack sample-rails --config sample-rails.yml
 
21
 
 
22
Deploy and relate database
44
23
 
45
24
    juju deploy postgresql
46
 
    juju add-relation postgresql:db rack
47
 
 
48
 
#### Mongodb setup
49
 
 
50
 
If you use Mongodb with Mongoid then on a step 4 you should run
 
25
    juju add-relation postgresql:db sample-rails
 
26
 
 
27
Now you can run migrations:
 
28
 
 
29
    juju ssh sample-rails/0 run rake db:migrate
 
30
 
 
31
Seed database
 
32
 
 
33
    juju ssh sample-rails/0 run rake db:seed
 
34
 
 
35
And finally expose the Rack service:
 
36
 
 
37
    juju expose rack
 
38
 
 
39
Find the Rack instance's public URL from
 
40
 
 
41
    juju status
 
42
 
 
43
### MySQL setup
 
44
 
 
45
    juju deploy mysql
 
46
    juju add-relation mysql rack
 
47
 
 
48
## Sinatra example
 
49
 
 
50
Configure your application, for example html2haml
 
51
 
 
52
**html2haml.yml**
 
53
 
 
54
    html2haml:
 
55
      repo: https://github.com/twilson63/html2haml.git
 
56
 
 
57
Deploy your Rack service
 
58
 
 
59
    juju deploy rack html2haml --config html2haml.yml
 
60
 
 
61
Expose Rack service:
 
62
 
 
63
    juju expose html2haml
 
64
 
 
65
## Source code updates
 
66
 
 
67
    juju set <service_name> revision=<revision>
 
68
 
 
69
## Executing commands
 
70
 
 
71
    juju ssh <unit_name> run <command>
 
72
 
 
73
## Restart application
 
74
 
 
75
    juju ssh <unit_name> sudo restart rack
 
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
 
 
90
    source "https://rubygems.org"
 
91
    ruby "1.9.3"
 
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
 
 
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
 
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
 
 
112
    juju deploy rack --config rack.yml
 
113
 
 
114
You have to enable mod_proxy_balancer and mod_proxy_http modules in your Apache2 config:
 
115
 
 
116
**apache2.yml** example
 
117
 
 
118
    apache2:
 
119
      enable_modules: proxy_balancer proxy_http
 
120
 
 
121
Deploy Apache2
 
122
 
 
123
    juju deploy apache2 --config apache2.yml
 
124
 
 
125
Create balancer relation between Apache2 and Rack application
 
126
 
 
127
    juju add-relation apache2:balancer rack
 
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
 
 
133
    <VirtualHost *:80>
 
134
      ServerName rack
 
135
      ProxyPass / balancer://rack/ lbmethod=byrequests stickysession=BALANCEID
 
136
      ProxyPassReverse / balancer://rack/
 
137
    </VirtualHost>
 
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
 
 
145
    juju expose apache2
 
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
 
 
151
    juju deploy kibana
 
152
    juju deploy logstash-indexer
 
153
    juju add-relation kibana logstash-indexer:rest
 
154
 
 
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
 
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
 
 
165
    juju deploy nagios
 
166
    juju add-relation rack nagios
 
167
 
 
168
Additionally you can perform disk, mem, and swap checks with NRPE extension:
 
169
 
 
170
    juju deploy nrpe
 
171
    juju add-relation rack nrpe
 
172
    juju add-relation nrpe nagios
 
173
 
 
174
# MongoDB relation
 
175
 
 
176
Deploy MonogDB service and relate it to Rack application:
51
177
 
52
178
    juju deploy mongodb
53
179
    juju add-relation mongodb rack
54
180
 
55
 
#### Resque setup
56
 
 
57
 
Coming soon...
58
 
 
59
 
### Sinatra example
60
 
 
61
 
1. Deploy web-server
62
 
 
63
 
        juju deploy apache2-passenger
64
 
 
65
 
2. Configure your application, for example html2haml
66
 
 
67
 
    **html2haml.yml**
68
 
 
69
 
        html2haml:
70
 
          repo_url: https://github.com/twilson63/html2haml.git
71
 
          app_name: html2haml
72
 
 
73
 
3. Deploy your application with Rack Charm
74
 
 
75
 
        juju deploy rack html2haml --config html2haml.yml
76
 
 
77
 
4. Relate it to web-server
78
 
 
79
 
        juju add-relation html2haml apache2-passenger
80
 
 
81
 
5. Open the stack up to the outside world.
82
 
 
83
 
        juju expose apache2-passenger
84
 
 
85
 
## Configuration
86
 
 
87
 
Here is an example configuration for Rails 3 installation from svn trunk. Additionally it will install imagemagick package.
88
 
 
89
 
    rails:
90
 
      repo_type: svn
91
 
      repo_url: https://github.com/pavelpachkovskij/sample-rails
92
 
      repo_branch: trunk
93
 
      app_name: sample-rails
94
 
      install_root: /var/www
95
 
      extra_packages: imagemagick
96
 
      port: 80
97
 
      migrate_database: true
98
 
      seed_database: true
99
 
      compile_assets: true
100
 
 
101
 
If you don't have or don't need to migrate database, seed database or compile assets, disable them in your config. They are enabled by default.
102
 
 
103
 
## Under the hood
104
 
 
105
 
- installs all dependencies and extra packages
106
 
- install node.js from ppa:chris-lea/node.js
107
 
- fetch an application from configured repository
108
 
- runs bundler
 
181
Rack charm will set environment variables which you can use to configure your Mongodb adapter.
 
182
 
 
183
    MONGODB_URL   => mongodb://host:port/database
 
184
 
 
185
## Mongoid 2.x
 
186
 
 
187
Your mongoid.yml should look like:
 
188
 
 
189
    production:
 
190
      uri: <%= ENV['MONGODB_URL'] %>
 
191
 
 
192
## Mongoid 3.x
 
193
 
 
194
Your mongoid.yml should look like:
 
195
 
 
196
    production:
 
197
      sessions:
 
198
        default:
 
199
          uri: <%= ENV['MONGODB_URL'] %>
 
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
 
 
207
    juju deploy memcached
 
208
    juju add-relation memcached rack
 
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
 
 
212
    MEMCACHE_PASSWORD    => xxxxxxxxxxxx
 
213
    MEMCACHE_SERVERS     => instance.hostname.net
 
214
    MEMCACHE_USERNAME    => xxxxxxxxxxxx
 
215
 
 
216
# Redis relation
 
217
 
 
218
Deploy Redis service and relate it to Rack application:
 
219
 
 
220
    juju deploy redis-master
 
221
    juju add-relation redis-master:redis-master rack
 
222
 
 
223
Rack charm will set environment variables which you can use to configure your Redis adapter.
 
224
 
 
225
    REDIS_URL   => redis://username:password@my.host:6389
 
226
 
 
227
For example you can configure Redis adapter in config/initializers/redis.rb
 
228
 
 
229
    uri = URI.parse(ENV["REDIS_URL"])
 
230
    REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
 
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
 
 
238
    config.assets.initialize_on_precompile = false
 
239
 
 
240
If you can't do this you still can join database and compile assets manually:
 
241
 
 
242
    juju ssh rack/0 run rake assets:precompile
 
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
 
 
246
    juju ssh rack/0 sudo restart rack
 
247
 
 
248
# Configuration
 
249
 
 
250
## Deploy from Git
 
251
 
 
252
Sample Git config:
 
253
 
 
254
    rack:
 
255
      repo: <repository_url>
 
256
      revision: <revision_number>
 
257
 
 
258
To deploy from private repo via SSH add 'deploy_key' option:
 
259
 
 
260
    deploy_key: <private_key>
 
261
 
 
262
## Deploy from SVN
 
263
 
 
264
Sample SVN config:
 
265
 
 
266
    rack:
 
267
      scm_provider: svn
 
268
      repo: <repository_url>
 
269
      revision: <revision_number>
 
270
      svn_username: <username>
 
271
      svn_password: <password>
 
272
 
 
273
## Install extra packages
 
274
 
 
275
Specify list of packages separated by spaces:
 
276
 
 
277
    extra_packages: 'libsqlite3++-dev libmagick++-dev'
 
278
 
 
279
## Set ENV variables
 
280
 
 
281
You can set ENV variables, which will be available within all processes defined in a Procfile:
 
282
 
 
283
    env: 'AWS_ACCESS_KEY_ID=aws_access_key_id AWS_SECRET_ACCESS_KEY=aws_secret_access_key'
 
 
b'\\ No newline at end of file'