Class: Chef::Knife::CookbookDownload

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

Constant Summary

Constants inherited from Chef::Knife

DEFAULT_SUBCOMMAND_FILES

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args

Instance Method Summary collapse

Methods inherited from Chef::Knife

#ask_question, #bulk_delete, category, #configure_chef, #confirm, #create_object, #delete_object, #edit_data, #edit_object, #file_exists_and_is_readable?, #format_for_display, #format_list_for_display, guess_category, inherited, #initialize, list_commands, load_commands, #load_from_file, #msg, msg, #output, #parse_options, #pretty_print, #rest, run, #show_usage, snake_case_name, #stdin, #stdout, subcommand_category, subcommand_class_from, subcommands, subcommands_by_category, unnamed?

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

#ask_which_versionObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/chef/knife/cookbook_download.rb', line 112

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]
    Chef::Log.error("'#{response}' is not a valid value.")
    exit(1)
  end
end

#available_versionsObject



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

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

#determine_versionObject



90
91
92
93
94
95
96
97
98
# File 'lib/chef/knife/cookbook_download.rb', line 90

def determine_version
  if available_versions.size == 1
    @version = available_versions.first
  elsif config[:latest]
    @version = available_versions.map { |v| Chef::Cookbook::Metadata::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



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

def run
  @cookbook_name, @version = @name_args

  if @cookbook_name.nil?
    show_usage
    Chef::Log.fatal("You must specify a cookbook name")
    exit 1
  elsif @version.nil?
    determine_version
  end
    
  Chef::Log.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
      Chef::Log.fatal("Directory #{basedir} exists, use --force to overwrite")
      exit
    end
  end
  
  Chef::CookbookVersion::COOKBOOK_SEGMENTS.each do |segment|
    next unless manifest.has_key?(segment)
    Chef::Log.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
  Chef::Log.info("Cookbook downloaded to #{basedir}")
end