Class: Pool

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/mkit/app/model/pool.rb

Instance Method Summary collapse

Instance Method Details

#check_statusObject



8
9
10
11
12
# File 'lib/mkit/app/model/pool.rb', line 8

def check_status
  if status == MKIt::PoolStatus::EXAUSTED
    raise PoolExaustedException.new
  end
end

#next_lease_ipObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mkit/app/model/pool.rb', line 14

def next_lease_ip
  self.check_status
  ips = range.split('-')
  next_ip = ips[0]
  next_ip = next_ip.to_i
  lease.select(:status == MKIt::PoolStatus::IN_USE || :status == MKIt::PoolStatus::RESERVED).each { |l|
    leased_ip = l.ip.split('.')[3]
    leased_ip = leased_ip.to_i
    if leased_ip >= next_ip
      next_ip = leased_ip+1
    end
  }
  if next_ip > ips[1].to_i
    self.status = MKIt::PoolStatus::EXAUSTED
    self.save
    raise PoolExaustedException.new
  end

  ip_add = self.ip.split('.')

  "#{ip_add[0]}.#{ip_add[1]}.#{ip_add[2]}.#{next_ip}"
end

#request(service:, status:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mkit/app/model/pool.rb', line 37

def request(service:, status:)
  lease_ip = next_lease_ip
  idx = lease_ip.split('.')[3]
  new_lease = Lease.new(
    pool: self,
    service: service,
    interface_name: "vmkit#{idx}",
    interface_type: 'tun',
    status: status,
    ip: lease_ip
  )
  new_lease.save
  new_lease
end

#request_for(service) ⇒ Object



52
53
54
# File 'lib/mkit/app/model/pool.rb', line 52

def request_for(service)
   request(service: service, status:  MKIt::PoolStatus::IN_USE)
end

#reserve_for(service) ⇒ Object



56
57
58
# File 'lib/mkit/app/model/pool.rb', line 56

def reserve_for(service)
   request(service: service, status:  MKIt::PoolStatus::RESERVED)
end