broham 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,134 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'broham'
4
+ require 'broham/script'
5
+
6
+ [
7
+ # ['sup', '', '(alias for hosts)'],
8
+ # ['yo', '', '(alias for register)'],
9
+ # ['diss', '', '(alias for unregister)'],
10
+ # ['fuck_all_yall', '', '(alias for unregister_like)'],
11
+ # ['word', '', '(alias for get)'],
12
+ #
13
+ ['hosts', 'role_pattern', 'Unregister all hosts prefixed by role_pattern'],
14
+ ['register', 'role [--extra_param=val...]', 'Registers this machine for the given role; stuff in any leftover params'],
15
+ ['unregister', 'role', 'Unregister that role'],
16
+ ['unregister_like', 'role_pattern', 'Unregister all hosts prefixed by role_pattern'],
17
+ ['get', 'role_pattern attr', 'show attr for all hosts prefixed by role_pattern']
18
+ ].each{|cmd| Configliere::COMMANDS << cmd }
19
+
20
+ class BrohamScript
21
+ attr_accessor :role_name, :attr
22
+ def command() Settings.command ; end
23
+ def role_name() Settings[:role_name] ; end
24
+ def attr() Settings[:attr] ; end
25
+
26
+ def hosts_cmd
27
+ BrohamScript.get_commandline_predicates :role_name
28
+ $stderr.puts %Q{Listing hosts like #{role_name}}
29
+ dump_table Broham.hosts_like(role_name)
30
+ end
31
+
32
+ def register_cmd
33
+ # 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
34
+ broham_args = Settings[:set]||{}
35
+ BrohamScript.get_commandline_predicates :role_name
36
+ $stderr.puts %Q{Registering #{role_name} -- #{broham_args}}
37
+ new_bro = Broham.register(role_name)
38
+ dump_table [new_bro]
39
+ end
40
+
41
+ def register_as_next_cmd
42
+ # 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
43
+ broham_args = Settings[:set]||{}
44
+ BrohamScript.get_commandline_predicates :role_name
45
+ $stderr.puts %Q{Registering #{role_name} -- #{broham_args}}
46
+ new_bro = Broham.register_as_next(role_name)
47
+ dump_table [new_bro]
48
+ end
49
+
50
+ def unregister_cmd
51
+ BrohamScript.get_commandline_predicates :role_name
52
+ $stderr.puts %Q{Unregistering #{role_name}}
53
+ dead_bro = Broham.unregister(role_name)
54
+ dump_table [dead_bro]
55
+ end
56
+
57
+ def unregister_like_cmd
58
+ BrohamScript.get_commandline_predicates :role_name
59
+ $stderr.puts %Q{Unregistering all hosts like #{role_name}}
60
+ dump_table Broham.unregister_like role_name
61
+ end
62
+
63
+ def get_cmd
64
+ BrohamScript.get_commandline_predicates :role_name, :attr
65
+ $stderr.puts %Q{Getting #{attr} for hosts like #{role_name}}
66
+ Broham.hosts_like(role_name).map do |bro|
67
+ $stdout.puts bro[attr]
68
+ end
69
+ end
70
+
71
+ def run
72
+ case Settings.command.to_s
73
+ when 'hosts', 'sup'
74
+ hosts_cmd
75
+ when 'register', 'yo'
76
+ register_cmd
77
+ when 'unregister', 'diss'
78
+ unregister_cmd
79
+ when 'unregister_like', 'fuck_all_yall'
80
+ unregister_like_cmd
81
+ when 'get', 'word'
82
+ get_cmd
83
+ when ''
84
+ Settings.die "Please use one of the commands listed above"
85
+ else
86
+ Settings.die "Don't know how to run command #{command}"
87
+ end
88
+ end
89
+
90
+ protected
91
+
92
+ # Dump a list of hashes as a formatted, equally-spaced table
93
+ def dump_table hosts_list
94
+ attr_lens = {}
95
+ # find all used fields, and their width
96
+ hosts_list.each do |host|
97
+ host.to_hash.each{|k,v| attr_lens[k] = [v.to_s.length, attr_lens[k].to_i].max }
98
+ end
99
+ # account for length of attr titles
100
+ attr_lens.each{|attr,len| attr_lens[attr] = [len, attr.length].max }
101
+ # take attrs in order, putting role first (and excluding id)
102
+ attrs = ['role', (attr_lens.keys - ['role', 'id']).sort].flatten
103
+ $stderr.puts attrs.map{|attr| "%-#{attr_lens[attr]}s"%attr}.join("\t")
104
+ hosts_list.each do |host|
105
+ $stdout.puts attrs.map{|attr| "%-#{attr_lens[attr]}s"%host[attr]}.join("\t")
106
+ end
107
+ end
108
+
109
+ # Loads defaults
110
+ def self.get_cluster_settings
111
+ Settings.read(ENV['HOME']+'/.poolparty/poolparty.yaml')
112
+ Settings.resolve!
113
+ Broham.establish_connection
114
+ end
115
+
116
+ # takes a list of attributes and pops each, in turn, from the commandline
117
+ # (the Settings.rest list)
118
+ def self.get_commandline_predicates *arg_names
119
+ arg_names.each do |arg_name|
120
+ Settings[arg_name] = Settings.rest.shift
121
+ end
122
+ check_args! *arg_names
123
+ end
124
+
125
+ #
126
+ def self.check_args! *arg_names
127
+ arg = arg_names.last
128
+ if Settings[arg].blank? then Settings.die "ERROR: Please supply #{arg_names.join(", ")} after the command." end
129
+ end
130
+
131
+ end
132
+
133
+ BrohamScript.get_cluster_settings
134
+ BrohamScript.new.run
@@ -0,0 +1,134 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'broham'
4
+ require 'broham/script'
5
+
6
+ [
7
+ # ['sup', '', '(alias for hosts)'],
8
+ # ['yo', '', '(alias for register)'],
9
+ # ['diss', '', '(alias for unregister)'],
10
+ # ['fuck_all_yall', '', '(alias for unregister_like)'],
11
+ # ['word', '', '(alias for get)'],
12
+ #
13
+ ['hosts', 'role_pattern', 'Unregister all hosts prefixed by role_pattern'],
14
+ ['register', 'role [--extra_param=val...]', 'Registers this machine for the given role; stuff in any leftover params'],
15
+ ['unregister', 'role', 'Unregister that role'],
16
+ ['unregister_like', 'role_pattern', 'Unregister all hosts prefixed by role_pattern'],
17
+ ['get', 'role_pattern attr', 'show attr for all hosts prefixed by role_pattern']
18
+ ].each{|cmd| Configliere::COMMANDS << cmd }
19
+
20
+ class BrohamScript
21
+ attr_accessor :role_name, :attr
22
+ def command() Settings.command ; end
23
+ def role_name() Settings[:role_name] ; end
24
+ def attr() Settings[:attr] ; end
25
+
26
+ def hosts_cmd
27
+ BrohamScript.get_commandline_predicates :role_name
28
+ $stderr.puts %Q{Listing hosts like #{role_name}}
29
+ dump_table Broham.hosts_like(role_name)
30
+ end
31
+
32
+ def register_cmd
33
+ # 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
34
+ broham_args = Settings[:set]||{}
35
+ BrohamScript.get_commandline_predicates :role_name
36
+ $stderr.puts %Q{Registering #{role_name} -- #{broham_args}}
37
+ new_bro = Broham.register(role_name)
38
+ dump_table [new_bro]
39
+ end
40
+
41
+ def register_as_next_cmd
42
+ # 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
43
+ broham_args = Settings[:set]||{}
44
+ BrohamScript.get_commandline_predicates :role_name
45
+ $stderr.puts %Q{Registering #{role_name} -- #{broham_args}}
46
+ new_bro = Broham.register_as_next(role_name)
47
+ dump_table [new_bro]
48
+ end
49
+
50
+ def unregister_cmd
51
+ BrohamScript.get_commandline_predicates :role_name
52
+ $stderr.puts %Q{Unregistering #{role_name}}
53
+ dead_bro = Broham.unregister(role_name)
54
+ dump_table [dead_bro]
55
+ end
56
+
57
+ def unregister_like_cmd
58
+ BrohamScript.get_commandline_predicates :role_name
59
+ $stderr.puts %Q{Unregistering all hosts like #{role_name}}
60
+ dump_table Broham.unregister_like role_name
61
+ end
62
+
63
+ def get_cmd
64
+ BrohamScript.get_commandline_predicates :role_name, :attr
65
+ $stderr.puts %Q{Getting #{attr} for hosts like #{role_name}}
66
+ Broham.hosts_like(role_name).map do |bro|
67
+ $stdout.puts bro[attr]
68
+ end
69
+ end
70
+
71
+ def run
72
+ case Settings.command.to_s
73
+ when 'hosts', 'sup'
74
+ hosts_cmd
75
+ when 'register', 'yo'
76
+ register_cmd
77
+ when 'unregister', 'diss'
78
+ unregister_cmd
79
+ when 'unregister_like', 'fuck_all_yall'
80
+ unregister_like_cmd
81
+ when 'get', 'word'
82
+ get_cmd
83
+ when ''
84
+ Settings.die "Please use one of the commands listed above"
85
+ else
86
+ Settings.die "Don't know how to run command #{command}"
87
+ end
88
+ end
89
+
90
+ protected
91
+
92
+ # Dump a list of hashes as a formatted, equally-spaced table
93
+ def dump_table hosts_list
94
+ attr_lens = {}
95
+ # find all used fields, and their width
96
+ hosts_list.each do |host|
97
+ host.to_hash.each{|k,v| attr_lens[k] = [v.to_s.length, attr_lens[k].to_i].max }
98
+ end
99
+ # account for length of attr titles
100
+ attr_lens.each{|attr,len| attr_lens[attr] = [len, attr.length].max }
101
+ # take attrs in order, putting role first (and excluding id)
102
+ attrs = ['role', (attr_lens.keys - ['role', 'id']).sort].flatten
103
+ $stderr.puts attrs.map{|attr| "%-#{attr_lens[attr]}s"%attr}.join("\t")
104
+ hosts_list.each do |host|
105
+ $stdout.puts attrs.map{|attr| "%-#{attr_lens[attr]}s"%host[attr]}.join("\t")
106
+ end
107
+ end
108
+
109
+ # Loads defaults
110
+ def self.get_cluster_settings
111
+ Settings.read(ENV['HOME']+'/.poolparty/poolparty.yaml')
112
+ Settings.resolve!
113
+ Broham.establish_connection
114
+ end
115
+
116
+ # takes a list of attributes and pops each, in turn, from the commandline
117
+ # (the Settings.rest list)
118
+ def self.get_commandline_predicates *arg_names
119
+ arg_names.each do |arg_name|
120
+ Settings[arg_name] = Settings.rest.shift
121
+ end
122
+ check_args! *arg_names
123
+ end
124
+
125
+ #
126
+ def self.check_args! *arg_names
127
+ arg = arg_names.last
128
+ if Settings[arg].blank? then Settings.die "ERROR: Please supply #{arg_names.join(", ")} after the command." end
129
+ end
130
+
131
+ end
132
+
133
+ BrohamScript.get_cluster_settings
134
+ BrohamScript.new.run
@@ -2,13 +2,133 @@
2
2
  require 'rubygems'
3
3
  require 'broham'
4
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
5
 
7
- Broham.get_cluster_settings
8
- Broham.get_command_line_args :role_name
9
- role_name, broham_args = [Settings[:role_name], Settings[:set]||{}]
6
+ [
7
+ # ['sup', '', '(alias for hosts)'],
8
+ # ['yo', '', '(alias for register)'],
9
+ # ['diss', '', '(alias for unregister)'],
10
+ # ['fuck_all_yall', '', '(alias for unregister_like)'],
11
+ # ['word', '', '(alias for get)'],
12
+ #
13
+ ['hosts', 'role_pattern', 'Unregister all hosts prefixed by role_pattern'],
14
+ ['register', 'role [--extra_param=val...]', 'Registers this machine for the given role; stuff in any leftover params'],
15
+ ['unregister', 'role', 'Unregister that role'],
16
+ ['unregister_like', 'role_pattern', 'Unregister all hosts prefixed by role_pattern'],
17
+ ['get', 'role_pattern attr', 'show attr for all hosts prefixed by role_pattern']
18
+ ].each{|cmd| Configliere::COMMANDS << cmd }
10
19
 
11
- $stderr.puts %Q{Registering as #{role_name} with #{broham_args.inspect}}
12
- resp = Broham.register role_name, broham_args
13
- node = Broham.host role_name
14
- $stderr.puts resp.to_pretty_json
20
+ class BrohamScript
21
+ attr_accessor :role_name, :attr
22
+ def command() Settings.command ; end
23
+ def role_name() Settings[:role_name] ; end
24
+ def attr() Settings[:attr] ; end
25
+
26
+ def hosts_cmd
27
+ BrohamScript.get_commandline_predicates :role_name
28
+ $stderr.puts %Q{Listing hosts like #{role_name}}
29
+ dump_table Broham.hosts_like(role_name)
30
+ end
31
+
32
+ def register_cmd
33
+ # 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
34
+ broham_args = Settings[:set]||{}
35
+ BrohamScript.get_commandline_predicates :role_name
36
+ $stderr.puts %Q{Registering #{role_name} -- #{broham_args}}
37
+ new_bro = Broham.register(role_name)
38
+ dump_table [new_bro]
39
+ end
40
+
41
+ def register_as_next_cmd
42
+ # 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
43
+ broham_args = Settings[:set]||{}
44
+ BrohamScript.get_commandline_predicates :role_name
45
+ $stderr.puts %Q{Registering #{role_name} -- #{broham_args}}
46
+ new_bro = Broham.register_as_next(role_name)
47
+ dump_table [new_bro]
48
+ end
49
+
50
+ def unregister_cmd
51
+ BrohamScript.get_commandline_predicates :role_name
52
+ $stderr.puts %Q{Unregistering #{role_name}}
53
+ dead_bro = Broham.unregister(role_name)
54
+ dump_table [dead_bro]
55
+ end
56
+
57
+ def unregister_like_cmd
58
+ BrohamScript.get_commandline_predicates :role_name
59
+ $stderr.puts %Q{Unregistering all hosts like #{role_name}}
60
+ dump_table Broham.unregister_like role_name
61
+ end
62
+
63
+ def get_cmd
64
+ BrohamScript.get_commandline_predicates :role_name, :attr
65
+ $stderr.puts %Q{Getting #{attr} for hosts like #{role_name}}
66
+ Broham.hosts_like(role_name).map do |bro|
67
+ $stdout.puts bro[attr]
68
+ end
69
+ end
70
+
71
+ def run
72
+ case Settings.command.to_s
73
+ when 'hosts', 'sup'
74
+ hosts_cmd
75
+ when 'register', 'yo'
76
+ register_cmd
77
+ when 'unregister', 'diss'
78
+ unregister_cmd
79
+ when 'unregister_like', 'fuck_all_yall'
80
+ unregister_like_cmd
81
+ when 'get', 'word'
82
+ get_cmd
83
+ when ''
84
+ Settings.die "Please use one of the commands listed above"
85
+ else
86
+ Settings.die "Don't know how to run command #{command}"
87
+ end
88
+ end
89
+
90
+ protected
91
+
92
+ # Dump a list of hashes as a formatted, equally-spaced table
93
+ def dump_table hosts_list
94
+ attr_lens = {}
95
+ # find all used fields, and their width
96
+ hosts_list.each do |host|
97
+ host.to_hash.each{|k,v| attr_lens[k] = [v.to_s.length, attr_lens[k].to_i].max }
98
+ end
99
+ # account for length of attr titles
100
+ attr_lens.each{|attr,len| attr_lens[attr] = [len, attr.length].max }
101
+ # take attrs in order, putting role first (and excluding id)
102
+ attrs = ['role', (attr_lens.keys - ['role', 'id']).sort].flatten
103
+ $stderr.puts attrs.map{|attr| "%-#{attr_lens[attr]}s"%attr}.join("\t")
104
+ hosts_list.each do |host|
105
+ $stdout.puts attrs.map{|attr| "%-#{attr_lens[attr]}s"%host[attr]}.join("\t")
106
+ end
107
+ end
108
+
109
+ # Loads defaults
110
+ def self.get_cluster_settings
111
+ Settings.read(ENV['HOME']+'/.poolparty/poolparty.yaml')
112
+ Settings.resolve!
113
+ Broham.establish_connection
114
+ end
115
+
116
+ # takes a list of attributes and pops each, in turn, from the commandline
117
+ # (the Settings.rest list)
118
+ def self.get_commandline_predicates *arg_names
119
+ arg_names.each do |arg_name|
120
+ Settings[arg_name] = Settings.rest.shift
121
+ end
122
+ check_args! *arg_names
123
+ end
124
+
125
+ #
126
+ def self.check_args! *arg_names
127
+ arg = arg_names.last
128
+ if Settings[arg].blank? then Settings.die "ERROR: Please supply #{arg_names.join(", ")} after the command." end
129
+ end
130
+
131
+ end
132
+
133
+ BrohamScript.get_cluster_settings
134
+ BrohamScript.new.run
@@ -5,14 +5,14 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{broham}
8
- s.version = "0.0.9"
8
+ s.version = "0.0.10"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Philip (flip) Kromer"]
12
- s.date = %q{2010-04-14}
12
+ s.date = %q{2010-05-02}
13
13
  s.description = %q{Bro! Broham always knows where his bros are, bro. Using broham, a newly-created cloud machine can annouce its availability for a certain role ("nfs_server" or "db_slave-2"), allowing any other interested nodes to discover its public_ip, private_ip, etc. See also: http://j.mp/amongbros}
14
14
  s.email = %q{flip@infochimps.org}
15
- s.executables = ["broham", "broham-diss", "broham-fuck_all_yall", "broham-host", "broham-hosts_like", "broham-register", "broham-register_as_next", "broham-sup", "broham-sup_yall", "broham-unregister", "broham-unregister-like", "broham-yo"]
15
+ s.executables = ["broham", "broham-diss", "broham-fuck_all_yall", "broham-get", "broham-hosts", "broham-register", "broham-sup", "broham-unregister", "broham-unregister_like", "broham-word", "broham-yo"]
16
16
  s.extra_rdoc_files = [
17
17
  "LICENSE",
18
18
  "README.textile"
@@ -27,18 +27,20 @@ Gem::Specification.new do |s|
27
27
  "bin/broham",
28
28
  "bin/broham-diss",
29
29
  "bin/broham-fuck_all_yall",
30
- "bin/broham-host",
31
- "bin/broham-hosts_like",
30
+ "bin/broham-get",
31
+ "bin/broham-hosts",
32
32
  "bin/broham-register",
33
- "bin/broham-register_as_next",
34
33
  "bin/broham-sup",
35
- "bin/broham-sup_yall",
36
34
  "bin/broham-unregister",
37
- "bin/broham-unregister-like",
35
+ "bin/broham-unregister_like",
36
+ "bin/broham-word",
38
37
  "bin/broham-yo",
39
38
  "broham.gemspec",
40
39
  "lib/broham.rb",
41
40
  "lib/broham/script.rb",
41
+ "lib/broham/sdb.rb",
42
+ "lib/broham/sdb/active_sdb.rb",
43
+ "lib/broham/sdb/right_sdb_interface.rb",
42
44
  "spec/broham_spec.rb",
43
45
  "spec/spec.opts",
44
46
  "spec/spec_helper.rb"