rig 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,7 +12,7 @@ module Rig
12
12
  env = ENV['ENVIRONMENT'] || Rig.config.environment
13
13
  role = ENV['ROLE'] || Rig.config.role
14
14
  servers = Rig::Model::Environment.find(env).servers
15
- list = servers.select { |s| s.tags['Role'] == role }
15
+ list = role == 'all' ? servers : servers.select { |s| s.tags['Role'] == role }
16
16
 
17
17
  raise "Rig could not find any servers matching environment=#{env} and role=#{role}" unless list && list.count > 0
18
18
  list.each do |s|
@@ -41,8 +41,9 @@ configuration.load do
41
41
  Rig.config.environment = ARGV[0]
42
42
  Rig.config.role = ARGV[1]
43
43
 
44
+ # create dummy tasks for environment and role
44
45
  begin
45
- [ARGV[0], ARGV[1]].each do |arg|
46
+ ARGV.first(2).each do |arg|
46
47
  task arg do
47
48
  nil
48
49
  end
data/lib/rig/chef.rb CHANGED
@@ -42,6 +42,12 @@ module Rig
42
42
  client.destroy if client
43
43
  end
44
44
 
45
+ def client_list
46
+ configure
47
+ # TODO: better error handling
48
+ ::Chef::ApiClient.list
49
+ end
50
+
45
51
  def node_delete(name)
46
52
  configure
47
53
  # TODO: better error handling
data/lib/rig/command.rb CHANGED
@@ -15,6 +15,7 @@ require 'rig/command/environment/main'
15
15
  require 'rig/command/environment/list'
16
16
  require 'rig/command/environment/create'
17
17
  require 'rig/command/environment/destroy'
18
+ require 'rig/command/environment/protect'
18
19
 
19
20
  require 'rig/command/balancer/main'
20
21
  require 'rig/command/balancer/list'
@@ -4,9 +4,19 @@ module Rig
4
4
  module Command
5
5
  class Abstract < Clamp::Command
6
6
  option ["-n", "--test"], :flag, "use Fog.mock! to test command without creating / destroying cloud resources" do |o|
7
+ Rig.config.mock = true
7
8
  Fog.mock!
8
9
  end
9
- option ["-a", "--account"], "ACCOUNT", "set the rig account to use", :default => "default"
10
+
11
+ option ["-a", "--account"], "ACCOUNT", "set the rig account to use", :default => "default" do |a|
12
+ Rig.config.account = a
13
+ a
14
+ end
15
+
16
+ option ["-v", "--version"], :flag, "print version number and exit" do |v|
17
+ puts "Rig Version: #{Rig::Version::STRING}"
18
+ exit(0)
19
+ end
10
20
 
11
21
  def print_table(columns, rows)
12
22
  puts table(columns, *rows)
@@ -19,6 +29,7 @@ module Rig
19
29
  r << server.tags['Name']
20
30
  r << server.tags['Environment']
21
31
  r << server.tags['Role']
32
+ r << (server.tags['Protected'] ? '*' : '')
22
33
  r << server.public_ip_address.to_s
23
34
  r << server.private_ip_address.to_s
24
35
  r << server.flavor_id.to_s
@@ -27,7 +38,7 @@ module Rig
27
38
  rows << r
28
39
  end
29
40
 
30
- print_table(%w{Name Environment Role PublicIP PrivateIP Size InstanceID State}, rows)
41
+ print_table(%w{Name Environment Role Pr PublicIP PrivateIP Size InstanceID State}, rows)
31
42
  end
32
43
 
33
44
  def execute
@@ -17,9 +17,9 @@ module Rig
17
17
  rows = []
18
18
  envs.each do |e|
19
19
  env = Rig::Model::Environment.find(e)
20
- rows << [env.name, env.template, env.region, env.servers.count, env.balancers.count]
20
+ rows << [env.name, env.template, env.region, env.servers.count, env.balancers.count, env.protected?]
21
21
  end
22
- print_table(%w{Name Template Region Servers# Balancers#}, rows)
22
+ print_table(%w{Name Template Region Servers# Balancers# Protected?}, rows)
23
23
  end
24
24
  end
25
25
  end
@@ -0,0 +1,22 @@
1
+ module Rig
2
+ module Command
3
+ module Environment
4
+ class Protect < Abstract
5
+ option ["-u","--undo"], :flag, "disable protection instead of enabling"
6
+ parameter "NAME", "the environment to protect"
7
+
8
+ def execute
9
+ env = Rig::Model::Environment.find(name)
10
+ raise "Environment #{name} not found" if env.nil?
11
+ connection = Rig::Connection.compute
12
+ val = undo? ? "false" : "true"
13
+
14
+ env.servers.each do |s|
15
+ connection.create_tags(s.id, "Protected" => val)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ Rig::Command::Environment::Main.subcommand "protect", "[un]set protection on environment, preventing termination", Rig::Command::Environment::Protect
@@ -4,32 +4,12 @@ module Rig
4
4
  module Tag
5
5
  class Get < Abstract
6
6
  include Options::Config
7
- include Options::Instance
8
- include Options::InstanceName
9
7
 
10
- def execute
11
- unless iname || iid
12
- raise "Must set either instance name or instance id (-iname or -iid)"
13
- end
14
-
15
- connection = Rig::Connection.compute
16
- if iid
17
- instance = connection.servers.get(iid)
18
- else
19
- list = connection.servers.all({"tag:Name" => iname})
20
- if list.count > 1
21
- puts "there is more than one instance that matches:"
22
- instance_list(list)
23
- return
24
- end
25
- instance = list.first
26
- end
27
-
28
- unless instance
29
- puts "could not find instance"
30
- return
31
- end
8
+ parameter "NAME", "the name of the instance to get tags for"
32
9
 
10
+ def execute
11
+ instance = Rig::Model::Instance.find(name)
12
+ raise "instance not found #{name}" unless instance
33
13
  puts "#{instance.tags.inspect}"
34
14
  end
35
15
  end
@@ -4,13 +4,13 @@ module Rig
4
4
  class View < Abstract
5
5
  include Options::ShowAll
6
6
 
7
+ # TODO: something wrong here, view is showing all instances
7
8
  parameter "NAME", "name of instance"
8
9
 
9
10
  def execute
10
11
  list = Rig::Model::Instance.find(name)
11
12
  list.each do |item|
12
- puts item.attributes.to_yaml
13
- puts
13
+ ap item.attributes
14
14
  end
15
15
  end
16
16
  end
@@ -12,9 +12,24 @@ module Rig
12
12
  subcommand "accounts", "show account list" do
13
13
  def execute
14
14
  list = Rig::Model::Account.list
15
+ rows = []
15
16
  list.each do |n, f|
16
- puts " #{n} => #{f}"
17
+ rows << [(Rig.config.default_account == n.to_s), (Rig.account.name == n.to_s), n, f]
17
18
  end
19
+ print_table(%w{Default Current Name Location}, rows)
20
+ end
21
+ end
22
+
23
+ subcommand "chef", "test chef configuration" do
24
+ def execute
25
+ raise "chef not configured" unless Rig.config.chef
26
+
27
+ list = Rig::Chef.client_list
28
+ rows = []
29
+ list.each do |c|
30
+ rows << c
31
+ end
32
+ print_table(%w{Name Location}, rows)
18
33
  end
19
34
  end
20
35
 
data/lib/rig/config.rb CHANGED
@@ -1,9 +1,6 @@
1
1
 
2
2
  module Rig
3
3
  class << self
4
- @templates = []
5
- @accounts = []
6
- @cache_config = []
7
4
 
8
5
  def configdir
9
6
  @configdir ||= File.expand_path(ENV["RIG_CONFIG"] || "~/.rig")
@@ -12,15 +9,24 @@ module Rig
12
9
  def config
13
10
  @configuration ||= begin
14
11
  c = Rig::Oconfig.load_yaml_file("#{configdir}/config.yml")
15
- if c.chef
12
+
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)
16
19
  require 'rig/chef'
17
20
  end
21
+
18
22
  c
19
23
  end
20
24
  end
21
25
 
22
26
  def account(name=get_account)
23
- Rig::Model::Account.load(name)
27
+ a = Rig::Model::Account.load(name)
28
+ a.name = name
29
+ a
24
30
  end
25
31
 
26
32
  def get_config(name)
@@ -33,10 +39,12 @@ module Rig
33
39
 
34
40
  def get_account
35
41
  return ENV['RIG_ACCOUNT'] if ENV['RIG_ACCOUNT']
42
+ return Rig.config.account if Rig.config.account
36
43
  return Rig.config.default_account if Rig.config.default_account
37
44
  return Rig.config.accounts.first if Rig.config.accounts.count > 0
38
45
  "default"
39
46
  end
47
+
40
48
  end
41
49
 
42
50
  class Oconfig
@@ -10,28 +10,28 @@ module Rig
10
10
  attr_reader :region
11
11
  attr_reader :flavor
12
12
 
13
- TEMPLATES = {
14
- :solo => {
15
- :groups => %w{app-server db-inqcloud-dev},
16
- :instances => [
17
- :solo => {
18
- :flavor => 'c1.medium',
19
- :count => 1,
20
- :balance => true,
21
- :primary => true
22
- }
23
- ]
24
- },
25
- :multi => {
26
- :groups => %w{app-server db-inqcloud-dev},
27
- :instances => [
28
- :harvester => { :flavor => 'c1.large', :count => 3, :image => "ami-faketest" },
29
- :queue => { :flavor => 'c1.medium', :count => 3 },
30
- :cache => { :flavor => 'm1.medium', :count => 3 },
31
- :app => { :flavor => 'c1.large', :count => 3, :balance => true, :primary => true }
32
- ]
33
- }
34
- }
13
+ #TEMPLATES = {
14
+ # :solo => {
15
+ # :groups => %w{app-server db-inqcloud-dev},
16
+ # :instances => [
17
+ # :solo => {
18
+ # :flavor => 'c1.medium',
19
+ # :count => 1,
20
+ # :balance => true,
21
+ # :primary => true
22
+ # }
23
+ # ]
24
+ # },
25
+ # :multi => {
26
+ # :groups => %w{app-server db-inqcloud-dev},
27
+ # :instances => [
28
+ # :harvester => { :flavor => 'c1.large', :count => 3, :image => "ami-faketest" },
29
+ # :queue => { :flavor => 'c1.medium', :count => 3 },
30
+ # :cache => { :flavor => 'm1.medium', :count => 3 },
31
+ # :app => { :flavor => 'c1.large', :count => 3, :balance => true, :primary => true }
32
+ # ]
33
+ # }
34
+ #}
35
35
 
36
36
  class << self
37
37
  def list
@@ -55,6 +55,7 @@ module Rig
55
55
 
56
56
  def destroy(name, template=nil, opts={ })
57
57
  env = self.find(name, template, opts)
58
+ raise "environment has protected instances" if env.protected?
58
59
  puts "destroying: #{name}"
59
60
  env.destroy
60
61
  true
@@ -205,6 +206,11 @@ module Rig
205
206
  def reload
206
207
  find_servers
207
208
  find_balancers
209
+ @protected = @servers.any? {|s| s.tags["Protected"] == "true" }
210
+ end
211
+
212
+ def protected?
213
+ @protected
208
214
  end
209
215
 
210
216
  private
@@ -5,7 +5,7 @@ module Rig
5
5
  class Instance
6
6
  class << self
7
7
  def all(filters={})
8
- Rig::Connection.compute.servers.all(filters)
8
+ @all ||= Rig::Connection.compute.servers.all(filters)
9
9
  end
10
10
 
11
11
  def running
@@ -14,9 +14,11 @@ module Rig
14
14
 
15
15
  def find(id)
16
16
  if id =~ /^i-/
17
- Rig::Connection.compute.servers.find(id)
17
+ puts "find #{id}"
18
+ Rig::Connection.compute.servers.get(id)
18
19
  else
19
20
  # must be tag:Name
21
+ puts "all tag:Name => #{id}"
20
22
  Rig::Connection.compute.servers.all({"tag:Name"=>id})
21
23
  end
22
24
  end
@@ -24,6 +26,7 @@ module Rig
24
26
  def find_by_environment(env)
25
27
  Rig::Connection.compute.servers.all({"tag:Environment"=>env})
26
28
  end
29
+
27
30
  def find_by_role_and_environment(role, env)
28
31
  Rig::Connection.compute.servers.all({"tag:Environment"=> env, "tag:Role" => role})
29
32
  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 = 3
6
- TINY = 4
6
+ TINY = 5
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rig
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 4
10
- version: 0.3.4
9
+ - 5
10
+ version: 0.3.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Shawn Catanzarite
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-04-26 00:00:00 Z
18
+ date: 2012-04-30 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: clamp
@@ -139,6 +139,7 @@ files:
139
139
  - lib/rig/command/environment/destroy.rb
140
140
  - lib/rig/command/environment/list.rb
141
141
  - lib/rig/command/environment/main.rb
142
+ - lib/rig/command/environment/protect.rb
142
143
  - lib/rig/command/instance/create.rb
143
144
  - lib/rig/command/instance/destroy.rb
144
145
  - lib/rig/command/instance/list.rb
@@ -200,3 +201,4 @@ specification_version: 3
200
201
  summary: Cloud provisioning tool built on ruby fog
201
202
  test_files: []
202
203
 
204
+ has_rdoc: