Class: Vagrant::Box
Overview
Represents a "box," which is a package Vagrant environment that is used as a base image when creating a new guest machine.
Constant Summary collapse
- REQUIRED_METADATA_FIELDS =
The required fields in a boxes
metadata.jsonfile ["provider"]
- BOX_UPDATE_CHECK_INTERVAL =
Number of seconds to wait between checks for box updates
3600
Instance Attribute Summary collapse
-
#architecture ⇒ String
readonly
This is the architecture that this box is build for.
-
#directory ⇒ Pathname
readonly
This is the directory on disk where this box exists.
-
#metadata ⇒ Hash
readonly
This is the metadata for the box.
-
#metadata_url ⇒ String
readonly
This is the URL to the version info and other metadata for this box.
-
#name ⇒ String
readonly
The box name.
-
#provider ⇒ Symbol
readonly
This is the provider that this box is built for.
-
#version ⇒ String
readonly
The version of this box.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Implemented for comparison with other boxes.
-
#automatic_update_check_allowed? ⇒ Boolean
Check if a box update check is allowed.
-
#destroy! ⇒ Object
This deletes the box.
-
#has_update?(version = nil, download_options: {}) ⇒ Array
Checks if the box has an update and returns the metadata, version, and provider.
-
#in_use?(index) ⇒ Array<MachineIndex::Entry>
Checks if this box is in use according to the given machine index and returns the entries that appear to be using the box.
-
#initialize(name, provider, version, directory, architecture: nil, metadata_url: nil, hook: nil) ⇒ Box
constructor
This is used to initialize a box.
-
#load_metadata(download_options = {}) ⇒ BoxMetadata
Loads the metadata URL and returns the latest metadata associated with this box.
-
#repackage(path) ⇒ Boolean
This repackages this box and outputs it to the given path.
- #validate_metadata_json(metadata) ⇒ Object
Constructor Details
#initialize(name, provider, version, directory, architecture: nil, metadata_url: nil, hook: nil) ⇒ Box
This is used to initialize a box.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/vagrant/box.rb', line 74 def initialize(name, provider, version, directory, architecture: nil, metadata_url: nil, hook: nil) @name = name @version = version @provider = provider @directory = directory @architecture = architecture = @hook = hook = directory.join("metadata.json") raise Errors::BoxMetadataFileNotFound, name: @name if !.file? begin = JSON.parse(directory.join("metadata.json").read) () rescue JSON::ParserError raise Errors::BoxMetadataCorrupted, name: @name end @logger = Log4r::Logger.new("vagrant::box") end |
Instance Attribute Details
#architecture ⇒ String (readonly)
This is the architecture that this box is build for.
41 42 43 |
# File 'lib/vagrant/box.rb', line 41 def architecture @architecture end |
#directory ⇒ Pathname (readonly)
This is the directory on disk where this box exists.
51 52 53 |
# File 'lib/vagrant/box.rb', line 51 def directory @directory end |
#metadata ⇒ Hash (readonly)
This is the metadata for the box. This is read from the "metadata.json" file that all boxes require.
57 58 59 |
# File 'lib/vagrant/box.rb', line 57 def end |
#metadata_url ⇒ String (readonly)
This is the URL to the version info and other metadata for this box.
63 64 65 |
# File 'lib/vagrant/box.rb', line 63 def end |
#name ⇒ String (readonly)
The box name. This is the logical name used when adding the box.
31 32 33 |
# File 'lib/vagrant/box.rb', line 31 def name @name end |
#provider ⇒ Symbol (readonly)
This is the provider that this box is built for.
36 37 38 |
# File 'lib/vagrant/box.rb', line 36 def provider @provider end |
#version ⇒ String (readonly)
The version of this box.
46 47 48 |
# File 'lib/vagrant/box.rb', line 46 def version @version end |
Instance Method Details
#<=>(other) ⇒ Object
Implemented for comparison with other boxes. Comparison is implemented by comparing names, providers, and architectures.
252 253 254 255 256 257 258 |
# File 'lib/vagrant/box.rb', line 252 def <=>(other) return super if !other.is_a?(self.class) # Comparison is done by composing the name and provider "#{@name}-#{@version}-#{@provider}-#{@architecture}" <=> "#{other.name}-#{other.version}-#{other.provider}-#{other.architecture}" end |
#automatic_update_check_allowed? ⇒ Boolean
Check if a box update check is allowed. Uses a file in the box data directory to track when the last auto update check was performed and returns true if the BOX_UPDATE_CHECK_INTERVAL has passed.
216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/vagrant/box.rb', line 216 def automatic_update_check_allowed? check_path = directory.join("box_update_check") if check_path.exist? last_check_span = Time.now.to_i - check_path.mtime.to_i if last_check_span < BOX_UPDATE_CHECK_INTERVAL @logger.info("box update check is under the interval threshold") return false end end FileUtils.touch(check_path) true end |
#destroy! ⇒ Object
This deletes the box. This is NOT undoable.
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/vagrant/box.rb', line 109 def destroy! # Delete the directory to delete the box. FileUtils.rm_r(@directory) # Just return true always true rescue Errno::ENOENT # This means the directory didn't exist. Not a problem. return true end |
#has_update?(version = nil, download_options: {}) ⇒ Array
Checks if the box has an update and returns the metadata, version, and provider. If the box doesn't have an update that satisfies the constraints, it will return nil.
This will potentially make a network call if it has to load the metadata from the network.
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/vagrant/box.rb', line 189 def has_update?(version=nil, download_options: {}) if ! raise Errors::BoxUpdateNoMetadata, name: @name end if .delete(:automatic_check) && !automatic_update_check_allowed? @logger.info("Skipping box update check") return end version += ", " if version version ||= "" version += "> #{@version}" md = self.() newer = md.version(version, provider: @provider, architecture: @architecture) return nil if newer == nil || !md.compatible_version_update?(@version, newer.version, provider: @provider, architecture: @architecture) [md, newer, newer.provider(@provider, @architecture)] end |
#in_use?(index) ⇒ Array<MachineIndex::Entry>
Checks if this box is in use according to the given machine index and returns the entries that appear to be using the box.
The entries returned, if any, are not tested for validity with MachineIndex::Entry#valid?, so the caller should do that if the caller cares.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/vagrant/box.rb', line 129 def in_use?(index) results = [] index.each do |entry| box_data = entry.extra_data["box"] next if !box_data # If all the data matches, record it if box_data["name"] == self.name && box_data["provider"] == self.provider.to_s && box_data["architecture"] == self.architecture && box_data["version"] == self.version.to_s results << entry end end return nil if results.empty? results end |
#load_metadata(download_options = {}) ⇒ BoxMetadata
Loads the metadata URL and returns the latest metadata associated with this box.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/vagrant/box.rb', line 153 def (={}) tf = Tempfile.new("vagrant-load-metadata") tf.close url = if File.file?(url) || url !~ /^[a-z0-9]+:.*$/i url = File.(url) url = Util::Platform.cygwin_windows_path(url) url = "file:#{url}" end opts = { headers: ["Accept: application/json"] }.merge() d = Util::Downloader.new(url, tf.path, opts) if @hook @hook.call(:authenticate_box_downloader, downloader: d) end d.download! BoxMetadata.new(File.open(tf.path, "r"), url: url) rescue Errors::DownloaderError => e raise Errors::BoxMetadataDownloadError, message: e.extra_data[:message] ensure tf.unlink if tf end |
#repackage(path) ⇒ Boolean
This repackages this box and outputs it to the given path.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/vagrant/box.rb', line 234 def repackage(path) @logger.debug("Repackaging box '#{@name}' to: #{path}") Util::SafeChdir.safe_chdir(@directory) do # Find all the files in our current directory and tar it up! files = Dir.glob(File.join(".", "**", "*")).select { |f| File.file?(f) } # Package! Util::Subprocess.execute("bsdtar", "-czf", path.to_s, *files) end @logger.info("Repackaged box '#{@name}' successfully: #{path}") true end |
#validate_metadata_json(metadata) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/vagrant/box.rb', line 96 def () = .keys REQUIRED_METADATA_FIELDS.each do |field| if !.include?(field) raise Errors::BoxMetadataMissingRequiredFields, name: @name, required_field: field, all_fields: REQUIRED_METADATA_FIELDS.join(", ") end end end |