Class: Fum::Commands::Tail

Inherits:
Fum::Command show all
Defined in:
lib/fum/commands/tail.rb

Instance Method Summary collapse

Methods inherited from Fum::Command

#initialize, #stage

Methods included from Util

#die

Constructor Details

This class inherits a constructor from Fum::Command

Instance Method Details

#execute(options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fum/commands/tail.rb', line 14

def execute(options)
  beanstalk = Fog::AWS[:beanstalk]

  die "Directory #{options[:directory]} does not exist" if options[:directory] && !File.directory?(options[:directory])

  if options[:environment].nil?
    # Use the currently active environment

  end
  request_opts = {
      'ApplicationName' => options[:application],
      'EnvironmentName' => options[:environment],
      'InfoType' => 'tail'
  }

  if options[:environment] && options[:environment].match(/e-[a-zA-Z0-9]{10}/)
    request_opts['EnvironmentId'] = options[:environment]
  else
    request_opts['EnvironmentName'] = options[:environment]
  end

  puts "Requesting logs for environment #{options[:environment]}" if options[:verbose]
  beanstalk.request_environment_info(request_opts)

  begin
    files = Timeout::timeout(360) do
      # Sleep a few seconds
      sleep(5)
      begin
        puts "Retrieving logs for environment #{options[:environment]}" if options[:verbose]
        info = beanstalk.retrieve_environment_info(request_opts).body["RetrieveEnvironmentInfoResult"]["EnvironmentInfo"]
        puts "Logs not yet available, will try again in 10 seconds." if options[:verbose] && info.length == 0
      end while info.length == 0 && sleep(10)

      info = info.sort_by { |i| i['SampleTimestamp'] }.reverse
      instance_ids = []
      info = info.select { |i|
        seen = instance_ids.include?(i['Ec2InstanceId'])
        instance_ids << i['Ec2InstanceId']
        !seen
      }
    end
  rescue Timeout::Error
    die "Operation timed out, could not retrieve environment info."
  end


  require 'excon'
  files.each { |file|
    contents = Excon.get(file['Message']).body
    if options[:directory]
      filename = "#{file['Ec2InstanceId']}-#{file['SampleTimestamp'].strftime('%Y%m%dT%H%M')}.txt"
      file = File.join(File.expand_path(options[:directory]),filename)
      if File.exists?(file)
        puts "File already exists #{file}, will not overwrite."
        next
      end
      File.open(file, 'w') { |f|f.write(contents) }
      puts "Created file #{file}"
    else
      puts "##########################################"
      puts "# InstanceID #{file['Ec2InstanceId']}"
      puts "# Timestamp #{file['SampleTimestamp']}"
      puts "##########################################"
      puts contents
    end

  }

end

#parse_optionsObject



5
6
7
8
9
10
11
12
# File 'lib/fum/commands/tail.rb', line 5

def parse_options
  Trollop::options do
    banner "usage: tail [options], where options are:"
    opt :application, "Show events only for the specified application", :type => :string
    opt :environment, "Show events associated with the specified environment name or id", :type => :string
    opt :directory, "Output log files to the directory specified.", :type => :string
  end
end