Skip to content

Commit

Permalink
Merge branch 'idiot_proof'
Browse files Browse the repository at this point in the history
  • Loading branch information
pjaspers committed Mar 5, 2011
2 parents bce5b6d + 6cc79d4 commit 53e3222
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 50 deletions.
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ begin
gem.name = "capfire"
gem.summary = %Q{Send a notification to Campfire after a cap deploy}
gem.description = %Q{Inspired by http://github.com/blog/609-tracking-deploys-with-compare-view}
gem.email = "junkiesxl@gmail.com"
gem.homepage = "http://github.com/pjaspers/Capfire"
gem.email = "piet@10to1.be"
gem.homepage = "http://github.com/pjaspers/capfire"
gem.authors = ["pjaspers", "atog"]
gem.files = FileList['[A-Z]*',
'generators/**/*.*',
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.1
0.3.0
10 changes: 5 additions & 5 deletions capfire.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

Gem::Specification.new do |s|
s.name = %q{capfire}
s.version = "0.2.1"
s.version = "0.3.0"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["pjaspers", "atog"]
s.date = %q{2010-10-28}
s.date = %q{2011-03-05}
s.description = %q{Inspired by http://github.com/blog/609-tracking-deploys-with-compare-view}
s.email = %q{junkiesxl@gmail.com}
s.email = %q{piet@10to1.be}
s.extra_rdoc_files = [
"LICENSE",
"README.rdoc"
Expand All @@ -24,10 +24,10 @@ Gem::Specification.new do |s|
"generators/capfire_generator.rb",
"generators/lib/insert_commands.rb",
"generators/templates/capistrano_hook.rb",
"lib/Capfire.rb",
"lib/capfire.rb",
"lib/capfire/capistrano.rb"
]
s.homepage = %q{http://github.com/pjaspers/Capfire}
s.homepage = %q{http://github.com/pjaspers/capfire}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.7}
Expand Down
3 changes: 2 additions & 1 deletion generators/capfire_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def manifest
ssl: false
room: #{options[:chat_room]}
message: "I (#deployer#) deployed #application# with `cap #args#` (#compare_url#)"
cowsay: true
idiot_message: "LATFH: #deployer# wanted to deploy #application#, but forgot to push first."
cowsay: false
cow: random
CONF
unless campfire_file_exists?
Expand Down
6 changes: 0 additions & 6 deletions lib/Capfire.rb

This file was deleted.

108 changes: 108 additions & 0 deletions lib/capfire.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Gem for applications to automatically post to Campfire after an deploy.

class Capfire
# To see how it actually works take a gander at the generator
# or in the capistrano.rb
class << self
def config_file_exists?
File.exists?(File.join(ENV['HOME'],'.campfire'))
end

def valid_config?
config = self.config
config["message"] && config["room"] && config ["token"] && config["account"]
end

def config
YAML::load(File.open(File.join(ENV['HOME'],'.campfire')))["campfire"]
end

# Campfire room
def room
self.config["room"]
end

# Campfire account
def account
self.config["account"]
end

# Campfire token
def token
self.config["token"]
end

# `brew install cowsay && cowsay "capfire"`
# _________
#< capfire >
# ---------
# \ ^__^
# \ (oo)\_______
# (__)\ )\/\
# ||----w |
# || ||
def cowsay?
config["cowsay"] && self.bin_installed?("cowsay")
end

# Who is deploying
def deployer
Etc.getlogin
end

# Link to github's excellent Compare View
def github_compare_url(repo_url, first_commit, last_commit)
repo_url.gsub!(/git@/, 'http://')
repo_url.gsub!(/\.com:/,'.com/')
repo_url.gsub!(/\.git/, '')
"#{repo_url}/compare/#{first_commit}...#{last_commit}"
end

def default_idiot_message
"LATFH: #deployer# wanted to deploy #application#, but forgot to push first."
end

# Message to post on deploying without pushing
def idiot_message(application)
message = self.config["idiot_message"]
message = default_idiot_message unless message
message.gsub!(/#deployer#/, self.deployer)
message.gsub!(/#application#/, application)
message
end

# Message to post to campfire on deploy
def deploy_message(args,compare_url, application)
message = self.config["message"]
message.gsub!(/#deployer#/, deployer)
message.gsub!(/#application#/, application)
message.gsub!(/#args#/, args)
message.gsub!(/#compare_url#/, compare_url)
message
end

# Quick and irty way to check for installed bins
# Ideally this should also check if it's in the users
# path etc. Skipping for now.
def bin_installed?(bin_name)
!`which #{bin_name}`.empty?
end

# Initializes a broach campfire room
def broach_room
Broach.settings = {
'account' => self.account,
'token' => self.token,
'use_ssl' => true
}
Broach::Room.find_by_name(self.room)
end

# Posts to campfire
def speak(message)
self.broach_room.speak(message)
end

end

end
85 changes: 50 additions & 35 deletions lib/capfire/capistrano.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,64 @@
# Defines deploy:notify_campfire
# Capistrano task for posting to Campfire.
#
# There are two ways to use Capfire, either run the generator (see the README)
# or add 'require "capfire/capistrano"' to your deploy.rb.

require 'broach'
require 'capfire'

Capistrano::Configuration.instance(:must_exist).load do

# Don't bother users who have capfire installed but don't have a ~/.campfire file
if File.exists?(File.join(ENV['HOME'],'.campfire'))
after "deploy:update_code", "deploy:notify_campfire"

if Capfire.config_file_exists?
if Capfire.valid_config?
before "deploy:update_code", "capfire:check_for_push"
after "deploy:update_code", "capfire:post_to_campfire"
else
logger.info "Not all required keys found in your .campfire file. Please regenerate."
end
else
logger.info "Couldn't find a .campfire in your home directory."
end

namespace :deploy do
desc "Posting a message to Campfire"
task :notify_campfire do
namespace :capfire do

desc "Check if local version was pushed to github"
task :check_for_push do
deployed_version = current_revision[0,7] rescue "0000000"
local_version = `git rev-parse HEAD`[0,7]
if deployed_version == local_version
`say -v "Cellos" fail` if Capfire.bin_installed?("say")
Capfire.speak(Capfire.idiot_message(application)) unless dry_run
logger.important "\nDidn't you forget something? A hint: `git push`."
exit
end
end

desc <<-DESC
This will post to the campfire room as specified in your ~/.campfire. \
The message posted will contain a link to Github's excellent compare view, \
the commiters name, the project name and the arguments supplied to cap.
DESC
task :post_to_campfire do
begin
source_repo_url = repository
deployer = Etc.getlogin
deploying = `git rev-parse HEAD`[0,7]
begin
deployed = previous_revision[0,7]
rescue
deployed = "000000"
deployed_version = previous_revision[0,7] rescue "000000"
local_version = `git rev-parse HEAD`[0,7]

compare_url = Capfire.github_compare_url source_repo_url, deployed_version, local_version
message = Capfire.deploy_message(ARGV.join(' '), compare_url, application)
message = `cowsay "#{message}"` if Capfire.cowsay?

if dry_run
logger.info "Capfire would have posted:\n#{message}"
else
Capfire.speak message
end
puts "Posting to Campfire"
# Getting the github url
github_url = repository.gsub(/git@/, 'http://').gsub(/\.com:/,'.com/').gsub(/\.git/, '')
compare_url = "#{github_url}/compare/#{deployed}...#{deploying}"

# Reading the config file and drill in on the campfire section of course.
config = YAML::load(File.open(File.join(ENV['HOME'],'.campfire')))["campfire"]

# Ugly but it does the job.
message = config["message"].gsub(/#deployer#/, deployer).gsub(/#application#/, application).gsub(/#args#/, ARGV.join(' ')).gsub(/#compare_url#/,compare_url)

message = `cowsay "#{message}"` if config["cowsay"]

# Posting the message.
Broach.settings = {
'account' => config["account"],
'token' => config["token"],
'use_ssl' => true
}
room = Broach::Room.find_by_name(config["room"])
room.speak(message)
logger.info "Posting to Campfire"
rescue => e
puts e.message
# Making sure we don't make capistrano fail.
# Cause nothing sucks donkeyballs like not being able to deploy
logger.important e.message
end
end
end
Expand Down

0 comments on commit 53e3222

Please sign in to comment.