vagrant/plugins/kernel_v2/config/disk.rb

155 lines
4.1 KiB
Ruby
Raw Normal View History

require "log4r"
require "securerandom"
require "vagrant/util/numeric"
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
#
# @return [Integer,String]
attr_accessor :size
2019-10-30 20:35:21 +00:00
# Path to the location of the disk file (Optional)
#
# @return [String]
attr_accessor :file
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
#
# @return [Hash]
attr_accessor :provider_config
def initialize(type)
2019-10-30 22:37:01 +00:00
@logger = Log4r::Logger.new("vagrant::config::vm::disk")
2019-10-23 21:07:40 +00:00
@type = type
@provider_config = {}
2019-10-23 21:07:40 +00:00
@name = UNSET_VALUE
@provider_type = UNSET_VALUE
@size = UNSET_VALUE
2019-10-23 20:41:50 +00:00
@primary = UNSET_VALUE
2019-10-30 20:35:21 +00:00
@file = UNSET_VALUE
# Internal options
@id = SecureRandom.uuid
end
# Helper method for storing provider specific config options
#
# Expected format is:
#
# - `provider__diskoption: value`
# - `{provider: {diskoption: value, otherdiskoption: value, ...}`
#
# Duplicates will be overriden
#
# @param [Hash] options
def add_provider_config(**options, &block)
current = {}
options.each do |k,v|
opts = k.to_s.split("__")
if opts.size == 2
current[opts[0].to_sym] = {opts[1].to_sym => v}
elsif v.is_a?(Hash)
current[k] = v
else
@logger.warn("Disk option '#{k}' found that does not match expected provider disk config schema.")
end
end
current = @provider_config.merge(current) if !@provider_config.empty?
@provider_config = current
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-30 20:35:21 +00:00
@file = nil if @file == UNSET_VALUE
2019-10-23 22:32:04 +00:00
if @primary == UNSET_VALUE
@primary = false
2019-11-13 19:29:46 +00:00
else
@primary = true
2019-10-23 22:32:04 +00:00
end
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
@provider_config = nil if @provider_config == {}
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
if @size && !@size.is_a?(Integer)
if @size.is_a?(String)
@size = Vagrant::Util::Numeric.string_to_bytes(@size)
else
errors << "Config option size for disk is not an integer"
end
2019-10-23 21:07:40 +00:00
end
2019-10-30 20:35:21 +00:00
if @file
if !@file.is_a?(String)
errors << "Config option `file` for #{machine.name} disk config is not a string"
else
# Validate that file exists
end
end
errors
end
# The String representation of this Disk.
#
# @return [String]
def to_s
"disk config"
end
end
end
end