Class: Chef::Knife::CookbookUpload

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

Constant Summary collapse

CHECKSUM =
"checksum"
MATCH_CHECKSUM =
/[0-9a-f]{32,}/

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods inherited from Chef::Knife

#api_key, category, common_name, #configure_chef, #create_object, #delete_object, deps, #format_rest_error, guess_category, #highlight_config_error, #humanize_exception, #humanize_http_exception, inherited, #initialize, list_commands, load_commands, load_deps, msg, #noauth_rest, #parse_options, #read_config_file, reset_subcommands!, #rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_loader, subcommands, subcommands_by_category, ui, unnamed?, #username

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#cookbook_repoObject



116
117
118
119
120
121
# File 'lib/chef/knife/cookbook_upload.rb', line 116

def cookbook_repo
  @cookbook_loader ||= begin
    Chef::Cookbook::FileVendor.on_create { |manifest| Chef::Cookbook::FileSystemFileVendor.new(manifest, config[:cookbook_path]) }
    Chef::CookbookLoader.new(config[:cookbook_path])
  end
end

#environmentObject



131
132
133
# File 'lib/chef/knife/cookbook_upload.rb', line 131

def environment
  @environment ||= config[:environment] ? Environment.load(config[:environment]) : nil
end

#runObject



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
# File 'lib/chef/knife/cookbook_upload.rb', line 70

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

  assert_environment_valid!
  warn_about_cookbook_shadowing
  version_constraints_to_update = {}
  # Get a list of cookbooks and their versions from the server
  # for checking existence of dependending cookbooks.
  @server_side_cookbooks = Chef::CookbookVersion.list

  if config[:all]
    justify_width = cookbook_repo.cookbook_names.map {|name| name.size}.max.to_i + 2
    cookbook_repo.each do |cookbook_name, cookbook|
      cookbook.freeze_version if config[:freeze]
      upload(cookbook, justify_width)
      version_constraints_to_update[cookbook_name] = cookbook.version
    end
  else
    if @name_args.empty?
      show_usage
      ui.error("You must specify the --all flag or at least one cookbook name")
      exit 1
    end
    justify_width = @name_args.map {|name| name.size }.max.to_i + 2
    @name_args.each do |cookbook_name|
      begin
        cookbook = cookbook_repo[cookbook_name]
        if config[:depends]
          cookbook..dependencies.each do |dep, versions|
            @name_args.push dep
          end
        end
        cookbook.freeze_version if config[:freeze]
        upload(cookbook, justify_width)
        version_constraints_to_update[cookbook_name] = cookbook.version
      rescue Exceptions::CookbookNotFoundInRepo => e
        ui.error("Could not find cookbook #{cookbook_name} in your cookbook path, skipping it")
        Log.debug(e)
      end
    end
  end

  ui.info "upload complete"
  update_version_constraints(version_constraints_to_update) if config[:environment]
end

#update_version_constraints(new_version_constraints) ⇒ Object



123
124
125
126
127
128
# File 'lib/chef/knife/cookbook_upload.rb', line 123

def update_version_constraints(new_version_constraints)
  new_version_constraints.each do |cookbook_name, version|
    environment.cookbook_versions[cookbook_name] = "= #{version}"
  end
  environment.save
end

#warn_about_cookbook_shadowingObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/chef/knife/cookbook_upload.rb', line 135

def warn_about_cookbook_shadowing
  unless cookbook_repo.merged_cookbooks.empty?
    ui.warn "* " * 40
    ui.warn(<<-WARNING)
The cookbooks: #{cookbook_repo.merged_cookbooks.join(', ')} exist in multiple places in your cookbook_path.
A composite version of these cookbooks has been compiled for uploading.

#{ui.color('IMPORTANT:', :red, :bold)} In a future version of Chef, this behavior will be removed and you will no longer
be able to have the same version of a cookbook in multiple places in your cookbook_path.
WARNING
    ui.warn "The affected cookbooks are located:"
    ui.output ui.format_for_display(cookbook_repo.merged_cookbook_paths)
    ui.warn "* " * 40
  end
end