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::DEFAULT_DIRECTOR_PORT

Instance Attribute Summary

Attributes inherited from Base

#args, #options, #out, #runner, #work_dir

Instance Method Summary collapse

Methods included from DeploymentHelper

#deployment_changed?, #inspect_deployment_changes, #prepare_deployment_manifest

Methods included from VersionCalc

#major_version, #minor_version, #version_cmp, #version_greater, #version_less, #version_same

Methods inherited from Base

#add_option, #blob_manager, #blobstore, #cache, #config, #confirmed?, #deployment, #director, #exit_code, #initialize, #interactive?, #logged_in?, #non_interactive?, #password, #redirect, #release, #remove_option, #target, #target_name, #task_report, #username, #verbose?

Methods included from Bosh::Cli::CommandDiscovery

#desc, #method_added, #option, #register_command, #usage

Constructor Details

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

Instance Method Details

#fetch_logs(job, index) ⇒ Object



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
# File 'lib/cli/commands/log_management.rb', line 16

def fetch_logs(job, index)
  auth_required
  target_required
  no_track_unsupported

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

  if options[:agent]
    if options[:job]
      err("You can't use --job and --agent together")
    end
    log_type = "agent"
  else
    log_type = "job"
  end

  if options[:only]
    if options[:all]
      err("You can't use --only and --all together")
    end
    filters = options[:only].join(",")
  elsif options[:all]
    filters = "all"
  else
    filters = nil
  end

  if options[:agent] && filters && filters != "all"
    err("Custom filtering is not supported for agent logs")
  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