fpauser-vlad 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,155 @@
1
+ # Deploying Merb with Vlad
2
+
3
+ This tutorial has been adapted from [Deploying Merb with Vlad](http://effectif.com/articles/deploying-merb-with-vlad) by [Graham Ashton](http://effectif.com "Effectif Development").
4
+
5
+ You've built your Merb app, and you want to get it running on your web server. You could use Capistrano, but if you prefer the simple things in life you might find that [Vlad](http://rubyhitsquad.com/Vlad_the_Deployer.html "Vlad the Deployer") is a better fit.
6
+
7
+ This is a Merb version of the [Deploying Sinatra with Vlad](deploying-sinatra-with-vlad "Deploying Sinatra with Vlad") article that I wrote yesterday. There are big similarities to that article, but in this one we also look at how to restart Merb automatically after the code has been deployed.
8
+
9
+ ## Creating a sample application
10
+
11
+ Let's start by making ourselves a test app:
12
+
13
+ $ merb-gen flat hi
14
+ $ cd hi
15
+
16
+ We can check that the app works locally by running it...
17
+
18
+ $ merb
19
+
20
+ ...and then opening [http://localhost:4000](http://localhost:4000) in a web browser.
21
+
22
+ We need to create a `public` directory too (because this is a flat app I don't already have one), as Vlad assumes that we have a `public` directory for our static assets. I'm also going to make an empty CSS file so that the directory doesn't get ignored by Git:
23
+
24
+ $ mkdir public
25
+ $ touch public/master.css
26
+
27
+ We'll deploy our application from version control. I'm using Git, but you can use any system that Vlad supports; just check your files into a repository that will be accessible from your web server.
28
+
29
+ ## Configuring Vlad
30
+
31
+ Okay, we're ready for Vlad. It's a Ruby gem, so it's very easy to install:
32
+
33
+ $ sudo gem install vlad
34
+ Successfully installed vlad-1.2.0
35
+ 1 gem installed
36
+ Installing ri documentation for vlad-1.2.0...
37
+ Installing RDoc documentation for vlad-1.2.0...
38
+
39
+ There's no need to install Vlad on your server, just your workstation.
40
+
41
+ You access Vlad's functionality through Rake tasks. Add the following code to a file called `lib/tasks/vlad.rake` (you may have to make the directory first, but Merb will automatically find it):
42
+
43
+ begin
44
+ $TESTING = true # workaround a conflict between DataMapper and Vlad
45
+ require "vlad"
46
+ Vlad.load(:app => nil, :scm => "git")
47
+ rescue LoadError
48
+ # do nothing
49
+ end
50
+
51
+ Note that we've told Vlad that we intend to use Git (subversion is the default). We've set `:app` to `nil` as Vlad assumes that we'll run our application with `mongrel_rails` (it seems to think everybody who uses Mongrel is using Rails). Merb has support for clusters built in, so we'll setup some simple replacements later.
52
+
53
+ If you run `rake -T` now you should see a bunch of vlad tasks that are available to you. You can't run them yet; you need to configure Vlad. Create a `config/deploy.rb` file in your editor and set the following variables in it:
54
+
55
+ set :application, "hi"
56
+ set :repository, "ssh://your.git.server/path/to/project/hi.git"
57
+ set :domain, "your.web.server"
58
+ set :deploy_to, "/var/apps/#{application}"
59
+
60
+ Make sure that `:repository` correctly references your source control system, and that `:domain` is set to the hostname of your server.
61
+
62
+ I won't be able to create any directories under the `/var/apps` directory (I'm going to run vlad using my own username in this example), so I need to login to my server and make sure that I can create files in the `hi` directory:
63
+
64
+ $ ssh your.web.server
65
+ $ sudo mkdir -p /var/apps/hi
66
+ $ sudo chown yourusername /var/apps/hi
67
+
68
+ Now you can try running Vlad, to create all the directories necessary to serve your project. Back on your workstation, type:
69
+
70
+ $ rake vlad:setup
71
+
72
+ You should find that some directories have been created within `/var/apps/hi` on your server.
73
+
74
+ Let's trying deploying some code:
75
+
76
+ $ rake vlad:update
77
+ (in /Users/graham/data/effectif/projects/hi)
78
+ Initialized empty Git repository in /var/apps/hi/scm/repo/.git/
79
+ Switched to a new branch "deployed-HEAD"
80
+
81
+ You should now find that if you ssh into your server that you can run the application:
82
+
83
+ $ ssh your.web.server
84
+ $ cd /var/apps/hi/current
85
+ $ merb
86
+
87
+ Try making a change to your source, committing it to your repository, then run `vlad:update` again. Your code will be updated. If you restart Merb in the new directory you'll see your changes in the browser.
88
+
89
+ If you're following along with these commands, be careful that you're running `merb` in the freshly deployed directory. `current` is a symlink to a specific release directory, so you'll need to leave the directory and return to it to see the new source code (i.e. symlinks don't get updated under your shell's feet). This should do it:
90
+
91
+ $ cd ~ && cd -
92
+ $ merb
93
+
94
+ Okay, we're nearly there. All we need to do now is to automatically restart Merb when we deploy a new copy of the code with the `vlad:update` task. Add the following code to the bottom of your `config/deploy.rb` file:
95
+
96
+ Rake.clear_tasks("vlad:stop", "vlad:start")
97
+
98
+ namespace :vlad do
99
+ def stop
100
+ run "merb -m #{deploy_to}/current -K all"
101
+ end
102
+
103
+ def start
104
+ run "merb -m #{deploy_to}/current -e production -c 2"
105
+ end
106
+
107
+ remote_task :start, :roles => :app do
108
+ stop
109
+ start
110
+ end
111
+
112
+ remote_task :stop, :roles => :app do
113
+ stop
114
+ end
115
+
116
+ remote_task :update do
117
+ Rake::Task["vlad:start"].invoke
118
+ end
119
+ end
120
+
121
+ Note that in this example we've started a two process cluster with the `-c` option in the `start` method.
122
+
123
+ You'll find (with the `vlad:start` command I've shown above) that Merb is running on port 4000. If you want to run Merb on port 80 then I would recommend running these Merb processes behind Nginx, but I'll leave configuring Nginx as an exercise for the reader (Google will help you out there).
124
+
125
+ Let's just make sure that it's working. Run `rake vlad:update` a couple of times and check that Merb is getting stopped and started properly (it fails to stop Merb on the first run below simply because this was the first time I'd started it):
126
+
127
+ $ rake vlad:update
128
+ (in /Users/graham/data/effectif/projects/hi)
129
+ Loading init file from /Users/graham/data/effectif/projects/hi/config/init.rb
130
+ Initialized empty Git repository in /var/apps/hi/scm/repo/.git/
131
+ Switched to a new branch "deployed-HEAD"
132
+ ~ Could not find a PID file at /var/apps/hi/current/log/merb.main.pid. Most likely the process is no longer running and the pid file was not cleaned up.
133
+ ~ In 7643
134
+ $ rake vlad:update
135
+ (in /Users/graham/data/effectif/projects/hi)
136
+ Loading init file from /Users/graham/data/effectif/projects/hi/config/init.rb
137
+ Initialized empty Git repository in /var/apps/hi/scm/repo/.git/
138
+ Switched to a new branch "deployed-HEAD"
139
+ ~ Killing pid 7643 with INT
140
+ ~ In 7840
141
+
142
+ Now point your browser at port 4000 of your server – your application should be running!
143
+
144
+ ## Deploying from a Git branch
145
+
146
+ If you want to deploy from a specific Git branch (`master` is the default) you can set the `:revision` variable in `deploy.rb`:
147
+
148
+ set :revision, "origin/mybranch"
149
+
150
+ ## Deploying as a different user
151
+
152
+ It's not a great idea to deploy and run applications as your own login name (it's better practice to run web applications as users that don't have many privileges). I've not really addressed users in this article in order to focus on the basics of Vlad, but if you're interested you can deploy as a different user with these settings in `deploy.rb`:
153
+
154
+ set :user, "deploy"
155
+ set :domain, "#{user}@domain.com"
@@ -0,0 +1,119 @@
1
+ # Deploying Sinatra with Vlad
2
+
3
+ This tutorial has been adapted from [Deploying Sinatra with Vlad](http://effectif.com/articles/deploying-sinatra-with-vlad) by [Graham Ashton](http://effectif.com "Effectif Development").
4
+
5
+ So you've just written a nice new [Sinatra application](http://www.sinatrarb.com/ "Sinatra"), and you want to get it running on your web server. How hard can it be? Well with [Vlad the Deployer](http://rubyhitsquad.com/Vlad_the_Deployer.html "Vlad the Deployer"), it's actually rather easy.
6
+
7
+ ## Creating a sample application
8
+
9
+ Let's start by making ourselves a test app:
10
+
11
+ $ mkdir hello
12
+ $ cd hello
13
+ $ touch app.rb
14
+
15
+ Open `app.rb` in your editor and put this code in it:
16
+
17
+ require "rubygems"
18
+ require "sinatra"
19
+
20
+ get "/" do
21
+ "Hello!"
22
+ end
23
+
24
+ We can check that the app works locally by running it...
25
+
26
+ $ ruby app.rb
27
+
28
+ ...and then opening [http://localhost:4567](http://localhost:4567) in a web browser.
29
+
30
+ We need to create a `public` directory too, as Vlad assumes that we have a `public` directory for our static assets. I'm also going to make an empty CSS file so that the directory doesn't get ignored by Git:
31
+
32
+ $ mkdir public
33
+ $ touch public/master.css
34
+
35
+ We'll deploy our application from version control. I'm using Git, but you can use any system that Vlad supports; just check your files into a repository that will be accessible from your web server.
36
+
37
+ ## Configuring Vlad
38
+
39
+ Okay, we're ready for Vlad. It's a Ruby gem, so it's very easy to install:
40
+
41
+ $ sudo gem install vlad
42
+ Successfully installed vlad-1.2.0
43
+ 1 gem installed
44
+ Installing ri documentation for vlad-1.2.0...
45
+ Installing RDoc documentation for vlad-1.2.0...
46
+
47
+ There's no need to install Vlad on your server, just your workstation.
48
+
49
+ You access Vlad's functionality through Rake tasks. This means that we need a `Rakefile` which loads the Vlad code. Create `Rakefile` in the same directory as `app.rb`, then add the following code to it:
50
+
51
+ begin
52
+ require "vlad"
53
+ Vlad.load(:app => nil, :scm => "git")
54
+ rescue LoadError
55
+ # do nothing
56
+ end
57
+
58
+ Note that we've told Vlad that we intend to use Git (subversion is the default). We've set `:app` to `nil` as Vlad assumes that we'll run our application with [Mongrel](http://mongrel.rubyforge.org/ "Mongrel - Trac"). I'm not going to use Mongrel here, so we don't want Vlad to load its Mongrel recipes.
59
+
60
+ If you run `rake -T` now you should see a bunch of vlad tasks that are available to you. You can't run them yet; you need to configure Vlad with a `config/deploy.rb` file:
61
+
62
+ $ mkdir config
63
+ $ touch config/deploy.rb
64
+
65
+ Open `deploy.rb` in your editor and set the following variables:
66
+
67
+ set :application, "hello"
68
+ set :repository, "ssh://your.git.server/path/to/project/hello.git"
69
+ set :domain, "your.web.server"
70
+ set :deploy_to, "/var/apps/#{application}"
71
+
72
+ Make sure that `:repository` correctly references your source control system, and that `:domain` is set to the hostname of your server.
73
+
74
+ I won't be able to create any directories under the `/var/apps` directory (I'm going to run vlad using my own username in this example), so I need to login to my server and make sure that I can create files in the `hello` directory:
75
+
76
+ $ ssh your.web.server
77
+ $ sudo mkdir -p /var/apps/hello
78
+ $ sudo chown yourusername /var/apps/hello
79
+
80
+ Now you can try running Vlad, to create all the directories necessary to serve your project. Back on your workstation, type:
81
+
82
+ $ rake vlad:setup
83
+
84
+ You should find that some directories have been created within `/var/apps/hello` on your server.
85
+
86
+ Let's trying deploying some code:
87
+
88
+ $ rake vlad:update
89
+ (in /Users/graham/data/effectif/projects/hello)
90
+ Initialized empty Git repository in /var/apps/hello/scm/repo/.git/
91
+ Switched to a new branch "deployed-HEAD"
92
+
93
+ You should now find that if you ssh into your server that you can run the application:
94
+
95
+ $ ssh your.web.server
96
+ $ cd /var/apps/hello/current
97
+ $ ruby app.rb
98
+
99
+ Try making a change to your source, committing it to your repository, then run `vlad:update` again. Your code will be updated. If you restart Sinatra in the new directory you'll see your changes in the browser.
100
+
101
+ If you're following along with these commands, be careful that you're running `app.rb` in the freshly deployed directory. `current` is a symlink to a specific release directory, so you'll need to leave the directory and return to it to see the new source code (i.e. symlinks don't get updated under your shell's feet). This should do it:
102
+
103
+ $ cd ~ && cd -
104
+ $ ruby app.rb
105
+
106
+ You may now be wondering how to get Thin running automatically, and how to re-start it when you run `vlad:update`. That should be the subject of my next blog post (you can [subscribe](/articles.xml) if you need it).
107
+
108
+ ## Deploying from a Git branch
109
+
110
+ If you want to deploy from a specific Git branch (`master` is the default) you can set the `:revision` variable in `deploy.rb`:
111
+
112
+ set :revision, "origin/mybranch"
113
+
114
+ ## Deploying as a different user
115
+
116
+ It's not a great idea to deploy and run applications as your own login name (it's better practice to run web applications as users that don't have many privileges). I've not really addressed users in this article in order to focus on the basics of Vlad, but if you're interested you can deploy as a different user with these settings in `deploy.rb`:
117
+
118
+ set :user, "deploy"
119
+ set :domain, "#{user}@domain.com"
data/doco/faq.txt ADDED
@@ -0,0 +1,136 @@
1
+ == Rake & Recipes
2
+
3
+ === Q: Why is there no vlad:restart?
4
+ === A: It is cleaner!
5
+
6
+ We don't want to have to think about what state we're in and where. So vlad:start does a restart if necessary. Restart is just "start again" after all... That is what start does.
7
+
8
+ === Q: Why is there no vlad:deploy?
9
+ === A: Because everyone is a unique beautiful flower.
10
+
11
+ Everyone's deployment is different. Everyone. Unique scaling
12
+ requirements. Yadda yadda yadda. So rather than supply something that
13
+ nobody will use, we decided not to supply anything at all. Here is an
14
+ example deploy that I stole from the web (and improved) that you may like:
15
+
16
+ desc "Full deployment cycle"
17
+ task "vlad:deploy" => %w[
18
+ vlad:update
19
+ vlad:migrate
20
+ vlad:reset_session
21
+ vlad:start
22
+ vlad:cleanup
23
+ ]
24
+
25
+ Just pop that in your config/deploy.rb, tweak it as necessary, and have at it.
26
+
27
+ === Q: Why are there no before_action and after_action hooks?
28
+ === A: Because we use rake!
29
+
30
+ Rake don't need no stinkin' hooks! They're too clever. Last I checked before_after_before_start worked in cap... how? why? I dunno...
31
+
32
+ To extend a task (adding something after), just define it again:
33
+
34
+ task :action1 do
35
+ puts "one fish, two fish"
36
+ end
37
+
38
+ task :action1 do
39
+ puts "red fish, blue fish"
40
+ end
41
+
42
+ To prepend on a task, add a dependency:
43
+
44
+ task :action2 do
45
+ puts "red fish, blue fish"
46
+ end
47
+
48
+ task :myaction do
49
+ puts "one fish, two fish"
50
+ end
51
+
52
+ task :action2 => :myaction
53
+
54
+ === Q: How can I replace a rake task instead of just adding to it?
55
+ === A: Use Rake.clear_tasks str_or_regexp
56
+
57
+ NOTE: Rake.clear_tasks was moved to Hoe so it could be used more generally.
58
+
59
+ require 'hoe/rake'
60
+
61
+ namespace :vlad do
62
+ # Clear existing update task so that we can redefine instead of
63
+ # adding to it.
64
+ Rake.clear_tasks('vlad:update')
65
+
66
+ remote_task :update, :roles => :app do
67
+ #custom update stuff
68
+ end
69
+ end
70
+
71
+ === Q: How do I invoke another rule?
72
+ === A: The easiest way is via dependencies.
73
+
74
+ task :shazam! => [:action1, :action2]
75
+
76
+ The other way is to look it up and call invoke:
77
+
78
+ task :shazam! do
79
+ Rake::Task[:action1].invoke
80
+ Rake::Task[:action2].invoke
81
+ end
82
+
83
+ (Or, cheat and call out to rake again: sh "rake action1")
84
+
85
+ == Using SSH
86
+
87
+ === Q: Is there any way to set the ssh user?
88
+ === A: Yes, using ~/.ssh/config
89
+
90
+ Host example.com
91
+ User fluffy_bunny
92
+
93
+ OR: Alternatively, you can do this within your recipes like so:
94
+
95
+ set :user, "fluffy_bunny"
96
+ set :domain, "#{user}@example.com"
97
+
98
+ === Q: Is there any way to speed up ssh connections?
99
+ === A: Yes, add to your Host entry in ~/.ssh/config:
100
+
101
+ ControlMaster auto
102
+ ControlPath ~/.ssh/master-%r@%h:%p
103
+
104
+ === Q: I'm tired of typing in my password!
105
+ === A: Me too!
106
+
107
+ Put a password on your key, distribute your public key to the server and then use ssh-agent.
108
+
109
+ Check out this tiny tutorial at LBL: A brief ssh-agent tutorial <http://upc.lbl.gov/docs/user/sshagent.html>
110
+
111
+ If you're on a mac (on tiger, not leopard), use SSHKeychain, we love it. <http://www.sshkeychain.org/>. If you are on leopard, you get all of this for free.
112
+
113
+ === Q: How do I use Vlad with a gateway?
114
+ === A: Add the following to your deploy.rb variables:
115
+
116
+ set :ssh_flags, "-A #{mygateway}"
117
+ set :rsync_flags, "--rsh ssh -A #{mygateway} ssh"
118
+
119
+ === Q: OMG subversion is stupid! It keeps asking for "Authentication Realm"
120
+ === A: Yes, yes it is.
121
+
122
+ If you're seeing local checkouts work fine but they don't over ssh
123
+ (even to localhost!) then ssh into that machine (yes, even localhost)
124
+ and do a checkout there to a temporary directory. From then on,
125
+ checkout over ssh should work fine.
126
+
127
+ % svn co https://blah/blah /tmp/happy
128
+ ... works fine ...
129
+ % ssh localhost svn co https://blah/blah /tmp/sad
130
+ ... asks for authentication and then hangs ...
131
+ % ssh localhost
132
+ % svn co https://blah/blah /tmp/sad-no-happy
133
+ ... asks for authentication ...
134
+ ... works fine ...
135
+ % ssh localhost svn co https://blah/blah /tmp/happy2
136
+ ... works fine ...
@@ -0,0 +1,61 @@
1
+
2
+ == Quick Start for a 1-Server Solution:
3
+
4
+ === Setup
5
+
6
+ * Create a deploy file, usually in "config/deploy.rb":
7
+
8
+ set :application, "project"
9
+ set :domain, "example.com"
10
+ set :deploy_to, "/path/to/install"
11
+ set :repository, 'http://svn.example.com/project/branches/stable/'
12
+
13
+ This defaults to using 'svn export' from +repository+, and a single
14
+ server for +app+, +db+, and +www+. If you need to tweak these things,
15
+ refer to the variable documentation.
16
+
17
+ * If you want a multi-config environment, change your config like so:
18
+
19
+ set :application, "project"
20
+ set :repository, 'http://svn.example.com/project/branches/stable/'
21
+
22
+ task :beta do
23
+ set :domain, "beta.example.com"
24
+ set :deploy_to, "/path/to/install-beta"
25
+ end
26
+
27
+ task :dev do
28
+ set :domain, "dev.example.com"
29
+ set :deploy_to, "/path/to/install-dev"
30
+ end
31
+
32
+ task :prod do
33
+ set :domain, "example.com"
34
+ set :deploy_to, "/path/to/install"
35
+ end
36
+
37
+ * Add the following to your Rakefile:
38
+
39
+ begin
40
+ require 'vlad'
41
+ Vlad.load
42
+ rescue LoadError
43
+ # do nothing
44
+ end
45
+
46
+ Vlad.load has a lot of flexibility. See the rdoc for full information.
47
+
48
+ You don't need the begin/rescue/end block if you ensure that Vlad is
49
+ installed on all your servers. To be lazy, you can install vlad via:
50
+
51
+ % rake vlad:invoke COMMAND='sudo gem install vlad -y'
52
+
53
+ === Initial Launch
54
+
55
+ * Run <tt>rake vlad:setup vlad:update vlad:migrate vlad:start</tt>
56
+
57
+ === Subsequent Updates:
58
+
59
+ * <tt>rake vlad:update vlad:migrate vlad:start</tt>
60
+
61
+ Each step may be run separately.