Class: Bosh::Director::DeploymentPlan::DatabaseIpRepo

Inherits:
Object
  • Object
show all
Includes:
IpUtil
Defined in:
lib/bosh/director/deployment_plan/ip_provider/database_ip_repo.rb

Defined Under Namespace

Classes: IpFoundInDatabaseAndCanBeRetried, NoMoreIPsAvailableAndStopRetrying

Instance Method Summary collapse

Methods included from IpUtil

#each_ip, #format_ip, #ip_to_i, #ip_to_netaddr

Constructor Details

#initialize(logger) ⇒ DatabaseIpRepo

Returns a new instance of DatabaseIpRepo.



7
8
9
# File 'lib/bosh/director/deployment_plan/ip_provider/database_ip_repo.rb', line 7

def initialize(logger)
  @logger = Bosh::Director::TaggedLogger.new(logger, 'network-configuration')
end

Instance Method Details

#add(reservation) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bosh/director/deployment_plan/ip_provider/database_ip_repo.rb', line 24

def add(reservation)
  cidr_ip = CIDRIP.new(reservation.ip)

  reservation_type = reservation.network.ip_type(cidr_ip)

  reserve_with_instance_validation(
    reservation.instance_model,
    cidr_ip,
    reservation,
    reservation_type.eql?(:static)
  )

  reservation.resolve_type(reservation_type)
  reservation.mark_reserved
  @logger.debug("Reserved ip '#{cidr_ip}' for #{reservation.network.name} as #{reservation_type}")
end

#allocate_dynamic_ip(reservation, subnet) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bosh/director/deployment_plan/ip_provider/database_ip_repo.rb', line 41

def allocate_dynamic_ip(reservation, subnet)
  begin
    ip_address = try_to_allocate_dynamic_ip(reservation, subnet)
  rescue NoMoreIPsAvailableAndStopRetrying
    @logger.debug('Failed to allocate dynamic ip: no more available')
    return nil
  rescue IpFoundInDatabaseAndCanBeRetried
    @logger.debug('Retrying to allocate dynamic ip: probably a race condition with another deployment')
    # IP can be taken by other deployment that runs in parallel
    # retry until succeeds or out of range
    retry
  end

  @logger.debug("Allocated dynamic IP '#{ip_address.ip}' for #{reservation.network.name}")
  ip_address.to_i
end

#delete(ip, _) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bosh/director/deployment_plan/ip_provider/database_ip_repo.rb', line 11

def delete(ip, _)
  cidr_ip = CIDRIP.new(ip)

  ip_address = Bosh::Director::Models::IpAddress.first(address: cidr_ip.to_i)

  if ip_address
    @logger.debug("Releasing ip '#{cidr_ip}'")
    ip_address.destroy
  else
    @logger.debug("Skipping releasing ip '#{cidr_ip}': not reserved")
  end
end