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.



126
127
128
129
130
131
132
133
134
135
# File 'lib/vagrant/box_metadata.rb', line 126

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)


124
125
126
# File 'lib/vagrant/box_metadata.rb', line 124

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.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
# File 'lib/vagrant/box_metadata.rb', line 139

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>)


199
200
201
202
203
204
205
# File 'lib/vagrant/box_metadata.rb', line 199

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