Class: Chef::Knife::CookbookUpload

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/knife/chop/cookbook_upload.rb,
lib/chef/knife/chop/cookbook_upload.rb

Instance Method Summary collapse

Instance Method Details

#runObject



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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/chef/knife/chop/cookbook_upload.rb', line 36

def run
  unless Chef::VERSION.match(%r/^(11|12)\./)
    raise StandardError.new("I was crafted from Chef::VERSION == '11.x'. Please verify that #{self.class.name}.run is still relevant in your version == '#{Chef::VERSION}'!")
  end
  # Sanity check before we load anything from the server
  unless config[:all]
    if @name_args.empty?
      show_usage
      ui.fatal("You must specify the --all flag or at least one cookbook name")
      exit 1
    end
  end

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

  if @name_args.empty? and ! config[:all]
    show_usage
    ui.fatal("You must specify the --all flag or at least one cookbook name")
    exit 1
  end

  assert_environment_valid!
  warn_about_cookbook_shadowing
  version_constraints_to_update = {}
  upload_failures = 0
  upload_ok = 0

  # Get a list of cookbooks and their versions from the server
  # to check for the existence of a cookbook's dependencies.
  @server_side_cookbooks = ::Chef::CookbookVersion.list_all_versions
  justify_width = @server_side_cookbooks.map {|name| name.size}.max.to_i + 2
  if config[:all]
    cookbook_repo.load_cookbooks
    cbs = []
    cookbook_repo.each do |cookbook_name, cookbook|
      cbs << cookbook
      cookbook.freeze_version if config[:freeze]
      version_constraints_to_update[cookbook_name] = cookbook.version
    end
    begin
      upload(cbs, justify_width)
    rescue ::Chef::Exceptions::CookbookFrozen
      ui.warn("Not updating version constraints for some cookbooks in the environment as the cookbook is frozen.")
    end
    ui.step("Uploaded all cookbooks.")
  else
    if @name_args.empty?
      show_usage
      ui.error("You must specify the --all flag or at least one cookbook name")
      exit 1
    end

    cookbooks_to_upload.each do |cookbook_name, cookbook|
      cookbook.freeze_version if config[:freeze]
      begin
        upload([cookbook], justify_width)
        upload_ok += 1
        version_constraints_to_update[cookbook_name] = cookbook.version
      rescue ::Chef::Exceptions::CookbookNotFoundInRepo => e
        upload_failures += 1
        ui.fatal("Could not find cookbook #{cookbook_name} in your cookbook path, skipping it")
        Log.debug(e)
        upload_failures += 1
      rescue ::Chef::Exceptions::CookbookFrozen
        ui.error("Not updating version constraints for #{cookbook_name} in the environment as the cookbook is frozen.")
        upload_failures += 1
      end
    end

    # BEGIN changes DLDInternet
    # upload_failures += @name_args.length - @cookbooks_to_upload.length
    #
    # if upload_failures == 0
    #   ui.info "Uploaded #{upload_ok} cookbook#{upload_ok > 1 ? "s" : ""}."
    # elsif upload_failures > 0 && upload_ok > 0
    #   ui.warn "Uploaded #{upload_ok} cookbook#{upload_ok > 1 ? "s" : ""} but #{upload_failures} " +
    #               "cookbook#{upload_failures > 1 ? "s" : ""} upload failed."
    # elsif upload_failures > 0 && upload_ok == 0
    #   ui.error "Failed to upload #{upload_failures} cookbook#{upload_failures > 1 ? "s" : ""}."
    #   exit 1
    # end
    upload_skips = @name_args.length - @cookbooks_to_upload.length

    if upload_failures == 0
      if upload_skips == 0
        ui.step "Uploaded #{upload_ok} cookbook".plural(upload_ok)+"."
      elsif upload_skips > 0 && upload_ok > 0
        ui.step "Uploaded #{upload_ok} #{'cookbook'.plural(upload_ok)} but #{upload_skips} #{'cookbook'.plural(upload_skips)} were not included."
      elsif upload_ok == 0
        ui.fatal "Did not upload any cookbooks."
        exit 1
      end
    elsif upload_failures > 0 && upload_ok > 0
      if upload_skips == 0
        ui.error "Uploaded #{upload_ok} #{'cookbook'.plural(upload_ok)} but #{upload_failures} #{'cookbook'.plural(upload_failures)} failed upload."
      elsif upload_skips > 0
        ui.error "Uploaded #{upload_ok} #{'cookbook'.plural(upload_ok)} but #{upload_skips} #{'cookbook'.plural(upload_skips)} were not included and #{upload_failures} #{'cookbook'.plural(upload_failures)} failed upload."
      end
    elsif upload_failures > 0 && upload_ok == 0
      ui.fatal "Failed to upload #{upload_failures} #{'cookbook'.plural(upload_failures)}."
      exit 1
    end
	# END Changes DLDInternet
  end

  unless version_constraints_to_update.empty?
    update_version_constraints(version_constraints_to_update) if config[:environment]
  end
end

#upload(cookbooks, justify_width) ⇒ Object

DLDInternet monkey patch of original



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/chef/knife/chop/cookbook_upload.rb', line 148

def upload(cookbooks, justify_width)
  cookbooks.each do |cb|
    # BEGIN changes DLDInternet
    ui.step("Uploading #{cb.name.to_s.ljust(justify_width + 10)} [#{cb.version}]")
    # END changes DLDInternet
    check_for_broken_links!(cb)
    check_for_dependencies!(cb)
  end
  ::Chef::CookbookUploader.new(cookbooks, config[:cookbook_path], :force => config[:force]).upload_cookbooks
rescue ::Chef::Exceptions::CookbookFrozen => e
  ui.error e
  raise
end