Class: Cheftacular::ChefDataBag

Inherits:
Object
  • Object
show all
Defined in:
lib/cheftacular/chef/data_bag.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, config = {}) ⇒ ChefDataBag

Returns a new instance of ChefDataBag.



4
5
6
# File 'lib/cheftacular/chef/data_bag.rb', line 4

def initialize options={}, config={}
  @options, @config = options, config
end

Instance Method Details

#init_bag(bag_env, bag_name, encrypted = true) ⇒ Object

this will only intialize bags (and their hashes) if they don’t exist. Use ridley data bag methods to reload the data etc



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cheftacular/chef/data_bag.rb', line 9

def init_bag bag_env, bag_name, encrypted=true
  self.instance_eval("@config['ridley'].data_bag.create(name: '#{ bag_env }')") if self.instance_eval("@config['ridley'].data_bag.find('#{ bag_env }').nil?")

  if self.instance_eval("@config['ridley'].data_bag.find('#{ bag_env }').item.find('#{ bag_name }').nil?")
    self.instance_eval("@config['ridley'].data_bag.find('#{ bag_env }').item.create(id: '#{ bag_name }')")
  end

  @config[bag_env] ||= {}

  if !@config[bag_env].has_key?("#{ bag_name }_bag") || !@config[bag_env].has_key?("#{ bag_name }_bag_hash")
    self.instance_eval "@config['#{ bag_env }']['#{ bag_name }_bag'] ||= @config['ridley'].data_bag.find('#{ bag_env }').item.find('#{ bag_name }')"

    self.instance_eval "@config['#{ bag_env }']['#{ bag_name }_bag_hash'] ||= @config['#{ bag_env }']['#{ bag_name }_bag']#{ encrypted ? '.decrypt' : '' }.to_hash"
  end
end

#save_addresses_bag(bag_env = "options") ⇒ Object



75
76
77
78
79
# File 'lib/cheftacular/chef/data_bag.rb', line 75

def save_addresses_bag bag_env="options"
  env = bag_env == 'options' ? @options['env'] : bag_env

  save_bag 'addresses', bag_env, @config[env]['addresses_bag'], @config[env]['addresses_bag_hash']
end

#save_audit_bag(bag_env = "options") ⇒ Object

TODO special save for bag that will compile the data into a different bag for storage (the data will be stored as an audit log and zlib’d)



53
54
55
56
57
# File 'lib/cheftacular/chef/data_bag.rb', line 53

def save_audit_bag bag_env="options"
  env = bag_env == 'options' ? @options['env'] : bag_env

  save_bag 'audit', bag_env, @config[env]['audit_bag'], @config[env]['audit_bag_hash']
end

#save_authentication_bag(bag_env = "default") ⇒ Object



59
60
61
# File 'lib/cheftacular/chef/data_bag.rb', line 59

def save_authentication_bag bag_env="default"
  save_bag 'authentication', bag_env, @config['default']['authentication_bag'], @config['default']['authentication_bag_hash'], true
end

#save_chef_passwords_bag(bag_env = "options") ⇒ Object



63
64
65
66
67
# File 'lib/cheftacular/chef/data_bag.rb', line 63

def save_chef_passwords_bag bag_env="options"
  env = bag_env == 'options' ? @options['env'] : bag_env

  save_bag 'chef_passwords', bag_env, @config[env]['chef_passwords_bag'], @config[env]['chef_passwords_bag_hash'], true
end

#save_config_bag(bag_env = "options") ⇒ Object



81
82
83
84
85
# File 'lib/cheftacular/chef/data_bag.rb', line 81

def save_config_bag bag_env="options"
  env = bag_env == 'options' ? @options['env'] : bag_env

  save_bag 'config', bag_env, @config[env]['config_bag'], @config[env]['config_bag_hash']
end

#save_logs_bag(bag_env = "options") ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cheftacular/chef/data_bag.rb', line 25

def save_logs_bag bag_env="options"
  return true if @config['helper'].running_on_chef_node?
  
  env = bag_env == 'options' ? @options['env'] : bag_env

  item = @config[env]['logs_bag'].reload

  #TODO use zlib gem to store and display logs https://stackoverflow.com/questions/17882463/compressing-large-string-in-ruby
  #Zlib::Deflate.deflate(data_to_compress)
  #Zlib::Inflate.inflate(data_compressed)
  item.attributes = item.attributes.deep_merge(@config[env]['logs_bag_hash'].dup)

  begin
    item.save
  rescue Ridley::Errors::HTTPRequestEntityTooLarge => e
    puts "WARNING! #{ e }! The logs from this run will not be saved on the chef server. Wiping the bag so future runs can be saved."

    item.attributes = @config[env]['logs_bag_hash'].keep_if {|key,val| key == 'id'}

    sleep 5

    item.save

    @config[env]['logs_bag_hash'] = @config[env]['logs_bag'].reload.to_hash
  end
end

#save_node_roles_bag(bag_env = "options") ⇒ Object



87
88
89
90
91
# File 'lib/cheftacular/chef/data_bag.rb', line 87

def save_node_roles_bag bag_env="options"
  env = bag_env == 'options' ? @options['env'] : bag_env

  save_bag 'node_roles', bag_env, @config[env]['node_roles_bag'], @config[env]['node_roles_bag_hash']
end

#save_server_passwords_bag(bag_env = "options") ⇒ Object



69
70
71
72
73
# File 'lib/cheftacular/chef/data_bag.rb', line 69

def save_server_passwords_bag bag_env="options"
  env = bag_env == 'options' ? @options['env'] : bag_env

  save_bag 'server_passwords', bag_env, @config[env]['server_passwords_bag'], @config[env]['server_passwords_bag_hash'], true
end