Module Rudy::Huxtable
  1. lib/rudy/huxtable.rb

Rudy::Huxtable

Huxtable gives access to instances for config, global, and logger to any class that includes it.

class Rudy::Hello
  include Rudy::Huxtable

  def print_config
    p @@config.defaults  # {:nocolor => true, ...}
    p @@global.verbose   # => 1
    p @@logger.class     # => StringIO
  end

end

Public class methods

config ()
[show source]
    # File lib/rudy/huxtable.rb, line 29
29:     def self.config; @@config; end
create_domain ()
[show source]
    # File lib/rudy/huxtable.rb, line 49
49:     def self.create_domain
50:       @sdb = Rudy::AWS::SDB.new(@@global.accesskey, @@global.secretkey, @@global.region)
51:       @sdb.create_domain Rudy::DOMAIN
52:     end
domain ()
[show source]
    # File lib/rudy/huxtable.rb, line 59
59:     def self.domain
60:       Rudy::DOMAIN
61:     end
domain_exists? ()
[show source]
    # File lib/rudy/huxtable.rb, line 54
54:     def self.domain_exists?
55:       @sdb = Rudy::AWS::SDB.new(@@global.accesskey, @@global.secretkey, @@global.region)
56:       (@sdb.list_domains || []).member? Rudy::DOMAIN
57:     end
global ()
[show source]
    # File lib/rudy/huxtable.rb, line 30
30:     def self.global; @@global; end
keypair_path_to_name (kp)
[show source]
     # File lib/rudy/huxtable.rb, line 223
223:     def self.keypair_path_to_name(kp)
224:       return nil unless kp
225:       name = File.basename kp
226:       #name.gsub(/key-/, '')   # We keep the key- now
227:     end
ld (*msg)

Puts msg to +@@logger+ if Rudy.debug? returns true

[show source]
    # File lib/rudy/huxtable.rb, line 68
68:     def self.ld(*msg)
69:       return unless Rudy.debug?
70:       @@logger.puts "D:  " << msg.join("#{$/}D:  ")
71:     end
le (*msg)

Puts msg to +@@logger+ with “ERROR: “ prepended

[show source]
    # File lib/rudy/huxtable.rb, line 66
66:     def self.le(*msg); @@logger.puts "  " << msg.join("#{$/}  "); end
li (*msg)

Puts msg to +@@logger+

[show source]
    # File lib/rudy/huxtable.rb, line 64
64:     def self.li(*msg); msg.each { |m| @@logger.puts m } if !@@global.quiet; end
logger ()
[show source]
    # File lib/rudy/huxtable.rb, line 31
31:     def self.logger; @@logger; end
reset_config ()
[show source]
    # File lib/rudy/huxtable.rb, line 46
46:     def self.reset_config; @@config = Rudy::Config.new; end
reset_global ()
[show source]
    # File lib/rudy/huxtable.rb, line 47
47:     def self.reset_global; @@global = Rudy::Global.new; end
update_config (path=nil)
[show source]
    # File lib/rudy/huxtable.rb, line 33
33:     def self.update_config(path=nil)
34:       @@config.verbose = (@@global.verbose >= 3)   # -vvv
35:       # nil and bad paths sent to look_and_load are ignored
36:       @@config.look_and_load(path || @@global.config)
37:       @@global.apply_config(@@config)
38:     end
update_global (ghash)
[show source]
    # File lib/rudy/huxtable.rb, line 40
40:     def self.update_global(ghash); @@global.update(ghash); end
update_logger (logger)
[show source]
    # File lib/rudy/huxtable.rb, line 41
41:     def self.update_logger(logger)
42:       return if logger.nil?
43:       @@logger = logger 
44:     end

Public instance methods

config_dirname ()
[show source]
    # File lib/rudy/huxtable.rb, line 77
77:     def config_dirname
78:       raise "No config paths defined" unless @@config.is_a?(Rudy::Config) && @@config.paths.is_a?(Array)
79:       base_dir = File.dirname @@config.paths.first
80:       raise "Config directory doesn't exist #{base_dir}" unless File.exists?(base_dir)
81:       base_dir
82:     end
current_group_name ()
[show source]
     # File lib/rudy/huxtable.rb, line 173
173:     def current_group_name
174:       "grp-#{@@global.zone}-#{current_machine_group}"
175:     end
current_machine_address (position='01')
[show source]
     # File lib/rudy/huxtable.rb, line 206
206:     def current_machine_address(position='01')
207:       #raise NoConfig unless @@config
208:       #raise NoMachinesConfig unless @@config.machines
209:       raise "Position cannot be nil" if position.nil?
210:       addresses = [fetch_machine_param(:addresses)].flatten.compact
211:       addresses[position.to_i-1]
212:     end
current_machine_bucket ()
[show source]
     # File lib/rudy/huxtable.rb, line 219
219:     def current_machine_bucket
220:       @@global.bucket || fetch_machine_param(:bucket) || nil
221:     end
current_machine_count ()
[show source]
     # File lib/rudy/huxtable.rb, line 177
177:     def current_machine_count
178:       fetch_machine_param(:positions) || 1
179:     end
current_machine_group ()
[show source]
     # File lib/rudy/huxtable.rb, line 169
169:     def current_machine_group
170:       [@@global.environment, @@global.role].join(Rudy::DELIM)
171:     end
current_machine_hostname ()
[show source]
     # File lib/rudy/huxtable.rb, line 181
181:     def current_machine_hostname
182:       # NOTE: There is an issue with Caesars that a keyword that has been
183:       # defined as forced_array (or forced_hash, etc...) is like that for
184:       # all subclasses of Caesars. There is a conflict between "hostname" 
185:       # in the machines config and routines config. The routines config 
186:       # parses hostname with forced_array because it's a shell command
187:       # in Rye::Cmd. Machines config expects just a symbol. The issue
188:       # is with Caesars so this is a workaround to return a symbol.
189:       hn = fetch_machine_param(:hostname) || :rudy
190:       hn = hn.flatten.compact.first if hn.is_a?(Array)
191:       hn
192:     end
current_machine_image ()
[show source]
     # File lib/rudy/huxtable.rb, line 194
194:     def current_machine_image
195:       fetch_machine_param(:ami)
196:     end
current_machine_name ()

TODO: fix machine_group to include zone

[show source]
     # File lib/rudy/huxtable.rb, line 215
215:     def current_machine_name
216:       [@@global.zone, current_machine_group, @@global.position].join(Rudy::DELIM)
217:     end
current_machine_os ()
[show source]
     # File lib/rudy/huxtable.rb, line 198
198:     def current_machine_os
199:       fetch_machine_param(:os) || 'linux'
200:     end
current_machine_root ()
[show source]
    # File lib/rudy/huxtable.rb, line 92
92:     def current_machine_root
93:       (fetch_machine_param(:root) || default_root).to_s
94:     end
current_machine_size ()
[show source]
     # File lib/rudy/huxtable.rb, line 202
202:     def current_machine_size
203:       fetch_machine_param(:size) || 'm1.small'
204:     end
current_machine_user ()
[show source]
    # File lib/rudy/huxtable.rb, line 96
96:     def current_machine_user
97:       (@@global.user || fetch_machine_param(:user) || default_user || Rudy.sysinfo.user).to_s
98:     end
current_user_is_root? (user=nil)
[show source]
     # File lib/rudy/huxtable.rb, line 124
124:     def current_user_is_root?(user=nil)
125:       user ||= current_machine_user
126:       user.to_s == current_machine_root
127:     end
current_user_keypairname ()
[show source]
     # File lib/rudy/huxtable.rb, line 120
120:     def current_user_keypairname
121:       user_keypairname current_machine_user
122:     end
current_user_keypairpath ()
[show source]
     # File lib/rudy/huxtable.rb, line 146
146:     def current_user_keypairpath
147:       user_keypairpath current_machine_user
148:     end
default_root ()
[show source]
    # File lib/rudy/huxtable.rb, line 84
84:     def default_root
85:       (@@config.defaults.root || 'root').to_s
86:     end
default_user ()
[show source]
    # File lib/rudy/huxtable.rb, line 88
88:     def default_user
89:       (@@config.defaults.user || current_machine_root).to_s
90:     end
defined_keypairpath (name=nil)
[show source]
     # File lib/rudy/huxtable.rb, line 150
150:     def defined_keypairpath(name=nil)
151:       name ||= current_machine_user
152:       raise Rudy::Error, "No user provided" unless name
153:       ## NOTE: I think it is more appropriate to return nil here
154:       ## than raise errors. This stuff should be checked already
155:       ##raise NoConfig unless @@config
156:       ##raise NoMachinesConfig unless @@config.machines
157:       ##raise NoGlobal unless @@global
158:       return unless @@global && @@config && @@config.machines
159:       zon, env, rol = @@global.zone, @@global.environment, @@global.role
160:       path = @@global.identity
161:       path ||= @@config.machines.find_deferred(zon, env, rol, [:users, name, :keypair])
162:       path ||= @@config.machines.find_deferred(env, rol, [:users, name, :keypair])
163:       path ||= @@config.machines.find_deferred(rol, [:users, name, :keypair])
164:       path ||= @@config.machines.find_deferred(@@global.region, [:users, name, :keypair])
165:       path
166:     end
known_machine_group? ()

Looks for ENV-ROLE configuration in machines. There must be at least one definition in the config for this to return true That’s how Rudy knows the current group is defined.

[show source]
     # File lib/rudy/huxtable.rb, line 233
233:     def known_machine_group?
234:       raise NoConfig unless @@config
235:       return true if default_machine_group?
236:       raise NoMachinesConfig unless @@config.machines
237:       return false if !@@config && !@@global
238:       zon, env, rol = @@global.zone, @@global.environment, @@global.role
239:       conf = @@config.machines.find_deferred(@@global.region, zon, [env, rol])
240:       conf ||= @@config.machines.find_deferred(zon, [env, rol])
241:       !conf.nil?
242:     end
ld (*msg)
[show source]
    # File lib/rudy/huxtable.rb, line 75
75:     def ld(*msg); Rudy::Huxtable.ld *msg; end
le (*msg)
[show source]
    # File lib/rudy/huxtable.rb, line 74
74:     def le(*msg); Rudy::Huxtable.le *msg; end
li (*msg)
[show source]
    # File lib/rudy/huxtable.rb, line 73
73:     def li(*msg); Rudy::Huxtable.li *msg; end
root_keypairname ()
[show source]
     # File lib/rudy/huxtable.rb, line 117
117:     def root_keypairname
118:       user_keypairname current_machine_root
119:     end
root_keypairpath ()
[show source]
     # File lib/rudy/huxtable.rb, line 143
143:     def root_keypairpath
144:       user_keypairpath current_machine_root
145:     end
user_keypairname (user=nil)

Returns the name of the current keypair for the given user. If there’s a private key path in the config this will return the basename (it’s assumed the Amazon Keypair has the same name as the file). Otherwise this returns the Rudy style name: key-ZONE-ENV-ROLE-USER. Or if this the user is root: key-ZONE-ENV-ROLE

[show source]
     # File lib/rudy/huxtable.rb, line 107
107:     def user_keypairname(user=nil)
108:       user ||= current_machine_user
109:       path = defined_keypairpath user
110:       if path
111:         Huxtable.keypair_path_to_name(path)
112:       else
113:         n = current_user_is_root?(user) ? '' : "-#{user}"
114:         "key-%s-%s%s" % [@@global.zone, current_machine_group, n]
115:       end    
116:     end
user_keypairpath (name=nil)
[show source]
     # File lib/rudy/huxtable.rb, line 129
129:     def user_keypairpath(name=nil)
130:       name ||= current_machine_user
131:       path = defined_keypairpath name
132:       # If we can't find a user defined key, we'll 
133:       # check the config path for a generated one.
134:       if path
135:         raise "Private key file not found (#{path})" unless File.exists?(path)
136:         path = File.expand_path(path)
137:       else
138:         ssh_key_dir = @@config.defaults.keydir || Rudy::SSH_KEY_DIR
139:         path = File.join(ssh_key_dir, user_keypairname(name))
140:       end
141:       path
142:     end