From aa5a3ef7f74f0029f89f594e6c12cefe324c416d Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Thu, 17 Oct 2019 15:24:27 -0700 Subject: [PATCH] Add initial disk management config class --- plugins/kernel_v2/config/disk.rb | 79 ++++++++++++++++++++++++++++++++ plugins/kernel_v2/config/vm.rb | 24 ++++++++++ 2 files changed, 103 insertions(+) create mode 100644 plugins/kernel_v2/config/disk.rb diff --git a/plugins/kernel_v2/config/disk.rb b/plugins/kernel_v2/config/disk.rb new file mode 100644 index 000000000..427a5f1b5 --- /dev/null +++ b/plugins/kernel_v2/config/disk.rb @@ -0,0 +1,79 @@ +require "log4r" +require "securerandom" + +module VagrantPlugins + module Kernel_V2 + class VagrantConfigDisk < Vagrant.plugin("2", :config) + #------------------------------------------------------------------- + # Config class for a given Disk + #------------------------------------------------------------------- + + # Note: This value is for internal use only + # + # @return [String] + attr_reader :id + + # Name for the given Disk. Defaults to nil. + # + # TODO: Should probably default to a string+short integer id + # + # @return [String] + attr_accessor :name + + # Type of disk to create + # + # @return [Symbol] + attr_accessor :type + + # Size of disk to create + # + # @return [Integer] + attr_accessor :size + + # Provider specific options + # + # TODO: INTERNAL?? + # + # @return [Hash] + attr_accessor :options + + def initialize(type) + @logger = Log4r::Logger.new("vagrant::config::vm::trigger::config") + + @name = UNSET_VALUE + @type = UNSET_VALUE + @size = UNSET_VALUE + @options = UNSET_VALUE + + # Internal options + @id = SecureRandom.uuid + end + + def finalize! + # Ensure all config options are set to nil or default value if untouched + # by user + @name = nil if @name == UNSET_VALUE + @type = nil if @type == UNSET_VALUE + @size = nil if @size == UNSET_VALUE + + @options = nil if @options == 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 + + errors + end + + # The String representation of this Disk. + # + # @return [String] + def to_s + "disk config" + end + end + end +end diff --git a/plugins/kernel_v2/config/vm.rb b/plugins/kernel_v2/config/vm.rb index b80bda8ca..b2d110fa8 100644 --- a/plugins/kernel_v2/config/vm.rb +++ b/plugins/kernel_v2/config/vm.rb @@ -11,6 +11,7 @@ require "vagrant/util/experimental" require File.expand_path("../vm_provisioner", __FILE__) require File.expand_path("../vm_subvm", __FILE__) +require File.expand_path("../disk", __FILE__) module VagrantPlugins module Kernel_V2 @@ -79,6 +80,7 @@ module VagrantPlugins @__compiled_provider_configs = {} @__defined_vm_keys = [] @__defined_vms = {} + @__drives = {} @__finalized = false @__networks = {} @__providers = {} @@ -123,6 +125,11 @@ module VagrantPlugins end end + # Merge defined drives + # TODO: Actually write this + other_drives = other.instance_variable_get(:@__drives) + new_drives = @__drives.dup + # Merge the providers by prepending any configuration blocks we # have for providers onto the new configuration. other_providers = other.instance_variable_get(:@__providers) @@ -185,6 +192,7 @@ module VagrantPlugins result.instance_variable_set(:@__defined_vm_keys, new_defined_vm_keys) result.instance_variable_set(:@__defined_vms, new_defined_vms) + result.instance_variable_set(:@__drives, new_drives) result.instance_variable_set(:@__providers, new_providers) result.instance_variable_set(:@__provider_order, new_order) result.instance_variable_set(:@__provider_overrides, new_overrides) @@ -384,6 +392,17 @@ module VagrantPlugins @__defined_vms[name].config_procs << [options[:config_version], block] if block end + def disk(type, &block) + disk = VagrantConfigDisk.new(type) + if block.is_a?(Hash) + disk.set_options(block) + else + block.call(disk, VagrantConfigDisk) + end + + @__drives[disk[:id]] = disk + end + #------------------------------------------------------------------- # Internal methods, don't call these. #------------------------------------------------------------------- @@ -547,6 +566,11 @@ module VagrantPlugins end end + # TODO: This might need to be more complicated + @__drives.each do |d| + d.finalize! + end + if !current_dir_shared && !@__synced_folders["/vagrant"] synced_folder(".", "/vagrant") end