Class: Bosh::Director::Jobs::FetchLogs

Inherits:
BaseJob show all
Includes:
LockHelper
Defined in:
lib/bosh/director/jobs/fetch_logs.rb

Constant Summary collapse

DEFAULT_BUNDLE_LIFETIME =

10 days

86400 * 10

Instance Attribute Summary collapse

Attributes inherited from BaseJob

#task_id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LockHelper

#with_compile_lock, #with_deployment_lock, #with_release_lock, #with_release_locks, #with_stemcell_lock

Methods inherited from BaseJob

#begin_stage, #event_log, #logger, perform, #result_file, #single_step_stage, #task_cancelled?, #task_checkpoint, #track_and_log

Constructor Details

#initialize(instance_id, options = {}) ⇒ FetchLogs

Returns a new instance of FetchLogs.



18
19
20
21
22
23
24
25
# File 'lib/bosh/director/jobs/fetch_logs.rb', line 18

def initialize(instance_id, options = {})
  @blobstore = options.fetch(:blobstore) { App.instance.blobstores.blobstore }
  @instance_id = instance_id
  @log_type = options["type"] || "job"
  @filters = options["filters"]
  @instance_manager = Api::InstanceManager.new
  @bundle_lifetime = DEFAULT_BUNDLE_LIFETIME
end

Instance Attribute Details

#bundle_lifetimeObject

Returns the value of attribute bundle_lifetime.



12
13
14
# File 'lib/bosh/director/jobs/fetch_logs.rb', line 12

def bundle_lifetime
  @bundle_lifetime
end

Class Method Details

.job_typeObject



14
15
16
# File 'lib/bosh/director/jobs/fetch_logs.rb', line 14

def self.job_type
  :fetch_logs
end

Instance Method Details

#cleanup_old_bundlesObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/bosh/director/jobs/fetch_logs.rb', line 68

def cleanup_old_bundles
  old_bundles = Models::LogBundle.filter("timestamp <= ?",
                                         Time.now - bundle_lifetime)
  count = old_bundles.count

  if count == 0
    logger.info("No old bundles to delete")
    return
  end

  logger.info("Deleting #{count} old log bundle#{count > 1 ? "s" : ""}")

  old_bundles.each do |bundle|
    begin
      logger.info("Deleting log bundle #{bundle.id}: " +
                  "#{bundle.blobstore_id}")
      @blobstore.delete(bundle.blobstore_id)
      bundle.delete
    rescue Bosh::Blobstore::BlobstoreError => e
      logger.warn("Could not delete #{bundle.blobstore_id}: #{e}")
      # Assuming object has been deleted from blobstore by someone else,
      # cleaning up DB record accordingly
      if e.kind_of?(Bosh::Blobstore::NotFound)
        bundle.delete
      end
    end
  end
end

#performObject



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
# File 'lib/bosh/director/jobs/fetch_logs.rb', line 27

def perform
  instance = @instance_manager.find_instance(@instance_id)
  desc = "#{instance.job}/#{instance.index}"

  deployment = instance.deployment

  if deployment.nil?
    raise InstanceDeploymentMissing,
          "`#{desc}' doesn't belong to any deployment"
  end

  with_deployment_lock(deployment) do
    cleanup_old_bundles

    event_log.begin_stage("Fetching logs for #{desc}", 1)

    agent = @instance_manager.agent_client_for(instance)
    blobstore_id = nil

    track_and_log("Finding and packing log files") do
      logger.info("Fetching logs from agent: " +
                   "log_type='#{@log_type}', filters='#{@filters}'")

      task = agent.fetch_logs(@log_type, @filters)
      blobstore_id = task["blobstore_id"]
    end

    if blobstore_id.nil?
      raise AgentTaskNoBlobstoreId,
            "Agent didn't return a blobstore object id for packaged logs"
    end

    Models::LogBundle.create(:blobstore_id => blobstore_id,
                             :timestamp => Time.now)

    # The returned value of this method is used as task result
    # and gets extracted by CLI as a tarball blobstore id
    blobstore_id
  end
end