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
Methods
public class
- config
- create_domain
- domain
- domain_exists?
- global
- keypair_path_to_name
- ld
- le
- li
- logger
- reset_config
- reset_global
- update_config
- update_global
- update_logger
public instance
- config_dirname
- current_group_name
- current_machine_address
- current_machine_bucket
- current_machine_count
- current_machine_group
- current_machine_hostname
- current_machine_image
- current_machine_name
- current_machine_os
- current_machine_root
- current_machine_size
- current_machine_user
- current_user_is_root?
- current_user_keypairname
- current_user_keypairpath
- default_root
- default_user
- defined_keypairpath
- known_machine_group?
- ld
- le
- li
- root_keypairname
- root_keypairpath
- user_keypairname
- user_keypairpath
Public class methods
# 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
# File lib/rudy/huxtable.rb, line 59 59: def self.domain 60: Rudy::DOMAIN 61: end
# 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
# 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
Puts msg to +@@logger+ if Rudy.debug? returns true
# 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
Puts msg to +@@logger+ with “ERROR: “ prepended
# File lib/rudy/huxtable.rb, line 66 66: def self.le(*msg); @@logger.puts " " << msg.join("#{$/} "); end
Puts msg to +@@logger+
# File lib/rudy/huxtable.rb, line 64 64: def self.li(*msg); msg.each { |m| @@logger.puts m } if !@@global.quiet; end
# File lib/rudy/huxtable.rb, line 46 46: def self.reset_config; @@config = Rudy::Config.new; end
# File lib/rudy/huxtable.rb, line 47 47: def self.reset_global; @@global = Rudy::Global.new; end
# 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
# File lib/rudy/huxtable.rb, line 40 40: def self.update_global(ghash); @@global.update(ghash); end
# 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
# 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
# File lib/rudy/huxtable.rb, line 173 173: def current_group_name 174: "grp-#{@@global.zone}-#{current_machine_group}" 175: end
# 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
# File lib/rudy/huxtable.rb, line 219 219: def current_machine_bucket 220: @@global.bucket || fetch_machine_param(:bucket) || nil 221: end
# File lib/rudy/huxtable.rb, line 177 177: def current_machine_count 178: fetch_machine_param(:positions) || 1 179: end
# File lib/rudy/huxtable.rb, line 169 169: def current_machine_group 170: [@@global.environment, @@global.role].join(Rudy::DELIM) 171: end
# 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
# File lib/rudy/huxtable.rb, line 194 194: def current_machine_image 195: fetch_machine_param(:ami) 196: end
TODO: fix machine_group to include zone
# 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
# File lib/rudy/huxtable.rb, line 198 198: def current_machine_os 199: fetch_machine_param(:os) || 'linux' 200: end
# File lib/rudy/huxtable.rb, line 92 92: def current_machine_root 93: (fetch_machine_param(:root) || default_root).to_s 94: end
# File lib/rudy/huxtable.rb, line 202 202: def current_machine_size 203: fetch_machine_param(:size) || 'm1.small' 204: end
# 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
# 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
# File lib/rudy/huxtable.rb, line 120 120: def current_user_keypairname 121: user_keypairname current_machine_user 122: end
# File lib/rudy/huxtable.rb, line 146 146: def current_user_keypairpath 147: user_keypairpath current_machine_user 148: end
# File lib/rudy/huxtable.rb, line 84 84: def default_root 85: (@@config.defaults.root || 'root').to_s 86: end
# File lib/rudy/huxtable.rb, line 88 88: def default_user 89: (@@config.defaults.user || current_machine_root).to_s 90: end
# 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
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.
# 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
# File lib/rudy/huxtable.rb, line 75 75: def ld(*msg); Rudy::Huxtable.ld *msg; end
# File lib/rudy/huxtable.rb, line 74 74: def le(*msg); Rudy::Huxtable.le *msg; end
# File lib/rudy/huxtable.rb, line 73 73: def li(*msg); Rudy::Huxtable.li *msg; end
# File lib/rudy/huxtable.rb, line 117 117: def root_keypairname 118: user_keypairname current_machine_root 119: end
# File lib/rudy/huxtable.rb, line 143 143: def root_keypairpath 144: user_keypairpath current_machine_root 145: end
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
# 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
# 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