Class: PuppetSDB::PuppetSDB

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PuppetSDB

Returns a new instance of PuppetSDB.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/puppetsdb.rb', line 10

def initialize(options={})
  cfg_file_paths = ['/etc/puppetsdb/config.yml']
  if ENV['HOME']
    cfg_file_paths << '~/.puppetsdb/config.yml'
  else
    cfg_file_paths << '~puppet/.puppetsdb/config.yml'
  end
  final_conf = {}
  cfg_file_paths.each do |file|
    file = File.expand_path(file)
    next unless File.exists?(file)
    file_conf = YAML.load(File.read(file))
    file_conf.keys.each do |tlevel_key|
      final_conf[tlevel_key] ||= {}
      final_conf[tlevel_key].merge!(file_conf[tlevel_key]) 
    end
  end

  AWS.config(final_conf['aws'])
  domain = options[:domain] || final_conf['puppetsdb']['domain']
  @simpledb = AWS::SimpleDB.new
  @domain = domain
end

Instance Attribute Details

#domainObject

Returns the value of attribute domain.



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

def domain
  @domain
end

#simpledbObject

Returns the value of attribute simpledb.



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

def simpledb
  @simpledb
end

Instance Method Details

#construct_attrs(yaml) ⇒ Object



101
102
103
104
# File 'lib/puppetsdb.rb', line 101

def construct_attrs(yaml)
  attrs = {'Time Modified' => get_timestamp_utc,
           'Node Data' => encode_yaml(yaml)}
end

#create_domain(domain_name) ⇒ Object



38
39
40
# File 'lib/puppetsdb.rb', line 38

def create_domain(domain_name)
  @simpledb.domains.create(domain_name)
end

#decode_base64(base64) ⇒ Object



93
94
95
# File 'lib/puppetsdb.rb', line 93

def decode_base64(base64)
  Base64.decode64(base64)
end

#delete_domain(domain_name, with_items = false) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/puppetsdb.rb', line 42

def delete_domain(domain_name, with_items=false)
  if with_items
    @simpledb.domains[domain_name].delete!
  else
    @simpledb.domains[domain_name].delete
  end
end

#delete_item(item) ⇒ Object



67
68
69
# File 'lib/puppetsdb.rb', line 67

def delete_item(item)
  @simpledb.domains[@domain].items[item].delete
end

#encode_yaml(yaml) ⇒ Object



89
90
91
# File 'lib/puppetsdb.rb', line 89

def encode_yaml(yaml)
  Base64.encode64(yaml)
end

#get_item_attrs(item) ⇒ Object



71
72
73
# File 'lib/puppetsdb.rb', line 71

def get_item_attrs(item)
  @simpledb.domains[@domain].items[item].data.attributes
end

#get_node_yaml(item) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/puppetsdb.rb', line 75

def get_node_yaml(item)
  attrs = get_item_attrs(item)
  if attrs.empty?
    ""
  else
    decode_base64(attrs['Node Data'][0])
  end
end

#get_timestamp_utcObject



97
98
99
# File 'lib/puppetsdb.rb', line 97

def get_timestamp_utc
  Time.now.utc
end

#list_domainsObject



34
35
36
# File 'lib/puppetsdb.rb', line 34

def list_domains
  @simpledb.domains.collect(&:name)
end

#list_item_attrs(item) ⇒ Object



63
64
65
# File 'lib/puppetsdb.rb', line 63

def list_item_attrs(item)
  @simpledb.domains[@domain].items[item].attributes.collect(&:name)
end

#list_itemsObject



50
51
52
# File 'lib/puppetsdb.rb', line 50

def list_items
  @simpledb.domains[@domain].items.collect(&:name)
end

#list_items_detailedObject



54
55
56
57
58
59
60
61
# File 'lib/puppetsdb.rb', line 54

def list_items_detailed
  attrs = ['Time Modified']
  results = []
  @simpledb.domains[@domain].items.select(*attrs).each do |item_data|
    results << [item_data.name, item_data.attributes]
  end
  results
end

#update_item(item, attrs) ⇒ Object



84
85
86
87
# File 'lib/puppetsdb.rb', line 84

def update_item(item, attrs)
  @simpledb.domains[@domain].items[item].attributes.put(
    :replace => attrs)
end

#validate_yaml(yaml) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/puppetsdb.rb', line 106

def validate_yaml(yaml)
  begin
    YAML.load(yaml)
  rescue
    puts "Validation of YAML failed"
    raise
  end
end

#write_node_data(nodename, yaml) ⇒ Object



115
116
117
118
# File 'lib/puppetsdb.rb', line 115

def write_node_data(nodename, yaml)
  validate_yaml(yaml)
  update_item(nodename, construct_attrs(yaml))
end