Class: Bosh::Cli::Command::LogManagement

Inherits:
Base show all
Includes:
DeploymentHelper
Defined in:
lib/cli/commands/log_management.rb

Constant Summary

Constants inherited from Base

Base::BLOBS_DIR, Base::BLOBS_INDEX_FILE

Instance Attribute Summary

Attributes inherited from Base

#cache, #config, #options, #out, #usage, #work_dir

Instance Method Summary collapse

Methods included from DeploymentHelper

#deployment_changed?, #inspect_deployment_changes, #prepare_deployment_manifest

Methods inherited from Base

#blob_manager, #blobstore, command, #confirmed?, #director, #dry_run?, #exit_code, #full_target_name, #initialize, #interactive?, #logged_in?, #non_interactive?, #redirect, #release, #run, #show_usage, #target_name, #target_version, #task_report, #verbose?

Constructor Details

This class inherits a constructor from Bosh::Cli::Command::Base

Instance Method Details

#fetch_logs(*args) ⇒ Object



7
8
9
10
11
12
13
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
# File 'lib/cli/commands/log_management.rb', line 7

def fetch_logs(*args)
  auth_required
  target_required

  job = args.shift
  index = args.shift
  filters = nil
  log_type = nil

  for_job = args.delete("--job")
  for_agent = args.delete("--agent")

  if for_job && for_agent
    err("Please specify which logs you want, job or agent")
  elsif for_agent
    log_type = "agent"
  else # default log type is 'job'
    log_type = "job"
  end

  if args.include?("--only")
    pos = args.index("--only")
    filters = args[pos+1]
    if filters.nil?
      err("Please provide a list of filters separated by comma")
    end
    args.delete("--only")
    args.delete(filters)
  elsif args.include?("--all")
    args.delete("--all")
    filters = "all"
  end

  if for_agent && !filters.nil? && filters != "all"
    err("Custom filtering is not supported for agent logs")
  end

  if index !~ /^\d+$/
    err("Job index is expected to be a positive integer")
  end

  if args.size > 0
    err("Unknown arguments: #{args.join(", ")}")
  end

  manifest = prepare_deployment_manifest

  resource_id = director.fetch_logs(manifest["name"], job, index,
                                    log_type, filters)

  if resource_id.nil?
    err("Error retrieving logs")
  end

  nl
  say("Downloading log bundle (#{resource_id.to_s.green})...")

  begin
    time = Time.now.strftime("%Y-%m-%d@%H-%M-%S")
    log_file = File.join(Dir.pwd, "#{job}.#{index}.#{time}.tgz")

    tmp_file = director.download_resource(resource_id)

    FileUtils.mv(tmp_file, log_file)
    say("Logs saved in `#{log_file.green}'")
  rescue Bosh::Cli::DirectorError => e
    err("Unable to download logs from director: #{e}")
  ensure
    FileUtils.rm_rf(tmp_file) if File.exists?(tmp_file)
  end

end