Class: Chef::Knife::VaultUpdate

Inherits:
Chef::Knife show all
Includes:
VaultAdmins, VaultBase
Defined in:
lib/chef/knife/vault_update.rb

Direct Known Subclasses

EncryptUpdate

Instance Method Summary collapse

Methods included from VaultBase

included, #show_usage

Instance Method Details

#runObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/chef/knife/vault_update.rb', line 50

def run
  vault = @name_args[0]
  item = @name_args[1]
  values = @name_args[2]
  search = config[:search]
  json_file = config[:json]
  file = config[:file]
  clean = config[:clean]

  set_mode(config[:vault_mode])

  if vault && item && ((values || json_file || file) || (search || admins))
    begin
      vault_item = ChefVault::Item.load(vault, item)

      # Keys management first
      if clean
        clients = vault_item.clients().clone().sort()
        clients.each do |client|
          $stdout.puts "Deleting #{client}"
          vault_item.keys.delete(client, "clients")
        end
      end

      vault_item.search(search) if search
      vault_item.clients(search) if search
      vault_item.admins(admins) if admins

      # Save only the keys if no value is provided, otherwise save the item
      if values || json_file || file
        merge_values(values, json_file).each do |key, value|
          vault_item[key] = value
        end

        if file
          vault_item["file-name"] = File.basename(file)
          vault_item["file-content"] = File.open(file) { |f| f.read() }
        end

        vault_item.save
      else
        vault_item.save_keys
      end

    rescue ChefVault::Exceptions::KeysNotFound,
           ChefVault::Exceptions::ItemNotFound
      raise ChefVault::Exceptions::ItemNotFound,
        "#{vault}/#{item} does not exist, "\
        "use 'knife vault create' to create."
    end
  else
    show_usage
  end
end