Module: ClusterBomb::Configuration

Included in:
Bomb::Config
Defined in:
lib/cluster_bomb/configuration.rb

Constant Summary collapse

HOME =
File.join(ENV["HOME"], ".cluster_bomb")
CFG_FILE_NAME =
File.join(HOME,"config.yaml")
KEYS =
{
  :screen_width=>{:default=>120},
  :logging=>{:default=>true},
  :logfile=>{:default=>"logs/cb-#{Time.now().strftime('%m-%d-%Y')}.log}"},
  :max_run_time=>{:default=>nil},
  :max_history=>{:default=>1000},
  :username=>{:default=>ENV["USER"]}
}

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/cluster_bomb/configuration.rb', line 73

def method_missing(symbol, *args)
  str = symbol.to_s
  if str.match(/=$/)
    self.set(str.sub('=', ''),args[0])
  else
    self.get(str)
  end
end

Class Method Details

.check_dirObject

Check for home dir, and create it if it doesn’t exist



20
21
22
# File 'lib/cluster_bomb/configuration.rb', line 20

def self.check_dir
  `mkdir #{HOME}` unless File.exists? HOME
end

Instance Method Details

#configurationObject



28
29
30
# File 'lib/cluster_bomb/configuration.rb', line 28

def configuration
  @configuration
end

#get(key) ⇒ Object



37
38
39
40
41
42
# File 'lib/cluster_bomb/configuration.rb', line 37

def get(key)
  k = key.class==Symbol ? key : key.to_sym
  raise "Config.get ==> Uknown key #{k}" unless (@configuration.has_key? k or KEYS.has_key? k)
  ret = @configuration[k] || KEYS[k][:default]
  ret
end

#has_ssh_options?(username) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/cluster_bomb/configuration.rb', line 69

def has_ssh_options?(username)
  return true if  @ssh_options_for_user[username] 
  File.exists? File.join(HOME,"ssh_#{username}.yml")
end

#initializeObject



14
15
16
17
18
# File 'lib/cluster_bomb/configuration.rb', line 14

def initialize
  @configuration={}
  @ssh_options_for_user={} # By user
  Configuration.check_dir
end

#keysObject



43
44
45
# File 'lib/cluster_bomb/configuration.rb', line 43

def keys
  KEYS.keys
end

#load!Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cluster_bomb/configuration.rb', line 49

def load!
  Configuration.check_dir
  begin
    buf = File.read(CFG_FILE_NAME)
  rescue
    `touch #{CFG_FILE_NAME}`
    buf = File.read(CFG_FILE_NAME)
  end
  unless buf.empty?
    @configuration = YAML.load(buf)
  end
end

#saveObject

Save it out



24
25
26
27
# File 'lib/cluster_bomb/configuration.rb', line 24

def save
  Configuration.check_dir
  File.open(CFG_FILE_NAME,"w") {|f| f.write(@configuration.to_yaml)}
end

#set(key, val) ⇒ Object



31
32
33
34
35
36
# File 'lib/cluster_bomb/configuration.rb', line 31

def set(key, val)
  k = key.class==Symbol ? key : key.to_sym
  # TODO: VALIDATION
  raise "Invalid setting [#{key}]" unless KEYS[k]
  @configuration[k] = val
end

#ssh_options(username) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/cluster_bomb/configuration.rb', line 61

def ssh_options(username)
  if has_ssh_options? username
    if !@ssh_options_for_user[username]
      @ssh_options_for_user[username] = YAML.load(File.read(File.join(HOME,"ssh_#{username}.yml")))
    end
  end
  @ssh_options_for_user[username] || {}
end

#valid_key?(k) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/cluster_bomb/configuration.rb', line 46

def valid_key?(k)
  KEYS.has_key? k.to_sym
end