cloudboot 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,15 +23,19 @@ opts = OptionParser.new do |opts|
23
23
  options[:action] = :cronjob
24
24
  end
25
25
 
26
- opts.on("--list-instances", "Show a list of instances") do
27
- options[:action] = :list_instances
26
+ opts.on("--list", "Show a list of instances") do
27
+ options[:action] = :list
28
+ end
29
+
30
+ opts.on("--hosts", "Update the /etc/hosts file") do
31
+ options[:action] = :hosts
28
32
  end
29
33
 
30
- opts.on("-s", "--server", "Generates a server skeleton") do |name|
34
+ opts.on("--server", "Generates a server skeleton") do |name|
31
35
  options[:action] = :server
32
36
  end
33
37
 
34
- opts.on("-c", "--client", "Generates a client skeleton") do |name|
38
+ opts.on("--client", "Generates a client skeleton") do |name|
35
39
  options[:action] = :client
36
40
  end
37
41
 
@@ -92,7 +96,7 @@ module Cloudboot
92
96
  f.puts(cfg.to_yaml)
93
97
  end
94
98
 
95
- when :list_instances
99
+ when :list
96
100
  require 'activerecord'
97
101
  ActiveRecord::Base.establish_connection(config["database"])
98
102
 
@@ -103,6 +107,25 @@ module Cloudboot
103
107
  when :userdata
104
108
  puts config["server"].to_yaml
105
109
 
110
+ when :hosts
111
+ hosts = File.read("/etc/hosts")
112
+
113
+ require 'activerecord'
114
+ ActiveRecord::Base.establish_connection(config["database"])
115
+
116
+ entries = Domain.find_by_name(config["server"]["domain"]).instances.collect{|instance| "#{instance.public_ip} #{instance.fqdn} #{instance.name}" }
117
+
118
+ # remove any existing entries
119
+ hosts = hosts.gsub(/#cloudboot-default-start(.*)#cloudboot-default-end/m, "")
120
+
121
+ hosts << "#cloudboot-default-start\n"
122
+ hosts << entries.join("\n")
123
+ hosts << "\n#cloudboot-default-end"
124
+
125
+ File.open("/etc/hosts", "w") do |f|
126
+ f.puts(hosts)
127
+ end
128
+
106
129
  when :server
107
130
  File.open("cloudboot-server.rb", "w") do |f|
108
131
  f.puts Cloudboot::Generator.server_script(config)
@@ -6,10 +6,11 @@ module Cloudboot
6
6
  class Domain < ActiveRecord::Base
7
7
  has_many :instances, :class_name => "Cloudboot::Instance"
8
8
 
9
- def self.ensure(name, instance_prefix, num)
9
+ def self.ensure(name, instance_prefix, num, refresh_timeout)
10
10
  ActiveRecord::Base.transaction do
11
11
 
12
12
  domain = find_or_create_by_name(name) do |d|
13
+ d.refresh_timeout = refresh_timeout
13
14
  d.instance_prefix = instance_prefix
14
15
  end
15
16
 
@@ -30,7 +31,7 @@ module Cloudboot
30
31
 
31
32
  end
32
33
  end
33
-
34
+
34
35
  def self.register(domainname, hostname, public_ip, private_ip)
35
36
 
36
37
  puts "registering:"
@@ -41,15 +42,48 @@ module Cloudboot
41
42
 
42
43
  domain = find_by_name(domainname)
43
44
  raise "unknown domain: #{domainname}" unless domain
44
-
45
- instance = domain.instances.find_by_name_and_public_ip_and_private_ip(hostname, public_ip, private_ip)
45
+
46
+ instance = domain.instances.find_by_name(hostname)
46
47
 
47
48
  now = DateTime.now
48
-
49
+
50
+ if instance
51
+ puts "found existing hostname #{hostname} in domain #{domainname}"
52
+ reset = false
53
+
54
+ if instance.refresh > now - domain.refresh_timeout.seconds && instance.refresh != instance.created_at
55
+
56
+ puts "refresh within #{domain.refresh_timeout} seconds - maybe conflict"
57
+
58
+ if instance.public_ip != public_ip
59
+ puts "public_ip is different - reset"
60
+ instance = nil
61
+ else
62
+ puts "public_ip is the same"
63
+ end
64
+
65
+ if instance
66
+ if instance.private_ip != private_ip
67
+ puts "private_ip is different - reset"
68
+ instance = nil
69
+ else
70
+ puts "private_ip is the same"
71
+ end
72
+ end
73
+
74
+ else
75
+ puts "no refresh since #{domain.refresh_timeout} seconds"
76
+ end
77
+ else
78
+ puts "no existing hostname #{hostname} in domain #{domainname}"
79
+ end
80
+
81
+ # instance = domain.instances.find_by_name_and_public_ip_and_private_ip(hostname, public_ip, private_ip)
82
+
49
83
  unless instance
50
- instance = domain.instances.find(:first, :conditions => ["refresh < ? or refresh = created_at", now - 60.seconds], :order => "name asc")
84
+ instance = domain.instances.find(:first, :conditions => ["refresh < ? or refresh = created_at", now - domain.refresh_timeout.seconds], :order => "name asc")
51
85
  end
52
-
86
+
53
87
  raise "no available instance in domain: #{domainname}" unless instance
54
88
 
55
89
  ActiveRecord::Base.transaction do
@@ -58,7 +92,9 @@ module Cloudboot
58
92
  instance.refresh = now
59
93
  instance.save
60
94
  end
61
-
95
+
96
+ puts "hostname #{hostname} -> hostname #{instance.name}"
97
+
62
98
  instance
63
99
  end
64
100
 
@@ -64,7 +64,7 @@ ActiveRecord::Base.establish_connection({
64
64
 
65
65
  Cloudboot::Generator.setup_tables
66
66
 
67
- Cloudboot::Domain.ensure("#{server["domain"]}", "srv", 3)
67
+ Cloudboot::Domain.ensure("#{server["domain"]}", "srv", 3, 120)
68
68
 
69
69
  set :server, ["webrick"]
70
70
  set :environment, :production
@@ -96,6 +96,7 @@ end
96
96
  (
97
97
  id bigint not null AUTO_INCREMENT,
98
98
  name varchar(255) not null,
99
+ refresh_timeout bigint not null,
99
100
  instance_prefix varchar(255) not null,
100
101
  primary key (id)
101
102
  )
@@ -5,5 +5,6 @@ module Cloudboot
5
5
  def fqdn
6
6
  "#{name}.#{domain.name}"
7
7
  end
8
+
8
9
  end
9
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudboot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Zimmek