Class: Chef::Knife::CookbookDownload

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

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, common_name, #config_file_settings, #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, #locate_config_file, #merge_configs, 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?, use_separate_defaults?, #username

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::PathSanity

#enforce_path_sanity

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/chef/knife/cookbook_download.rb', line 118

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



107
108
109
110
111
112
113
114
115
116
# File 'lib/chef/knife/cookbook_download.rb', line 107

def available_versions
  @available_versions ||= begin
    versions = Chef::CookbookVersion.available_versions(@cookbook_name).map do |version|
      Chef::Version.new(version)
    end
    versions.sort!
    versions
  end
  @available_versions
end

#determine_versionObject



97
98
99
100
101
102
103
104
105
# File 'lib/chef/knife/cookbook_download.rb', line 97

def determine_version
  if available_versions.size == 1
    @version = available_versions.first
  elsif config[:latest]
    @version = available_versions.map { |v| Chef::Version.new(v) }.sort.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
# 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
  end

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

  cookbook = rest.get_rest("cookbooks/#{@cookbook_name}/#{@version}")
  manifest = cookbook.manifest

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

  Chef::CookbookVersion::COOKBOOK_SEGMENTS.each do |segment|
    next unless manifest.has_key?(segment)
    ui.info("Downloading #{segment}")
    manifest[segment].each do |segment_file|
      dest = File.join(basedir, segment_file['path'].gsub('/', File::SEPARATOR))
      Chef::Log.debug("Downloading #{segment_file['path']} to #{dest}")
      FileUtils.mkdir_p(File.dirname(dest))
      rest.sign_on_redirect = false
      tempfile = rest.get_rest(segment_file['url'], true)
      FileUtils.mv(tempfile.path, dest)
    end
  end
  ui.info("Cookbook downloaded to #{basedir}")
end