Class: Opsicle::FailureLog

Inherits:
Object
  • Object
show all
Defined in:
lib/opsicle/commands/failure_log.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment) ⇒ FailureLog

Returns a new instance of FailureLog.



5
6
7
8
9
# File 'lib/opsicle/commands/failure_log.rb', line 5

def initialize(environment)
  @environment = environment
  @client = Client.new(environment)
  @stack = Opsicle::Stack.new(@client)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/opsicle/commands/failure_log.rb', line 3

def client
  @client
end

#stackObject (readonly)

Returns the value of attribute stack.



3
4
5
# File 'lib/opsicle/commands/failure_log.rb', line 3

def stack
  @stack
end

Instance Method Details

#executeObject



11
12
13
14
15
# File 'lib/opsicle/commands/failure_log.rb', line 11

def execute
  puts "Getting most recent failure log..."

  fetch
end

#fetchObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/opsicle/commands/failure_log.rb', line 17

def fetch
  failed_deployments = fetch_failed_deployments

  unless failed_deployments.empty?
    failed_deployment_id = failed_deployments.first.deployment_id
    failed_deployments_instances = failed_deployments.first.instance_ids

    unless failed_deployments_instances.empty?
      involved_instance_id = fetch_instance_id(failed_deployments_instances)

      target_failed_command = fetch_target_command(involved_instance_id, failed_deployment_id)
      log_url = target_failed_command.first.log_url
    
      system("open", log_url) if log_url
      puts "Unable to find a url to open." unless log_url
    else
      puts "There is at least one failed deployment, but there is no log available for that failure."
    end
  else
    puts "No failed deployments in available history."
  end
end

#fetch_failed_deploymentsObject



40
41
42
43
# File 'lib/opsicle/commands/failure_log.rb', line 40

def fetch_failed_deployments
  deployments = @client.opsworks.describe_deployments(stack_id: @stack.stack_id).deployments
  deployments.select{ |deploy| deploy.status.eql? "failed" }
end

#fetch_instance_id(failed_deployments_instances) ⇒ Object



45
46
47
48
49
# File 'lib/opsicle/commands/failure_log.rb', line 45

def fetch_instance_id(failed_deployments_instances)
  involved_instances = @client.opsworks.describe_instances(instance_ids: failed_deployments_instances).instances
  choice = select_instance(involved_instances)
  involved_instances[choice-1].instance_id
end

#fetch_target_command(involved_instance_id, failed_deployment_id) ⇒ Object



51
52
53
54
# File 'lib/opsicle/commands/failure_log.rb', line 51

def fetch_target_command(involved_instance_id, failed_deployment_id)
  command_list = @client.opsworks.describe_commands(instance_id: involved_instance_id)[:commands]
  command_list.select{ |command| command.deployment_id == failed_deployment_id }
end

#select_instance(instance_list) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/opsicle/commands/failure_log.rb', line 56

def select_instance(instance_list)
  if instance_list.length == 1
    choice = 1
  else
    Output.say "Choose an Opsworks instance:"
    instance_list.each_with_index do |instance, index|
      Output.say "#{index+1}) #{instance[:hostname]}"
    end
    choice = Output.ask("? ", Integer) { |q| q.in = 1..instance_list.length }
  end
  return choice
end