Module: Chef::Knife::SoloDataBagHelpers

Included in:
SoloDataBagCreate, SoloDataBagEdit, SoloDataBagList, SoloDataBagShow
Defined in:
lib/chef/knife/solo_data_bag_helpers.rb

Instance Method Summary collapse

Instance Method Details

#bag_item_pathObject



8
9
10
# File 'lib/chef/knife/solo_data_bag_helpers.rb', line 8

def bag_item_path
  File.expand_path File.join(bag_path, "#{item_name}.json")
end

#bag_pathObject



12
13
14
# File 'lib/chef/knife/solo_data_bag_helpers.rb', line 12

def bag_path
  File.expand_path File.join(bags_path, bag_name)
end

#bags_pathObject



16
17
18
19
20
21
22
# File 'lib/chef/knife/solo_data_bag_helpers.rb', line 16

def bags_path
  if config[:data_bag_path]
    Chef::Config[:data_bag_path] = config[:data_bag_path]
  end

  Chef::Config[:data_bag_path]
end

#convert_json_stringObject



43
44
45
# File 'lib/chef/knife/solo_data_bag_helpers.rb', line 43

def convert_json_string
  JSON.parse config[:json_string]
end

#persist_bag_item(item) ⇒ Object



24
25
26
27
28
# File 'lib/chef/knife/solo_data_bag_helpers.rb', line 24

def persist_bag_item(item)
  File.open bag_item_path, 'w' do |f|
    f.write JSON.pretty_generate(item.raw_data)
  end
end

#secret_keyObject



34
35
36
37
# File 'lib/chef/knife/solo_data_bag_helpers.rb', line 34

def secret_key
  return config[:secret] if config[:secret]
  Chef::EncryptedDataBagItem.load_secret(config[:secret_file] || secret_path)
end

#secret_pathObject



30
31
32
# File 'lib/chef/knife/solo_data_bag_helpers.rb', line 30

def secret_path
  Chef::Config[:encrypted_data_bag_secret]
end

#should_be_encrypted?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/chef/knife/solo_data_bag_helpers.rb', line 39

def should_be_encrypted?
  config[:secret] || config[:secret_file] || secret_path
end

#validate_bag_name_providedObject



47
48
49
50
51
52
53
# File 'lib/chef/knife/solo_data_bag_helpers.rb', line 47

def validate_bag_name_provided
  unless bag_name
    show_usage
    ui.fatal 'You must supply a name for the data bag'
    exit 1
  end
end

#validate_bags_path_existsObject



55
56
57
58
59
60
# File 'lib/chef/knife/solo_data_bag_helpers.rb', line 55

def validate_bags_path_exists
  unless File.directory? bags_path
    raise Chef::Exceptions::InvalidDataBagPath,
      "Configured data bag path '#{bags_path}' is invalid"
  end
end

#validate_json_stringObject



62
63
64
65
66
67
68
# File 'lib/chef/knife/solo_data_bag_helpers.rb', line 62

def validate_json_string
  begin
    JSON.parse config[:json_string], :create_additions => false
  rescue => error
    raise "Syntax error in #{config[:json_string]}: #{error.message}"
  end
end

#validate_multiple_secrets_were_not_providedObject



70
71
72
73
74
75
76
77
78
# File 'lib/chef/knife/solo_data_bag_helpers.rb', line 70

def validate_multiple_secrets_were_not_provided
  if config[:secret] && config[:secret_file]
    show_usage
    ui.fatal 'Please specify either --secret or --secret-file only'
    exit 1
  elsif (config[:secret] && secret_path) || (config[:secret_file] && secret_path)
    ui.warn 'The encrypted_data_bag_secret option defined in knife.rb was overriden by the command line.'
  end
end