Class: Cloudboot::Domain

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/cloudboot/domain.rb

Class Method Summary collapse

Class Method Details

.ensure(name, instance_prefix, num, refresh_timeout) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cloudboot/domain.rb', line 9

def self.ensure(name, instance_prefix, num, refresh_timeout)
  ActiveRecord::Base.transaction do

    domain = find_or_create_by_name(name) do |d|
      d.refresh_timeout = refresh_timeout
      d.instance_prefix = instance_prefix
    end
  
    raise "domain '#{name}' has a different instance_prefix '#{domain.instance_prefix}' <=> '#{instance_prefix}'" if domain.instance_prefix != instance_prefix
  
    num.times do |i|
      hostname = "#{domain.instance_prefix}#{(i+1).to_s.rjust(3, '0')}"
      now = DateTime.now
    
      domain.instances.find_or_create_by_name(hostname) do |instance|
        instance.public_ip = "0.0.0.0"
        instance.private_ip = "0.0.0.0"
        instance.refresh = now
        instance.created_at = now 
      end
    
    end
  
  end
end

.register(domainname, hostname, public_ip, private_ip) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cloudboot/domain.rb', line 35

def self.register(domainname, hostname, public_ip, private_ip)

  puts "registering:"
  puts "  domain: #{domainname}"
  puts "  hostname: #{hostname}"
  puts "  public_ip: #{public_ip}"
  puts "  private_ip: #{private_ip}"

  domain = find_by_name(domainname)
  raise "unknown domain: #{domainname}" unless domain

  instance = domain.instances.find_by_name(hostname)

  now = DateTime.now
  
  if instance
    puts "found existing hostname #{hostname} in domain #{domainname}"
    reset = false
    
    if instance.refresh > now - domain.refresh_timeout.seconds && instance.refresh != instance.created_at
      
      puts "refresh within #{domain.refresh_timeout} seconds - maybe conflict"
      
      if instance.public_ip != public_ip  
        puts "public_ip is different - reset"
        instance = nil
      else                                
        puts "public_ip is the same"
      end

      if instance
        if instance.private_ip != private_ip  
          puts "private_ip is different - reset"
          instance = nil
        else
          puts "private_ip is the same"
        end
      end
      
    else
      puts "no refresh since #{domain.refresh_timeout} seconds"
    end
  else
    puts "no existing hostname #{hostname} in domain #{domainname}"
  end
  
  # instance = domain.instances.find_by_name_and_public_ip_and_private_ip(hostname, public_ip, private_ip)

  unless instance
    instance = domain.instances.find(:first, :conditions => ["refresh < ? or refresh = created_at", now - domain.refresh_timeout.seconds], :order => "name asc")
  end

  raise "no available instance in domain: #{domainname}" unless instance

  ActiveRecord::Base.transaction do
    instance.public_ip    = public_ip
    instance.private_ip   = private_ip
    instance.refresh      = now
    instance.save
  end
  
  puts "hostname #{hostname} -> hostname #{instance.name}"
  
  instance
end