rig 0.6.2 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
data/bin/rig CHANGED
@@ -2,4 +2,9 @@
2
2
  require 'rig'
3
3
  require 'rig/command'
4
4
 
5
- Rig::Command::Main.run
5
+ begin
6
+ Rig::Command::Main.run
7
+ rescue => e
8
+ Rig::Log.info "** #{e.message}"
9
+ Rig::Log.trace e.backtrace
10
+ end
@@ -25,10 +25,10 @@ update_rubygems
25
25
  gem install <%= gem %> --no-ri --no-rdoc
26
26
  <% end %>
27
27
 
28
- cat <<FACTS > /etc/facts.yml
29
- rig_name: <%= @name %>
30
- rig_roles: <%= @roles.join(",") %>
31
- rig_environment: <%= @environment %>
28
+ cat <<FACTS > /etc/rig.yml
29
+ name: <%= @name %>
30
+ roles: <%= @roles.join(",") %>
31
+ environment: <%= @environment %>
32
32
  FACTS
33
33
 
34
34
  cat <<JSON > /etc/chef/bootstrap.json
@@ -52,6 +52,8 @@ file_backup_path "/var/backups/chef"
52
52
  pid_file "/var/run/chef/client.pid"
53
53
  node_name "<%= @name %>";
54
54
  Chef::Log::Formatter.show_time = true
55
+
56
+ Ohai::Config[:plugin_path] << '/etc/ohai/plugins'
55
57
  CLIENT
56
58
 
57
59
  <% @files.each do |file|; local = file["local"]; remote = file["remote"] %>
@@ -9,8 +9,8 @@ require 'capistrano'
9
9
  module Rig
10
10
  module Capistrano
11
11
  def servers
12
- env = ENV['ENVIRONMENT'] || Rig.config[:environment]
13
- role = ENV['ROLE'] || Rig.config[:role]
12
+ env = Rig.config[:environment]
13
+ role = Rig.config[:role]
14
14
  servers = Rig::Model::Environment.find(env).servers
15
15
  list = role == 'all' ? servers : servers.select { |s| (s.tags['Roles']||"").split(",").include?(role) }
16
16
 
@@ -33,8 +33,8 @@ configuration = Capistrano::Configuration.respond_to?(:instance) ?
33
33
  configuration.load do
34
34
  puts " * reading rig information..."
35
35
 
36
- Rig.config[:environment] = ARGV[0]
37
- Rig.config[:role] = ARGV[1]
36
+ Rig.config[:environment] = ENV['ENVIRONMENT'] || ARGV[0]
37
+ Rig.config[:role] = ENV['ROLE'] || ARGV[1]
38
38
 
39
39
  # create dummy tasks for environment and role
40
40
  begin
@@ -17,6 +17,10 @@ module Rig
17
17
  exit(0)
18
18
  end
19
19
 
20
+ option %w{-d --debug}, :flag, "debug logging" do |d|
21
+ Rig::Log.level = :debug
22
+ end
23
+
20
24
  def print_table(columns, rows)
21
25
  puts table(columns, *rows)
22
26
  end
@@ -14,13 +14,13 @@ module Rig
14
14
  option %w{--add}, :flag, "add this to the existing servers of this role and environment"
15
15
  option "--[no-]chef", :flag, "use chef to bootstrap", :default => true
16
16
 
17
+ parameter "NAME", "the name of the server, will automatically add a number"
17
18
  parameter "ENVIRONMENT", "the environment this server should belong to"
18
- parameter "ROLE", "the server role", :default => 'solo'
19
+ parameter "ROLE ...", "the server roles"
19
20
 
20
21
  def execute
21
- name = role
22
22
  # figure out if there are others of this role, if there are add a number to name to distinguish.
23
- n = "#{name}.#{environment}.env"
23
+ n = "#{name}1.#{environment}.env"
24
24
  chef = Rig.config[:chef] ? true : false
25
25
 
26
26
  unless flavor
@@ -32,9 +32,9 @@ module Rig
32
32
  :flavor_id => flavor,
33
33
  :key_name => keypair,
34
34
  :groups => groups.split(','),
35
- :user_data => Rig::Model::Userdata.create(n, role, environment, :chef => chef)
35
+ :user_data => Rig::Model::Userdata.create(n, role_list, environment, :chef => chef)
36
36
  }
37
- tags = { 'Name' => n, 'Environment' => environment, 'Role' => role }
37
+ tags = { 'Name' => n, 'Environment' => environment, 'Role' => role_list.join(",") }
38
38
  server = Rig::Model::Instance.create(o, tags)
39
39
  puts "created: #{server.id}"
40
40
  rescue => e
@@ -42,6 +42,10 @@ module Rig
42
42
  raise Clamp::HelpWanted, self
43
43
  end
44
44
 
45
+ def default_flavor
46
+ Rig.account[:flavor]
47
+ end
48
+
45
49
  def default_image
46
50
  Rig.account[:image].is_a?(Hash) ? Rig.account[:image].first.last : Rig.account[:image]
47
51
  end
@@ -84,8 +88,10 @@ module Rig
84
88
  parameter "NAME", "the name of the server to get tags for"
85
89
 
86
90
  def execute
87
- instance = Rig::Model::Instance.find(name)
88
- raise "server not found #{name}" unless instance
91
+ list = Rig::Model::Instance.find(name)
92
+ raise "server not found #{name}" unless list && list.count > 0
93
+ raise "search found more than one server" if list.count > 1
94
+ instance = list.first
89
95
  puts "#{instance.tags.inspect}"
90
96
  end
91
97
  end
data/lib/rig/log.rb CHANGED
@@ -22,7 +22,7 @@ module Rig
22
22
  console_level = LEVELS[console_config[:level].to_sym]
23
23
  end
24
24
  console.level = console_level || Logger::INFO
25
- add(console, proc {|s, _, _, m| "#{m}\n"})
25
+ add(console, proc {|s, _, _, m| "#{"debug: " if s=='DEBUG'}#{m}\n"})
26
26
 
27
27
  logging.each do |name, data|
28
28
  level = data[:level]
@@ -71,6 +71,13 @@ module Rig
71
71
  end
72
72
  METHOD_DEFN
73
73
  end
74
+
75
+ def trace(backtrace)
76
+ debug "trace: #{backtrace.shift}"
77
+ backtrace.each do |l|
78
+ debug " #{l}"
79
+ end
80
+ end
74
81
  end
75
82
  end
76
83
  end
@@ -99,6 +99,19 @@ module Rig
99
99
  'PolicyNames' => policy_names
100
100
  }
101
101
  end
102
+
103
+ def sticky(type, balancer_name, expires_or_cookie, policy_name='LBCookieStickinessPolicy')
104
+ elb = find(balancer_name)
105
+ raise "balancer #{balancer_name} not found" unless elb
106
+ case type.to_sym
107
+ when :app, :application
108
+ Rig::Connection.balancer.create_app_cookie_stickiness_policy(balancer_name, policy_name, expires_or_cookie)
109
+ when :lb, :load_balancer
110
+ Rig::Connection.balancer.create_lb_cookie_stickiness_policy(balancer_name, policy_name, expires_or_cookie)
111
+ else
112
+ raise "unknown sticky type #{type}"
113
+ end
114
+ end
102
115
  end
103
116
  end
104
117
  end
data/lib/rig/model/dns.rb CHANGED
@@ -49,6 +49,25 @@ module Rig
49
49
  Rig::Log.info "record not found: #{name}"
50
50
  end
51
51
  end
52
+
53
+ def remove_environment(name)
54
+ zone = zone(Rig.get_config(:dns_zone))
55
+ list = zone.records.all.select {|e| e.attributes[:name] =~ /\.#{name}\.env/ }
56
+ list.each do |e|
57
+ Rig::Log.info "record: #{e.attributes[:name]}"
58
+ destroy(e.attributes[:name])
59
+ end
60
+ end
61
+
62
+ def remove_instance(dns)
63
+ zone = zone(Rig.get_config(:dns_zone))
64
+ dns = dns.gsub(/\.$/, '')
65
+ list = zone.records.all.select {|e| e.attributes[:value] == "#{dns}." }
66
+ list.each do |e|
67
+ Rig::Log.info "record: #{e.attributes[:name]}"
68
+ destroy(e.attributes[:name])
69
+ end
70
+ end
52
71
  end
53
72
  end
54
73
  end
@@ -49,11 +49,12 @@ module Rig
49
49
 
50
50
  }.merge(opts)
51
51
 
52
- @name = name
53
- @template = Rig.get_template(template) unless template.nil?
52
+ @name = name
53
+ @template = Rig.get_template(template) unless template.nil?
54
54
  @template_name = template
55
- @servers = []
56
- @balancers = []
55
+ @servers = []
56
+ @dnsdelayed = []
57
+ @balancers = []
57
58
  end
58
59
 
59
60
  def save
@@ -62,14 +63,17 @@ module Rig
62
63
  set = { }
63
64
 
64
65
  @template.servers.each do |s|
66
+ Rig::Log.debug "server[#{s.count}]= #{s.inspect}"
65
67
  1.upto(s.count) do |i|
66
68
  n = "#{s.name}#{i}.#@name.env"
69
+ Rig::Log.debug " name= #{n}"
70
+ userdata = s.userdata || Rig.get_config(:userdata)
67
71
  o = {
68
72
  :image_id => s.image || @image,
69
73
  :flavor_id => s.flavor || @flavor,
70
74
  :key_name => s.keypair || @keypair,
71
75
  :groups => s.groups || @groups,
72
- :user_data => Rig::Model::Userdata.create(n, s.roles, @name)
76
+ :user_data => Rig::Model::Userdata.create(n, s.roles, @name, :package => userdata)
73
77
  }
74
78
 
75
79
  # throw this error, because Fog and AWS don't care if you don't set a keypair
@@ -78,9 +82,22 @@ module Rig
78
82
 
79
83
  Rig::Log.info "creating instance: name:#{n} environment:#@name roles:#{s.roles.inspect}"
80
84
  instance = Rig::Model::Instance.create(o, 'Name' => n, 'Environment' => name, 'Roles' => s.roles.join(","))
81
- set[s.balancer] ||= []
82
- set[s.balancer] << instance if s.balancer
85
+ if s.balancer
86
+ set[s.balancer] ||= []
87
+ set[s.balancer] << instance
88
+ end
83
89
  @servers << instance
90
+ @dnsdelayed << {
91
+ :server => instance.id,
92
+ }
93
+ if s.count == 1 && s.dnsnames.count > 0
94
+ s.dnsnames.each do |dns|
95
+ @dnsdelayed << {
96
+ :record => "#{dns}.#{Rig.get_config(:dns_zone)}",
97
+ :server => instance.id,
98
+ }
99
+ end
100
+ end
84
101
  end
85
102
  end
86
103
 
@@ -100,22 +117,31 @@ module Rig
100
117
  end
101
118
  end
102
119
 
103
- Rig::Log.info "sleeping 3 seconds"
104
- sleep 3
105
- reload
120
+ server_count = @servers.count
121
+ Rig::Log.info "waiting "
122
+ begin
123
+ sleep 1
124
+ reload
125
+ Rig::Log.info " . #{server_count} != #{@servers.count} : dns=#{@servers.select{|e| e.dns_name.nil? }.count == 0}"
126
+ end while server_count != @servers.count && @servers.select{|e| e.dns_name.nil? }.count == 0
106
127
 
107
128
  Rig::Log.info "associating servers' DNS"
108
- @servers.each do |server|
109
- record = "#{server.tags['Name']}.#{Rig.get_config(:dns_zone)}"
129
+ @dnsdelayed.each do |h|
130
+ server = @server_hash[h[:server]]
131
+ record = h[:record]
132
+ record ||= "#{server.tags['Name']}.#{Rig.get_config(:dns_zone)}" if server.tags
133
+ next unless record
110
134
  dns = server.attributes[:dns_name]
111
135
  ip = server.attributes[:public_ip_address]
112
136
 
113
137
  Rig::Log.info ".. #{record} dns=#{dns} ip=#{ip}"
114
- if dns
115
- Rig::Model::Dns.create(record, dns)
116
- else
117
- Rig::Log.warn "server #{server.id} doesn't have public_ip_address"
138
+
139
+ unless dns
140
+ Rig::Log.warn "server #{server.id} doesn't have dns_name"
141
+ next
118
142
  end
143
+
144
+ Rig::Model::Dns.create(record, dns)
119
145
  end
120
146
 
121
147
  Rig::Plugin.run "environment:create", self
@@ -125,14 +151,18 @@ module Rig
125
151
  end
126
152
 
127
153
  def destroy
128
- Rig::Log.info "removing dns from servers"
154
+ Rig::Log.info "removing all server dns entries"
129
155
  @servers.each do |server|
130
- Rig::Model::Dns.destroy("#{server.tags['Name']}.#{Rig.get_config(:dns_zone)}") if server.tags['Name']
156
+ #Rig::Model::Dns.destroy("#{server.tags['Name']}.#{Rig.get_config(:dns_zone)}") if server.tags['Name']
157
+ Rig::Model::Dns.remove_instance(server.dns_name) if server.dns_name
131
158
  end
159
+ Rig::Log.info "removing all environment dns entries"
160
+ Rig::Model::Dns.remove_environment(@name)
161
+
132
162
 
133
163
  roles = @servers.collect { |s| s.tags['Role'] }.compact
134
164
  roles.each do |r|
135
- Rig::Log.info "removing role balancer dns"
165
+ Rig::Log.info "removing role balancer dns (#{r})"
136
166
  Rig::Model::Dns.destroy("#{r}.#{name}.env.#{Rig.get_config(:dns_zone)}")
137
167
  end
138
168
 
@@ -183,6 +213,7 @@ module Rig
183
213
 
184
214
  def find_servers
185
215
  @servers = Rig::Model::Instance.find_by_environment(@name) || []
216
+ @server_hash = @servers.inject({}){|h, e| h[e.id] = e; h}
186
217
  end
187
218
 
188
219
  def find_balancers
@@ -6,7 +6,7 @@ module Rig
6
6
  class Userdata
7
7
  class << self
8
8
  def create(name, roles, environment, opts={ })
9
- package = Rig.get_config(:userdata)
9
+ package = opts.delete(:package) || Rig.get_config(:userdata)
10
10
  directory = "#{Rig::Config.dir}/userdata/#{package}"
11
11
  config = YAML.load_file("#{directory}/userdata.yml")
12
12
 
data/lib/rig/template.rb CHANGED
@@ -64,55 +64,97 @@ module Rig
64
64
  end
65
65
  end
66
66
 
67
- class Server
68
- attr_reader :name, :roles, :groups
67
+ class Base
68
+ def attr(name, value=nil)
69
+ @attrs[name] = value unless value.nil?
70
+ @attrs[name]
71
+ end
72
+
73
+ def list_attr(name, value=nil)
74
+ @attrs[name] ||= []
75
+ unless value.nil?
76
+ if value.kind_of?(Array)
77
+ @attrs[name] += value
78
+ else
79
+ @attrs[name] << value
80
+ end
81
+ end
82
+ @attrs[name]
83
+ end
84
+ end
69
85
 
86
+ class Server < Base
70
87
  def initialize(name)
71
- @name = name.to_sym
72
- @roles = []
73
- @groups = []
74
- @count = 1
75
- @image = nil
76
- @flavor = nil
77
- @keypair = nil
78
- @userdata = "default"
88
+ @attrs = {
89
+ :name => name.to_sym,
90
+ :roles => [],
91
+ :groups => [],
92
+ :count => 1,
93
+ :image => nil,
94
+ :flavor => nil,
95
+ :keypair => nil,
96
+ :userdata => "default",
97
+ :dnsnames => [],
98
+ }
99
+ end
100
+
101
+ def name
102
+ @attrs[:name]
103
+ end
104
+ def roles
105
+ @attrs[:roles]
106
+ end
107
+ def groups
108
+ @attrs[:groups]
109
+ end
110
+ def dnsnames
111
+ @attrs[:dnsnames]
79
112
  end
80
113
 
81
114
  def role(r)
82
- @roles << r
115
+ list_attr(:roles, r)
83
116
  end
84
117
 
85
118
  def count(c=nil)
86
- c.nil? ? @count : @count = c
119
+ attr(:count, c)
87
120
  end
88
121
 
89
122
  def image(i=nil)
90
- i.nil? ? @image : @image = i
123
+ attr(:image, i)
91
124
  end
92
125
 
93
126
  def flavor(f=nil)
94
- f.nil? ? @flavor : @flavor = f
127
+ attr(:flavor, f)
95
128
  end
96
129
 
97
130
  def group(g)
98
- @groups << g
131
+ list_attr(:groups, g)
99
132
  end
100
133
 
101
134
  def keypair(k=nil)
102
- k.nil? ? @keypair : @keypair = k
135
+ attr(:keypair, k)
103
136
  end
104
137
 
105
138
  def userdata(u=nil)
106
- u.nil? ? @userdata : @userdata = u.to_sym
139
+ attr(:userdata, u)
107
140
  end
108
141
 
109
- def balancer(b=nil)
110
- b.nil? ? @balancer : @balancer = b.to_sym
142
+ def balancer(b=nil, &block)
143
+ if block_given?
144
+ raise "balancer block must specify name" unless b
145
+ Rig::Template::DSL.balancer(b, &block)
146
+ end
147
+ attr(:balancer, b)
148
+ end
149
+
150
+ def dns(name)
151
+ n = name.gsub(Rig.get_config(:dns_zone), "")
152
+ list_attr(:dnsnames, n)
111
153
  end
112
154
  end
113
155
 
114
156
  class Balancer
115
- attr_reader :name
157
+ attr_reader :name, :sticky_type, :sticky_arg
116
158
 
117
159
  def initialize(name)
118
160
  @listeners = []
@@ -131,7 +173,12 @@ module Rig
131
173
  end
132
174
 
133
175
  def listeners
134
- @listeners.map {|e| e.spec}
176
+ @listeners.map { |e| e.spec }
177
+ end
178
+
179
+ def sticky(type, expires_or_cookie)
180
+ @sticky_type = type
181
+ @sticky_arg = expires_or_cookie
135
182
  end
136
183
  end
137
184
 
@@ -160,9 +207,9 @@ module Rig
160
207
 
161
208
  def spec
162
209
  {
163
- :from => "#@from_proto:#@from_port",
164
- :to => "#@to_proto:#@to_port",
165
- :cert => @cert,
210
+ :from => "#@from_proto:#@from_port",
211
+ :to => "#@to_proto:#@to_port",
212
+ :cert => @cert,
166
213
  }
167
214
  end
168
215
  end
data/lib/rig/version.rb CHANGED
@@ -3,7 +3,7 @@ unless defined?(Rig::Version)
3
3
  module Version
4
4
  MAJOR = 0
5
5
  MINOR = 6
6
- TINY = 2
6
+ TINY = 3
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-26 00:00:00.000000000 Z
12
+ date: 2012-07-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: clamp