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
# 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
  ip_add = self.ip.split('.')
  while next_ip <= ips[1].to_i + 1 do
    if (lease.select{|l| l.ip == "#{ip_add[0]}.#{ip_add[1]}.#{ip_add[2]}.#{next_ip}"}.size == 0)
      break
    else
      next_ip+=1
    end
  end

  if next_ip > ips[1].to_i
    self.status = MKIt::PoolStatus::EXAUSTED
    self.save
    raise PoolExaustedException.new
  end
  "#{ip_add[0]}.#{ip_add[1]}.#{ip_add[2]}.#{next_ip}"
end

#request(service:, status:) ⇒ Object



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

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



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

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

#reserve_for(service) ⇒ Object



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

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