broham 0.0.9 → 0.0.10

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.
@@ -1,15 +1,16 @@
1
1
  # SimpleDB interface
2
2
  require 'right_aws'
3
- RightAws::SdbInterface::API_VERSION = '2009-04-15'
4
3
  require 'sdb/active_sdb'
4
+ require 'broham/sdb'
5
+
5
6
  # Machine information
6
7
  require 'ohai'
7
8
  OHAI_INFO = Ohai::System.new unless defined?(OHAI_INFO)
8
9
  OHAI_INFO.all_plugins
9
10
  # Settings from Configliere.
10
11
  require 'configliere'
11
- Settings.define :access_key, :required => true, :description => "Amazon AWS access key ID -- found in your AWS console (http://bit.ly/awsconsole)"
12
- Settings.define :secret_access_key, :required => true, :description => "Amazon AWS secret access key -- found in your AWS console (http://bit.ly/awsconsole)"
12
+ Settings.define :access_key, :required => true, :description => "Amazon AWS access key ID -- found in your AWS console (http://bit.ly/awsconsole)", :broham => true
13
+ Settings.define :secret_access_key, :required => true, :description => "Amazon AWS secret access key -- found in your AWS console (http://bit.ly/awsconsole)", :broham => true
13
14
 
14
15
  ::Log = Logger.new(STDERR) unless defined?(Log)
15
16
 
@@ -30,11 +31,6 @@ class Broham < RightAws::ActiveSdb::Base
30
31
  select_by_role(role, :order => 'timestamp DESC')
31
32
  end
32
33
 
33
- # # Returns all hosts in the given role
34
- # def self.hosts role
35
- # select_all_by_role(role, :order => 'timestamp DESC')
36
- # end
37
-
38
34
  # Returns all hosts in the given role
39
35
  def self.hosts_like role
40
36
  select(:all, :order => 'timestamp DESC').select{|bro| bro[:role].to_s =~ /^#{role}/ }
@@ -110,7 +106,7 @@ class Broham < RightAws::ActiveSdb::Base
110
106
  # alternative syntax for #unregister
111
107
  def self.diss(*args) unregister *args ; end
112
108
  # alternative syntax for #unregister_like
113
- def self.fuck_all_yall(*arg) unregister_like *args ; end
109
+ def self.fuck_all_yall(*args) unregister_like *args ; end
114
110
 
115
111
  #
116
112
  # Registration attributes
@@ -1,24 +1,5 @@
1
1
  require 'json'
2
+ Configliere.use :config_file, :commandline, :git_style_binaries
2
3
 
3
4
  class Broham < RightAws::ActiveSdb::Base
4
- def self.get_cluster_settings
5
- Configliere.use :commandline, :config_file
6
- Settings.read('broham.yaml')
7
- Settings.resolve!
8
- self.establish_connection
9
- end
10
-
11
- def self.get_command_line_args *arg_names
12
- arg_names.each do |arg_name|
13
- Settings[arg_name] = Settings.rest.shift
14
- end
15
- check_args! *arg_names
16
- end
17
-
18
- def self.check_args! *arg_names
19
- if Settings[:role_name].blank?
20
- warn "Please supply a role as the first argument"
21
- exit(-1)
22
- end
23
- end
24
5
  end
@@ -0,0 +1,3 @@
1
+ RightAws::SdbInterface::API_VERSION = '2009-04-15'
2
+ require 'broham/sdb/active_sdb'
3
+ require 'broham/sdb/right_sdb_interface.rb'
@@ -0,0 +1,55 @@
1
+ class RightAws::ActiveSdb::Base
2
+
3
+ # Store in-memory attributes to SDB.
4
+ # Replaces the attributes values already stored at SDB by in-memory data.
5
+ # Returns a hash of stored attributes.
6
+ #
7
+ # sandy = Client.new(:name => 'Sandy') #=> #<Client:0xb775a7a8 @attributes={"name"=>["Sandy"]}, @new_record=true>
8
+ # sandy['toys'] = 'boys'
9
+ # sandy.put
10
+ # sandy['toys'] = 'patchwork'
11
+ # sandy.put
12
+ # sandy['toys'] = 'kids'
13
+ # sandy.put
14
+ # puts sandy.attributes.inspect #=> {"name"=>["Sandy"], "id"=>"b2832ce2-e461-11dc-b13c-001bfc466dd7", "toys"=>["kids"]}
15
+ # sandy.reload #=> {"name"=>["Sandy"], "id"=>"b2832ce2-e461-11dc-b13c-001bfc466dd7", "toys"=>["kids"]}
16
+ #
17
+ # compare to +put+ method
18
+ def save expected_attributes={ }
19
+ @attributes = uniq_values(@attributes)
20
+ prepare_for_update
21
+ connection.put_attributes(domain, id, @attributes, :replace, expected_attributes)
22
+ mark_as_old
23
+ @attributes
24
+ end
25
+
26
+ def save_if expected_attributes={ }
27
+ begin
28
+ save expected_attributes
29
+ rescue RightAws::AwsError => e
30
+ false
31
+ end
32
+ end
33
+
34
+ # Replaces the attributes at SDB by the given values.
35
+ # +Attrs+ is a hash: { attribute1 => values1, ..., attributeN => valuesN }.
36
+ # The other in-memory attributes are not being saved.
37
+ # Returns a hash of stored attributes.
38
+ #
39
+ # see +save+ method
40
+ def save_attributes(attrs, expected_attributes={})
41
+ prepare_for_update
42
+ attrs = uniq_values(attrs)
43
+ # if 'id' is present in attrs hash then replace internal 'id' attribute
44
+ unless attrs['id'].blank?
45
+ @attributes['id'] = attrs['id']
46
+ else
47
+ attrs['id'] = id
48
+ end
49
+ connection.put_attributes(domain, id, attrs, :replace, expected_attributes) unless attrs.blank?
50
+ attrs.each { |attribute, values| attrs[attribute] = values }
51
+ mark_as_old
52
+ attrs
53
+ end
54
+
55
+ end
@@ -0,0 +1,141 @@
1
+ class RightAws::SdbInterface < RightAws::RightAwsBase
2
+
3
+ # Prepare attributes for putting.
4
+ # (used by put_attributes)
5
+ def pack_attributes(items_or_attributes, replace = false, batch = false, expected_attributes = {}) #:nodoc:
6
+ if batch
7
+ index = 0
8
+ items_or_attributes.inject({}){|result, (item_name, attributes)|
9
+ item_prefix = "Item.#{index}."
10
+ result["#{item_prefix}ItemName"] = item_name.to_s
11
+ result.merge!(
12
+ pack_single_item_attributes(attributes, replace, item_prefix, expected_attributes))
13
+ index += 1
14
+ result
15
+ }
16
+ else
17
+ pack_single_item_attributes(items_or_attributes, replace)
18
+ end
19
+ end
20
+
21
+ def pack_single_item_attributes(attributes, replace, prefix = "", expected_attributes={})
22
+ result = {}
23
+ if attributes
24
+ idx = 0
25
+ skip_values = attributes.is_a?(Array)
26
+ attributes.each do |attribute, values|
27
+ # set replacement attribute
28
+ result["#{prefix}Attribute.#{idx}.Replace"] = 'true' if replace
29
+
30
+ # set expected attribute
31
+ expected_attributes = expected_attributes.to_mash
32
+ if expected_attributes.include?(attribute)
33
+ result["Expected.#{idx}.Name"] = attribute
34
+ if expected_attributes[attribute].nil?
35
+ result["Expected.#{idx}.Exists"] = 'false'
36
+ else
37
+ result["Expected.#{idx}.Value"] = expected_attributes[attribute]
38
+ end
39
+ end
40
+
41
+ # pack Name/Value
42
+ unless values.nil?
43
+ # Array(values) does not work here:
44
+ # - Array('') => [] but we wanna get here ['']
45
+ [values].flatten.each do |value|
46
+ result["#{prefix}Attribute.#{idx}.Name"] = attribute
47
+ result["#{prefix}Attribute.#{idx}.Value"] = ruby_to_sdb(value) unless skip_values
48
+ idx += 1
49
+ end
50
+ else
51
+ result["#{prefix}Attribute.#{idx}.Name"] = attribute
52
+ result["#{prefix}Attribute.#{idx}.Value"] = ruby_to_sdb(nil) unless skip_values
53
+ idx += 1
54
+ end
55
+ end
56
+ end
57
+ result
58
+ end
59
+
60
+ # Add/Replace item attributes.
61
+ #
62
+ # Params:
63
+ # domain_name = DomainName
64
+ # item_name = ItemName
65
+ # attributes = {
66
+ # 'nameA' => [valueA1,..., valueAN],
67
+ # ...
68
+ # 'nameZ' => [valueZ1,..., valueZN]
69
+ # }
70
+ # replace = :replace | any other value to skip replacement
71
+ #
72
+ # Returns a hash: { :box_usage, :request_id } on success or an exception on error.
73
+ # (Amazon raises no errors if the attribute was not overridden, as when the :replace param is unset).
74
+ #
75
+ # Example:
76
+ #
77
+ # sdb = RightAws::SdbInterface.new
78
+ # sdb.create_domain 'family'
79
+ #
80
+ # attributes = {}
81
+ # # create attributes for Jon and Silvia
82
+ # attributes['Jon'] = %w{ car beer }
83
+ # attributes['Silvia'] = %w{ beetle rolling_pin kids }
84
+ # sdb.put_attributes 'family', 'toys', attributes #=> ok
85
+ # # now: Jon=>[car, beer], Silvia=>[beetle, rolling_pin, kids]
86
+ #
87
+ # # add attributes to Jon
88
+ # attributes.delete('Silvia')
89
+ # attributes['Jon'] = %w{ girls pub }
90
+ # sdb.put_attributes 'family', 'toys', attributes #=> ok
91
+ # # now: Jon=>[car, beer, girls, pub], Silvia=>[beetle, rolling_pin, kids]
92
+ #
93
+ # # replace attributes for Jon and add to a cat (the cat had no attributes before)
94
+ # attributes['Jon'] = %w{ vacuum_cleaner hammer spade }
95
+ # attributes['cat'] = %w{ mouse clew Jons_socks }
96
+ # sdb.put_attributes 'family', 'toys', attributes, :replace #=> ok
97
+ # # now: Jon=>[vacuum_cleaner, hammer, spade], Silvia=>[beetle, rolling_pin, kids], cat=>[mouse, clew, Jons_socks]
98
+ #
99
+ # see: http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/SDB_API_PutAttributes.html
100
+ #
101
+ def put_attributes(domain_name, item_name, attributes, replace = false, expected_attributes = {})
102
+ begin
103
+ params = { 'DomainName' => domain_name,
104
+ 'ItemName' => item_name }.merge(pack_attributes(attributes, replace, false, expected_attributes))
105
+ link = generate_request("PutAttributes", params)
106
+ request_info( link, QSdbSimpleParser.new )
107
+ rescue RightAws::AwsError => e
108
+ # Don't sever the connection on a conditional put failure.
109
+ (e.to_s =~ /ConditionalCheckFailed:/) ? raise : on_exception()
110
+ rescue Exception
111
+ on_exception
112
+ end
113
+ end
114
+
115
+ # Delete value, attribute or item.
116
+ #
117
+ # Example:
118
+ # # delete 'vodka' and 'girls' from 'Jon' and 'mice' from 'cat'.
119
+ # sdb.delete_attributes 'family', 'toys', { 'Jon' => ['vodka', 'girls'], 'cat' => ['mice'] }
120
+ #
121
+ # # delete the all the values from attributes (i.e. delete the attributes)
122
+ # sdb.delete_attributes 'family', 'toys', { 'Jon' => [], 'cat' => [] }
123
+ # # or
124
+ # sdb.delete_attributes 'family', 'toys', [ 'Jon', 'cat' ]
125
+ #
126
+ # # delete all the attributes from item 'toys' (i.e. delete the item)
127
+ # sdb.delete_attributes 'family', 'toys'
128
+ #
129
+ # see http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/SDB_API_DeleteAttributes.html
130
+ #
131
+ def delete_attributes(domain_name, item_name, attributes = nil, expected_attributes = {})
132
+ params = { 'DomainName' => domain_name,
133
+ 'ItemName' => item_name }.merge(pack_attributes(attributes, false, false, expected_attributes))
134
+ link = generate_request("DeleteAttributes", params)
135
+ request_info( link, QSdbSimpleParser.new )
136
+ rescue Exception
137
+ on_exception
138
+ end
139
+ end
140
+
141
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 9
9
- version: 0.0.9
8
+ - 10
9
+ version: 0.0.10
10
10
  platform: ruby
11
11
  authors:
12
12
  - Philip (flip) Kromer
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-14 00:00:00 -07:00
17
+ date: 2010-05-02 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -49,14 +49,13 @@ executables:
49
49
  - broham
50
50
  - broham-diss
51
51
  - broham-fuck_all_yall
52
- - broham-host
53
- - broham-hosts_like
52
+ - broham-get
53
+ - broham-hosts
54
54
  - broham-register
55
- - broham-register_as_next
56
55
  - broham-sup
57
- - broham-sup_yall
58
56
  - broham-unregister
59
- - broham-unregister-like
57
+ - broham-unregister_like
58
+ - broham-word
60
59
  - broham-yo
61
60
  extensions: []
62
61
 
@@ -73,18 +72,20 @@ files:
73
72
  - bin/broham
74
73
  - bin/broham-diss
75
74
  - bin/broham-fuck_all_yall
76
- - bin/broham-host
77
- - bin/broham-hosts_like
75
+ - bin/broham-get
76
+ - bin/broham-hosts
78
77
  - bin/broham-register
79
- - bin/broham-register_as_next
80
78
  - bin/broham-sup
81
- - bin/broham-sup_yall
82
79
  - bin/broham-unregister
83
- - bin/broham-unregister-like
80
+ - bin/broham-unregister_like
81
+ - bin/broham-word
84
82
  - bin/broham-yo
85
83
  - broham.gemspec
86
84
  - lib/broham.rb
87
85
  - lib/broham/script.rb
86
+ - lib/broham/sdb.rb
87
+ - lib/broham/sdb/active_sdb.rb
88
+ - lib/broham/sdb/right_sdb_interface.rb
88
89
  - spec/broham_spec.rb
89
90
  - spec/spec.opts
90
91
  - spec/spec_helper.rb
@@ -1,13 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require 'broham'
4
- require 'broham/script'
5
- require 'json'
6
-
7
- Broham.get_cluster_settings
8
- Broham.get_command_line_args :role_name
9
- role_name = Settings[:role_name]
10
-
11
- $stderr.puts %Q{Retrieving '#{role_name}'}
12
- resp = Broham.host role_name
13
- $stderr.puts resp.to_pretty_json
@@ -1,12 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require 'broham'
4
- require 'broham/script'
5
-
6
- Broham.get_cluster_settings
7
- Broham.get_command_line_args :role_name
8
- role_name = Settings[:role_name]
9
-
10
- $stderr.puts %Q{Retrieving all hosts like '#{role_name}'}
11
- resp = Broham.hosts_like role_name
12
- $stderr.puts resp.map(&:to_pretty_json)
@@ -1,13 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require 'broham'
4
- require 'broham/script'
5
- Settings.define :set, :description => %Q{Any arg prefixed with "--set" will become an extra arg to register: 'broham-register foo --set-path=/path/to/foo' sets :path => '/path/to/foo' as an additional attribute}, :type => Hash
6
-
7
- Broham.get_cluster_settings
8
- role_name, broham_args = [Settings[:role_name], Settings[:set]||{}]
9
-
10
- $stderr.puts %Q{Registering as next #{role_name} with #{broham_args.inspect}}
11
- resp = Broham.register_as_next role_name, broham_args
12
- $stderr.puts resp.to_pretty_json
13
-
@@ -1,12 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require 'broham'
4
- require 'broham/script'
5
-
6
- Broham.get_cluster_settings
7
- Broham.get_command_line_args :role_name
8
- role_name = Settings[:role_name]
9
-
10
- $stderr.puts %Q{Retrieving all hosts like '#{role_name}'}
11
- resp = Broham.hosts_like role_name
12
- $stderr.puts resp.map(&:to_pretty_json)
@@ -1,13 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require 'broham'
4
- require 'broham/script'
5
-
6
- Broham.get_cluster_settings
7
- Broham.get_command_line_args :role_name
8
- role_name = Settings[:role_name]
9
-
10
- $stderr.puts %Q{Removing all like #{role_name}}
11
- resps = Broham.unregister_like role_name
12
- $stderr.puts resps.map{|resp| resp.to_pretty_json }
13
-