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
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
|