Class: Knife::CookbookSync

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/cookbook_sync.rb

Instance Method Summary collapse

Instance Method Details

#distill_manifest(cookbook) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/chef/knife/cookbook_sync.rb', line 50

def distill_manifest(cookbook)
  files = { }
  cookbook.manifest.values.select { |x| x.kind_of?(Array) }.flatten.each { |f| files[f['path']] = f['checksum'] }
  # don't check metadata.json since json output is indeterministic, metadata.rb should be all that's needed anw
  files.delete('metadata.json')
  return files
end

#force_make_noise(&block) ⇒ Object



46
47
48
# File 'lib/chef/knife/cookbook_sync.rb', line 46

def force_make_noise(&block)
  @print_mutex.synchronize(&block) if block
end

#make_noise(&block) ⇒ Object



42
43
44
# File 'lib/chef/knife/cookbook_sync.rb', line 42

def make_noise(&block)
  force_make_noise(&block) if block and !config[:quiet]
end

#runObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chef/knife/cookbook_sync.rb', line 125

def run
  Thread.abort_on_exception = true

  @print_mutex = Mutex.new

  Chef::Config[:cookbook_path] = config[:cookbook_path] if config[:cookbook_path]

  Chef::Cookbook::FileVendor.on_create { |manifest| Chef::Cookbook::FileSystemFileVendor.new(manifest) }

  cl = Chef::CookbookLoader.new(Chef::Config[:cookbook_path])

  if config[:all]
    sync_cookbooks cl.cookbooks.map(&:name), cl
  else
    sync_cookbooks name_args, cl
  end
end

#sync_cookbooks(cookbooks, cl) ⇒ Object



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
122
123
# File 'lib/chef/knife/cookbook_sync.rb', line 58

def sync_cookbooks(cookbooks, cl)
  log_level = Chef::Log.level

  # mutes the CookbookVersion noise when the cookbook doesn't exist on the server.
  Chef::Log.level = :fatal

  to_upload = Queue.new

  cookbooks.map(&:to_s).each do |cookbook|
    Thread.new do
      upload = false

      make_noise do
        ui.msg "Checking cookbook '#{cookbook}' for sync necessity"
      end

      remote_cookbook = Chef::CookbookVersion.available_versions(cookbook) &&
        (Chef::CookbookVersion.load(cookbook.to_s) rescue nil)
      local_cookbook = cl[cookbook.to_s] rescue nil

      unless local_cookbook
        make_noise do
          ui.fatal "Cookbook '#{cookbook}' does not exist locally."
        end
        exit 1
      end

      if local_cookbook and !remote_cookbook
        upload = true
      else
        remote_files = distill_manifest(remote_cookbook) rescue { }
        local_files  = distill_manifest(local_cookbook) rescue { }

        if local_files.keys.length != remote_files.keys.length
          upload = true
        else
          (local_files.keys + remote_files.keys).uniq.each do |filename|
            if local_files[filename] != remote_files[filename]
              upload = true
              break
            end
          end
        end
      end

      if upload
        make_noise do
          ui.msg "sync necessary; uploading '#{cookbook}'"
        end

        to_upload << cl[cookbook]
      end
    end
  end

  Thread.list.reject { |x| x == Thread.current }.each(&:join) # wait for threads to settle

  cookbooks_to_upload = []
  loop { cookbooks_to_upload << to_upload.shift(true) } rescue nil

  Chef::Log.level = log_level # restore log level now that we're done checking
  Chef::CookbookUploader.new(cookbooks_to_upload, Chef::Config[:cookbook_path]).upload_cookbooks

  # exit with an exit status of 5 if we've uploaded anything.
  exit cookbooks_to_upload.empty? ? 0 : 5
end