Module: Cuken::Api::Chef::Cookbook

Defined in:
lib/cuken/api/chef/cookbook.rb

Instance Method Summary collapse

Instance Method Details

#check_cookbook_presence(ckbk, expect_presence = true) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cuken/api/chef/cookbook.rb', line 93

def check_cookbook_presence(ckbk, expect_presence = true)
  chef.cookbook_paths.each do |pn|
    curr_ckbk = pn.basename.to_s
    condition = Pathname(pn).exist?
    given_ckbk = Pathname(ckbk).basename.to_s if ckbk
    result = given_ckbk == curr_ckbk
    if condition && result
      given_ckbk.should == curr_ckbk
      announce_or_puts(%{Found cookbook: #{pn}}) if @announce_env
    end
  end
end

#check_cookbook_table_presence(table, expect_presence = true) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/cuken/api/chef/cookbook.rb', line 36

def check_cookbook_table_presence(table, expect_presence = true)
  table.hashes.each do |hsh|
    ckbk, ckbk_src = parse_to_cookbooks_path(hsh)
    full_cookbook_src = find_path_in_cookbook_folders(ckbk, ckbk_src, ckbk)
    announce_or_puts(%{Looking for cookbook: #{full_cookbook_src}}) if @announce_env
    check_cookbook_presence(full_cookbook_src, expect_presence)
  end
end

#check_default_cookbooks_pathsObject



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cuken/api/chef/cookbook.rb', line 80

def check_default_cookbooks_paths
  in_chef_root do
    ['site-cookbooks', 'cookbooks'].each do |dir|
      path = (Pathname('.')+dir)
      chef.cookbooks_paths << path if path.exist?
    end
    if chef.cookbooks_paths.empty?
      raise(RuntimeError, "chef.cookbooks_paths AND chef.cookbook_paths cannot both be empty.", caller)
    end
    chef.cookbooks_paths.uniq
  end
end

#check_remote_cookbook_table_presence(table, expect_presence = true) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cuken/api/chef/cookbook.rb', line 45

def check_remote_cookbook_table_presence(table, expect_presence = true)
  books = cookbooks_list
  table.hashes.each do |hsh|
    if expect_presence
      books.should include(hsh['cookbook']) if not (hsh['cookbook'].nil? || hsh['cookbook'].empty?)
      books.should include(hsh['site-cookbook']) if not (hsh['site-cookbook'].nil? || hsh['site-cookbook'].empty?)
    else
      books.should_not include(hsh['cookbook']) if not (hsh['cookbook'].nil? || hsh['cookbook'].empty?)
      books.should_not include(hsh['site-cookbook']) if not (hsh['site-cookbook'].nil? || hsh['site-cookbook'].empty?)
    end
  end
end

#cookbook_delete(ckbk, version = 'all') ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/cuken/api/chef/cookbook.rb', line 139

def cookbook_delete(ckbk, version='all')

  knife_config_file_error_handling()

  ckbk_pth = nil
  in_chef_root do
    ckbk_pth = unless chef.cookbooks_paths.empty?
          (chef.cookbooks_paths.collect { |pn| pn.expand_path.to_s }).join(':')
      end
  end

  argv = ['cookbook', 'delete', ckbk]
  version == 'all' ? argv << '--all' : argv << version
  argv << '--yes' << '--no-editor'
  run_knife(argv)
end

#cookbook_load(ckbk) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/cuken/api/chef/cookbook.rb', line 123

def cookbook_load(ckbk)

  knife_config_file_error_handling()

  ckbk_pth = nil
  in_chef_root do
    ckbk_pth = unless chef.cookbooks_paths.empty?
          (chef.cookbooks_paths.collect { |pn| pn.expand_path.to_s }).join(':')
      end
  end

  argv = ['cookbook', 'upload', ckbk]
  ( argv << '--cookbook-path' << ckbk_pth ) if ckbk_pth
  run_knife(argv)
end

#cookbooks_delete(table) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/cuken/api/chef/cookbook.rb', line 114

def cookbooks_delete(table)
  table.hashes.each do |hsh|
    ckbk, ckbk_src = parse_to_cookbooks_path(hsh)
    full_cookbook_src = find_path_in_cookbook_folders(ckbk, ckbk_src, ckbk)
    version = hsh['version'] ? hsh['version'] : 'all'
    cookbook_delete(ckbk, version)
  end
end

#cookbooks_listObject



156
157
158
159
160
161
162
# File 'lib/cuken/api/chef/cookbook.rb', line 156

def cookbooks_list

  knife_config_file_error_handling()

  argv = ['cookbook', 'list']
  run_knife(argv)
end

#cookbooks_load(table) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/cuken/api/chef/cookbook.rb', line 106

def cookbooks_load(table)
  table.hashes.each do |hsh|
    ckbk, ckbk_src = parse_to_cookbooks_path(hsh)
    full_cookbook_src = find_path_in_cookbook_folders(ckbk, ckbk_src, ckbk)
    cookbook_load(ckbk)
  end
end

#find_path_in_cookbook_folders(ckbk, ckbk_src, path_fragment1, path_fagment2 = '') ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cuken/api/chef/cookbook.rb', line 58

def find_path_in_cookbook_folders(ckbk, ckbk_src, path_fragment1, path_fagment2='')
  verify_cookbook_folders
  full_data_bag_src = nil
  in_chef_root do
    list1 = chef.cookbooks_paths.find_all { |dir| Pathname(dir + path_fragment1 + path_fagment2).exist? }
    list2 = chef.cookbook_paths.find_all { |dir| (dir.to_s[/#{ckbk_src}/] && Pathname(dir+path_fagment2).exist?) }
    loc = list2[0] || ((list1[0] + ckbk) if list1[0] && list1[0].exist?)
    if loc.nil? || !(loc.exist?)
      # TODO: error handling if data bags or cookbooks do not exist
    else
      full_data_bag_src = (loc + path_fagment2).expand_path.realdirpath.to_s
    end
  end
  full_data_bag_src
end

#parse_to_cookbooks_path(hsh) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cuken/api/chef/cookbook.rb', line 22

def parse_to_cookbooks_path(hsh)
  case
    when !(hsh['cookbook'].nil? || hsh['cookbook'].empty?)
      ckbk = hsh['cookbook']
      ckbk_src = "cookbooks/#{ckbk}"
    when !(hsh['site-cookbook'].nil? || hsh['site-cookbook'].empty?)
      ckbk = hsh['site-cookbook']
      ckbk_src = "site-cookbooks/#{ckbk}"
    else
      src =""
  end
  return ckbk, ckbk_src
end

#verify_cookbook_foldersObject



74
75
76
77
78
# File 'lib/cuken/api/chef/cookbook.rb', line 74

def verify_cookbook_folders
  if (chef.cookbooks_paths.empty? && chef.cookbook_paths.empty?)
    check_default_cookbooks_paths
  end
end