Class: Bosh::Director::DeploymentPlan::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/director/deployment_plan/instance.rb

Overview

Represents a single job instance.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_name, index, virtual_state, vm_type, stemcell, env, compilation, deployment_model, instance_state, availability_zone, logger) ⇒ Instance

Returns a new instance of Instance.



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
79
80
81
82
83
# File 'lib/bosh/director/deployment_plan/instance.rb', line 50

def initialize(
  job_name,
  index,
  virtual_state,
  vm_type,
  stemcell,
  env,
  compilation,
  deployment_model,
  instance_state,
  availability_zone,
  logger
)
  @index = index
  @availability_zone = availability_zone
  @logger = logger
  @deployment_model = deployment_model
  @job_name = job_name
  @vm_type = vm_type
  @stemcell = stemcell
  @env = env
  @compilation = compilation

  @configuration_hash = nil
  @template_hashes = nil
  @vm = nil
  @current_state = instance_state || {}

  # reservation generated from current state/DB
  @existing_network_reservations = InstanceNetworkReservations.new(logger)
  @dns_manager = DnsManagerProvider.create

  @virtual_state = virtual_state
end

Instance Attribute Details

#availability_zoneObject (readonly)

Returns the value of attribute availability_zone.



30
31
32
# File 'lib/bosh/director/deployment_plan/instance.rb', line 30

def availability_zone
  @availability_zone
end

#configuration_hashString

Returns Checksum all of the configuration templates.

Returns:

  • (String)

    Checksum all of the configuration templates



17
18
19
# File 'lib/bosh/director/deployment_plan/instance.rb', line 17

def configuration_hash
  @configuration_hash
end

#current_stateObject (readonly)

Returns the value of attribute current_state.



28
29
30
# File 'lib/bosh/director/deployment_plan/instance.rb', line 28

def current_state
  @current_state
end

#existing_network_reservationsObject (readonly)

Returns the value of attribute existing_network_reservations.



32
33
34
# File 'lib/bosh/director/deployment_plan/instance.rb', line 32

def existing_network_reservations
  @existing_network_reservations
end

#indexInteger (readonly)

Returns Instance index.

Returns:

  • (Integer)

    Instance index



9
10
11
# File 'lib/bosh/director/deployment_plan/instance.rb', line 9

def index
  @index
end

#modelModels::Instance (readonly)

Returns Instance model.

Returns:



14
15
16
# File 'lib/bosh/director/deployment_plan/instance.rb', line 14

def model
  @model
end

#rendered_templates_archiveBosh::Director::Core::Templates::RenderedTemplatesArchive

Returns:

  • (Bosh::Director::Core::Templates::RenderedTemplatesArchive)


23
24
25
# File 'lib/bosh/director/deployment_plan/instance.rb', line 23

def rendered_templates_archive
  @rendered_templates_archive
end

#template_hashesHash

Returns A hash of template SHA1 hashes.

Returns:

  • (Hash)

    A hash of template SHA1 hashes



20
21
22
# File 'lib/bosh/director/deployment_plan/instance.rb', line 20

def template_hashes
  @template_hashes
end

#uuidObject (readonly)

Returns the value of attribute uuid.



11
12
13
# File 'lib/bosh/director/deployment_plan/instance.rb', line 11

def uuid
  @uuid
end

#virtual_stateString (readonly)

Returns job state.

Returns:

  • (String)

    job state



26
27
28
# File 'lib/bosh/director/deployment_plan/instance.rb', line 26

def virtual_state
  @virtual_state
end

Class Method Details

.create_from_job(job, index, virtual_state, deployment_model, instance_state, availability_zone, logger) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bosh/director/deployment_plan/instance.rb', line 34

def self.create_from_job(job, index, virtual_state, deployment_model, instance_state, availability_zone, logger)
  new(
    job.name,
    index,
    virtual_state,
    job.vm_type,
    job.stemcell,
    job.env,
    job.compilation?,
    deployment_model,
    instance_state,
    availability_zone,
    logger
  )
end

Instance Method Details

#agent_clientObject



184
185
186
# File 'lib/bosh/director/deployment_plan/instance.rb', line 184

def agent_client
  AgentClient.with_vm_credentials_and_agent_id(@model.credentials, @model.agent_id)
end

#apply_initial_vm_state(spec) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/bosh/director/deployment_plan/instance.rb', line 157

def apply_initial_vm_state(spec)
  # Agent will return dynamic network settings, we need to update spec with it
  # so that we can render templates with new spec later.
  agent_spec_keys = ['networks', 'deployment', 'job', 'index', 'id']
  agent_partial_state = spec.as_apply_spec.select { |k, _| agent_spec_keys.include?(k) }
  agent_client.apply(agent_partial_state)

  instance_spec_keys = agent_spec_keys + ['stemcell', 'vm_type', 'env']
  instance_partial_state = spec.full_spec.select { |k, _| instance_spec_keys.include?(k) }
  @current_state.merge!(instance_partial_state)

  agent_state = agent_client.get_state
  unless agent_state.nil?
    @current_state['networks'] = agent_state['networks']
    @model.update(spec: @current_state)
  end
end

#apply_vm_state(spec) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/bosh/director/deployment_plan/instance.rb', line 149

def apply_vm_state(spec)
  @logger.info('Applying VM state')

  @current_state = spec.full_spec
  agent_client.apply(spec.as_apply_spec)
  @model.update(spec: @current_state)
end

#assign_availability_zone(availability_zone) ⇒ Object



237
238
239
240
# File 'lib/bosh/director/deployment_plan/instance.rb', line 237

def assign_availability_zone(availability_zone)
  @availability_zone = availability_zone
  @model.update(availability_zone: availability_zone_name)
end

#availability_zone_nameObject



278
279
280
281
282
# File 'lib/bosh/director/deployment_plan/instance.rb', line 278

def availability_zone_name
  return nil if @availability_zone.nil?

  @availability_zone.name
end

#bind_existing_instance_model(existing_instance_model) ⇒ Object

Updates this domain object to reflect an existing instance running on an existing vm



139
140
141
142
143
# File 'lib/bosh/director/deployment_plan/instance.rb', line 139

def bind_existing_instance_model(existing_instance_model)
  @uuid = existing_instance_model.uuid
  check_model_not_bound
  @model = existing_instance_model
end

#bind_existing_reservations(reservations) ⇒ Object



145
146
147
# File 'lib/bosh/director/deployment_plan/instance.rb', line 145

def bind_existing_reservations(reservations)
  @existing_network_reservations = reservations
end

#bind_new_instance_modelObject



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/bosh/director/deployment_plan/instance.rb', line 109

def bind_new_instance_model
  @model = Models::Instance.create({
      deployment_id: @deployment_model.id,
      job: @job_name,
      index: index,
      state: state,
      compilation: @compilation,
      uuid: SecureRandom.uuid,
      bootstrap: false
    })
  @uuid = @model.uuid
end

#bootstrap?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/bosh/director/deployment_plan/instance.rb', line 85

def bootstrap?
  @model && @model.bootstrap
end

#cloud_propertiesObject



270
271
272
273
274
275
276
# File 'lib/bosh/director/deployment_plan/instance.rb', line 270

def cloud_properties
  if @availability_zone.nil?
    vm_type.cloud_properties
  else
    @availability_zone.cloud_properties.merge(vm_type.cloud_properties)
  end
end

#cloud_properties_changed?Boolean

Returns:

  • (Boolean)


194
195
196
197
198
# File 'lib/bosh/director/deployment_plan/instance.rb', line 194

def cloud_properties_changed?
  changed = cloud_properties != @model.cloud_properties_hash
  log_changes(__method__, @model.cloud_properties_hash, cloud_properties) if changed
  changed
end

#compilation?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/bosh/director/deployment_plan/instance.rb', line 89

def compilation?
  @compilation
end

#configuration_changed?Boolean

Returns true if the expected configuration hash differs from the one provided by the VM

Returns:

  • (Boolean)

    returns true if the expected configuration hash differs from the one provided by the VM



203
204
205
206
207
# File 'lib/bosh/director/deployment_plan/instance.rb', line 203

def configuration_changed?
  changed = configuration_hash != @current_state['configuration_hash']
  log_changes(__method__, @current_state['configuration_hash'], configuration_hash) if changed
  changed
end

#current_job_specObject



209
210
211
# File 'lib/bosh/director/deployment_plan/instance.rb', line 209

def current_job_spec
  @current_state['job']
end

#current_job_stateObject



217
218
219
# File 'lib/bosh/director/deployment_plan/instance.rb', line 217

def current_job_state
  @current_state['job_state']
end

#current_packagesObject



213
214
215
# File 'lib/bosh/director/deployment_plan/instance.rb', line 213

def current_packages
  @current_state['packages']
end

#deployment_modelObject



134
135
136
# File 'lib/bosh/director/deployment_plan/instance.rb', line 134

def deployment_model
  @deployment_model
end

#dns_record_name(hostname, network_name) ⇒ String

Returns dns record name.

Returns:

  • (String)

    dns record name



190
191
192
# File 'lib/bosh/director/deployment_plan/instance.rb', line 190

def dns_record_name(hostname, network_name)
  [hostname, job.canonical_name, Canonicalizer.canonicalize(network_name), Canonicalizer.canonicalize(@deployment_model.name), @dns_manager.dns_domain_name].join('.')
end

#ensure_model_boundObject



105
106
107
# File 'lib/bosh/director/deployment_plan/instance.rb', line 105

def ensure_model_bound
  @model ||= find_or_create_model
end

#envObject



130
131
132
# File 'lib/bosh/director/deployment_plan/instance.rb', line 130

def env
  @env.spec
end

#job_nameObject



93
94
95
# File 'lib/bosh/director/deployment_plan/instance.rb', line 93

def job_name
  @job_name
end

#mark_as_bootstrapObject



229
230
231
# File 'lib/bosh/director/deployment_plan/instance.rb', line 229

def mark_as_bootstrap
  @model.update(bootstrap: true)
end

#stateObject



242
243
244
245
246
247
248
249
250
251
# File 'lib/bosh/director/deployment_plan/instance.rb', line 242

def state
  case @virtual_state
    when 'recreate'
      'started'
    when 'restart'
      'started'
    else
      @virtual_state
  end
end

#stemcellObject



126
127
128
# File 'lib/bosh/director/deployment_plan/instance.rb', line 126

def stemcell
  @stemcell
end

#to_sObject



97
98
99
100
101
102
103
# File 'lib/bosh/director/deployment_plan/instance.rb', line 97

def to_s
  if @uuid.nil?
    "#{@job_name}/#{@index}"
  else
    "#{@job_name}/#{@index} (#{@uuid})"
  end
end

#trusted_certs_changed?Boolean

Checks if the target VM already has the same set of trusted SSL certificates as the director currently wants to install on all managed VMs. This will differ for VMs that existed before the director’s configuration changed.

Returns:

  • (Boolean)

    true if the VM needs to be sent a new set of trusted certificates



259
260
261
262
263
264
# File 'lib/bosh/director/deployment_plan/instance.rb', line 259

def trusted_certs_changed?
  config_trusted_certs = Digest::SHA1.hexdigest(Bosh::Director::Config.trusted_certs)
  changed = config_trusted_certs != @model.trusted_certs_sha1
  log_changes(__method__, @model.trusted_certs_sha1, config_trusted_certs) if changed
  changed
end

#unmark_as_bootstrapObject



233
234
235
# File 'lib/bosh/director/deployment_plan/instance.rb', line 233

def unmark_as_bootstrap
  @model.update(bootstrap: false)
end

#update_cloud_properties!Object



180
181
182
# File 'lib/bosh/director/deployment_plan/instance.rb', line 180

def update_cloud_properties!
  @model.update(cloud_properties: JSON.dump(cloud_properties))
end

#update_descriptionObject



225
226
227
# File 'lib/bosh/director/deployment_plan/instance.rb', line 225

def update_description
  @model.update(job: job_name, index: index)
end

#update_stateObject



221
222
223
# File 'lib/bosh/director/deployment_plan/instance.rb', line 221

def update_state
  @model.update(state: state)
end

#update_templates(templates) ⇒ Object



284
285
286
287
288
289
290
291
292
# File 'lib/bosh/director/deployment_plan/instance.rb', line 284

def update_templates(templates)
  transactor = Transactor.new
  transactor.retryable_transaction(Bosh::Director::Config.db) do
    @model.remove_all_templates
    templates.map(&:model).each do |template_model|
      @model.add_template(template_model)
    end
  end
end

#update_trusted_certsObject



175
176
177
178
# File 'lib/bosh/director/deployment_plan/instance.rb', line 175

def update_trusted_certs
  agent_client.update_settings(Config.trusted_certs)
  @model.update(:trusted_certs_sha1 => Digest::SHA1.hexdigest(Config.trusted_certs))
end

#vm_created?Boolean

Returns:

  • (Boolean)


266
267
268
# File 'lib/bosh/director/deployment_plan/instance.rb', line 266

def vm_created?
  !@model.vm_cid.nil?
end

#vm_typeObject



122
123
124
# File 'lib/bosh/director/deployment_plan/instance.rb', line 122

def vm_type
  @vm_type
end