class Object

Public Instance Methods

application(name, opts={}, &block) click to toggle source

Defines an application with an optional owner, group, and configuration block.

# File lib/crapapult.rb, line 122
def application(name, opts={}, &block)
  app_block = block || lambda {}
  set :application_name, name
  set :owner, opts[:owner] || name
  set :owner_group, opts[:group] || name
  set :healthcheck_url, opts[:healthcheck]
  namespace :deploy do
    task :configure do
      setup
      puts "> Configuring the application"
      app_block.call
    end
    
    if opts[:healthcheck]
      task :verify do
        puts("> Verifying")
        for server in roles[:app].servers
          uri = URI.parse(erb(healthcheck_url))
          req = Net::HTTP::Get.new(uri.path)
          Net::HTTP.start(uri.host, uri.port) do |http|
            resp = http.request(req)
            if resp.code.to_i == 200
              puts(">> #{server.host} is OK")
            else
              puts(">> #{server.host} is DOWN!!!")
              puts(resp.body.split("\n").map { |s| ">>> #{s}" }.join("\n"))
            end
          end
        end
      end
    else
      task :verify do
        puts("> No healthcheck configured for the application. Hope it's running.")
      end
    end
  end
end
artifact_id(name) click to toggle source

Defines the artifact’s artifactId.

# File lib/crapapult.rb, line 87
def artifact_id(name)
  set :current_artifact_id, name
end
asset(asset, path, opts={}) click to toggle source

Uploads an asset to the given path on each server.

# File lib/crapapult.rb, line 180
def asset(asset, path, opts={})
  puts ">> Uploading #{asset} to #{path}"
  opts = { :owner => owner, :group => owner_group, :mode => "0644" }.merge(opts)
  filename = "#{temp_dir}/#{File.basename(path)}"
  upload(File.join("assets", asset), filename)
  run "#{sudo} cp #{filename} #{path}"
  run "#{sudo} chmod #{opts[:mode]} #{path}"
  run "#{sudo} chown -R #{opts[:owner]}:#{opts[:group]} #{path}"
end
check_required_parameters!() click to toggle source
# File lib/crapapult.rb, line 337
def check_required_parameters!
  unless exists?(:application_name)
    abort("No application defined.")
  end
  
  unless exists?(:current_repo)
    abort("No Maven repository defined.")
  end

  unless exists?(:current_group_id)
    abort("No group ID defined.")
  end

  unless exists?(:current_artifact_id)
    abort("No artifact ID defined.")
  end
  
  unless exists?(:current_environment)
    abort("No target environment specified. Please use one of the to:* tasks.")
  end
end
data(name, value) click to toggle source

Defines a piece of configuration data.

# File lib/crapapult.rb, line 161
def data(name, value)
  @data[name] = value
end
environment(name, opts={}, &block) click to toggle source

Defines an environment which might allow snapshots and an optional environment-specific configuration block.

# File lib/crapapult.rb, line 99
def environment(name, opts={}, &block)
  env_block = block || lambda {}

  desc "Deploy to the #{name} environment"
  task name.to_sym do
    set :current_environment, name
    set :allow_snapshots, opts[:allow_snapshots]
    env_block.call
    execute_task(find_task("deploy"))
  end
end
erb(text) click to toggle source

Internal ###################################

# File lib/crapapult.rb, line 279
def erb(text)
  Erubis::Eruby.new(text).result(@data)
end
group_id(name) click to toggle source

Defines the artifact’s groupId.

# File lib/crapapult.rb, line 82
def group_id(name)
  set :current_group_id, name
end
host(name, &block) click to toggle source

Defines a host with an optional host-specific configuration block.

# File lib/crapapult.rb, line 92
def host(name, &block)
  @hosts[name] = block || lambda {}
  server name, :app
end
maven(url) click to toggle source

Defines a Maven repository.

# File lib/crapapult.rb, line 112
def maven(url)
  if exists?(:current_repo)
    abort "You're calling #maven twice. Stop it."
  else
    set :current_repo, url
  end
end
parallelized(role, &block) click to toggle source
# File lib/crapapult.rb, line 283
def parallelized(role, &block)
  $parallel_block = block
  original_data = @data.dup
  for server in roles[role].servers
    @data = original_data
    @data[:host] = server.host
    @hosts[server.host].call
    $current_host = server.host
    execute_task namespaces[:execute].tasks[:parallel]
  end
  @data = original_data
end
template(template, path, opts={}) click to toggle source

Renders a template to the given path on each server.

# File lib/crapapult.rb, line 166
def template(template, path, opts={})
  puts ">> Uploading #{template} to #{path}"
  opts = { :owner => owner, :group => owner_group, :mode => "0644" }.merge(opts)
  filename = "#{temp_dir}/#{File.basename(path)}"
  parallelized(:app) do
    output = erb(File.read("templates/#{template}"))
    put output, filename
    run "#{sudo} cp #{filename} #{path}"
    run "#{sudo} chmod #{opts[:mode]} #{path}"
    run "#{sudo} chown -R #{opts[:owner]}:#{opts[:group]} #{path}"
  end
end
upstart(config_file) click to toggle source

Uploads and adds an Upstart configuration file for the application.

# File lib/crapapult.rb, line 191
def upstart(config_file)
  puts ">> Configuring Upstart for #{application_name}"
  base = "#{application_name}.conf"
  filename = "#{temp_dir}/#{base}"
  upload(File.join("assets", config_file), filename)
  run "#{sudo} cp #{filename} /etc/init/#{base}"
  run "#{sudo} ln -sf /lib/init/upstart-job /etc/init.d/#{application_name}"
end
yammer(token, opts={}) click to toggle source

Post a message to your Yammer network when you deploy.

# File lib/crapapult.rb, line 201
def yammer(token, opts={})
  after :deploy do
    set :deployed_version, exists?(:build) ? "#{version} (#{build})" : version
    opts = {:host => "www.yammer.com",
            :body => %Q{<%= application_name %> v<%= deployed_version %> was just deployed to the <%= current_environment %> environment by @<%= `whoami`.strip %>.}
    }.merge(opts)
    puts "> Sending notification to Yammer"
    opts[:body] = Erubis::Eruby.new(opts[:body]).result(binding)
    http = Net::HTTP.new(opts.delete(:host), 443)
    http.use_ssl = true
    request = Net::HTTP::Post.new("/api/v1/messages.json?access_token=#{token}")
    request.set_form_data(opts)

    response = http.request(request)
    if response.is_a? Net::HTTPSuccess
      puts ">> Sent!"
    else
      puts "!! Error sending notification: #{response.code} #{response.message}"
    end
  end
end