class Mmtrix::Cli::Deployments

Attributes

control[R]

Public Class Methods

command() click to toggle source
# File lib/mmtrix/cli/commands/deployments.rb, line 20
def self.command; "deployments"; end
new(command_line_args) click to toggle source

Initialize the deployment uploader with command line args. Use -h to see options. When command_line_args is a hash, we are invoking directly and it’s treated as an options with optional string values for :user, :description, :appname, :revision, :environment, :license_key, and :changes.

Will throw CommandFailed exception if there’s any error.

Calls superclass method Mmtrix::Cli::Command::new
# File lib/mmtrix/cli/commands/deployments.rb, line 31
def initialize command_line_args
  @control = Mmtrix::Control.instance
  super(command_line_args)
  @description ||= @leftover && @leftover.join(" ")
  @user ||= ENV['USER']
  control.env = @environment if @environment

  load_yaml_from_env(control.env)
  @appname ||= Mmtrix::Agent.config.app_names[0] || control.env || 'development'
  @license_key ||= Mmtrix::Agent.config[:license_key]

  setup_logging(control.env)
end

Public Instance Methods

load_yaml_from_env(env) click to toggle source
# File lib/mmtrix/cli/commands/deployments.rb, line 45
def load_yaml_from_env(env)
  yaml = Mmtrix::Agent::Configuration::YamlSource.new(Mmtrix::Agent.config[:config_path], env)
  if yaml.failed?
    messages = yaml.failures.flatten.map(&:to_s).join("\n")
    raise Mmtrix::Cli::Command::CommandFailure.new("Error in loading mmtrix.yml.\n#{messages}")
  end

  Mmtrix::Agent.config.replace_or_add_config(yaml)
end
run() click to toggle source

Run the Deployment upload in Mmtrix via Active Resource. Will possibly print errors and exit the VM

# File lib/mmtrix/cli/commands/deployments.rb, line 62
def run
  begin
    @description = nil if @description && @description.strip.empty?
    create_params = {}
    {
          :application_id => @appname,
          :host => Mmtrix::Agent::Hostname.get,
          :description => @description,
          :user => @user,
          :revision => @revision,
          :changelog => @changelog
    }.each do |k, v|
      create_params["deployment[#{k}]"] = v unless v.nil? || v == ''
    end
    http = ::Mmtrix::Agent::MmtrixService.new(nil, control.api_server).http_connection

    uri = "/deployments.xml"

    if @license_key.nil? || @license_key.empty?
      raise "license_key was not set in mmtrix.yml for #{control.env}"
    end
    request = Net::HTTP::Post.new(uri, {'x-license-key' => @license_key})
    request.content_type = "application/octet-stream"

    request.set_form_data(create_params)

    response = http.request(request)

    if response.is_a? Net::HTTPSuccess
      info "Recorded deployment to '#{@appname}' (#{@description || Time.now })"
    else
      err_string = REXML::Document.new(response.body).elements['errors/error'].map(&:to_s).join("; ") rescue  response.message
      raise Mmtrix::Cli::Command::CommandFailure, "Deployment not recorded: #{err_string}"
    end
  rescue SystemCallError, SocketError => e
    # These include Errno connection errors
    err_string = "Transient error attempting to connect to #{control.api_server} (#{e})"
    raise Mmtrix::Cli::Command::CommandFailure.new(err_string)
  rescue Mmtrix::Cli::Command::CommandFailure
    raise
  rescue => e
    err "Unexpected error attempting to connect to #{control.api_server}"
    info "#{e}: #{e.backtrace.join("\n   ")}"
    raise Mmtrix::Cli::Command::CommandFailure.new(e.to_s)
  end
end
setup_logging(env) click to toggle source
# File lib/mmtrix/cli/commands/deployments.rb, line 55
def setup_logging(env)
  Mmtrix::Agent.logger = Mmtrix::Agent::AgentLogger.new
  Mmtrix::Agent.logger.info("Running Capistrano from '#{env}' environment for application '#{@appname}'")
end

Private Instance Methods

options() { |o| ... } click to toggle source
# File lib/mmtrix/cli/commands/deployments.rb, line 111
def options
  OptionParser.new %Q{Usage: #{$0} #{self.class.command} [OPTIONS] ["description"] }, 40 do |o|
    o.separator "OPTIONS:"
    o.on("-a", "--appname=NAME", String,
           "Set the application name.",
           "Default is app_name setting in mmtrix.yml") { | e | @appname = e }
    o.on("-e", "--environment=name", String,
             "Override the (RAILS|MERB|RUBY|RACK)_ENV setting",
             "currently: #{control.env}") { | e | @environment = e }
    o.on("-u", "--user=USER", String,
           "Specify the user deploying, for information only",
           "Default: #{@user || '<none>'}") { | u | @user = u }
    o.on("-r", "--revision=REV", String,
           "Specify the revision being deployed") { | r | @revision = r }
    o.on("-l", "--license-key=KEY", String,
           "Specify the license key of the account for the app being deployed") { | l | @license_key = l }
    o.on("-c", "--changes",
           "Read in a change log from the standard input") { @changelog = STDIN.read }
    yield o if block_given?
  end
end