Class: Bosh::Director::ProblemHandlers::Base

Inherits:
Object
  • Object
show all
Includes:
CloudcheckHelper
Defined in:
lib/bosh/director/problem_handlers/base.rb

Constant Summary

Constants included from CloudcheckHelper

CloudcheckHelper::DEFAULT_AGENT_TIMEOUT

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CloudcheckHelper

#delete_vm, #delete_vm_from_cloud, #delete_vm_reference, #reboot_vm, #recreate_vm, #recreate_vm_skip_post_start

Constructor Details

#initialize(resource_id, data) ⇒ Base

Problem state is described by constructor parameters. Problem handler can reach out to check if the problem is still present and attempt to fix it by applying a potential resolution tagged with one or more labels.



39
40
41
42
43
# File 'lib/bosh/director/problem_handlers/base.rb', line 39

def initialize(resource_id, data)
  @logger = Config.logger
  @event_log = Config.event_log
  @job = nil
end

Class Attribute Details

.handlersObject

Returns the value of attribute handlers.



92
93
94
# File 'lib/bosh/director/problem_handlers/base.rb', line 92

def handlers
  @handlers
end

.resolutionsObject (readonly)

Returns the value of attribute resolutions.



102
103
104
# File 'lib/bosh/director/problem_handlers/base.rb', line 102

def resolutions
  @resolutions
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/bosh/director/problem_handlers/base.rb', line 9

def data
  @data
end

#jobObject

so we can checkpoint task



10
11
12
# File 'lib/bosh/director/problem_handlers/base.rb', line 10

def job
  @job
end

Class Method Details

.action(&block) ⇒ Object



130
131
132
# File 'lib/bosh/director/problem_handlers/base.rb', line 130

def self.action(&block)
  @actions[@pending_name.to_s] = block
end

.action_for(resolution) ⇒ Object



122
123
124
# File 'lib/bosh/director/problem_handlers/base.rb', line 122

def self.action_for(resolution)
  @actions[resolution.to_s]
end

.auto_resolution(name) ⇒ Object



138
139
140
# File 'lib/bosh/director/problem_handlers/base.rb', line 138

def self.auto_resolution(name)
  @auto_resolution = name
end

.create_by_type(type, resource_id, data) ⇒ Object

create_by_type might not be able to initialize problem handler if some part of its state or dependencies is invalid. In this case it just substitutes a generic “invalid_problem” handler that reports the fact that the original problem is invalid and offers closing it as the only solution.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bosh/director/problem_handlers/base.rb', line 23

def self.create_by_type(type, resource_id, data)
  handler_class = Base.handlers[type.to_s]
  if handler_class.nil?
    raise "Cannot find handler for '#{type}' problem"
  end

  handler_class.new(resource_id, data)
rescue ProblemHandlerError => e
  create_by_type(:invalid_problem, resource_id,
                 {"error" => e, "original_type" => type.to_s})
end

.create_from_model(model) ⇒ Object



12
13
14
# File 'lib/bosh/director/problem_handlers/base.rb', line 12

def self.create_from_model(model)
  create_by_type(model.type, model.resource_id, model.data)
end

.get_auto_resolutionObject



134
135
136
# File 'lib/bosh/director/problem_handlers/base.rb', line 134

def self.get_auto_resolution
  @auto_resolution
end

.inherited(base) ⇒ Object



114
115
116
# File 'lib/bosh/director/problem_handlers/base.rb', line 114

def self.inherited(base)
  base.class_eval { init_dsl_data }
end

.init_dsl_dataObject



105
106
107
108
109
110
# File 'lib/bosh/director/problem_handlers/base.rb', line 105

def self.init_dsl_data
  @resolutions = []
  @plans = {}
  @actions = {}
  @auto_resolution = nil
end

.plan(&block) ⇒ Object



126
127
128
# File 'lib/bosh/director/problem_handlers/base.rb', line 126

def self.plan(&block)
  @plans[@pending_name.to_s] = block
end

.plan_for(resolution) ⇒ Object



118
119
120
# File 'lib/bosh/director/problem_handlers/base.rb', line 118

def self.plan_for(resolution)
  @plans[resolution.to_s]
end

.register_as(type) ⇒ Object



95
96
97
98
# File 'lib/bosh/director/problem_handlers/base.rb', line 95

def self.register_as(type)
  Base.handlers ||= {}
  Base.handlers[type.to_s] = self
end

.resolution(name, &block) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/bosh/director/problem_handlers/base.rb', line 142

def self.resolution(name, &block)
  @resolutions << name
  @pending_name = name
  instance_eval(&block)
ensure
  @pending_name = nil
end

Instance Method Details

#apply_resolution(resolution) ⇒ Object

Parameters:

  • resolution

    desired resolution



78
79
80
81
82
83
84
# File 'lib/bosh/director/problem_handlers/base.rb', line 78

def apply_resolution(resolution)
  action = self.class.action_for(resolution)
  if action.nil?
    handler_error("Cannot find '#{resolution}' resolution for '#{self.class}'")
  end
  instance_eval(&action)
end

#auto_resolutionObject



73
74
75
# File 'lib/bosh/director/problem_handlers/base.rb', line 73

def auto_resolution
  self.class.get_auto_resolution
end

#auto_resolveObject



86
87
88
# File 'lib/bosh/director/problem_handlers/base.rb', line 86

def auto_resolve
  apply_resolution(auto_resolution)
end

#checkpointObject



45
46
47
# File 'lib/bosh/director/problem_handlers/base.rb', line 45

def checkpoint
  @job.task_checkpoint if @job
end

#cloudObject

Talking to cloud should only be possible in the context of DJ job



51
52
53
54
55
56
# File 'lib/bosh/director/problem_handlers/base.rb', line 51

def cloud
  if @job.nil?
    handler_error("Cannot talk to cloud outside of job context")
  end
  super
end

#descriptionObject

Problem description



59
# File 'lib/bosh/director/problem_handlers/base.rb', line 59

def description; end

#resolution_plan(resolution) ⇒ Object



67
68
69
70
71
# File 'lib/bosh/director/problem_handlers/base.rb', line 67

def resolution_plan(resolution)
  plan = self.class.plan_for(resolution)
  return nil if plan.nil?
  instance_eval(&plan)
end

#resolutionsObject



61
62
63
64
65
# File 'lib/bosh/director/problem_handlers/base.rb', line 61

def resolutions
  self.class.resolutions.map do |name|
    { :name => name.to_s, :plan => resolution_plan(name) }
  end
end