Class: Assh::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



7
8
9
10
# File 'lib/configuration.rb', line 7

def initialize
  @hosts = {}
  @groups = {}
end

Instance Attribute Details

#groupsObject

Returns the value of attribute groups.



5
6
7
# File 'lib/configuration.rb', line 5

def groups
  @groups
end

#hostsObject

Returns the value of attribute hosts.



5
6
7
# File 'lib/configuration.rb', line 5

def hosts
  @hosts
end

Instance Method Details

#add_host(group_name, host_name, host) ⇒ Object



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

def add_host(group_name, host_name, host)
  puts "WARNING: Duplicate host named: #{name}. Only latest host is included" if @hosts[host_name]

  @groups[group_name] = {} unless @groups.has_key?(group_name)
  @groups[group_name][host_name] = host
  @hosts[host_name] = host
end

#generated_at(timestamp_file = File.expand_path(Assh::CONFIG_CACHE_AT)) ⇒ Object



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

def generated_at(timestamp_file = File.expand_path(Assh::CONFIG_CACHE_AT))
  return File.read(timestamp_file).to_i if File.exists?(timestamp_file)
  0
end

#load_cache!(config_file = File.expand_path(Assh::CONFIG_CACHE)) ⇒ Object



33
34
35
36
37
# File 'lib/configuration.rb', line 33

def load_cache!(config_file = File.expand_path(Assh::CONFIG_CACHE))
  cache = YAML::load_file(config_file)
  @hosts = cache[:hosts]
  @groups = cache[:groups]
end

#needs_generating?(timestamp_file = File.expand_path(Assh::CONFIG_CACHE_AT)) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/configuration.rb', line 39

def needs_generating?(timestamp_file = File.expand_path(Assh::CONFIG_CACHE_AT))
  (current_time - generated_at(timestamp_file)) > 72000
end

#save_cache!(config_file = File.expand_path(Assh::CONFIG_CACHE), timestamp_file = File.expand_path(Assh::CONFIG_CACHE_AT)) ⇒ Object

TODO: Move generated-at to here and call config-cached-at



22
23
24
25
26
27
28
29
30
31
# File 'lib/configuration.rb', line 22

def save_cache!(config_file = File.expand_path(Assh::CONFIG_CACHE),
                timestamp_file = File.expand_path(Assh::CONFIG_CACHE_AT))
  cache = {
      hosts: @hosts,
      groups: @groups
  }
  File.open(config_file, 'w') { |f| f << cache.to_yaml }
  File.open(timestamp_file, 'w') { |f| f << current_time }

end