From 7b95826dd7eb5f364c7d9b50e1efc545deefd489 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Wed, 10 Jun 2020 15:38:18 -0700 Subject: [PATCH] Add type as param for cloud_init config --- plugins/kernel_v2/config/cloud_init.rb | 25 ++++++++++++++++++- plugins/kernel_v2/config/vm.rb | 7 ++++-- templates/locales/en.yml | 3 +++ .../kernel_v2/config/cloud_init_test.rb | 18 ++++++++++++- .../pages/docs/cloud-init/configuration.mdx | 21 ++++++++++++++++ 5 files changed, 70 insertions(+), 4 deletions(-) diff --git a/plugins/kernel_v2/config/cloud_init.rb b/plugins/kernel_v2/config/cloud_init.rb index 0f3f7ad6a..fc7a1bc28 100644 --- a/plugins/kernel_v2/config/cloud_init.rb +++ b/plugins/kernel_v2/config/cloud_init.rb @@ -14,11 +14,19 @@ module VagrantPlugins "text/x-include-once-url", "text/x-include-url", "text/x-shellscript"].map(&:freeze).freeze + DEFAULT_CONFIG_TYPE = :user_data + # @note This value is for internal use only # # @return [String] attr_reader :id + # The 'type' of data being stored. If not defined, + # will default to :user_data + # + # @return [Symbol] + attr_accessor :type + # @return [String] attr_accessor :content_type @@ -28,9 +36,11 @@ module VagrantPlugins # @return [String] attr_accessor :inline - def initialize() + def initialize(type=nil) @logger = Log4r::Logger.new("vagrant::config::vm::cloud_init") + @type = type if type + @content_type = UNSET_VALUE @path = UNSET_VALUE @inline = UNSET_VALUE @@ -40,6 +50,12 @@ module VagrantPlugins end def finalize! + if !@type + @type = DEFAULT_CONFIG_TYPE + else + @type = @type.to_sym + end + @content_type = nil if @content_type == UNSET_VALUE @path = nil if @path == UNSET_VALUE @inline = nil if @inline == UNSET_VALUE @@ -49,6 +65,13 @@ module VagrantPlugins def validate(machine) errors = _detected_errors + if @type && @type != DEFAULT_CONFIG_TYPE + errors << I18n.t("vagrant.cloud_init.incorrect_type_set", + type: @type, + machine: machine.name, + default_type: DEFAULT_CONFIG_TYPE) + end + if !@content_type errors << I18n.t("vagrant.cloud_init.content_type_not_set", machine: machine.name, diff --git a/plugins/kernel_v2/config/vm.rb b/plugins/kernel_v2/config/vm.rb index 117396f4e..287a001fb 100644 --- a/plugins/kernel_v2/config/vm.rb +++ b/plugins/kernel_v2/config/vm.rb @@ -473,10 +473,13 @@ module VagrantPlugins # Stores config options for cloud_init # + # @param [Symbol] type # @param [Hash] options # @param [Block] block - def cloud_init(**options, &block) - cloud_init_config = VagrantConfigCloudInit.new() + def cloud_init(type=nil, **options, &block) + type = type.to_sym if type + + cloud_init_config = VagrantConfigCloudInit.new(type) if block_given? block.call(cloud_init_config, VagrantConfigCloudInit) diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 785696be0..5538bc37a 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -2240,6 +2240,9 @@ en: incorrect_path_type: |- The path '%{path}' given for guest '%{machine}' is of type '%{type}'. Config option 'path' must be a String. + incorrect_type_set: |- + The type '%{type}' defined for a cloud_init config with guest '%{machine}' + is not a valid type. Currently, Vagrant only supports type: '%{default_type}'. path_and_inline_set: |- Guest '%{machine}' defines both a 'path' and 'inline' for its cloud_init config, however only one config option should be defined for cloud_init. diff --git a/test/unit/plugins/kernel_v2/config/cloud_init_test.rb b/test/unit/plugins/kernel_v2/config/cloud_init_test.rb index 29f15ab6c..5fe263c7a 100644 --- a/test/unit/plugins/kernel_v2/config/cloud_init_test.rb +++ b/test/unit/plugins/kernel_v2/config/cloud_init_test.rb @@ -5,7 +5,7 @@ require Vagrant.source_root.join("plugins/kernel_v2/config/cloud_init") describe VagrantPlugins::Kernel_V2::VagrantConfigCloudInit do include_context "unit" - subject { described_class.new } + subject { described_class.new(:user_data) } let(:provider) { double("provider") } let(:machine) { double("machine", name: "rspec", provider: provider, @@ -46,6 +46,22 @@ describe VagrantPlugins::Kernel_V2::VagrantConfigCloudInit do subject.finalize! expect(subject.content_type).to eq("text/cloud-config") end + + context "with no type set" do + let(:type_subject) { described_class.new } + + before do + type_subject.content_type = "text/cloud-config" + type_subject.inline = <<-CONFIG + package_update: true + CONFIG + end + + it "defaults to a type" do + type_subject.finalize! + expect(type_subject.type).to eq(:user_data) + end + end end context "with an invalid option set" do diff --git a/website/pages/docs/cloud-init/configuration.mdx b/website/pages/docs/cloud-init/configuration.mdx index dfa8fc557..031e1c9fb 100644 --- a/website/pages/docs/cloud-init/configuration.mdx +++ b/website/pages/docs/cloud-init/configuration.mdx @@ -41,3 +41,24 @@ option. Examples of how to define these options can be found in the [usage documentation](/docs/cloud-init/configuration). + +### cloud_init Type + +When defining a config for cloud_init, you can optionally define a `type` for the config: + +```ruby +config.vm.cloud_init :user_data, content_type: "text/cloud-config", path: "config.cfg" + +config.vm.cloud_init :user_data do |cloud_init| + cloud_init.content_type = "text/cloud-config" + cloud_init.path = "config.cfg" +end + +``` + +However, this is not a requirement. Leaving off `type` will default to `:user_data`. + +- `type` (Symbol) - This is an optional config that defines the type of +cloud-init config. Currently, the only supported `type` is `:user_data`. If a type +is not defined, it will default to `:user_data`. +