class Bosh::Director::DeploymentPlan::InstancePlan

Attributes

desired_instance[R]
existing_instance[R]
instance[R]
network_plans[RW]
recreate_deployment[R]
skip_drain[R]

Public Class Methods

new(attrs) click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 5
def initialize(attrs)
  @existing_instance = attrs.fetch(:existing_instance)
  @desired_instance = attrs.fetch(:desired_instance)
  @instance = attrs.fetch(:instance)
  @network_plans = attrs.fetch(:network_plans, [])
  @skip_drain = attrs.fetch(:skip_drain, false)
  @recreate_deployment = attrs.fetch(:recreate_deployment, false)
  @logger = attrs.fetch(:logger, Config.logger)
  @dns_manager = DnsManagerProvider.create
end

Public Instance Methods

already_detached?() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 252
def already_detached?
  return false if new?

  @existing_instance.state == 'detached'
end
changed?() click to toggle source

@return [Boolean] returns true if the any of the expected specifications

differ from the ones provided by the VM
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 23
def changed?
  !changes.empty?
end
changes() click to toggle source

@return [Set<Symbol>] returns a set of all of the specification differences

# File lib/bosh/director/deployment_plan/instance_plan.rb, line 29
def changes
  return @changes if @changes

  @changes = Set.new
  @changes << :dirty if @instance.dirty?
  @changes << :restart if needs_restart?
  @changes << :recreate if needs_recreate?
  @changes << :cloud_properties if instance.cloud_properties_changed?
  @changes << :stemcell if stemcell_changed?
  @changes << :env if env_changed?
  @changes << :network if networks_changed?
  @changes << :packages if packages_changed?
  @changes << :persistent_disk if persistent_disk_changed?
  @changes << :configuration if configuration_changed?
  @changes << :job if job_changed?
  @changes << :state if state_changed?
  @changes << :dns if dns_changed?
  @changes << :trusted_certs if instance.trusted_certs_changed?
  @changes
end
configuration_changed?() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 140
def configuration_changed?
  changed = instance.configuration_hash != instance_model.spec_p('configuration_hash')
  log_changes(__method__, instance_model.spec_p('configuration_hash'), instance.configuration_hash, instance) if changed
  changed
end
desired_az_name() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 213
def desired_az_name
  @desired_instance.az ? @desired_instance.az.name : nil
end
dns_changed?() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 130
def dns_changed?
  return false unless @dns_manager.dns_enabled?

  network_settings.dns_record_info.any? do |name, ip|
    not_found = @dns_manager.find_dns_record(name, ip).nil?
    @logger.debug("#{__method__} The requested dns record with name '#{name}' and ip '#{ip}' was not found in the db.") if not_found
    not_found
  end
end
existing?() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 166
def existing?
  !new? && !obsolete?
end
find_existing_reservation_for_network(network) click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 209
def find_existing_reservation_for_network(network)
  @instance.existing_network_reservations.find_for_network(network)
end
instance_model() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 67
def instance_model
  new? ? instance.model : existing_instance
end
job_changed?() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 231
def job_changed?
  job = @desired_instance.job
  return true if @instance.current_job_spec.nil?

  # The agent job spec could be in legacy form.  job_spec cannot be,
  # though, because we got it from the spec function in job.rb which
  # automatically makes it non-legacy.
  converted_current = InstanceGroup.convert_from_legacy_spec(@instance.current_job_spec)
  changed = job.spec != converted_current
  log_changes(__method__, converted_current, job.spec, @instance) if changed
  changed
end
mark_desired_network_plans_as_existing() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 146
def mark_desired_network_plans_as_existing
  network_plans.select(&:desired?).each { |network_plan| network_plan.existing = true }
end
needs_disk?() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 258
def needs_disk?
  job = @desired_instance.job

  job && job.persistent_disk_type && job.persistent_disk_type.disk_size > 0
end
needs_recreate?() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 79
def needs_recreate?
  if @recreate_deployment
    @logger.debug("#{__method__} job deployment is configured with \"recreate\" state")
    true
  else
    @instance.virtual_state == 'recreate'
  end
end
needs_restart?() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 75
def needs_restart?
  @instance.virtual_state == 'restart'
end
needs_shutting_down?() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 200
def needs_shutting_down?
  return true if obsolete?

  instance.cloud_properties_changed? ||
    stemcell_changed? ||
    env_changed? ||
    needs_recreate?
end
network_address(network_name) click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 192
def network_address(network_name)
  network_settings.network_address(network_name)
end
network_addresses() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 196
def network_addresses
  network_settings.network_addresses
end
network_plan_for_network(network) click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 217
def network_plan_for_network(network)
  @network_plans.find { |plan| plan.reservation.network == network }
end
network_settings() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 170
def network_settings
  desired_reservations = network_plans
                           .reject(&:obsolete?)
                           .map { |network_plan| network_plan.reservation }

  DeploymentPlan::NetworkSettings.new(
    @instance.job_name,
    @instance.model.deployment.name,
    @desired_instance.job.default_network,
    desired_reservations,
    @instance.current_networks,
    @instance.availability_zone,
    @instance.index,
    @instance.uuid,
    @dns_manager
  )
end
network_settings_hash() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 188
def network_settings_hash
  network_settings.to_hash
end
networks_changed?() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 88
def networks_changed?
  desired_network_plans = network_plans.select(&:desired?)
  obsolete_network_plans = network_plans.select(&:obsolete?)

  old_network_settings = new? ? {} : @existing_instance.spec_p('networks')
  new_network_settings = network_settings.to_hash

  changed = false
  if obsolete_network_plans.any?
    @logger.debug("#{__method__} obsolete reservations: [#{obsolete_network_plans.map(&:reservation).map(&:to_s).join(", ")}]")
    changed = true
  end

  if desired_network_plans.any?
    @logger.debug("#{__method__} desired reservations: [#{desired_network_plans.map(&:reservation).map(&:to_s).join(", ")}]")
    changed = true
  end

  if network_settings_changed?(old_network_settings, new_network_settings)
    @logger.debug("#{__method__} network settings changed FROM: #{old_network_settings} TO: #{new_network_settings} on instance #{@existing_instance}")
    changed = true
  end

  changed
end
new?() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 162
def new?
  existing_instance.nil?
end
obsolete?() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 158
def obsolete?
  desired_instance.nil?
end
packages_changed?() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 244
def packages_changed?
  job = @desired_instance.job

  changed = job.package_spec != @instance.current_packages
  log_changes(__method__, @instance.current_packages, job.package_spec, @instance) if changed
  changed
end
persist_current_spec() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 264
def persist_current_spec
  instance_model.update(spec: spec.full_spec)
end
persistent_disk_changed?() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 50
def persistent_disk_changed?
  if @existing_instance && obsolete?
    return !@existing_instance.persistent_disk.nil?
  end

  job = @desired_instance.job
  new_disk_size = job.persistent_disk_type ? job.persistent_disk_type.disk_size : 0
  new_disk_cloud_properties = job.persistent_disk_type ? job.persistent_disk_type.cloud_properties : {}
  changed = new_disk_size != disk_size
  log_changes(__method__, "disk size: #{disk_size}", "disk size: #{new_disk_size}", @existing_instance) if changed
  return true if changed

  changed = new_disk_size != 0 && new_disk_cloud_properties != disk_cloud_properties
  log_changes(__method__, disk_cloud_properties, new_disk_cloud_properties, @existing_instance) if changed
  changed
end
release_all_network_plans() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 154
def release_all_network_plans
  network_plans.clear
end
release_obsolete_network_plans() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 150
def release_obsolete_network_plans
  network_plans.delete_if(&:obsolete?)
end
should_be_ignored?() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 71
def should_be_ignored?
   !instance_model.nil? && instance_model.ignore
end
spec() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 221
def spec
  return InstanceSpec.create_empty if obsolete?

  InstanceSpec.create_from_instance_plan(self)
end
state_changed?() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 114
def state_changed?
  if instance.state == 'detached' &&
    existing_instance.state != instance.state
    @logger.debug("Instance '#{instance}' needs to be detached")
    return true
  end

  if instance.state == 'stopped' && instance.current_job_state == 'running' ||
    instance.state == 'started' && instance.current_job_state != 'running'
    @logger.debug("Instance state is '#{instance.state}' and agent reports '#{instance.current_job_state}'")
    return true
  end

  false
end
templates() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 227
def templates
  @desired_instance.job.templates
end

Private Instance Methods

disk_cloud_properties() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 315
def disk_cloud_properties
  if @instance.model.nil?
    raise DirectorError, "Instance '#{@instance}' model is not bound"
  end

  if @instance.model.persistent_disk
    @instance.model.persistent_disk.cloud_properties
  else
    {}
  end
end
disk_size() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 303
def disk_size
  if @instance.model.nil?
    raise DirectorError, "Instance '#{@instance}' model is not bound"
  end

  if @instance.model.persistent_disk
    @instance.model.persistent_disk.size
  else
    0
  end
end
env_changed?() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 275
def env_changed?
  job = @desired_instance.job

  if @existing_instance && @existing_instance.vm_env && job.env.spec != @existing_instance.vm_env
    log_changes(__method__, @existing_instance.vm_env, job.env.spec, @existing_instance)
    return true
  end
  false
end
log_changes(method_sym, old_state, new_state, instance) click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 299
def log_changes(method_sym, old_state, new_state, instance)
  @logger.debug("#{method_sym} changed FROM: #{old_state} TO: #{new_state} on instance #{instance}")
end
network_settings_changed?(old_network_settings, new_network_settings) click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 270
def network_settings_changed?(old_network_settings, new_network_settings)
  return false if old_network_settings == {}
  old_network_settings != new_network_settings
end
stemcell_changed?() click to toggle source
# File lib/bosh/director/deployment_plan/instance_plan.rb, line 285
def stemcell_changed?
  if @existing_instance && @instance.stemcell.name != @existing_instance.spec_p('stemcell.name')
    log_changes(__method__, @existing_instance.spec_p('stemcell.name'), @instance.stemcell.name, @existing_instance)
    return true
  end

  if @existing_instance && @instance.stemcell.version != @existing_instance.spec_p('stemcell.version')
    log_changes(__method__, "version: #{@existing_instance.spec_p('stemcell.version')}", "version: #{@instance.stemcell.version}", @existing_instance)
    return true
  end

  false
end