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



55
56
57
58
59
60
61
# File 'lib/chef/knife/cookbook_sync.rb', line 55

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



51
52
53
# File 'lib/chef/knife/cookbook_sync.rb', line 51

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

#make_noise(&block) ⇒ Object



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

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

#runObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/chef/knife/cookbook_sync.rb', line 133

def run
  Thread.abort_on_exception = true

  @print_mutex = Mutex.new

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

  if !Chef::Config[:cookbook_path] or Chef::Config[:cookbook_path].empty?
    ui.msg "No cookbook path in config file or provided on command-line. Aborting."
    exit 1
  end

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

  cl = Chef::CookbookLoader.new(Chef::Config[:cookbook_path])
  cl.load_cookbooks if cl.respond_to?(:load_cookbooks)

  if config[:all]
    if cl.respond_to?(:cookbook_names)
      names = cl.cookbook_names
    else
      names = cl.cookbooks.map(&:name)
    end

    if !names or names.empty?
      ui.msg "No cookbooks found to upload in cookbook path: #{Chef::Config[:cookbook_path].inspect} -- not continuing"
      ui.msg "Set cookbook_path in knife.rb or pass the -o option."
      exit 1
    end

    sync_cookbooks names, cl
  else
    sync_cookbooks name_args, cl
  end
end

#sync_cookbooks(cookbooks, cl) ⇒ Object



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
124
125
126
127
128
129
130
131
# File 'lib/chef/knife/cookbook_sync.rb', line 63

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).map 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.each(&:join)

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

  # exit 0 if there's nothing to upload
  if cookbooks_to_upload.empty?
    exit 0
  end

  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 5
end