Class: Chef::Knife::Annex

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/annex.rb

Constant Summary collapse

DATA_BAG =
'annex'
IGNORE_USERS =
['admin']

Instance Method Summary collapse

Instance Method Details

#adminsObject



20
21
22
23
24
# File 'lib/chef/knife/annex.rb', line 20

def admins
  @admins ||= Chef::User.list.
    keys.
    select { |u| !IGNORE_USERS.include?(u) && Chef::User.load(u).admin }
end

#annex_fileObject



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

def annex_file
  ENV['ANNEX_FILE']
end

#annex_keyObject



26
27
28
# File 'lib/chef/knife/annex.rb', line 26

def annex_key
  ENV['ANNEX_KEY'].gsub(/[^[:alnum:]_\-]+/, '_')
end

#runObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
# File 'lib/chef/knife/annex.rb', line 34

def run
  case ENV['ANNEX_ACTION']
  when 'store'
    begin
      item = ChefVault::Item.load(DATA_BAG, annex_key)
    rescue ChefVault::Exceptions::KeysNotFound,
           ChefVault::Exceptions::ItemNotFound
      item = ChefVault::Item.new(DATA_BAG, annex_key)
    end
    item['data'] = File.read(annex_file)
    item.admins(admins.join(','))
    item.save
  when 'retrieve'
    item = ChefVault::Item.load(DATA_BAG, annex_key)
    if annex_file
      File.write(annex_file, item['data'])
    else
      puts item['data']
    end
  when 'remove'
    delete_object(ChefVault::Item, "#{vault}/#{item}", "chef_vault_item") do
      ChefVault::Item.load(DATA_BAG, annex_key).destroy
    end
  when 'checkpresent'
    begin
      ChefVault::Item.load(DATA_BAG, annex_key)
    rescue ChefVault::Exceptions::KeysNotFound,
           ChefVault::Exceptions::ItemNotFound
      # not found, we do nothing
    else
      # found
      puts annex_key
    end
  else
    items = ( @name_args.empty? ?
      Chef::DataBag.load(DATA_BAG).keys.reject { |k| k =~ /_keys$/ } :
      @name_args )
    if config[:rotate_keys]
      p rotate: items
    else
      puts "Use this command as git-annex hook"
    end
  end
end