Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## mattly's vlad-fu
- # ~/.ruby is in ruby's load path
- ## ~/.ruby/vlad/basic.rb
- # meant to be used as part of Vlad the Deployer, as a 'core' set of tasks:
- # Vlad.load :core => :basic
- require 'vlad'
- # used by update, out here so we can ensure all threads have the same value
- def now
- @now ||= Time.now.utc.strftime("%Y%m%d%H%M.%S")
- end
- namespace :vlad do
- desc "Show the vlad setup. This is all the default variables for vlad
- tasks.".cleanup
- task :debug do
- require 'yaml'
- # force them into values
- Rake::RemoteTask.env.keys.each do |key|
- next if key =~ /_release|releases|sudo_password/
- Rake::RemoteTask.fetch key
- end
- puts "# Environment:"
- puts
- y Rake::RemoteTask.env
- puts "# Roles:"
- y Rake::RemoteTask.roles
- end
- desc "Setup your servers. Before you can use any of the deployment
- tasks with your project, you will need to make sure all of your
- servers have been prepared with 'rake vlad:setup'. It is safe to
- run this task on servers that have already been set up; it will
- not destroy any deployed revisions or data.".cleanup
- remote_task :setup do
- run "mkdir -p #{[deploy_to, releases_path, scm_path, shared_path].join(' ')}"
- end
- desc "Updates your application server to the latest revision. Syncs
- a copy of the repository, exports it as the latest release, fixes
- up your symlinks, symlinks the latest revision to current and logs
- the update.".cleanup
- remote_task :update, :roles => :app do
- symlink = false
- rev = revision rescue 'head' # If revision doesn't exist, default to 'head'
- begin
- run [ "cd #{scm_path}",
- "#{source.checkout rev, '.'}",
- "#{source.export ".", release_path}",
- "chmod -R g+w #{latest_release}",
- ].join(" && ")
- symlink = true
- run "rm -f #{current_path} && ln -s #{latest_release} #{current_path}"
- run "echo #{now} $USER #{rev} #{File.basename release_path} >> #{deploy_to}/revisions.log"
- rescue => e
- run "rm -f #{current_path} && ln -s #{previous_release} #{current_path}" if
- symlink
- run "rm -rf #{release_path}"
- raise e
- end
- end
- desc "Invoke a single command on every remote server. This is useful for
- performing one-off commands that may not require a full task to be written
- for them. Simply specify the command to execute via the COMMAND
- environment variable. To execute the command only on certain roles,
- specify the ROLES environment variable as a comma-delimited list of role
- names.
- $ rake vlad:invoke COMMAND='uptime'".cleanup
- remote_task :invoke do
- command = ENV["COMMAND"]
- abort "Please specify a command to execute on the remote servers (via the COMMAND environment variable)" unless command
- puts run(command)
- end
- desc "Copy arbitrary files to the currently deployed version using
- FILES=a,b,c. This is useful for updating files piecemeal when you
- need to quickly deploy only a single file.
- To use this task, specify the files and directories you want to copy as a
- comma-delimited list in the FILES environment variable. All directories
- will be processed recursively, with all files being pushed to the
- deployment servers. Any file or directory starting with a '.' character
- will be ignored.
- $ rake vlad:upload FILES=templates,controller.rb".cleanup
- remote_task :upload do
- file_list = (ENV["FILES"] || "").split(",")
- files = file_list.map do |f|
- f = f.strip
- File.directory?(f) ? Dir["#{f}/**/*"] : f
- end.flatten
- files = files.reject { |f| File.directory?(f) || File.basename(f)[0] == ?. }
- abort "Please specify at least one file to update (via the FILES environment variable)" if files.empty?
- files.each do |file|
- rsync file, File.join(current_path, file)
- end
- end
- desc "Rolls back to a previous version and restarts. This is handy if you
- ever discover that you've deployed a lemon; 'rake vlad:rollback' and
- you're right back where you were, on the previously deployed
- version.".cleanup
- remote_task :rollback do
- if releases.length < 2 then
- abort "could not rollback the code because there is no prior release"
- else
- run "rm #{current_path}; ln -s #{previous_release} #{current_path} && rm -rf #{current_release}"
- end
- Rake::Task['vlad:start'].invoke
- end
- desc "Clean up old releases. By default, the last 5 releases are kept on
- each server (though you can change this with the keep_releases variable).
- All other deployed revisions are removed from the servers.".cleanup
- remote_task :cleanup do
- count = keep_releases
- old_releases = (releases - releases.last(count))
- if old_releases.empty? # count >= releases.size
- puts "no old releases to clean up"
- else
- puts "keeping #{count} of #{releases.length} deployed releases"
- run "sudo -p Password: rm -rf #{old_releases.map {|rel| File.join releases_path, rel}.join(' ')}"
- end
- end
- end # namespace vlad
- ## ~/.ruby/vlad/nginx.rb
- # meant to be used as part of Vlad the Deployer, as an 'app' set of tasks:
- # Vlad.load :app => :nginx
- require 'vlad'
- desc "generates the config file from the template and yaml using generate_nginx_config gem"
- task :generate do
- `generate_nginx_config -y -t nginx.erb`
- end
- namespace :vlad do
- def sudo(command)
- run [sudo_cmd, '-p Password:', sudo_flags, command].join(' ')
- end
- set :control_via, :initd
- set :nginx_location, "/usr/bin/env nginx"
- set :config_location, "/etc/nginx"
- set :config_filename, "nginx.conf"
- set :pid_location, "/var/run/nginx.pid"
- set :raw, {
- :start => "#{nginx_location} -c #{config_location}/#{config_filename}",
- :stop => "kill -15 `cat #{pid_location}`",
- :reload => "kill -HUP `cat #{pid_location}`"
- }
- set :initd_command, "/etc/init.d/nginx"
- set :initd, {
- :start => "#{initd_command} start",
- :stop => "#{initd_command} stop",
- :reload => "#{initd_command} restart"
- }
- def command(cmd)
- set = self.send(control_via)
- set[cmd]
- end
- desc "Reloads the Nginx config"
- remote_task :reload do
- sudo command(:reload)
- end
- desc "Starts up Nginx from a cold start"
- remote_task :start do
- sudo command(:start)
- end
- desc "Stops Nginx Dead"
- remote_task :stop do
- sudo command(:stop)
- end
- end
- ## ~/.ruby/vlad/postfix.rb
- namespace :vlad do
- def sudo(command)
- run [sudo_cmd, "-p Password:", sudo_flags, command].join(' ')
- end
- set :postfix_command, '/usr/sbin/postfix'
- set :postfix_init, '/etc/init.d/postfix'
- set :postmap_command, '/usr/sbin/postmap'
- set :etc_path, '/etc/postfix'
- desc "copies the files to /etc/postfix"
- remote_task :copy_files_to_etc do
- sudo "cp -R #{current_path}/* #{etc_path}/"
- end
- # WARNING: CAUSES WEIRD PERMISSION ERRORS ON UBUNTU 7!
- # desc "symlinks the deployment location to /etc"
- # remote_task :update_symlink do
- # sudo "ln -nfs #{current_path} #{etc_path}"
- # end
- desc "compiles the hashmap files"
- remote_task :compile_maps do
- sudo "#{postmap_command} #{etc_path}/*.maps"
- end
- # don't have to do this if we're copying instead of symlinking
- # desc "sets owner of the symlink and the deploy location to root so postfix doesn't get cranky"
- # remote_task :fix_permissions => :compile_maps do
- # sudo "#{postfix_command} set-permissions"
- # sudo "chown -h root:root #{etc_path}"
- # sudo "chown -R root:root #{current_release}"
- # sudo "chmod -R 644 #{current_release}"
- # sudo "chmod 755 #{current_release}/post-install"
- # sudo "chmod 755 #{current_release}/postfix-script"
- #
- # sudo "chown -R $USER:$USER #{previous_release}"
- # end
- desc "Checks the main and master .cf files have ok syntax. Does NOT check that map files are compiled."
- remote_task :check_configuration => :compile_maps do
- sudo "#{postfix_command} check"
- end
- desc "Reloads the Postfix configuration."
- remote_task :reload_configuration do
- sudo "#{postfix_command} reload"
- end
- desc "Reloads or Starts Postfix as necessary"
- remote_task :restart_app do
- # how do we determine if postfix is running?
- end
- desc "Stops the Postfix server."
- remote_task :stop_app do
- sudo "#{postfix_init} stop"
- end
- desc "Starts the Postfix server."
- remote_task :start_app do
- sudo "#{postfix_init} start"
- end
- end
Add Comment
Please, Sign In to add comment