Module: Nucleus::Adapters::V1::CloudFoundryV2::Logs

Included in:
Nucleus::Adapters::V1::CloudFoundryV2
Defined in:
lib/nucleus/adapters/v1/cloud_foundry_v2/logs.rb

Defined Under Namespace

Classes: Envelope, Message

Constant Summary collapse

LOGGREGATOR_TYPES =
[Enums::ApplicationLogfileType::API,
Enums::ApplicationLogfileType::APPLICATION,
Enums::ApplicationLogfileType::REQUEST,
Enums::ApplicationLogfileType::SYSTEM]
CRLF =

Carriage return (newline in Mac OS) + line feed (newline in Unix) == CRLF (newline in Windows)

"\r\n"
WSP =
"\s"

Instance Method Summary collapse

Instance Method Details

#log?(application_name_or_id, log_id) ⇒ Boolean

Returns:

  • (Boolean)

See Also:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/nucleus/adapters/v1/cloud_foundry_v2/logs.rb', line 52

def log?(application_name_or_id, log_id)
  app_guid = app_guid(application_name_or_id)
  # test file existence
  log_id = 'staging_task.log' if log_id.to_sym == Enums::ApplicationLogfileType::BUILD
  # checks also if application is even valid
  response = get("/v2/apps/#{app_guid}/instances/0/files/logs/#{log_id}",
                 follow_redirects: false, expects: [200, 302, 400])
  return true if response == 200 || log_stream?(log_id)
  return false if response == 400
  # if 302 (only remaining option), followup...

  # download log file
  download_file(app_guid, "logs/#{log_id}")
  # no error, file exists
  true
rescue Errors::AdapterResourceNotFoundError, Errors::UnknownAdapterCallError,
       Excon::Errors::NotFound, Excon::Errors::BadRequest
  false
end

#log_entries(application_name_or_id, log_id) ⇒ Object

See Also:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/nucleus/adapters/v1/cloud_foundry_v2/logs.rb', line 80

def log_entries(application_name_or_id, log_id)
  app_guid = app_guid(application_name_or_id)
  # first check if this log is a file or must be fetched from the loggregator
  if log_stream?(log_id)
    # fetch recent data from loggregator and return an array of log entries
    recent_decoded = recent_log_messages(app_guid, loggregator_filter(log_id))
    recent_decoded.collect { |log_msg| construct_log_entry(log_msg) }
  elsif log_id.to_sym == Enums::ApplicationLogfileType::BUILD
    # handle special staging log
    build_log_entries(app_guid)
  else
    download_logfile_entries(app_guid, log_id)
  end
end

#logs(application_name_or_id) ⇒ Object

See Also:



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
# File 'lib/nucleus/adapters/v1/cloud_foundry_v2/logs.rb', line 15

def logs(application_name_or_id)
  app_guid = app_guid(application_name_or_id)
  # retrieve app for timestamps only :/
  app_created = get("/v2/apps/#{app_guid}").body[:metadata][:created_at]
  logs = []

  begin
    log_files_list = download_file(app_guid, 'logs')
    # parse raw response to array
    log_files_list.split(CRLF).each do |logfile_line|
      filename = logfile_line.rpartition(' ').first.strip
      if filename == 'staging_task.log'
        filename = 'build'
        log_type = Enums::ApplicationLogfileType::BUILD
      else
        log_type = Enums::ApplicationLogfileType::OTHER
      end
      # TODO: right now, we always assume the log has recently been updated
      logs.push(id: filename, name: filename, type: log_type, created_at: app_created,
                updated_at: Time.now.utc.iso8601)
    end
  rescue Errors::AdapterError
    log.debug('no logs directory found for cf application')
  end

  # add the default logtypes, available according to:
  # http://docs.cloudfoundry.org/devguide/deploy-apps/streaming-logs.html#format
  LOGGREGATOR_TYPES.each do |type|
    logs.push(id: type, name: type, type: type, created_at: app_created, updated_at: Time.now.utc.iso8601)
  end
  # TODO: 'all' is probably not perfect, since the build log wont be included
  logs.push(id: 'all', name: 'all', type: Enums::ApplicationLogfileType::OTHER,
            created_at: app_created, updated_at: Time.now.utc.iso8601)
  logs
end

#tail(application_name_or_id, log_id, stream) ⇒ Object

See Also:



73
74
75
76
77
# File 'lib/nucleus/adapters/v1/cloud_foundry_v2/logs.rb', line 73

def tail(application_name_or_id, log_id, stream)
  app_guid = app_guid(application_name_or_id)
  return tail_stream(app_guid, log_id, stream) if log_stream?(log_id)
  tail_file(app_guid, log_id, stream)
end