Class: Vagrant::BoxMetadata::Version

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/box_metadata.rb

Overview

Represents a single version within the metadata.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw = nil, **_) ⇒ Version

Returns a new instance of Version.



152
153
154
155
156
157
158
159
160
161
# File 'lib/vagrant/box_metadata.rb', line 152

def initialize(raw=nil, **_)
  return if !raw

  @version = raw["version"]
  @providers = raw.fetch("providers", []).map do |data|
    Provider.new(data)
  end
  @provider_map = @providers.group_by(&:name)
  @provider_map = Util::HashWithIndifferentAccess.new(@provider_map)
end

Instance Attribute Details

#versionString

The version that this Version object represents.

Returns:

  • (String)


150
151
152
# File 'lib/vagrant/box_metadata.rb', line 150

def version
  @version
end

Instance Method Details

#provider(name, architecture = nil) ⇒ Object

Returns a [Provider] for the given name, or nil if it isn't supported by this version.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/vagrant/box_metadata.rb', line 165

def provider(name, architecture=nil)
  name = name.to_sym
  arch_name = architecture
  arch_name = Util::Platform.architecture if arch_name == :auto
  arch_name = arch_name.to_s if arch_name

  # If the provider doesn't exist in the map, return immediately
  return if !@provider_map.key?(name)

  # If the arch_name value is set, filter based
  # on architecture and return match if found. If
  # no match is found and architecture wasn't automatically
  # detected, return nil as an explicit match is
  # being requested
  if arch_name
    match = @provider_map[name].detect do |p|
      p.architecture == arch_name
    end

    return match if match || architecture != :auto
  end

  # If the passed architecture value was :auto and no explicit
  # match for the architecture was found, check for a provider
  # that is flagged as the default architecture, and has an
  # architecture value of "unknown"
  #
  # NOTE: This preserves expected behavior with legacy boxes
  if architecture == :auto
    match = @provider_map[name].detect do |p|
      p.architecture == "unknown" &&
        p.default_architecture
    end

    return match if match
  end

  # If the architecture value is set to nil, then just return
  # whatever is defined as the default architecture
  if architecture.nil?
    match = @provider_map[name].detect(&:default_architecture)

    return match if match
  end

  # The metadata consumed may not include architecture information,
  # in which case the match would just be the single provider
  # defined within the provider map for the name
  if @provider_map[name].size == 1 && !@provider_map[name].first.architecture_support?
    return @provider_map[name].first
  end

  # Otherwise, there is no match
  nil
end

#providers(architecture = nil) ⇒ Array<Symbol>

Returns the providers that are available for this version of the box.

Returns:

  • (Array<Symbol>)


225
226
227
228
229
230
231
# File 'lib/vagrant/box_metadata.rb', line 225

def providers(architecture=nil)
  return @provider_map.keys.map(&:to_sym) if architecture.nil?

  @provider_map.keys.find_all { |k|
    provider(k, architecture)
  }.map(&:to_sym)
end