Class: Chef::Knife::CookbookSiteInstall

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

Direct Known Subclasses

CookbookSiteVendor

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, common_name, #config_file_settings, config_loader, #configure_chef, #create_object, #delete_object, dependency_loaders, deps, #format_rest_error, guess_category, #humanize_exception, #humanize_http_exception, inherited, #initialize, 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

Methods included from Mixin::PathSanity

#enforce_path_sanity

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Attribute Details

#cookbook_nameObject (readonly)

Returns the value of attribute cookbook_name.



62
63
64
# File 'lib/chef/knife/cookbook_site_install.rb', line 62

def cookbook_name
  @cookbook_name
end

#vendor_pathObject (readonly)

Returns the value of attribute vendor_path.



63
64
65
# File 'lib/chef/knife/cookbook_site_install.rb', line 63

def vendor_path
  @vendor_path
end

Instance Method Details

#clear_existing_files(cookbook_path) ⇒ Object



152
153
154
155
# File 'lib/chef/knife/cookbook_site_install.rb', line 152

def clear_existing_files(cookbook_path)
  ui.info("Removing pre-existing version.")
  FileUtils.rmtree(cookbook_path) if File.directory?(cookbook_path)
end

#convert_path(upstream_file) ⇒ Object



157
158
159
160
161
162
163
164
# File 'lib/chef/knife/cookbook_site_install.rb', line 157

def convert_path(upstream_file)
  # converts a Windows path (C:\foo) to a mingw path (/c/foo)
  if ENV["MSYSTEM"] == "MINGW32"
    return upstream_file.sub(/^([[:alpha:]]):/, '/\1')
  else
    return Shellwords.escape upstream_file
  end
end

#download_cookbook_to(download_path) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/chef/knife/cookbook_site_install.rb', line 134

def download_cookbook_to(download_path)
  downloader = Chef::Knife::CookbookSiteDownload.new
  downloader.config[:file] = download_path
  downloader.name_args = name_args
  downloader.run
  downloader
end

#extract_cookbook(upstream_file, version) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/chef/knife/cookbook_site_install.rb', line 142

def extract_cookbook(upstream_file, version)
  ui.info("Uncompressing #{@cookbook_name} version #{version}.")
  # FIXME: Detect if we have the bad tar from git on Windows: https://github.com/opscode/chef/issues/1753
  extract_command = "tar zxvf \"#{convert_path upstream_file}\""
  if Chef::Platform.windows?
    extract_command << " --force-local"
  end
  shell_out!(extract_command, :cwd => @install_path)
end

#parse_name_args!Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/chef/knife/cookbook_site_install.rb', line 121

def parse_name_args!
  if name_args.empty?
    ui.error("Please specify a cookbook to download and install.")
    exit 1
  elsif name_args.size >= 2
    unless name_args.last.match(/^(\d+)(\.\d+){1,2}$/) and name_args.size == 2
      ui.error("Installing multiple cookbooks at once is not supported.")
      exit 1
    end
  end
  name_args.first
end

#preferred_metadataChef::Cookbook::Metadata

Get the preferred metadata path on disk. Chef prefers the metadata.rb over the metadata.json.

Returns:

Raises:

  • if there is no metadata in the cookbook



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/chef/knife/cookbook_site_install.rb', line 172

def 
  md = Chef::Cookbook::Metadata.new

  rb = File.join(@install_path, @cookbook_name, "metadata.rb")
  if File.exist?(rb)
    md.from_file(rb)
    return md
  end

  json = File.join(@install_path, @cookbook_name, "metadata.json")
  if File.exist?(json)
    json = IO.read(json)
    md.from_json(json)
    return md
  end

  raise Chef::Exceptions::MetadataNotFound.new(@install_path, @cookbook_name)
end

#runObject



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

def run
  extend Chef::Mixin::ShellOut

  if config[:cookbook_path]
    Chef::Config[:cookbook_path] = config[:cookbook_path]
  else
    config[:cookbook_path] = Chef::Config[:cookbook_path]
  end

  @cookbook_name = parse_name_args!
  # Check to ensure we have a valid source of cookbooks before continuing
  #
  @install_path = File.expand_path(Array(config[:cookbook_path]).first)
  ui.info "Installing #@cookbook_name to #{@install_path}"

  @repo = CookbookSCMRepo.new(@install_path, ui, config)
  #cookbook_path = File.join(vendor_path, name_args[0])
  upstream_file = File.join(@install_path, "#{@cookbook_name}.tar.gz")

  @repo.sanity_check
  unless config[:use_current_branch]
    @repo.reset_to_default_state
    @repo.prepare_to_import(@cookbook_name)
  end

  downloader = download_cookbook_to(upstream_file)
  clear_existing_files(File.join(@install_path, @cookbook_name))
  extract_cookbook(upstream_file, downloader.version)

  # TODO: it'd be better to store these outside the cookbook repo and
  # keep them around, e.g., in ~/Library/Caches on OS X.
  ui.info("Removing downloaded tarball")
  File.unlink(upstream_file)

  if @repo.finalize_updates_to(@cookbook_name, downloader.version)
    unless config[:use_current_branch]
      @repo.reset_to_default_state
    end
    @repo.merge_updates_from(@cookbook_name, downloader.version)
  else
    unless config[:use_current_branch]
      @repo.reset_to_default_state
    end
  end

  unless config[:no_deps]
    .dependencies.each do |cookbook, version_list|
      # Doesn't do versions.. yet
      nv = self.class.new
      nv.config = config
      nv.name_args = [ cookbook ]
      nv.run
    end
  end
end