rig 0.4.5 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/Gemfile +1 -0
  2. data/conf/accounts/default.yml +3 -3
  3. data/conf/config.yml +33 -6
  4. data/conf/plugins/chef.rb +105 -0
  5. data/conf/plugins/mongo.rb +135 -0
  6. data/{lib/rig/command/database.rb → conf/plugins/mysql.rb} +12 -8
  7. data/conf/plugins/scout.rb +81 -0
  8. data/conf/userdata/chef/userdata.sh.erb +7 -1
  9. data/lib/rig.rb +11 -1
  10. data/lib/rig/account.rb +43 -0
  11. data/lib/rig/command.rb +92 -41
  12. data/lib/rig/command/abstract.rb +1 -2
  13. data/lib/rig/command/balancer.rb +102 -0
  14. data/lib/rig/command/dns.rb +47 -0
  15. data/lib/rig/command/environment.rb +67 -0
  16. data/lib/rig/command/keypair.rb +15 -0
  17. data/lib/rig/command/server.rb +181 -0
  18. data/lib/rig/config.rb +35 -105
  19. data/lib/rig/log.rb +66 -0
  20. data/lib/rig/model/account.rb +1 -1
  21. data/lib/rig/model/connection.rb +13 -6
  22. data/lib/rig/model/environment.rb +12 -8
  23. data/lib/rig/model/instance.rb +3 -8
  24. data/lib/rig/model/template.rb +1 -1
  25. data/lib/rig/model/userdata.rb +1 -1
  26. data/lib/rig/plugin.rb +61 -0
  27. data/lib/rig/version.rb +2 -2
  28. metadata +15 -35
  29. data/lib/rig/chef.rb +0 -59
  30. data/lib/rig/command/balancer/destroy.rb +0 -16
  31. data/lib/rig/command/balancer/list.rb +0 -30
  32. data/lib/rig/command/balancer/listener.rb +0 -31
  33. data/lib/rig/command/balancer/main.rb +0 -40
  34. data/lib/rig/command/balancer/view.rb +0 -19
  35. data/lib/rig/command/dns/create.rb +0 -19
  36. data/lib/rig/command/dns/destroy.rb +0 -16
  37. data/lib/rig/command/dns/edit.rb +0 -20
  38. data/lib/rig/command/dns/list.rb +0 -21
  39. data/lib/rig/command/dns/main.rb +0 -13
  40. data/lib/rig/command/environment/create.rb +0 -21
  41. data/lib/rig/command/environment/destroy.rb +0 -18
  42. data/lib/rig/command/environment/list.rb +0 -31
  43. data/lib/rig/command/environment/main.rb +0 -15
  44. data/lib/rig/command/environment/protect.rb +0 -22
  45. data/lib/rig/command/instance/create.rb +0 -60
  46. data/lib/rig/command/instance/destroy.rb +0 -19
  47. data/lib/rig/command/instance/list.rb +0 -26
  48. data/lib/rig/command/instance/main.rb +0 -16
  49. data/lib/rig/command/instance/tag/get.rb +0 -20
  50. data/lib/rig/command/instance/tag/main.rb +0 -14
  51. data/lib/rig/command/instance/tag/remove.rb +0 -44
  52. data/lib/rig/command/instance/tag/set.rb +0 -46
  53. data/lib/rig/command/instance/view.rb +0 -20
  54. data/lib/rig/command/keypair/list.rb +0 -14
  55. data/lib/rig/command/keypair/main.rb +0 -11
  56. data/lib/rig/command/main.rb +0 -90
  57. data/lib/rig/model/database.rb +0 -23
  58. data/lib/rig/model/database/base.rb +0 -11
  59. data/lib/rig/model/database/mongodb.rb +0 -70
  60. data/lib/rig/model/database/mysql.rb +0 -12
@@ -1,49 +1,50 @@
1
-
2
1
  module Rig
3
- class << self
2
+ module Config
3
+ class << self
4
+ attr_reader :loaded
5
+ attr_reader :dir
4
6
 
5
- def configdir
6
- @configdir ||= File.expand_path(ENV["RIG_CONFIG"] || "~/.rig")
7
- end
7
+ @config = {}
8
+ @loaded = false
8
9
 
9
- def config
10
- @configuration ||= begin
11
- c = YAML::load_file("#{configdir}/config.yml")
10
+ def data
11
+ @data ||= begin
12
+ c = YAML::load_file("#{dir}/config.yml")
13
+
14
+ Rig::Log.load(c[:logging])
12
15
 
13
- # prevent Chef and Capistrano being loaded at the same time
14
- # requirements on net-ssh conflict (chef ~> 2.1.4)
15
- #
16
- # this makes it so we don't have to use bundler to run
17
- # rig and cap together.
18
- if c[:chef] && !defined?(Rig::Capistrano)
19
- require 'rig/chef'
16
+ Rig::Log.debug "loading plugins from configuration"
17
+ Rig::Plugin.load(c[:plugins]) if c[:plugins]
18
+
19
+ c
20
20
  end
21
+ @loaded = true
22
+ @data
23
+ end
21
24
 
22
- c
25
+ def dir
26
+ @dir ||= File.expand_path(ENV["RIG_CONFIG"] || "~/.rig")
23
27
  end
24
- end
25
28
 
26
- def save_configuration
27
- yaml = config.to_yaml
28
- file = "#{configdir}/config.yml"
29
- back = "#{file}.#{Time.now.to_i}"
30
- puts "backing up configuration -> #{back}"
31
- FileUtils.cp(file, back)
32
- File.open(file, "w") {|f| f.write(yaml+"\n")}
33
- puts "new configuration saved."
29
+ def save
30
+ yaml = @config.to_yaml
31
+ file = "#@dir/config.yml"
32
+ back = "#{file}.#{Time.now.to_i}"
33
+ puts "backing up configuration -> #{back}"
34
+ FileUtils.cp(file, back)
35
+ File.open(file, "w") { |f| f.write(yaml+"\n") }
36
+ puts "new configuration saved."
37
+ end
34
38
  end
39
+ end
35
40
 
36
- def account(name=get_account)
37
- @account ||= begin
38
- a = Rig::Model::Account.load(name)
39
- a[:name] = name
40
- a
41
- end
41
+ class << self
42
+ def config
43
+ Rig::Config.data
42
44
  end
43
45
 
44
- def save_account
45
- name = Rig.account[:name]
46
- Rig::Model::Account.save(name, Rig.account)
46
+ def account
47
+ Rig::Account.data
47
48
  end
48
49
 
49
50
  def get_config(name)
@@ -54,76 +55,5 @@ module Rig
54
55
  Rig::Model::Template.load(name)
55
56
  end
56
57
 
57
- def get_account
58
- return ENV['RIG_ACCOUNT'] if ENV['RIG_ACCOUNT']
59
- return Rig.config[:account] if Rig.config[:account]
60
- return Rig.config[:default_account] if Rig.config[:default_account]
61
- return Rig.config[:accounts].first if Rig.config[:accounts] && Rig.config[:accounts].count > 0
62
- "default"
63
- end
64
-
65
- end
66
-
67
- class Oconfig
68
- class << self
69
- def load_yaml_file(path)
70
- file = File.expand_path(path)
71
- raise "Configuration not found: #{path} (#{file})" unless File.exists?(file)
72
- yaml = YAML.load_file(file)
73
- yaml = yaml[yaml.keys.first] if yaml.keys.count == 1
74
- self.new(yaml)
75
- end
76
- end
77
-
78
- def initialize(data={})
79
- @data = {}
80
- update!(data)
81
- end
82
-
83
- def update!(data)
84
- data.each do |key, value|
85
- self[key] = value
86
- end
87
- end
88
-
89
- def [](key)
90
- @data[key.to_sym]
91
- end
92
-
93
- def []=(key, value)
94
- if value.kind_of?(Hash)
95
- @data[key.to_sym] = Oconfig.new(value)
96
- elsif value.kind_of?(Array)
97
- @data[key.to_sym] = []
98
- value.each do |v|
99
- @data[key.to_sym] << Oconfig.new(v)
100
- end
101
- else
102
- @data[key.to_sym] = value
103
- end
104
- end
105
-
106
- def method_missing(sym, *args)
107
- if sym.to_s =~ /(.+)=$/
108
- self[$1] = args.first
109
- else
110
- self[sym]
111
- end
112
- end
113
-
114
- def to_hash
115
- out = {}
116
- @data.each do |k, v|
117
- out[k] = v.kind_of?(Oconfig) ? v.to_hash : v
118
- end
119
- out
120
- end
121
-
122
- if defined?(YAML)
123
- def to_yaml
124
- to_hash.to_yaml
125
- end
126
- end
127
-
128
58
  end
129
59
  end
@@ -0,0 +1,66 @@
1
+ require 'logger'
2
+
3
+ module Rig
4
+ module Log
5
+ LEVELS = {
6
+ :debug => ::Logger::DEBUG,
7
+ :info => ::Logger::INFO,
8
+ :warn => ::Logger::WARN,
9
+ :error => ::Logger::ERROR,
10
+ :fatal => ::Logger::FATAL
11
+ }
12
+ @loggers = []
13
+
14
+ class << self
15
+ def load(logging={ })
16
+ begin
17
+ logging.each do |name, data|
18
+ level = data[:level]
19
+ opts = [*data[:opts]].flatten
20
+ dest = opts.shift
21
+
22
+ if %w{STDOUT STDERR}.include?(dest)
23
+ # convert string names to constants
24
+ dest = Kernel.const_get(dest)
25
+ elsif dest !~ /^\//
26
+ # relative paths are relative to configdir
27
+ dest = "#{Rig::Config.dir}/#{dest}"
28
+ end
29
+
30
+ opts = opts.unshift(dest)
31
+
32
+ l = Logger.new(*opts)
33
+ l.level = Rig::Log::LEVELS[level.to_sym]
34
+
35
+ Rig::Log.add(l)
36
+ end
37
+
38
+ Rig::Log.debug "logging initialized"
39
+ rescue => e
40
+ puts "error configuring logging: #{e.message} at #{e.backtrace.first}"
41
+ end
42
+ end
43
+
44
+ def add(logger)
45
+ logger.formatter = proc { |severity, datetime, progname, msg| "#{$$}:#{datetime.strftime("%s")} #{severity[0]} #{msg}\n" }
46
+ @loggers << logger
47
+ end
48
+
49
+ def level=(level)
50
+ int = LEVELS.key?(level) ? LEVELS[level] : level
51
+ @loggers.each { |l| l.level = int }
52
+ end
53
+
54
+ # Define the standard logger methods on this class programmatically.
55
+ # No need to incur method_missing overhead on every log call.
56
+ # borrowed from Chef::Log
57
+ LEVELS.keys.each do |method_name|
58
+ class_eval(<<-METHOD_DEFN, __FILE__, __LINE__)
59
+ def #{method_name}(msg=nil, &block)
60
+ @loggers.each {|l| l.#{method_name}(msg, &block) }
61
+ end
62
+ METHOD_DEFN
63
+ end
64
+ end
65
+ end
66
+ end
@@ -27,7 +27,7 @@ module Rig
27
27
 
28
28
  private
29
29
  def dir
30
- "#{Rig.configdir}/accounts"
30
+ "#{Rig::Config.dir}/accounts"
31
31
  end
32
32
  end
33
33
  end
@@ -6,24 +6,31 @@ module Rig
6
6
  class << self
7
7
  def compute
8
8
  @compute ||= begin
9
- Fog::Compute.new(Rig.account[:compute])
9
+ if Rig.account[:compute]
10
+ Fog::Compute.new(Rig.account[:compute])
11
+ end
10
12
  end
11
13
  end
12
14
  def balancer
13
15
  @balancer ||= begin
14
- #TODO: figure out the standardized way of handling balancers
15
- Fog::AWS::ELB.new(Rig.account[:balancer])
16
+ if Rig.account[:balancer]
17
+ #TODO: figure out the standardized way of handling balancers
18
+ Fog::AWS::ELB.new(Rig.account[:balancer])
19
+ end
16
20
  end
17
21
  end
18
22
  def dns
19
23
  @dns ||= begin
20
- Fog::DNS.new(Rig.account[:dns])
24
+ if Rig.account[:dns]
25
+ Fog::DNS.new(Rig.account[:dns])
26
+ end
21
27
  end
22
28
  end
23
-
24
29
  def database
25
30
  @database ||= begin
26
- Rig::Model::Database.new(Rig.account[:database])
31
+ if Rig.account[:database]
32
+ Rig::Model::Database.new(Rig.account[:database])
33
+ end
27
34
  end
28
35
  end
29
36
  end
@@ -113,10 +113,10 @@ module Rig
113
113
  end
114
114
 
115
115
  # if we've got a chef config, use it
116
- if Rig.config[:chef]
117
- puts "creating chef environment"
118
- Rig::Chef.environment_create(name)
119
- end
116
+ #if Rig.config[:chef]
117
+ # puts "creating chef environment"
118
+ # Rig::Chef.environment_create(name)
119
+ #end
120
120
 
121
121
  # all of this crap to be absolutely sure that the instance has an ip and dns
122
122
  puts "sleeping 3"
@@ -136,6 +136,8 @@ module Rig
136
136
  puts "server #{server.id} doesn't have public_ip_address"
137
137
  end
138
138
  end
139
+
140
+ Rig::Plugin.run "environment:create", self
139
141
  rescue => e
140
142
  puts "error creating environment:\n#{e.message} at #{e.backtrace.join("\n")}"
141
143
  destroy
@@ -163,10 +165,12 @@ module Rig
163
165
  puts "destroying balancers"
164
166
  Rig::Model::Balancer.destroy(@balancers)
165
167
 
166
- if Rig.config[:chef]
167
- puts "destroying chef environment"
168
- Rig::Chef.environment_destroy(name)
169
- end
168
+ #if Rig.config[:chef]
169
+ # puts "destroying chef environment"
170
+ # Rig::Chef.environment_destroy(name)
171
+ #end
172
+
173
+ Rig::Plugin.run "environment:destroy", self
170
174
  end
171
175
 
172
176
  def reload
@@ -41,6 +41,7 @@ module Rig
41
41
  server = fog.servers.create(opts)
42
42
  fog.create_tags(server.id, tags) if tags.count > 0
43
43
  puts ".. created: #{server.id}"
44
+ Rig::Plugin.run "instance:create", server
44
45
  server
45
46
  end
46
47
 
@@ -52,17 +53,11 @@ module Rig
52
53
  ids = list.map {|e| e.kind_of?(String) ? e : e.id}
53
54
  puts ".. destroying: #{ids.inspect}"
54
55
 
55
- # if we've got a chef config, use it
56
- if Rig.config[:chef]
57
- list.each do |e|
58
- Rig::Chef.client_delete(e.tags['Name']) if e.tags['Name']
59
- Rig::Chef.node_delete(e.tags['Name']) if e.tags['Name']
60
- end
61
- end
62
-
63
56
  # destroy the instances
64
57
  list.each do |e|
65
58
  e.destroy
59
+
60
+ Rig::Plugin.run "instance:destroy", e, e.tags
66
61
  end
67
62
 
68
63
  tags = %w{Name Environment Role}.inject({}) {|h, e| h[e] = nil; h }
@@ -26,7 +26,7 @@ module Rig
26
26
 
27
27
  private
28
28
  def dir
29
- "#{Rig.configdir}/templates"
29
+ "#{Rig::Config.dir}/templates"
30
30
  end
31
31
  end
32
32
  end
@@ -7,7 +7,7 @@ module Rig
7
7
  class << self
8
8
  def create(name, role, environment, opts={ })
9
9
  package = Rig.get_config(:userdata)
10
- directory = "#{Rig.configdir}/userdata/#{package}"
10
+ directory = "#{Rig::Config.dir}/userdata/#{package}"
11
11
  config = YAML.load_file("#{directory}/userdata.yml")
12
12
 
13
13
  data = {
@@ -0,0 +1,61 @@
1
+ module Rig
2
+ module Plugin
3
+ class << self
4
+ def run(event, *args)
5
+ @hooks ||= []
6
+ @hooks.select { |e| e[:event] == event }.each do |plugin|
7
+ klass = plugin[:class]
8
+ block = plugin[:block]
9
+
10
+ Rig::Log.debug "calling #{klass} :: #{event}"
11
+ block.call(args.dup)
12
+ end
13
+ end
14
+
15
+ def on(klass, event, &block)
16
+ @hooks ||= []
17
+ @hooks << { :event => event, :class => klass, :block => block }
18
+ end
19
+
20
+ def load(plugins={ })
21
+ plugins.each do |plugin, data|
22
+ begin
23
+ f = "#{Rig::Config.dir}/plugins/#{plugin}"
24
+ Rig::Log.debug "loading plugin: #{plugin} #{f}"
25
+ require f
26
+ rescue LoadError, StandardError => e
27
+ Rig::Log.error "error while loading plugin: #{plugin}: #{e.message} at #{e.backtrace.first}"
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ module Base
34
+ def self.included(base)
35
+ base.extend self
36
+ end
37
+
38
+ def on(event, &block)
39
+ Rig::Plugin.on(self, event, &block)
40
+ end
41
+
42
+ def command(name, desc, klass=nil, &block)
43
+ on "commands:loaded" do |args|
44
+ command = args.shift
45
+ if klass
46
+ command.subcommand name, "#{desc} (#{self})", klass
47
+ else
48
+ command.subcommand name, "#{desc} (#{self})", &block
49
+ end
50
+ end
51
+ end
52
+
53
+ def config_for(klass)
54
+ name = klass.name.split('::').last.downcase.to_sym
55
+ return Rig.config[:plugins][name] if Rig.config[:plugins] && Rig.config[:plugins][name]
56
+ return Rig.account[:plugins][name] if Rig.account[:plugins] && Rig.account[:plugins][name]
57
+ { }
58
+ end
59
+ end
60
+ end
61
+ end
@@ -2,8 +2,8 @@ unless defined?(Rig::Version)
2
2
  module Rig
3
3
  module Version
4
4
  MAJOR = 0
5
- MINOR = 4
6
- TINY = 5
5
+ MINOR = 5
6
+ TINY = 0
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.4.5
4
+ version: 0.5.0
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-05-24 00:00:00.000000000 Z
12
+ date: 2012-06-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: clamp
@@ -124,6 +124,10 @@ files:
124
124
  - bin/rig
125
125
  - conf/accounts/default.yml
126
126
  - conf/config.yml
127
+ - conf/plugins/chef.rb
128
+ - conf/plugins/mongo.rb
129
+ - conf/plugins/mysql.rb
130
+ - conf/plugins/scout.rb
127
131
  - conf/templates/multi.yml
128
132
  - conf/templates/solo.yml
129
133
  - conf/userdata/chef/userdata.sh.erb
@@ -132,56 +136,31 @@ files:
132
136
  - conf/userdata/default/userdata.sh.erb
133
137
  - conf/userdata/default/userdata.yml
134
138
  - lib/rig.rb
139
+ - lib/rig/account.rb
135
140
  - lib/rig/capistrano.rb
136
- - lib/rig/chef.rb
137
141
  - lib/rig/command.rb
138
142
  - lib/rig/command/abstract.rb
139
143
  - lib/rig/command/account.rb
140
- - lib/rig/command/balancer/destroy.rb
141
- - lib/rig/command/balancer/list.rb
142
- - lib/rig/command/balancer/listener.rb
143
- - lib/rig/command/balancer/main.rb
144
- - lib/rig/command/balancer/view.rb
144
+ - lib/rig/command/balancer.rb
145
145
  - lib/rig/command/config.rb
146
- - lib/rig/command/database.rb
147
- - lib/rig/command/dns/create.rb
148
- - lib/rig/command/dns/destroy.rb
149
- - lib/rig/command/dns/edit.rb
150
- - lib/rig/command/dns/list.rb
151
- - lib/rig/command/dns/main.rb
152
- - lib/rig/command/environment/create.rb
153
- - lib/rig/command/environment/destroy.rb
154
- - lib/rig/command/environment/list.rb
155
- - lib/rig/command/environment/main.rb
156
- - lib/rig/command/environment/protect.rb
157
- - lib/rig/command/instance/create.rb
158
- - lib/rig/command/instance/destroy.rb
159
- - lib/rig/command/instance/list.rb
160
- - lib/rig/command/instance/main.rb
161
- - lib/rig/command/instance/tag/get.rb
162
- - lib/rig/command/instance/tag/main.rb
163
- - lib/rig/command/instance/tag/remove.rb
164
- - lib/rig/command/instance/tag/set.rb
165
- - lib/rig/command/instance/view.rb
166
- - lib/rig/command/keypair/list.rb
167
- - lib/rig/command/keypair/main.rb
168
- - lib/rig/command/main.rb
146
+ - lib/rig/command/dns.rb
147
+ - lib/rig/command/environment.rb
148
+ - lib/rig/command/keypair.rb
169
149
  - lib/rig/command/options.rb
150
+ - lib/rig/command/server.rb
170
151
  - lib/rig/command/ssh.rb
171
152
  - lib/rig/config.rb
153
+ - lib/rig/log.rb
172
154
  - lib/rig/model.rb
173
155
  - lib/rig/model/account.rb
174
156
  - lib/rig/model/balancer.rb
175
157
  - lib/rig/model/connection.rb
176
- - lib/rig/model/database.rb
177
- - lib/rig/model/database/base.rb
178
- - lib/rig/model/database/mongodb.rb
179
- - lib/rig/model/database/mysql.rb
180
158
  - lib/rig/model/dns.rb
181
159
  - lib/rig/model/environment.rb
182
160
  - lib/rig/model/instance.rb
183
161
  - lib/rig/model/template.rb
184
162
  - lib/rig/model/userdata.rb
163
+ - lib/rig/plugin.rb
185
164
  - lib/rig/version.rb
186
165
  - rig.gemspec
187
166
  homepage: https://github.com/shawncatz/rig
@@ -209,3 +188,4 @@ signing_key:
209
188
  specification_version: 3
210
189
  summary: Cloud provisioning tool built on ruby fog
211
190
  test_files: []
191
+ has_rdoc: