Module: Chef::LXC::KnifeHelper

Included in:
Fleet
Defined in:
lib/chef/lxc/knife_helper.rb

Instance Method Summary collapse

Instance Method Details

#chef_config(config = {}) {|Chef::Config| ... } ⇒ Object

Yields:

  • (Chef::Config)


13
14
15
16
17
18
19
# File 'lib/chef/lxc/knife_helper.rb', line 13

def chef_config(config = {})
  config.each do |key, value|
    Chef::Config[key] = value
  end
  yield Chef::Config if block_given?
  Chef::Config
end

#create_data_bag(name) ⇒ Object



29
30
31
# File 'lib/chef/lxc/knife_helper.rb', line 29

def create_data_bag(name)
  knife Chef::Knife::DataBagCreate, name
end

#create_environment(name, opts = {}) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/chef/lxc/knife_helper.rb', line 74

def create_environment(name, opts ={})
  e = Chef::Environment.new
  e.name(name)
  e.default_attributes(opts[:default_attributes])
  e.save
  e
end

#create_role(name, run_list) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/chef/lxc/knife_helper.rb', line 82

def create_role(name, run_list)
  role = Chef::Role.new
  role.name(name)
  Array(run_list).each do |item|
    role.run_list << item
  end
  role.save
  role
end

#knife(klass, *args) {|plugin.config| ... } ⇒ Object

Yields:

  • (plugin.config)


4
5
6
7
8
9
10
11
# File 'lib/chef/lxc/knife_helper.rb', line 4

def knife(klass, *args)
  klass.load_deps
  plugin = klass.new
  plugin.name_args = args
  yield plugin.config if block_given?
  plugin.run
  plugin
end

#load_secret(secret_file = nil) ⇒ Object



45
46
47
48
49
50
# File 'lib/chef/lxc/knife_helper.rb', line 45

def load_secret(secret_file = nil)
  config = chef_config
  Chef::EncryptedDataBagItem.load_secret(
    secret_file || config[:knife][:secret_file]
  )
end

#update_data_bag_item(data_bag_name, item_name, update_hash, opts = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/chef/lxc/knife_helper.rb', line 52

def update_data_bag_item(data_bag_name, item_name, update_hash, opts = {})
  hash = Chef::DataBagItem.load(data_bag_name, item_name)
  if opts[:encrypted]
    secret = load_secret(opts[:secret_file])
    hash = Chef::EncryptedDataBagItem.load(data_bag_name, item_name, secret).to_hash
  end
  updated_hash = hash.merge(update_hash)
  upload_data_bag_item_from_hash(data_bag_name, updated_hash, opts)
end

#upload_cookbooks(path, *cookbooks) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/chef/lxc/knife_helper.rb', line 21

def upload_cookbooks(path, *cookbooks)
  cookbook_dirs = Array(path)
  knife Chef::Knife::CookbookUpload, *cookbooks do |config|
    config[:all] = true if cookbooks.empty?
    config[:cookbook_path] = cookbook_dirs
  end
end

#upload_data_bag(name, path, opts = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/chef/lxc/knife_helper.rb', line 33

def upload_data_bag(name, path, opts = {})
  items = Dir["#{path}/*"]
  name_args = [name, items].flatten
  plugin = knife(Chef::Knife::DataBagFromFile, *name_args) do |config|
    if opts[:encrypted]
      config[:secret_file] = opts[:secret_file]
      config[:encrypt] = true
      Chef::Config[:knife][:secret_file] = opts[:secret_file]
    end
  end
end

#upload_data_bag_item_from_hash(data_bag_name, hash, opts = {}) ⇒ Object

hash must contain an entry of the form ‘id’ => item_name



63
64
65
66
67
68
69
70
71
72
# File 'lib/chef/lxc/knife_helper.rb', line 63

def upload_data_bag_item_from_hash(data_bag_name, hash, opts = {})
  config = chef_config
  if opts[:encrypted]
    secret = load_secret(opts[:secret_file])
    hash = Chef::EncryptedDataBagItem.encrypt_data_bag_item(hash, secret)
  end
  item = Chef::DataBagItem.from_hash(hash)
  item.data_bag(data_bag_name)
  item.save
end