From 15c5efefde5a8a1f7a3ab36de6c4167ddf6fd4cd Mon Sep 17 00:00:00 2001 From: James Armes Date: Tue, 24 Sep 2019 10:18:21 -0400 Subject: [PATCH] Added template file as a configuration option. (#264) --- lib/moonshot/commands/create.rb | 4 ++++ lib/moonshot/commands/delete.rb | 7 +++++++ lib/moonshot/commands/update.rb | 4 ++++ lib/moonshot/controller_config.rb | 1 + lib/moonshot/stack.rb | 7 +++++++ lib/plugins/dynamic_template.rb | 6 +++--- spec/fs_fixtures/moonshot/custom.json | 12 ++++++++++++ spec/moonshot/stack_spec.rb | 12 ++++++++++++ 8 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 spec/fs_fixtures/moonshot/custom.json diff --git a/lib/moonshot/commands/create.rb b/lib/moonshot/commands/create.rb index c33b807a..56ec6e2b 100644 --- a/lib/moonshot/commands/create.rb +++ b/lib/moonshot/commands/create.rb @@ -21,6 +21,10 @@ def parser parser.on('--version VERSION_NAME', 'Version for initial deployment. If unset, a new development build is created from the local directory') do |v| # rubocop:disable LineLength @version = v end + + parser.on('--template-file=FILE', 'Override the path to the CloudFormation template.') do |v| # rubocop:disable LineLength + Moonshot.config.template_file = v + end end def execute diff --git a/lib/moonshot/commands/delete.rb b/lib/moonshot/commands/delete.rb index bc1cc403..1f86107e 100644 --- a/lib/moonshot/commands/delete.rb +++ b/lib/moonshot/commands/delete.rb @@ -6,6 +6,13 @@ class Delete < Moonshot::Command self.usage = 'delete [options]' self.description = 'Delete an existing environment' + def parser + parser = super + parser.on('--template-file=FILE', 'Override the path to the CloudFormation template.') do |v| # rubocop:disable LineLength + Moonshot.config.template_file = v + end + end + def execute controller.delete end diff --git a/lib/moonshot/commands/update.rb b/lib/moonshot/commands/update.rb index 4b2d98fb..2c2b8997 100644 --- a/lib/moonshot/commands/update.rb +++ b/lib/moonshot/commands/update.rb @@ -22,6 +22,10 @@ def parser parser.on('--refresh-parameters', TrueClass, 'Update parameters from parent stacks') do |v| @refresh_parameters = v end + + parser.on('--template-file=FILE', 'Override the path to the CloudFormation template.') do |v| # rubocop:disable LineLength + Moonshot.config.template_file = v + end end def execute diff --git a/lib/moonshot/controller_config.rb b/lib/moonshot/controller_config.rb index 14f65a79..77893360 100644 --- a/lib/moonshot/controller_config.rb +++ b/lib/moonshot/controller_config.rb @@ -26,6 +26,7 @@ class ControllerConfig attr_accessor :ssh_command attr_accessor :ssh_config attr_accessor :ssh_instance + attr_accessor :template_file attr_accessor :template_s3_bucket def initialize diff --git a/lib/moonshot/stack.rb b/lib/moonshot/stack.rb index 3c0a3add..8d2819ce 100644 --- a/lib/moonshot/stack.rb +++ b/lib/moonshot/stack.rb @@ -159,7 +159,14 @@ def load_template_file ) ] + # If a template file has been specified in the config, look there first. + if @config.template_file + templates.unshift YamlStackTemplate.new(@config.template_file) + templates.unshift JsonStackTemplate.new(@config.template_file) + end + template = templates.find(&:exist?) + raise 'No template found in moonshot/template.{yml,json}!' unless template template end diff --git a/lib/plugins/dynamic_template.rb b/lib/plugins/dynamic_template.rb index 10cbe509..e15b8ffd 100644 --- a/lib/plugins/dynamic_template.rb +++ b/lib/plugins/dynamic_template.rb @@ -14,9 +14,9 @@ def run_hook end def cli_hook(parser) - parser.on('-sPATH', '--destination-path=PATH', - 'Destination path for the dynamically generated template', String) do |value| - @dynamic_template.destination = value + parser.on('--template-file=FILE', 'Override the path to the CloudFormation template.') do |v| # rubocop:disable LineLength + @dynamic_template.destination = v + Moonshot.config.template_file = v end end diff --git a/spec/fs_fixtures/moonshot/custom.json b/spec/fs_fixtures/moonshot/custom.json new file mode 100644 index 00000000..f42da782 --- /dev/null +++ b/spec/fs_fixtures/moonshot/custom.json @@ -0,0 +1,12 @@ +{ + "Resources": { + + }, + + "Parameters": { + "Parent1": { + "Type": "String", + "Description": "This is imported from the parent stack on create." + } + } +} diff --git a/spec/moonshot/stack_spec.rb b/spec/moonshot/stack_spec.rb index f5e01e12..7d8a0cfe 100644 --- a/spec/moonshot/stack_spec.rb +++ b/spec/moonshot/stack_spec.rb @@ -152,5 +152,17 @@ expect { subject.template } .to raise_error(RuntimeError, /No template found/) end + + context 'a custom template file is specified' do + let(:custom_path) { File.join(Dir.pwd, 'moonshot', 'custom.json') } + + before(:each) do + config.template_file = custom_path + end + + it 'should load the custom template' do + expect(subject.template.filename).to eq(custom_path) + end + end end end