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
  @config['ridley'].data_bag.create(name: bag_env) if @config['ridley'].data_bag.find(bag_env).nil?

  if @config['ridley'].data_bag.find(bag_env).item.find(bag_name).nil?
    @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")
    @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

#reset_addresses_bag(bag_env = "options") ⇒ Object



97
98
99
100
101
# File 'lib/cheftacular/chef/data_bag.rb', line 97

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

  reset_bag 'addresses', env
end

#reset_audit_bag(bag_env = 'options') ⇒ Object



103
104
105
106
107
# File 'lib/cheftacular/chef/data_bag.rb', line 103

def reset_audit_bag bag_env='options'
  env = bag_env == 'options' ? @options['env'] : bag_env

  reset_bag 'audit', env
end

#reset_cheftacular_bag(bag_env = "default") ⇒ Object



109
110
111
# File 'lib/cheftacular/chef/data_bag.rb', line 109

def reset_cheftacular_bag bag_env="default"
  reset_bag 'cheftacular', bag_env
end

#reset_environment_config_bag(bag_env = 'default') ⇒ Object



113
114
115
# File 'lib/cheftacular/chef/data_bag.rb', line 113

def reset_environment_config_bag bag_env='default'
  reset_bag 'environment_config', bag_env
end

#reset_node_roles_bag(bag_env = "options") ⇒ Object



117
118
119
120
121
# File 'lib/cheftacular/chef/data_bag.rb', line 117

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

  reset_bag 'node_roles', env
end

#save_addresses_bag(bag_env = "options") ⇒ Object



79
80
81
82
83
# File 'lib/cheftacular/chef/data_bag.rb', line 79

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



49
50
51
52
53
# File 'lib/cheftacular/chef/data_bag.rb', line 49

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



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

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



67
68
69
70
71
# File 'lib/cheftacular/chef/data_bag.rb', line 67

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_cheftacular_bag(bag_env = "default") ⇒ Object



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

def save_cheftacular_bag bag_env="default"
  save_bag 'cheftacular', bag_env, @config['default']['cheftacular_bag'], @config['default']['cheftacular_bag_hash']
end

#save_config_bag(bag_env = "options") ⇒ Object



85
86
87
88
89
# File 'lib/cheftacular/chef/data_bag.rb', line 85

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_environment_config_bag(bag_env = 'default') ⇒ Object



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

def save_environment_config_bag bag_env='default'
  save_bag 'environment_config', bag_env, @config['default']['environment_config_bag'], @config['default']['environment_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
# 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

  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



91
92
93
94
95
# File 'lib/cheftacular/chef/data_bag.rb', line 91

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



73
74
75
76
77
# File 'lib/cheftacular/chef/data_bag.rb', line 73

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