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



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

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



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

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

#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