Class: Chef::Knife::CookbookTarUpload

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

Constant Summary collapse

@@current_cookbooks =

This is the list of CookbookVersion’s that are already uploaded to the chef-server

nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.upload_cookbooks(tar_file) ⇒ Object



31
32
33
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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/chef/knife/cookbook_tar_upload.rb', line 31

def self.upload_cookbooks(tar_file)
  # This is the list of cookbooks and their groupings, we upload all the cookbooks in a group at a time
  cookbook_upload_groups = Array.new
  
  cookbook_paths = tar_file.cookbooks
  
  cookbook_group_index = 0
  
  total_cookbooks = cookbook_paths.length
  
  # Since we now allow you to upload multiple versions of the same cookbook we have to be smart about how we upload the cookbooks
  # If we upload a cookbook without the proper dependency we will get errors
  
  # So the strategy is to categorize cookbooks into groups based on their dependencies and what has been uploaded
  # Then move the cookbooks to a tmp directory (renaming their directories if necessary) and upload each group in order
  
  while !cookbook_paths.empty?
    
    cookbook_upload_groups.push(Array.new)
    
    # An array of current cookbook names we are uploading. This is used to ensure we dont upload the same cookbook 
    # (different version) in the same group
    cookbookNamesToUpload = Array.new
    
    cookbook_paths.each do |cookbook_path|
      cookbookName = CookbookTarUpload.get_cookbook_name_from_path cookbook_path
      
      # Verify we aren't uploading another version of the cookbook in this group
      unless cookbookNamesToUpload.index(cookbookName)
        
        # Check the cookbook has no dependencies or its dependencies have been uploaded
        
        md = Chef::Cookbook::Metadata.new
        md.from_file File.join(cookbook_path, "metadata.rb")
        
        uploadable = true
        md.dependencies.each do |cookbook_dependency, version_constraint|
            
          # Verify we have uploaded the specific cookbook / version
          unless CookbookTarUpload.is_dependency_uploaded? cookbook_dependency, version_constraint, cookbook_upload_groups
            ui.error "Cookbook #{cookbookName} depends on cookbook '#{cookbook_dependency}' version '#{version_constraint}',"
            ui.error "which is not currently being uploaded and cannot be found on the server."
            uploadable = false
            break
          end  
            
        end
        
        if uploadable
          # Add the cookbook to the group
          cookbook_upload_groups[cookbook_group_index].push cookbook_path
          # Add the cookbook's name to our list
          cookbookNamesToUpload.push(cookbookName)
        end
      end
    end
    
    # If we still have cookbooks that need to find a group but we did not add any to this group
    # we have an error. This can be caused by a missing dependency or a circular dependency.
    if cookbookNamesToUpload.empty?
      raise "Unable to upload cookbooks"
    end
    
    # Remove the cookbooks we have added to this group
    cookbook_upload_groups[cookbook_group_index].each do |cookbook_path|
      cookbook_paths.delete cookbook_path
    end
    
    cookbook_group_index+=1
    
  end
  
  puts "#{total_cookbooks} cookbooks have been grouped into #{cookbook_upload_groups.length} groups"
  puts "Uploading cookbooks ..."
  
  # Upload all cookbooks one group at a time
  cookbook_upload_groups.each do |cookbookGroup|
    cookbookTmpPath = ::TmpDirectory.new("cookbooks").path
    
    # Move cookbooks to tmp directory and rename if necessary
    cookbookGroup.each do |cookbook_path|
      FileUtils.cp_r cookbook_path, File.join(cookbookTmpPath, CookbookTarUpload.get_cookbook_name_from_path(cookbook_path))
    end
    
    #Upload cookbook group
    cookbookUploader = Chef::Knife::CookbookUpload.new
    cookbookUploader.config[:cookbook_path] = cookbookTmpPath
    cookbookUploader.config[:all] = true
    cookbookUploader.run
  end
end

Instance Method Details

#runObject

This method will be executed when you run this knife command.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/chef/knife/cookbook_tar_upload.rb', line 18

def run
  
  if @name_args.size != 1
    ui.info("Please specify a tarPath")
    show_usage
    exit 1
  end
  
  tar_file = Chef::TarFile.new(@name_args.first)
  CookbookTarUpload.upload_cookbooks tar_file
  
end