class Middleman::Cli::DeployWithNotifications
This class provides a “deploy” command for the middleman CLI.
Public Class Methods
exit_on_failure?()
click to toggle source
Tell Thor to exit with a nonzero exit code on failure
# File lib/middleman-deploy-with-notifications/commands.rb, line 22 def self.exit_on_failure? true end
Public Instance Methods
deploy()
click to toggle source
# File lib/middleman-deploy-with-notifications/commands.rb, line 31 def deploy # build before deploys Middleman::Cli::Build.new.build send("deploy_#{self.deploy_options.method}") end
Protected Instance Methods
deploy_ftp()
click to toggle source
# File lib/middleman-deploy-with-notifications/commands.rb, line 181 def deploy_ftp require 'net/ftp' require 'ptools' host = self.deploy_options.host user = self.deploy_options.user pass = self.deploy_options.password path = self.deploy_options.path puts "## Deploying via ftp to #{user}@#{host}:#{path}" notify_airbrake if self.deploy_options.airbrake_api_key notify_hipchat_start if self.deploy_options.hipchat_token ftp = Net::FTP.new(host) ftp.login(user, pass) ftp.chdir(path) ftp.passive = true Dir.chdir('build/') do files = Dir.glob('**/*', File::FNM_DOTMATCH) files.reject { |a| a =~ Regexp.new('\.$') }.each do |f| if File.directory?(f) begin ftp.mkdir(f) puts "Created directory #{f}" rescue end else begin if File.binary?(f) ftp.putbinaryfile(f, f) else ftp.puttextfile(f, f) end rescue Exception => e reply = e.message err_code = reply[0,3].to_i if err_code == 550 if File.binary?(f) ftp.putbinaryfile(f, f) else ftp.puttextfile(f, f) end end end puts "Copied #{f}" end end end ftp.close notify_hipchat_complete if self.deploy_options.hipchat_token end
deploy_git()
click to toggle source
# File lib/middleman-deploy-with-notifications/commands.rb, line 135 def deploy_git remote = self.deploy_options.remote branch = self.deploy_options.branch puts "## Deploying via git to remote=\"#{remote}\" and branch=\"#{branch}\"" notify_airbrake if self.deploy_options.airbrake_api_key notify_hipchat_start if self.deploy_options.hipchat_token #check if remote is not a git url unless remote =~ /\.git$/ remote = `git config --get remote.#{remote}.url`.chop end #if the remote name doesn't exist in the main repo if remote == '' puts "Can't deploy! Please add a remote with the name '#{self.deploy_options.remote}' to your repo." exit end Dir.chdir('build') do unless File.exists?('.git') `git init` `git remote add origin #{remote}` else #check if the remote repo has changed unless remote == `git config --get remote.origin.url`.chop `git remote rm origin` `git remote add origin #{remote}` end end #if there is a branch with that name, switch to it, otherwise create a new one and switch to it if `git branch`.split("\n").delete_if{ |r| r =~ Regexp.new(branch,true) }.count == 0 `git checkout #{branch}` else `git checkout -b #{branch}` end `git add -A` `git commit --allow-empty -am 'Automated commit at #{Time.now.utc} by #{PACKAGE} #{VERSION}'` `git push -f origin #{branch}` end notify_hipchat_complete if self.deploy_options.hipchat_token end
deploy_options()
click to toggle source
# File lib/middleman-deploy-with-notifications/commands.rb, line 79 def deploy_options options = nil begin options = ::Middleman::Application.server.inst.options rescue print_usage_and_die "You need to activate the deploy extension in config.rb." end if (!options.method) print_usage_and_die "The deploy extension requires you to set a method." end if (options.method == :rsync) if (!options.host || !options.user || !options.path) print_usage_and_die "The rsync deploy method requires host, user, and path to be set." end end if (options.method == :ftp) if (!options.host || !options.user || !options.password || !options.path) print_usage_and_die "The ftp deploy method requires host, user, password, and path to be set." end end options end
deploy_rsync()
click to toggle source
# File lib/middleman-deploy-with-notifications/commands.rb, line 107 def deploy_rsync host = self.deploy_options.host port = self.deploy_options.port user = self.deploy_options.user path = self.deploy_options.path notify_airbrake if self.deploy_options.airbrake_api_key notify_hipchat_start if self.deploy_options.hipchat_token puts "Deploying via rsync to #{user}@#{host}".red command = "rsync -avze '" + "ssh -p #{port}" + "' build/ #{user}@#{host}:#{path}" if options.has_key? "clean" clean = options.clean else clean = self.deploy_options.clean end if clean command += " --delete" end run command notify_hipchat_complete if self.deploy_options.hipchat_token end
exclaim()
click to toggle source
# File lib/middleman-deploy-with-notifications/notifications.rb, line 37 def exclaim phrases = [ "Holy cannoli", "Holy guacamole", "Holy hot chocolate", "Holy ravioli", "Hot buttery biscuits", ] return phrases[rand(phrases.size)] + "!" end
notify_airbrake()
click to toggle source
# File lib/middleman-deploy-with-notifications/notifications.rb, line 11 def notify_airbrake puts "Recording a timestamped deploy in Airbrake: #{self.deploy_options.revision_short}".green url = "api_key=#{self.deploy_options.airbrake_api_key}" + "&deploy[rails_env]=production" + "&deploy[local_username]=#{self.deploy_options.deploy_user}" + "&deploy[scm_repository]=git@github.com:140proof/280.140proof.com.git" + "&deploy[scm_revision]=#{self.deploy_options.revision_short}" `curl -d "#{url}" http://airbrake.io/deploys &> /dev/null` end
notify_hipchat_complete()
click to toggle source
# File lib/middleman-deploy-with-notifications/notifications.rb, line 28 def notify_hipchat_complete puts "Notifying hipchat room(s)".yellow message = "<a href='http://github.com/#{self.deploy_options.github_user}/#{self.deploy_options.github_project}/commit/#{self.deploy_options.revision}'>#{self.deploy_options.revision}</a> " + "(#{self.deploy_options.git_comment}) is now live on <b><a href='http://#{self.deploy_options.github_project}'>production</a></b>." send_hc_mesg(message, "html") end
notify_hipchat_start()
click to toggle source
# File lib/middleman-deploy-with-notifications/notifications.rb, line 22 def notify_hipchat_start send_hc_mesg( "#{exclaim} @#{self.deploy_options.deploy_user} is deploying #{self.deploy_options.github_project}:" ) end
print_usage_and_die(message)
click to toggle source
# File lib/middleman-deploy-with-notifications/commands.rb, line 39 def print_usage_and_die(message) raise Error, "ERROR: " + message + "\n" + <<EOF You should follow one of the three examples below to setup the deploy extension in config.rb. # To deploy the build directory to a remote host via rsync: activate :deploy do |deploy| deploy.method = :rsync # host, user, and path *must* be set deploy.user = "tvaughan" deploy.host = "www.example.com" deploy.path = "/srv/www/site" # clean is optional (default is false) deploy.clean = true end # To deploy to a remote branch via git (e.g. gh-pages on github): activate :deploy do |deploy| deploy.method = :git # remote is optional (default is "origin") # run `git remote -v` to see a list of possible remotes deploy.remote = "some-other-remote-name" # branch is optional (default is "gh-pages") # run `git branch -a` to see a list of possible branches deploy.branch = "some-other-branch-name" end # To deploy the build directory to a remote host via ftp: activate :deploy do |deploy| deploy.method = :ftp # host, user, passwword and path *must* be set deploy.host = "ftp.example.com" deploy.user = "tvaughan" deploy.password = "secret" deploy.path = "/srv/www/site" end EOF end
send_hc_mesg(mesg, fmt = "text")
click to toggle source
# File lib/middleman-deploy-with-notifications/notifications.rb, line 49 def send_hc_mesg(mesg, fmt = "text") uri = uri || URI.parse( "http://api.hipchat.com/v1/rooms/message" ) puts mesg self.deploy_options.hipchat_rooms.each do |room| Net::HTTP.post_form( uri, { "auth_token" => self.deploy_options.hipchat_token, "color" => "random", "format" => "json", "from" => "280 Deploy", "message" => mesg, "message_format" => fmt || "text", "room_id" => room, }) end end