Class: Chef::Knife::CookbookDownload

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

Constant Summary

Constants inherited from Chef::Knife

CHEF_ORGANIZATION_MANAGEMENT, OFFICIAL_PLUGINS, OPSCODE_HOSTED_CHEF_ACCESS_CONTROL

Instance Attribute Summary collapse

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods inherited from Chef::Knife

#api_key, #apply_computed_config, category, chef_config_dir, #cli_keys, common_name, #config_file_settings, config_loader, #config_source, #configure_chef, #create_object, #delete_object, dependency_loaders, deps, #format_rest_error, guess_category, #humanize_exception, #humanize_http_exception, inherited, #initialize, list_commands, load_commands, load_config, load_deps, #maybe_setup_fips, #merge_configs, msg, #noauth_rest, #parse_options, reset_config_loader!, reset_subcommands!, #rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_files, subcommand_loader, subcommands, subcommands_by_category, #test_mandatory_field, ui, unnamed?, use_separate_defaults?, #username

Methods included from Mixin::ConvertToClassName

#constantize, #convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #normalize_snake_case_name, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Attribute Details

#cookbook_nameObject

Returns the value of attribute cookbook_name.



27
28
29
# File 'lib/chef/knife/cookbook_download.rb', line 27

def cookbook_name
  @cookbook_name
end

#versionObject (readonly)

Returns the value of attribute version.



26
27
28
# File 'lib/chef/knife/cookbook_download.rb', line 26

def version
  @version
end

Instance Method Details

#ask_which_versionObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/chef/knife/cookbook_download.rb', line 123

def ask_which_version
  question = "Which version do you want to download?\n"
  valid_responses = {}
  available_versions.each_with_index do |version, index|
    valid_responses[(index + 1).to_s] = version
    question << "#{index + 1}. #{@cookbook_name} #{version}\n"
  end
  question += "\n"
  response = ask_question(question).strip

  unless @version = valid_responses[response]
    ui.error("'#{response}' is not a valid value.")
    exit(1)
  end
  @version
end

#available_versionsObject



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/chef/knife/cookbook_download.rb', line 111

def available_versions
  @available_versions ||= begin
    versions = Chef::CookbookVersion.available_versions(@cookbook_name)
    unless versions.nil?
      versions.map! { |version| Chef::Version.new(version) }
      versions.sort!
    end
    versions
  end
  @available_versions
end

#determine_versionObject



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/chef/knife/cookbook_download.rb', line 99

def determine_version
  if available_versions.nil?
    nil
  elsif available_versions.size == 1
    @version = available_versions.first
  elsif config[:latest]
    @version = available_versions.last
  else
    ask_which_version
  end
end

#runObject

TODO: tim/cw: 5-23-2010: need to implement knife-side specificity for downloads - need to implement –platform and –fqdn here



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

def run
  @cookbook_name, @version = @name_args

  if @cookbook_name.nil?
    show_usage
    ui.fatal("You must specify a cookbook name")
    exit 1
  elsif @version.nil?
    @version = determine_version
    if @version.nil?
      ui.fatal("No such cookbook found")
      exit 1
    end
  end

  ui.info("Downloading #{@cookbook_name} cookbook version #{@version}")

  cookbook = Chef::CookbookVersion.load(@cookbook_name, @version)
  manifest = cookbook.cookbook_manifest

  basedir = File.join(config[:download_directory], "#{@cookbook_name}-#{cookbook.version}")
  if File.exists?(basedir)
    if config[:force]
      Chef::Log.trace("Deleting #{basedir}")
      FileUtils.rm_rf(basedir)
    else
      ui.fatal("Directory #{basedir} exists, use --force to overwrite")
      exit
    end
  end

  manifest.by_parent_directory.each do |segment, files|
    ui.info("Downloading #{segment}")
    files.each do |segment_file|
      dest = File.join(basedir, segment_file["path"].gsub("/", File::SEPARATOR))
      Chef::Log.trace("Downloading #{segment_file["path"]} to #{dest}")
      FileUtils.mkdir_p(File.dirname(dest))
      tempfile = rest.streaming_request(segment_file["url"])
      FileUtils.mv(tempfile.path, dest)
    end
  end
  ui.info("Cookbook downloaded to #{basedir}")
end