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, vm_extensions, stemcell, env, compilation, deployment_model, instance_state, availability_zone, logger) ⇒ Instance

Returns a new instance of Instance.



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

def initialize(
  job_name,
  index,
  virtual_state,
  vm_type,
  vm_extensions,
  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
  @vm_extensions = vm_extensions
  @stemcell = stemcell
  @env = env
  @compilation = compilation

  @configuration_hash = nil
  @template_hashes = nil
  @vm = nil

  # This state is coming from the agent, we
  # only need networks and job_state from it.
  @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.



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

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

#existing_network_reservationsObject (readonly)

Returns the value of attribute existing_network_reservations.



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

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



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

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.vm_extensions,
    job.stemcell,
    job.env,
    job.compilation?,
    deployment_model,
    instance_state,
    availability_zone,
    logger
  )
end

Instance Method Details

#agent_clientObject



193
194
195
# File 'lib/bosh/director/deployment_plan/instance.rb', line 193

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

#apply_initial_vm_state(spec) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/bosh/director/deployment_plan/instance.rb', line 166

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



158
159
160
161
162
163
164
# File 'lib/bosh/director/deployment_plan/instance.rb', line 158

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



245
246
247
248
# File 'lib/bosh/director/deployment_plan/instance.rb', line 245

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

#availability_zone_nameObject



294
295
296
297
298
# File 'lib/bosh/director/deployment_plan/instance.rb', line 294

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



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

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



154
155
156
# File 'lib/bosh/director/deployment_plan/instance.rb', line 154

def bind_existing_reservations(reservations)
  @existing_network_reservations = reservations
end

#bind_new_instance_modelObject



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/bosh/director/deployment_plan/instance.rb', line 113

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,
      availability_zone: availability_zone_name,
      bootstrap: false
    })
  @uuid = @model.uuid
end

#bootstrap?Boolean

Returns:

  • (Boolean)


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

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

#cloud_propertiesObject



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/bosh/director/deployment_plan/instance.rb', line 278

def cloud_properties
  merged_cloud_properties = nil

  if !@availability_zone.nil?
    merged_cloud_properties = merge_cloud_properties(merged_cloud_properties, @availability_zone.cloud_properties)
  end

  merged_cloud_properties = merge_cloud_properties(merged_cloud_properties, vm_type.cloud_properties)

  Array(vm_extensions).each do |vm_extension|
    merged_cloud_properties = merge_cloud_properties(merged_cloud_properties, vm_extension.cloud_properties)
  end

  merged_cloud_properties
end

#cloud_properties_changed?Boolean

Returns:

  • (Boolean)


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

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)


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

def compilation?
  @compilation
end

#current_job_specObject



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

def current_job_spec
  @model.spec_p('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_networksObject



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

def current_networks
  @current_state['networks']
end

#current_packagesObject



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

def current_packages
  @model.spec_p('packages')
end

#deployment_modelObject



143
144
145
# File 'lib/bosh/director/deployment_plan/instance.rb', line 143

def deployment_model
  @deployment_model
end

#dirty?Boolean

Returns:

  • (Boolean)


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

def dirty?
  !@model.update_completed
end

#dns_record_name(hostname, network_name) ⇒ String

Returns dns record name.

Returns:

  • (String)

    dns record name



199
200
201
# File 'lib/bosh/director/deployment_plan/instance.rb', line 199

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



109
110
111
# File 'lib/bosh/director/deployment_plan/instance.rb', line 109

def ensure_model_bound
  @model ||= find_or_create_model
end

#envObject



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

def env
  @env.spec
end

#job_nameObject



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

def job_name
  @job_name
end

#mark_as_bootstrapObject



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

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

#stateObject



250
251
252
253
254
255
256
257
258
259
# File 'lib/bosh/director/deployment_plan/instance.rb', line 250

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

#stemcellObject



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

def stemcell
  @stemcell
end

#to_sObject



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

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



267
268
269
270
271
272
# File 'lib/bosh/director/deployment_plan/instance.rb', line 267

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



241
242
243
# File 'lib/bosh/director/deployment_plan/instance.rb', line 241

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

#update_cloud_properties!Object



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

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

#update_descriptionObject



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

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

#update_stateObject



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

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

#update_templates(templates) ⇒ Object



300
301
302
303
304
305
306
307
308
# File 'lib/bosh/director/deployment_plan/instance.rb', line 300

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



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

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)


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

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

#vm_extensionsObject



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

def vm_extensions
  @vm_extensions
end

#vm_typeObject



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

def vm_type
  @vm_type
end