vagrant/plugins/kernel_v2/config/disk.rb

140 lines
4.0 KiB
Ruby
Raw Normal View History

require "log4r"
require "securerandom"
module VagrantPlugins
module Kernel_V2
class VagrantConfigDisk < Vagrant.plugin("2", :config)
#-------------------------------------------------------------------
# Config class for a given Disk
#-------------------------------------------------------------------
2019-10-23 21:07:40 +00:00
DEFAULT_DISK_TYPES = [:disk, :dvd, :floppy].freeze
# Note: This value is for internal use only
#
# @return [String]
attr_reader :id
2019-10-23 21:17:07 +00:00
# File name for the given disk. Defaults to a generated name that is:
#
2019-10-23 21:17:07 +00:00
# vagrant_<disk_type>_<short_uuid>
2019-10-17 22:36:21 +00:00
#
# @return [String]
attr_accessor :name
2019-10-23 21:07:40 +00:00
# Type of disk to create. Defaults to `:disk`
#
# @return [Symbol]
attr_accessor :type
# Size of disk to create
#
2019-10-23 21:07:40 +00:00
# TODO: Should we have shortcuts for GB???
#
# @return [Integer]
attr_accessor :size
2019-10-23 21:07:40 +00:00
# Determines if this disk is the _main_ disk, or an attachment.
# Defaults to true.
2019-10-23 20:41:50 +00:00
#
# @return [Boolean]
attr_accessor :primary
# Provider specific options
#
2019-10-17 22:39:58 +00:00
# This should work similar to how a "Provisioner" class works:
#
# - This class is the base class where as this options value is actually a
# provider specific class for the given options for that provider, if required
#
# - Hopefully in general the top-scope disk options are enough for the general
# case that most people won't need provider specific options except for very specific cases
#
# @return [Object]
attr_accessor :config
def initialize(type)
@logger = Log4r::Logger.new("vagrant::config::vm::trigger::config")
2019-10-23 21:07:40 +00:00
@type = type
@name = UNSET_VALUE
@provider_type = UNSET_VALUE
@size = UNSET_VALUE
2019-10-23 20:41:50 +00:00
@primary = UNSET_VALUE
@config = nil
@invalid = false
# Internal options
@id = SecureRandom.uuid
# find disk provider plugin
2019-10-18 18:03:55 +00:00
# Need to pass in provider or figure out provider here
@config_class = nil
# @invalid = true if provider not found
if !@config_class
@logger.info(
"Disk config for '#{@provider_type}' not found. Ignoring config.")
@config_class = Vagrant::Config::V2::DummyConfig
end
end
def add_config(**options, &block)
return if invalid?
current = @config_class.new
current.set_options(options) if options
block.call(current) if block_given?
current = @config.merge(current) if @config
@config = current
end
# Returns true or false if disk provider is found
#
# @return [Bool]
def invalid?
@invalid
end
def finalize!
# Ensure all config options are set to nil or default value if untouched
# by user
2019-10-23 21:07:40 +00:00
@type = :disk if @type == UNSET_VALUE
@size = nil if @size == UNSET_VALUE
2019-10-23 20:41:50 +00:00
@primary = true if @primary == UNSET_VALUE
2019-10-23 21:14:38 +00:00
# Give the disk a default name if unset
2019-10-23 21:52:24 +00:00
# TODO: Name not required if primary?
2019-10-23 21:14:38 +00:00
@name = "vagrant_#{@type.to_s}_#{@id.split("-").last}" if @name == UNSET_VALUE
2019-10-23 21:07:40 +00:00
2019-10-18 18:03:55 +00:00
@config = nil if @config == UNSET_VALUE
end
# @return [Array] array of strings of error messages from config option validation
def validate(machine)
errors = _detected_errors
# validate type with list of known disk types
2019-10-23 21:07:40 +00:00
if !DEFAULT_DISK_TYPES.include?(@type)
errors << "Disk type '#{@type}' is not a valid type. Please pick one of the following supported disk types: #{DEFAULT_DISK_TYPES.join(', ')}"
end
# TODO: Convert a string to int here?
if @size && !@size.is_a?(Integer)
2019-10-23 21:07:40 +00:00
errors << "Config option size for disk is not an integer"
end
errors
end
# The String representation of this Disk.
#
# @return [String]
def to_s
"disk config"
end
end
end
end