jzimmek-ec2 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.10
1
+ 0.0.11
data/ec2.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ec2}
8
- s.version = "0.0.10"
8
+ s.version = "0.0.11"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jan Zimmek"]
12
- s.date = %q{2009-09-15}
12
+ s.date = %q{2009-09-24}
13
13
  s.email = %q{jan.zimmek@web.de}
14
14
  s.extra_rdoc_files = [
15
15
  "README"
data/lib/ec2/dsl/cloud.rb CHANGED
@@ -5,7 +5,7 @@ module Ec2
5
5
  module Dsl
6
6
  class Cloud < ::EasyDsl::Container
7
7
  def initialize(name)
8
- super(name, nil)
8
+ super(nil, name)
9
9
  nested :group, Group
10
10
 
11
11
  attribute :ssh_key_file, :required => true
@@ -26,7 +26,7 @@ module Ec2
26
26
 
27
27
  def dump
28
28
 
29
- print "hostname".ljust(10)
29
+ print "fqdn".ljust(30)
30
30
  print "ami".ljust(15)
31
31
  print "ari".ljust(15)
32
32
  print "aki".ljust(15)
@@ -39,7 +39,7 @@ module Ec2
39
39
 
40
40
  @all_group.values.each do |grp|
41
41
  grp.all_instance.values.each do |i|
42
- print i.name.ljust(10)
42
+ print i.fqdn.ljust(30)
43
43
  print i.ami.ljust(15)
44
44
  print (i.ari||"").ljust(15)
45
45
  print (i.aki||"").ljust(15)
data/lib/ec2/dsl/group.rb CHANGED
@@ -4,7 +4,7 @@ require 'ec2/dsl/instance'
4
4
  module Ec2
5
5
  module Dsl
6
6
  class Group < ::EasyDsl::Container
7
- def initialize(name, container)
7
+ def initialize(container, name)
8
8
  super
9
9
  nested :instance, Instance
10
10
 
@@ -15,6 +15,7 @@ module Ec2
15
15
  attribute :security_group, :inheritable => true
16
16
  attribute :availability_zone, :inheritable => true
17
17
  attribute :domain, :inheritable => true
18
+ attribute :version, :inheritable => true
18
19
  attribute :userdata, :inheritable => true, :base64 => true, :strip => true
19
20
 
20
21
  end
@@ -3,7 +3,7 @@ require 'easy_dsl/container'
3
3
  module Ec2
4
4
  module Dsl
5
5
  class Instance < ::EasyDsl::Container
6
- def initialize(name, container)
6
+ def initialize(container, name)
7
7
  super
8
8
  attribute :ami, :inheritable => true, :required => true
9
9
  attribute :ari, :inheritable => true
@@ -13,7 +13,16 @@ module Ec2
13
13
  attribute :availability_zone, :inheritable => true
14
14
  attribute :userdata, :inheritable => true, :base64 => true, :strip => true
15
15
  attribute :elastic_ip, :inheritable => true
16
- attribute :domain, :inheritable => true
16
+ attribute :hostname, :inheritable => true, :required => true
17
+ attribute :domain, :inheritable => true, :required => true
18
+ end
19
+
20
+ def options
21
+ @options ||= {}
22
+ end
23
+
24
+ def fqdn
25
+ "#{hostname}.#{domain}"
17
26
  end
18
27
  end
19
28
  end
@@ -6,7 +6,6 @@ require 'net/scp'
6
6
  module Ec2
7
7
  class InstanceBinding
8
8
 
9
- attr_reader :hostname
10
9
  attr_reader :public_ip
11
10
  attr_reader :state
12
11
 
@@ -21,15 +20,11 @@ module Ec2
21
20
  @private_dns_name = private_dns_name
22
21
 
23
22
  @ssh = Net::SSH.start(@public_dns_name, "root", :keys => [@cloud.ssh_key_file], :paranoid => false)
24
-
25
- # user_data = fetch_from_url("http://169.254.169.254/2009-04-04/user-data")
26
-
27
- # @hostname = user_data.index("Not Found") ? nil : user_data
28
23
 
29
- @hostname = fetch_from_file("/root/.hostname")
24
+ @fqdn = fetch_from_file("/root/.fqdn")
30
25
 
31
- if @hostname
32
- Pending.remove(@hostname)
26
+ if @fqdn
27
+ Pending.remove(@fqdn)
33
28
 
34
29
  @public_ip = fetch_from_url("http://169.254.169.254/latest/meta-data/public-ipv4")
35
30
  cfg = fetch_from_file("~/.configuration")
@@ -39,20 +34,48 @@ module Ec2
39
34
 
40
35
  end
41
36
 
37
+ def fqdn
38
+ instance.try(:fqdn) || @fqdn
39
+ end
40
+
41
+ def hostname
42
+ instance.hostname
43
+ end
44
+
42
45
  def configured?(name)
43
46
  @configuration[name] == true
44
47
  end
45
48
 
46
- def ensure_configured(name, &block)
47
- if configured?(name)
48
- puts "#{name} already configured on instance #{@hostname}"
49
+ def ensure_configured(name, opts={}, &block)
50
+
51
+ opts = {
52
+ :if => lambda { |binding| true }
53
+ }.merge(opts)
54
+
55
+ # only run this configuration when :if is true
56
+ if opts[:if].call(self) == true
57
+
58
+ if configured?(name)
59
+ puts "#{name} already configured on instance #{@fqdn}"
60
+ else
61
+ puts "#{name} not configured on instance #{@fqdn}"
62
+ block.call(@ssh)
63
+ __mark_configured(name)
64
+ end
65
+
49
66
  else
50
- puts "#{name} not configured on instance #{@hostname}"
51
- block.call(@ssh)
52
- __mark_configured(name)
67
+ puts "skipping configuration #{name} on instance #{@fqdn} - :if is false"
53
68
  end
54
69
  end
55
70
 
71
+ def file_exist?(file)
72
+ @ssh.exec!("[ -f #{file} ] && echo 1 || echo 0").strip == "1"
73
+ end
74
+
75
+ def dir_exist?(dir)
76
+ @ssh.exec!("[ -d #{fir} ] && echo 1 || echo 0").strip == "1"
77
+ end
78
+
56
79
  def terminate!
57
80
  @ws.terminate_instances(:instance_id => [@id])
58
81
  end
@@ -60,7 +83,7 @@ module Ec2
60
83
  def __mark_configured(name)
61
84
  @configuration[name] = true
62
85
  write_to_file("~/.configuration", @configuration.to_yaml)
63
- puts "#{name} configured on instance #{@hostname}"
86
+ puts "#{name} configured on instance #{@fqdn}"
64
87
  end
65
88
 
66
89
  def valid?
@@ -72,7 +95,7 @@ module Ec2
72
95
  end
73
96
 
74
97
  def instance
75
- @cloud.instances.find{|it| it.name == @hostname}
98
+ @cloud.instances.find{|it| it.fqdn == @fqdn}
76
99
  end
77
100
 
78
101
  def disconnect!
@@ -103,6 +126,9 @@ module Ec2
103
126
  end
104
127
 
105
128
  def upload_file(local, remote)
129
+
130
+ puts "uploading '#{local}' to '#{remote}'"
131
+
106
132
  Net::SCP.start(@public_dns_name, "root", :keys => [@cloud.ssh_key_file], :paranoid => false) do |scp|
107
133
  scp.upload!(local, remote)
108
134
  end
data/lib/ec2/manager.rb CHANGED
@@ -40,16 +40,18 @@ module Ec2
40
40
  binding = InstanceBinding.new(@cloud, @ws, id, launch_time, public_dns_name, private_dns_name)
41
41
  rescue Exception => e
42
42
  puts "instance #{id} failed to bind: #{e}"
43
+ puts e.backtrace
43
44
  next
44
45
  end
45
46
 
46
- hostname = binding.hostname
47
+ fqdn = binding.fqdn
47
48
 
48
- next unless hostname
49
-
50
- raise "multiple instance using hostname #{binding.hostname}" if bindings.key?(hostname)
49
+ # maybe a manually started instance ?
50
+ next unless fqdn
51
+
52
+ raise "multiple instance using fqdn #{binding.fqdn}" if bindings.key?(fqdn)
51
53
 
52
- bindings[hostname] = binding
54
+ bindings[fqdn] = binding
53
55
  end
54
56
 
55
57
  bindings
@@ -57,31 +59,31 @@ module Ec2
57
59
 
58
60
  def startup(instance)
59
61
 
60
- unless Pending.pending?(instance.name)
62
+ unless Pending.pending?(instance.fqdn)
61
63
 
62
64
  user_data = instance.userdata
63
65
 
64
- user_data << "\necho \"#{instance.name}\" > /root/.hostname" if user_data
66
+ user_data << "\necho \"#{instance.fqdn}\" > /root/.fqdn" if user_data
65
67
 
66
68
  opts = {
67
69
  :image_id => instance.ami,
68
70
  :min_count => 1,
69
71
  :max_count => 1,
70
72
  :key_name => instance.keypair,
71
- # :user_data => Base64.encode64(instance.name),
72
73
  :user_data => Base64.encode64(user_data),
74
+ :group_id => instance.security_group,
73
75
  :instance_type => "m1.small"
74
76
  }
75
77
 
76
78
  opts[:availability_zone] = instance.availability_zone if instance.availability_zone
77
79
 
78
- puts "startup #{instance.name}: #{opts.inspect}"
80
+ puts "startup #{instance.fqdn}: #{opts.inspect}"
79
81
 
80
82
  @ws.run_instances(opts)
81
83
 
82
- Pending.add(instance.name)
84
+ Pending.add(instance.fqdn)
83
85
  else
84
- puts "instance #{instance.name} is pending"
86
+ puts "instance #{instance.fqdn} is pending"
85
87
  end
86
88
  end
87
89
 
@@ -89,20 +91,20 @@ module Ec2
89
91
 
90
92
  bindings = instance_bindings
91
93
 
92
- bindings.each_pair do |hostname, binding|
94
+ bindings.each_pair do |fqdn, binding|
93
95
  begin
94
96
  yield(binding)
95
97
  rescue Exception => e
98
+ puts "error while processing fqdn: #{fqdn}"
96
99
  puts e
100
+ puts e.backtrace
97
101
  ensure
98
102
  binding.disconnect!
99
103
  end
100
104
  end
101
105
 
102
106
  @cloud.instances.each do |instance|
103
- unless bindings.key?(instance.name)
104
- startup(instance)
105
- end
107
+ startup(instance) unless bindings.key?(instance.fqdn)
106
108
  end
107
109
 
108
110
  end
data/test/ec2_test.rb CHANGED
@@ -12,17 +12,22 @@ class Ec2Test < Test::Unit::TestCase
12
12
  security_group "mysecuritygroup"
13
13
  group "app_server" do
14
14
  ami "ami999"
15
+ domain "mydomain"
15
16
  instance "app01" do
17
+ hostname "app01"
16
18
  security_group "default2"
17
19
  availability_zone "zone-1"
18
20
  end
19
21
  instance "app02" do
20
- ami "ami123"
22
+ hostname "app02"
23
+ ami "ami123"
21
24
  security_group "default"
22
25
  end
23
26
  end
24
27
  group "webserver" do
28
+ domain "mydomain"
25
29
  instance "web01" do
30
+ hostname "web01"
26
31
  ami "webXYZ"
27
32
  availability_zone "zone-3"
28
33
  end
@@ -51,6 +56,8 @@ class Ec2Test < Test::Unit::TestCase
51
56
  assert_equal "default2", app01.security_group
52
57
  assert_equal "ami999", app01.ami
53
58
  assert_equal "app01", app01.name
59
+ assert_equal "app01", app01.hostname
60
+ assert_equal "mydomain", app01.domain
54
61
  assert_equal "zone-1", app01.availability_zone
55
62
  assert_equal grp_app, app01.container
56
63
  assert_equal cloud, app01.cloud
@@ -59,6 +66,8 @@ class Ec2Test < Test::Unit::TestCase
59
66
  assert_equal "default", app02.security_group
60
67
  assert_equal "ami123", app02.ami
61
68
  assert_equal "app02", app02.name
69
+ assert_equal "app02", app02.hostname
70
+ assert_equal "mydomain", app01.domain
62
71
  assert_equal "zone-2", app02.availability_zone
63
72
  assert_equal grp_app, app02.container
64
73
  assert_equal cloud, app02.cloud
@@ -74,6 +83,8 @@ class Ec2Test < Test::Unit::TestCase
74
83
  assert_equal "mysecuritygroup", web01.security_group
75
84
  assert_equal "webXYZ", web01.ami
76
85
  assert_equal "web01", web01.name
86
+ assert_equal "web01", web01.hostname
87
+ assert_equal "mydomain", web01.domain
77
88
  assert_equal "zone-3", web01.availability_zone
78
89
  assert_equal grp_web, web01.container
79
90
  assert_equal cloud, web01.cloud
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jzimmek-ec2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Zimmek
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-15 00:00:00 -07:00
12
+ date: 2009-09-24 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -37,6 +37,7 @@ files:
37
37
  - test/test_helper.rb
38
38
  has_rdoc: false
39
39
  homepage: http://github.com/jzimmek/ec2
40
+ licenses:
40
41
  post_install_message:
41
42
  rdoc_options:
42
43
  - --charset=UTF-8
@@ -57,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
58
  requirements: []
58
59
 
59
60
  rubyforge_project:
60
- rubygems_version: 1.2.0
61
+ rubygems_version: 1.3.5
61
62
  signing_key:
62
63
  specification_version: 3
63
64
  summary: Management tool for EC2 clouds