ktheory-vlad 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,91 @@
1
+ require 'rubygems'
2
+ require 'thread'
3
+ require 'rake_remote_task'
4
+
5
+ $TESTING ||= false
6
+
7
+ ##
8
+ # Vlad the Deployer - Pragmatic application deployment automation, without mercy.
9
+ #
10
+ # Please read doco/getting_started.txt or http://rubyhitsquad.com/
11
+ #
12
+ # === Basic scenario:
13
+ #
14
+ # 1. rake vlad:setup (first time only)
15
+ # 2. rake vlad:update
16
+ # 3. rake vlad:migrate (optional)
17
+ # 4. rake vlad:start
18
+
19
+ module Vlad
20
+
21
+ ##
22
+ # This is the version of Vlad you are running.
23
+ VERSION = '2.0.0'
24
+
25
+ ##
26
+ # Base error class for all Vlad errors.
27
+ class Error < RuntimeError; end
28
+
29
+ ##
30
+ # Raised when you have incorrectly configured Vlad.
31
+ class ConfigurationError < Error; end
32
+
33
+ ##
34
+ # Raised when a remote command fails.
35
+ class CommandFailedError < Error; end
36
+
37
+ ##
38
+ # Raised when an environment variable hasn't been set.
39
+ class FetchError < Error; end
40
+
41
+ ##
42
+ # Loads tasks file +tasks_file+ and various recipe styles as a hash
43
+ # of category/style pairs. Recipes default to:
44
+ #
45
+ # :app => :passenger
46
+ # :config => 'config/deploy.rb'
47
+ # :core => :core
48
+ # :scm => :subversion
49
+ # :web => :apache
50
+ #
51
+ # You can override individual values and/or set to nil to
52
+ # deactivate. :config will get loaded last to ensure that user
53
+ # variables override default values.
54
+ #
55
+ # And by all means, feel free to skip this entirely if it doesn't
56
+ # fit for you. All it does is a fancy-pants require. Require
57
+ # whatever files you need as you see fit straight from your
58
+ # Rakefile. YAY for simple and clean!
59
+ def self.load options = {}
60
+ options = {:config => options} if String === options
61
+ order = [:core, :app, :config, :scm, :web]
62
+ order += options.keys - order
63
+
64
+ recipes = {
65
+ :app => :passenger,
66
+ :config => 'config/deploy.rb',
67
+ :core => :core,
68
+ :scm => :subversion,
69
+ :web => :apache,
70
+ }.merge(options)
71
+
72
+ order.each do |flavor|
73
+ recipe = recipes[flavor]
74
+ next if recipe.nil? or flavor == :config
75
+ require "vlad/#{recipe}"
76
+ end
77
+
78
+ Kernel.load recipes[:config]
79
+ Kernel.load "config/deploy_#{ENV['to']}.rb" if ENV['to']
80
+ end
81
+ end
82
+
83
+ class String #:nodoc:
84
+ def cleanup
85
+ if ENV['FULL'] then
86
+ gsub(/\s+/, ' ').strip
87
+ else
88
+ self[/\A.*?\./]
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,37 @@
1
+ require 'vlad'
2
+
3
+ namespace :vlad do
4
+ ##
5
+ # Apache web server
6
+
7
+ set :web_command, "apachectl"
8
+
9
+ desc "(Re)Start the web servers"
10
+
11
+ remote_task :start_web, :roles => :web do
12
+ run "#{web_command} restart"
13
+ end
14
+
15
+ desc "Stop the web servers"
16
+
17
+ remote_task :stop_web, :roles => :web do
18
+ run "#{web_command} stop"
19
+ end
20
+
21
+ ##
22
+ # Everything HTTP.
23
+
24
+ desc "(Re)Start the web and app servers"
25
+
26
+ remote_task :start do
27
+ Rake::Task['vlad:start_app'].invoke
28
+ Rake::Task['vlad:start_web'].invoke
29
+ end
30
+
31
+ desc "Stop the web and app servers"
32
+
33
+ remote_task :stop do
34
+ Rake::Task['vlad:stop_app'].invoke
35
+ Rake::Task['vlad:stop_web'].invoke
36
+ end
37
+ end
@@ -0,0 +1,191 @@
1
+ require 'vlad'
2
+
3
+ ##
4
+ # used by update, out here so we can ensure all threads have the same value
5
+ def now
6
+ @now ||= Time.now.utc.strftime("%Y%m%d%H%M.%S")
7
+ end
8
+
9
+ namespace :vlad do
10
+ desc "Show the vlad setup. This is all the default variables for vlad
11
+ tasks.".cleanup
12
+
13
+ task :debug do
14
+ require 'yaml'
15
+
16
+ # force them into values
17
+ Rake::RemoteTask.env.keys.each do |key|
18
+ next if key =~ /_release|releases|sudo_password/
19
+ Rake::RemoteTask.fetch key
20
+ end
21
+
22
+ puts "# Environment:"
23
+ puts
24
+ y Rake::RemoteTask.env
25
+ puts "# Roles:"
26
+ y Rake::RemoteTask.roles
27
+ end
28
+
29
+ desc "Setup your servers. Before you can use any of the deployment
30
+ tasks with your project, you will need to make sure all of your
31
+ servers have been prepared with 'rake vlad:setup'. It is safe to
32
+ run this task on servers that have already been set up; it will
33
+ not destroy any deployed revisions or data.".cleanup
34
+
35
+ task :setup do
36
+ Rake::Task['vlad:setup_app'].invoke
37
+ end
38
+
39
+ desc "Prepares application servers for deployment.".cleanup
40
+
41
+ remote_task :setup_app, :roles => :app do
42
+ dirs = [deploy_to, releases_path, scm_path, shared_path]
43
+ dirs += %w(system log pids).map { |d| File.join(shared_path, d) }
44
+ run "umask #{umask} && mkdir -p #{dirs.join(' ')}"
45
+ end
46
+
47
+ desc "Updates your application server to the latest revision. Syncs
48
+ a copy of the repository, exports it as the latest release, fixes
49
+ up your symlinks, symlinks the latest revision to current and logs
50
+ the update.".cleanup
51
+
52
+ task :update => [:update_code, :update_symlinks, :symlink]
53
+
54
+ desc "Syncs a copy of the repository, exports it as the latest
55
+ release".cleanup
56
+
57
+ remote_task :update_code, :roles => :app do
58
+ begin
59
+ run [ "cd #{scm_path}",
60
+ "#{source.checkout revision, scm_path}",
61
+ "#{source.export revision, release_path}",
62
+ "chmod -R g+w #{latest_release}",
63
+ "rm -rf #{latest_release}/log #{latest_release}/public/system #{latest_release}/tmp/pids",
64
+ "mkdir -p #{latest_release}/db #{latest_release}/tmp"
65
+ ].join(" && ")
66
+ rescue => e
67
+ run "rm -rf #{release_path}"
68
+ raise e
69
+ end
70
+ end
71
+
72
+ desc "Updates the symlinks for shared paths".cleanup
73
+
74
+ remote_task :update_symlinks, :roles => :app do
75
+ run [ "ln -s #{shared_path}/log #{latest_release}/log",
76
+ "ln -s #{shared_path}/system #{latest_release}/public/system",
77
+ "ln -s #{shared_path}/pids #{latest_release}/tmp/pids" ].join(" && ")
78
+ end
79
+
80
+ desc "Symlinks the latest revision to current and logs the update".cleanup
81
+
82
+ remote_task :symlink, :roles => :app do
83
+ begin
84
+ run "rm -f #{current_path} && ln -s #{latest_release} #{current_path}"
85
+
86
+ run "echo #{now} $USER #{revision} #{File.basename release_path} >> #{deploy_to}/revisions.log"
87
+ rescue => e
88
+ run "rm -f #{current_path} && ln -s #{previous_release} #{current_path}"
89
+ run "rm -rf #{release_path}"
90
+ raise e
91
+ end
92
+ end
93
+
94
+ desc "Run the migrate rake task for the the app. By default this is run in
95
+ the latest app directory. You can run migrations for the current app
96
+ directory by setting :migrate_target to :current. Additional environment
97
+ variables can be passed to rake via the migrate_env variable.".cleanup
98
+
99
+ # No application files are on the DB machine, also migrations should only be
100
+ # run once.
101
+ remote_task :migrate, :roles => :app do
102
+ break unless target_host == Rake::RemoteTask.hosts_for(:app).first
103
+
104
+ directory = case migrate_target.to_sym
105
+ when :current then current_path
106
+ when :latest then current_release
107
+ else raise ArgumentError, "unknown migration target #{migrate_target.inspect}"
108
+ end
109
+
110
+ run "cd #{directory}; #{rake_cmd} RAILS_ENV=#{rails_env} db:migrate #{migrate_args}"
111
+ end
112
+
113
+ desc "Invoke a single command on every remote server. This is useful for
114
+ performing one-off commands that may not require a full task to be written
115
+ for them. Simply specify the command to execute via the COMMAND
116
+ environment variable. To execute the command only on certain roles,
117
+ specify the ROLES environment variable as a comma-delimited list of role
118
+ names.
119
+
120
+ $ rake vlad:invoke COMMAND='uptime'".cleanup
121
+
122
+ remote_task :invoke do
123
+ command = ENV["COMMAND"]
124
+ abort "Please specify a command to execute on the remote servers (via the COMMAND environment variable)" unless command
125
+ puts run(command)
126
+ end
127
+
128
+ desc "Copy arbitrary files to the currently deployed version using
129
+ FILES=a,b,c. This is useful for updating files piecemeal when you
130
+ need to quickly deploy only a single file.
131
+
132
+ To use this task, specify the files and directories you want to copy as a
133
+ comma-delimited list in the FILES environment variable. All directories
134
+ will be processed recursively, with all files being pushed to the
135
+ deployment servers. Any file or directory starting with a '.' character
136
+ will be ignored.
137
+
138
+ $ rake vlad:upload FILES=templates,controller.rb".cleanup
139
+
140
+ remote_task :upload do
141
+ file_list = (ENV["FILES"] || "").split(",")
142
+
143
+ files = file_list.map do |f|
144
+ f = f.strip
145
+ File.directory?(f) ? Dir["#{f}/**/*"] : f
146
+ end.flatten
147
+
148
+ files = files.reject { |f| File.directory?(f) || File.basename(f)[0] == ?. }
149
+
150
+ abort "Please specify at least one file to update (via the FILES environment variable)" if files.empty?
151
+
152
+ files.each do |file|
153
+ rsync file, File.join(current_path, file)
154
+ end
155
+ end
156
+
157
+ desc "Rolls back to a previous version and restarts. This is handy if you
158
+ ever discover that you've deployed a lemon; 'rake vlad:rollback' and
159
+ you're right back where you were, on the previously deployed
160
+ version.".cleanup
161
+
162
+ remote_task :rollback do
163
+ if releases.length < 2 then
164
+ abort "could not rollback the code because there is no prior release"
165
+ else
166
+ run "rm -f #{current_path}; ln -s #{previous_release} #{current_path} && rm -rf #{current_release}"
167
+ end
168
+
169
+ Rake::Task['vlad:start'].invoke
170
+ end
171
+
172
+ desc "Clean up old releases. By default, the last 5 releases are kept on
173
+ each server (though you can change this with the keep_releases variable).
174
+ All other deployed revisions are removed from the servers.".cleanup
175
+
176
+ remote_task :cleanup do
177
+ max = keep_releases
178
+ if releases.length <= max then
179
+ puts "no old releases to clean up #{releases.length} <= #{max}"
180
+ else
181
+ puts "keeping #{max} of #{releases.length} deployed releases"
182
+
183
+ directories = (releases - releases.last(max)).map { |release|
184
+ File.join(releases_path, release)
185
+ }.join(" ")
186
+
187
+ run "rm -rf #{directories}"
188
+ end
189
+ end
190
+
191
+ end # namespace vlad
@@ -0,0 +1,24 @@
1
+ class Vlad::Darcs
2
+
3
+ set :source, Vlad::Darcs.new
4
+ set :darcs_cmd, "darcs"
5
+
6
+ ##
7
+ # Ignores +revision+ for now, exports into directory +destination+
8
+
9
+ def checkout(revision, destination)
10
+ [ %{(test ! -d #{destination}/_darcs && #{darcs_cmd} init "--repodir=#{destination}") || true},
11
+ %{#{darcs_cmd} pull -a "--repodir=#{destination}" #{repository}},
12
+ ].join(" && ")
13
+ end
14
+
15
+ def export(revision, destination)
16
+ [ %{mkdir -p #{destination}},
17
+ %{ls | grep ^[^_] | xargs -I vlad cp -R vlad #{destination}}
18
+ ].join(" && ")
19
+ end
20
+
21
+ def revision(revision)
22
+ revision
23
+ end
24
+ end
@@ -0,0 +1,86 @@
1
+ class Vlad::Git
2
+
3
+ # Duh.
4
+ VERSION = "2.1.0"
5
+
6
+ set :source, Vlad::Git.new
7
+ set :git_cmd, "git"
8
+
9
+ ##
10
+ # Returns the command that will check out +revision+ from the
11
+ # repository into directory +destination+. +revision+ can be any
12
+ # SHA1 or equivalent (e.g. branch, tag, etc...)
13
+
14
+ def checkout(revision, destination)
15
+ destination = File.join(destination, 'repo')
16
+ revision = 'HEAD' if revision =~ /head/i
17
+
18
+ if fast_checkout_applicable?(revision, destination)
19
+ [ "cd #{destination}",
20
+ "#{git_cmd} checkout -q origin",
21
+ "#{git_cmd} fetch",
22
+ "#{git_cmd} reset --hard #{revision}",
23
+ "#{git_cmd} submodule init",
24
+ "#{git_cmd} submodule update",
25
+ "#{git_cmd} branch -f deployed-#{revision} #{revision}",
26
+ "#{git_cmd} checkout deployed-#{revision}",
27
+ "cd -"
28
+ ].join(" && ")
29
+ else
30
+ [ "rm -rf #{destination}",
31
+ "#{git_cmd} clone #{repository} #{destination}",
32
+ "cd #{destination}",
33
+ "#{git_cmd} submodule init",
34
+ "#{git_cmd} submodule update",
35
+ "#{git_cmd} checkout -f -b deployed-#{revision} #{revision}",
36
+ "cd -"
37
+ ].join(" && ")
38
+ end
39
+ end
40
+
41
+ ##
42
+ # Returns the command that will export +revision+ from the current directory
43
+ # into the directory +destination+.
44
+ # Expects to be run from +scm_path+ after Vlad::Git#checkout
45
+
46
+ def export(revision, destination)
47
+ revision = 'HEAD' if revision =~ /head/i
48
+ revision = "deployed-#{revision}"
49
+
50
+ [ "mkdir -p #{destination}",
51
+ "cd repo",
52
+ "#{git_cmd} archive --format=tar #{revision} | (cd #{destination} && tar xf -)",
53
+ "#{git_cmd} submodule foreach '#{git_cmd} archive --format=tar $sha1 | (cd #{destination}/$path && tar xf -)'",
54
+ "cd -",
55
+ "cd .."
56
+ ].join(" && ")
57
+ end
58
+
59
+ ##
60
+ # Returns a command that maps human-friendly revision identifier +revision+
61
+ # into a git SHA1.
62
+
63
+ def revision(revision)
64
+ revision = 'HEAD' if revision =~ /head/i
65
+
66
+ "`#{git_cmd} rev-parse #{revision}`"
67
+ end
68
+
69
+ private
70
+
71
+ # Checks if fast-checkout is applicable
72
+ def fast_checkout_applicable?(revision, destination)
73
+ revision = 'HEAD' if revision =~ /head/i
74
+
75
+ begin
76
+ cmd = [ "if cd #{destination}",
77
+ "#{git_cmd} rev-parse #{revision}",
78
+ "#{git_cmd} remote -v | grep -q #{repository}",
79
+ "cd -; then exit 0; else exit 1; fi &>/dev/null" ].join(" && ")
80
+ run cmd
81
+ return true
82
+ rescue Vlad::CommandFailedError
83
+ return false
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,23 @@
1
+ require 'vlad'
2
+
3
+ namespace :vlad do
4
+ ## God module for merb app server
5
+
6
+ desc "Prepares application servers for deployment.".cleanup
7
+
8
+ remote_task :setup_app, :roles => :app do
9
+ # do nothing?
10
+ end
11
+
12
+ desc "Restart the app servers"
13
+
14
+ remote_task :start_app, :roles => :app do
15
+ run "god restart #{cluster_name}"
16
+ end
17
+
18
+ desc "Stop the app servers"
19
+
20
+ remote_task :stop_app, :roles => :app do
21
+ run "god stop #{cluster_name}"
22
+ end
23
+ end