claws 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/claws CHANGED
@@ -4,33 +4,10 @@ require 'optparse'
4
4
  require 'ostruct'
5
5
  require 'yaml'
6
6
 
7
- options = OpenStruct.new(
8
- {
9
- :config_file => nil,
10
- :connect => true,
11
- :initialize => false,
12
- :selection => nil,
13
- }
14
- )
7
+ options = Claws::Options.parse
15
8
 
16
- OptionParser.new do |opts|
17
- opts.banner = 'Usage: claws [options]'
9
+ Claws::Command::Initialize.exec if options.initialize
18
10
 
19
- opts.on('-s', '--status-only', 'Display host status only and exit') do
20
- options.connect = false
21
- end
11
+ Claws::Command::Version.exec if options.version
22
12
 
23
- opts.on('-c', '--choice N', Float, 'Enter the number of the host to automatically connect to') do |n|
24
- options.selection = n.to_i
25
- end
26
-
27
- opts.on('-i', '--init', 'Install the default configuration file for the application') do
28
- options.initialize = true
29
- end
30
- end.parse!
31
-
32
- if options.initialize
33
- Claws::Command::Initialize.exec
34
- else
35
- Claws::Command::EC2.exec options
36
- end
13
+ Claws::Command::EC2.exec options
@@ -1,6 +1,8 @@
1
+ require 'claws/version'
1
2
  require 'claws/options'
2
3
  require 'claws/configuration'
3
4
  require 'claws/collection/ec2'
4
5
  require 'claws/report/ec2'
5
6
  require 'claws/command/initialize'
6
7
  require 'claws/command/ec2'
8
+ require 'claws/command/version'
@@ -3,16 +3,12 @@ require 'aws-sdk'
3
3
  module Claws
4
4
  module Collection
5
5
  class Base
6
- def self.connect(credentials)
7
- AWS.config(credentials)
8
- AWS.start_memoizing
9
- end
6
+ attr_accessor :config
10
7
 
11
- # Seems unnecessary
12
- def self.build
13
- collection = []
14
- yield(collection)
15
- collection
8
+ def initialize( config )
9
+ self.config = config
10
+ AWS.config(config.aws)
11
+ AWS.start_memoizing
16
12
  end
17
13
  end
18
14
  end
@@ -5,11 +5,19 @@ require 'claws/presenter/ec2'
5
5
  module Claws
6
6
  module Collection
7
7
  class EC2 < Claws::Collection::Base
8
- def self.get(filters = {})
8
+ def get(filters = {})
9
9
  collection = []
10
- AWS::EC2.new.instances.each do |instance|
11
- collection << Claws::EC2::Presenter.new(instance)
10
+
11
+ AWS::EC2.new.regions.each do |region|
12
+ if config.ec2.regions
13
+ next unless config.ec2.regions.include?(region.name)
14
+ end
15
+
16
+ region.instances.each do |instance|
17
+ collection << Claws::EC2::Presenter.new(instance, :region => region.name)
18
+ end
12
19
  end
20
+
13
21
  collection
14
22
  end
15
23
  end
@@ -10,10 +10,8 @@ module Claws
10
10
  exit 1
11
11
  end
12
12
 
13
- Claws::Collection::EC2.connect( config.aws )
14
-
15
13
  begin
16
- instances = Claws::Collection::EC2.get
14
+ instances = Claws::Collection::EC2.new(config).get
17
15
  rescue Exception => e
18
16
  puts e.message
19
17
  end
@@ -0,0 +1,10 @@
1
+ module Claws
2
+ module Command
3
+ class Version
4
+ def self.exec
5
+ puts Claws::VERSION
6
+ exit 0
7
+ end
8
+ end
9
+ end
10
+ end
@@ -19,7 +19,12 @@ module Claws
19
19
  self.capistrano = OpenStruct.new( yaml['capistrano'] )
20
20
  self.ssh = OpenStruct.new( yaml['ssh'] )
21
21
  self.aws = yaml['aws']
22
- self.ec2 = OpenStruct.new( { :fields => yaml['ec2']['fields'] } )
22
+ self.ec2 = OpenStruct.new(
23
+ {
24
+ :fields => yaml['ec2']['fields'],
25
+ :regions => yaml['ec2']['regions'],
26
+ }
27
+ )
23
28
  end
24
29
  end
25
30
  end
@@ -5,31 +5,43 @@ module Claws
5
5
  def self.parse
6
6
  options = OpenStruct.new(
7
7
  {
8
+ :config_file => nil,
8
9
  :connect => true,
10
+ :initialize => false,
11
+ :version => false,
12
+ :selection => nil,
9
13
  :source => 'ec2',
10
- :choice => nil,
11
14
  }
12
15
  )
13
16
 
14
17
  OptionParser.new do |opts|
15
- opts.banner = "Usage: script/aws [options] [environment] [role]"
18
+ opts.banner = 'Usage: claws [options]'
16
19
 
17
- opts.on('-d', '--display-only', 'display host information only and exit') do
20
+ opts.on('-d', '--display-only', 'Display host status only and exit') do
18
21
  options.connect = false
19
22
  end
20
23
 
21
- opts.on('-c', '--choice N', Float, 'enter the identity number of the host to automatically connect to') do |n|
22
- options.choice = n.to_i
24
+ opts.on('-c', '--choice N', Float, 'Enter the number of the host to automatically connect to') do |n|
25
+ options.selection = n.to_i
23
26
  end
24
27
 
25
- opts.on('-s', '--source', 'define the AWS source - default is ec2') do
26
- options.source = 'elb'
27
- options.connect = false
28
+ opts.on('-i', '--init', 'Install the default configuration file for the application') do
29
+ options.initialize = true
30
+ end
31
+
32
+ opts.on('-s', '--source S', String, 'define the AWS source - default is ec2') do |source|
33
+ options.source = source
34
+ end
35
+
36
+ opts.on('-v', '--version', 'Display the version number and exit') do
37
+ options.version = true
28
38
  end
29
39
  end.parse!
30
40
 
31
- options.environment = ARGV.shift
32
- options.role = ARGV.shift
41
+ unless ARGV.empty?
42
+ options.environment = ARGV.shift
43
+ options.role = ARGV.shift
44
+ end
33
45
 
34
46
  options
35
47
  end
@@ -6,12 +6,17 @@ module Claws
6
6
  class Presenter
7
7
  attr_writer :roles
8
8
 
9
- def initialize(instance, has_roles = [])
9
+ def initialize(instance, options = {})
10
10
  @ec2 = instance.extend(Claws::Support)
11
- @roles = has_roles
11
+ @roles = options[:roles] || []
12
+ @region = options[:region]
12
13
  freeze
13
14
  end
14
15
 
16
+ def region
17
+ @region || 'N/A'
18
+ end
19
+
15
20
  def roles
16
21
  @roles.empty? ? 'N/A' : @roles.join(', ')
17
22
  end
@@ -1,3 +1,3 @@
1
1
  module Claws
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -5,26 +5,23 @@ require 'claws/configuration'
5
5
  describe Claws::Collection::Base do
6
6
  subject { Claws::Collection::Base }
7
7
 
8
- before :each do
9
- @yaml = {"capistrano_home"=>"test", "access_key_id"=>"asdf", "secret_access_key"=>"qwer"}
10
- @credentials = {:access_key_id => 'asdf', :secret_access_key => 'qwer'}
8
+ let(:credentials) do
9
+ {
10
+ :access_key_id => 'asdf',
11
+ :secret_access_key => 'qwer'
12
+ }
13
+ end
11
14
 
12
- @config = double('Claws::Configuration', :aws_credentials => @credentials)
13
- AWS.should_receive(:config).with(@credentials).and_return(true)
14
- AWS.should_receive(:start_memoizing).and_return(nil)
15
+ let(:config) do
16
+ double('Claws::Configuration', :aws => credentials)
15
17
  end
16
18
 
17
19
  it 'establishes a connection to the mothership' do
20
+ AWS.should_receive(:config).with(credentials).and_return(true)
21
+ AWS.should_receive(:start_memoizing).and_return(nil)
22
+
18
23
  expect {
19
- subject.connect(@config.aws_credentials)
24
+ subject.new(config)
20
25
  }.to_not raise_exception
21
26
  end
22
-
23
- it 'builds a collection' do
24
- subject.connect(@config.aws_credentials)
25
-
26
- subject.build do |collection|
27
- 10.times {|i| collection << i}
28
- end.should == (0..9).to_a
29
- end
30
27
  end
@@ -5,14 +5,81 @@ require 'claws/collection/ec2'
5
5
  describe Claws::Collection::EC2 do
6
6
  subject { Claws::Collection::EC2 }
7
7
 
8
- it 'gets all instances' do
9
- subject.should_receive(:get).with(no_args).and_return(
10
- [
11
- double('AWS::EC2::Instance'),
12
- double('AWS::EC2::Instance'),
13
- ]
8
+ let(:credentials) do
9
+ {
10
+ :access_key_id => 'asdf',
11
+ :secret_access_key => 'qwer'
12
+ }
13
+ end
14
+
15
+ let(:regions) do
16
+ [
17
+ double('AWS::EC2::Region',
18
+ :name => 'us-east-1',
19
+ :instances =>
20
+ [
21
+ double('AWS::EC2::Instance'),
22
+ double('AWS::EC2::Instance'),
23
+ ],
24
+ ),
25
+ double('AWS::EC2::Region',
26
+ :name => 'eu-east-1',
27
+ :instances =>
28
+ [
29
+ double('AWS::EC2::Instance'),
30
+ double('AWS::EC2::Instance'),
31
+ ],
32
+ ),
33
+ ]
34
+ end
35
+
36
+ context 'gets all instances in regions' do
37
+ it 'not defined in configuration' do
38
+ AWS.should_receive(:config).with(credentials).and_return(true)
39
+ AWS.should_receive(:start_memoizing).and_return(nil)
40
+
41
+ AWS::EC2.should_receive(:new).and_return(
42
+ double('AWS::EC2::RegionsCollection', :regions => regions)
43
+ )
44
+
45
+ config = double('Claws::Configuration',
46
+ :aws => credentials,
47
+ :ec2 => OpenStruct.new({:regions => nil}),
48
+ )
49
+
50
+ subject.new(config).get.size.should == 4
51
+ end
52
+
53
+ it 'defined in configuation' do
54
+ AWS.should_receive(:config).with(credentials).and_return(true)
55
+ AWS.should_receive(:start_memoizing).and_return(nil)
56
+
57
+ AWS::EC2.should_receive(:new).and_return(
58
+ double('AWS::EC2::RegionsCollection', :regions => regions)
59
+ )
60
+
61
+ config = double('Claws::Configuration',
62
+ :aws => credentials,
63
+ :ec2 => OpenStruct.new({:regions => %w(us-east-1 eu-east-1)}),
64
+ )
65
+
66
+ subject.new(config).get.size.should == 4
67
+ end
68
+ end
69
+
70
+ it 'gets all instances for specified regions' do
71
+ AWS.should_receive(:config).with(credentials).and_return(true)
72
+ AWS.should_receive(:start_memoizing).and_return(nil)
73
+
74
+ AWS::EC2.should_receive(:new).and_return(
75
+ double('AWS::EC2::RegionsCollection', :regions => regions)
76
+ )
77
+
78
+ config = double('Claws::Configuration',
79
+ :aws => credentials,
80
+ :ec2 => OpenStruct.new({:regions => %w(us-east-1)}),
14
81
  )
15
82
 
16
- subject.get.size.should == 2
83
+ subject.new(config).get.size.should == 2
17
84
  end
18
85
  end
@@ -50,10 +50,14 @@ describe Claws::Command::EC2 do
50
50
  let(:options) { OpenStruct.new( { :config_file => nil, } ) }
51
51
 
52
52
  context 'instance collections' do
53
-
54
53
  it 'retrieves' do
55
- Claws::Collection::EC2.should_receive(:connect).and_return(true)
56
- Claws::Collection::EC2.should_receive(:get).and_return([])
54
+ Claws::Collection::EC2.should_receive(:new).and_return(
55
+ double(Claws::Collection::EC2, :get =>
56
+ [
57
+ double(AWS::EC2::Instance, :id => 'test', :status => 'running', :dns_name => 'test.com'),
58
+ ]
59
+ )
60
+ )
57
61
 
58
62
  capture_stdout {
59
63
  subject.exec options
@@ -61,9 +65,11 @@ describe Claws::Command::EC2 do
61
65
  end
62
66
 
63
67
  it 'handles errors retrieving' do
64
- Claws::Collection::EC2.should_receive(:connect).and_return(true)
65
- Claws::Collection::EC2.should_receive(:get).and_raise(Exception)
66
- subject.should_receive(:puts).once
68
+ Claws::Collection::EC2.should_receive(:new).and_return(
69
+ double(Claws::Collection::EC2, :get => Exception.new)
70
+ )
71
+
72
+ #subject.should_receive(:puts).once
67
73
 
68
74
  expect {
69
75
  subject.exec options
@@ -72,8 +78,13 @@ describe Claws::Command::EC2 do
72
78
  end
73
79
 
74
80
  it 'performs report' do
75
- Claws::Collection::EC2.should_receive(:connect).and_return(true)
76
- Claws::Collection::EC2.should_receive(:get).and_return([])
81
+ Claws::Collection::EC2.should_receive(:new).and_return(
82
+ double(Claws::Collection::EC2, :get =>
83
+ [
84
+ double(AWS::EC2::Instance, :id => 'test', :status => 'running', :dns_name => 'test.com'),
85
+ ]
86
+ )
87
+ )
77
88
 
78
89
  expect {
79
90
  capture_stdout {
@@ -114,8 +125,9 @@ describe Claws::Command::EC2 do
114
125
  end
115
126
 
116
127
  it 'automatically connects to the server' do
117
- Claws::Collection::EC2.should_receive(:connect).and_return(true)
118
- Claws::Collection::EC2.should_receive(:get).and_return(instances)
128
+ Claws::Collection::EC2.should_receive(:new).and_return(
129
+ double(Claws::Collection::EC2, :get => instances)
130
+ )
119
131
 
120
132
  subject.should_receive(:puts).twice
121
133
  subject.should_receive(:system).with('ssh test@test.com').and_return(0)
@@ -136,8 +148,9 @@ describe Claws::Command::EC2 do
136
148
  end
137
149
 
138
150
  it 'handles user inputed selection from the command line' do
139
- Claws::Collection::EC2.should_receive(:connect).and_return(true)
140
- Claws::Collection::EC2.should_receive(:get).and_return(instances)
151
+ Claws::Collection::EC2.should_receive(:new).and_return(
152
+ double(Claws::Collection::EC2, :get => instances)
153
+ )
141
154
 
142
155
  subject.should_receive(:puts).twice
143
156
  subject.should_receive(:system).with('ssh test@test2.com').and_return(0)
@@ -149,8 +162,9 @@ describe Claws::Command::EC2 do
149
162
  end
150
163
 
151
164
  it 'presents a selection and connects to the server' do
152
- Claws::Collection::EC2.should_receive(:connect).and_return(true)
153
- Claws::Collection::EC2.should_receive(:get).and_return(instances)
165
+ Claws::Collection::EC2.should_receive(:new).and_return(
166
+ double(Claws::Collection::EC2, :get => instances)
167
+ )
154
168
 
155
169
  subject.should_receive(:gets).and_return('1\n')
156
170
  subject.should_receive(:puts).once
@@ -162,8 +176,9 @@ describe Claws::Command::EC2 do
162
176
  end
163
177
 
164
178
  it 'presents a selection and allows a user to quit' do
165
- Claws::Collection::EC2.should_receive(:connect).and_return(true)
166
- Claws::Collection::EC2.should_receive(:get).and_return(instances)
179
+ Claws::Collection::EC2.should_receive(:new).and_return(
180
+ double(Claws::Collection::EC2, :get => instances)
181
+ )
167
182
 
168
183
  subject.should_receive(:gets).and_return('q\n')
169
184
 
@@ -13,6 +13,10 @@ describe Claws::Configuration do
13
13
  'aws_user' => 'test',
14
14
  },
15
15
  'ec2' => {
16
+ 'regions' => [
17
+ 'us-east-1',
18
+ 'eu-east-1',
19
+ ],
16
20
  'fields' => {
17
21
  'id' => {
18
22
  'width' => 10,
@@ -80,6 +84,12 @@ describe Claws::Configuration do
80
84
  YAML.should_receive(:load_file).and_return(yaml)
81
85
  end
82
86
 
87
+ it 'defines regions' do
88
+ regions = config.ec2.regions
89
+ regions[0].should == 'us-east-1'
90
+ regions[1].should == 'eu-east-1'
91
+ end
92
+
83
93
  context 'fields' do
84
94
  it 'defines id hash' do
85
95
  id = config.ec2.fields['id']
@@ -1,8 +1,6 @@
1
1
  require 'spec_helper'
2
2
  require 'claws/options'
3
3
 
4
- ARGV.clear
5
-
6
4
  def cli(args)
7
5
  ARGV.push(*args)
8
6
  yield
@@ -15,7 +13,7 @@ describe Claws::Options do
15
13
  options = Claws::Options.parse
16
14
  options.connect.should be_true
17
15
  options.source.should == 'ec2'
18
- options.choice.should be_nil
16
+ options.selection.should be_nil
19
17
  end
20
18
  end
21
19
 
@@ -35,11 +33,11 @@ describe Claws::Options do
35
33
 
36
34
  it 'accepts a choice flag' do
37
35
  cli %w{-c 10} do
38
- Claws::Options.parse.choice.should == 10
36
+ Claws::Options.parse.selection.should == 10
39
37
  end
40
38
 
41
39
  cli %w{--choice 10} do
42
- Claws::Options.parse.choice.should == 10
40
+ Claws::Options.parse.selection.should == 10
43
41
  end
44
42
  end
45
43
 
@@ -47,25 +45,23 @@ describe Claws::Options do
47
45
  cli %w{-s elb} do
48
46
  options = Claws::Options.parse
49
47
  options.source.should == 'elb'
50
- options.connect.should be_false
51
48
  end
52
49
 
53
50
  cli %w{--source elb} do
54
51
  options = Claws::Options.parse
55
52
  options.source.should == 'elb'
56
- options.connect.should be_false
57
53
  end
58
54
  end
59
55
 
60
56
  context 'capistrano' do
61
57
  it 'defines the environment' do
62
- cli %w{-s production app} do
58
+ cli %w{production app} do
63
59
  Claws::Options.parse.environment.should == 'production'
64
60
  end
65
61
  end
66
62
 
67
63
  it 'defines the role' do
68
- cli %w{-s production app} do
64
+ cli %w{production app} do
69
65
  Claws::Options.parse.role.should == 'app'
70
66
  end
71
67
  end
@@ -28,7 +28,7 @@ describe Claws::EC2::Presenter do
28
28
  cap = double('Claws::Capistrano')
29
29
  cap.stub(:roles).with(host).and_return(%w{app web})
30
30
 
31
- @full_presenter = subject.new(full_instance, cap.roles(full_instance.public_dns))
31
+ @full_presenter = subject.new(full_instance, :region => 'us-east-1', :roles => cap.roles(full_instance.public_dns))
32
32
 
33
33
  less_instance = double(AWS::EC2, :tags => nil)
34
34
  @less_presenter = subject.new(less_instance)
@@ -42,6 +42,16 @@ describe Claws::EC2::Presenter do
42
42
  end
43
43
  end
44
44
 
45
+ describe '#region' do
46
+ it 'can be defined' do
47
+ @full_presenter.region.should == 'us-east-1'
48
+ end
49
+
50
+ it 'is not required' do
51
+ @less_presenter.region.should == 'N/A'
52
+ end
53
+ end
54
+
45
55
  describe '#roles' do
46
56
  it 'can be defined' do
47
57
  @full_presenter.roles.should == 'app, web'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: claws
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-05-24 00:00:00.000000000 Z
13
+ date: 2012-05-27 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
17
- requirement: &2156422520 !ruby/object:Gem::Requirement
17
+ requirement: &2151922880 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 1.0.0
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2156422520
25
+ version_requirements: *2151922880
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: command_line_reporter
28
- requirement: &2156421940 !ruby/object:Gem::Requirement
28
+ requirement: &2151922140 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 3.2.1
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *2156421940
36
+ version_requirements: *2151922140
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: aws-sdk
39
- requirement: &2156420960 !ruby/object:Gem::Requirement
39
+ requirement: &2151921480 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,7 +44,7 @@ dependencies:
44
44
  version: '1.0'
45
45
  type: :runtime
46
46
  prerelease: false
47
- version_requirements: *2156420960
47
+ version_requirements: *2151921480
48
48
  description: A command line tool that provides a configurable report on the status
49
49
  of all of your EC2 hosts and provides trivial ssh access for connectivity. Never
50
50
  copy and paste the public dns for a host again!
@@ -59,6 +59,7 @@ files:
59
59
  - lib/claws/collection/ec2.rb
60
60
  - lib/claws/command/ec2.rb
61
61
  - lib/claws/command/initialize.rb
62
+ - lib/claws/command/version.rb
62
63
  - lib/claws/configuration.rb
63
64
  - lib/claws/options.rb
64
65
  - lib/claws/presenter/ec2.rb