Class: CfnBackup::Generate

Inherits:
Thor::Group
  • Object
show all
Includes:
Log, Thor::Actions, Thor::Shell
Defined in:
lib/cfnbackup/generate.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

colors, logger, logger=

Class Method Details

.source_rootObject



20
21
22
# File 'lib/cfnbackup/generate.rb', line 20

def self.source_root
  File.dirname(__FILE__)
end

Instance Method Details

#create_build_directoryObject

Creates the build dir based on the stack name



41
42
43
44
45
# File 'lib/cfnbackup/generate.rb', line 41

def create_build_directory
  @build_dir = "output/#{@stack_name}"
  Log.logger.debug("Creating output directory #{@build_dir}")
  FileUtils.mkdir_p(@build_dir)
end

#generate_cloudformationObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/cfnbackup/generate.rb', line 72

def generate_cloudformation
  Log.logger.debug("Populating CfHighlander file from template")
  # Inject the initalised config list into the text template which will use these to populate parameters
  template('templates/cfnbackup.cfhighlander.rb.tt', "#{@build_dir}/#{@stack_name}.cfhighlander.rb", @config, force: true)
  Log.logger.debug("Generating CloudFormation template from #{@build_dir}/#{@stack_name}.cfhighlander.rb")
  # Initalise the CfHighlander object and run a render, this will compile and validate the component, outputting cloudformation
  cfhl = CfnBackup::CfHighlander.new(@options['region'], @stack_name, @config, @build_dir)
  template_path = cfhl.render()
  Log.logger.debug("CloudFormation template generated and validated")
end

#initialize_configObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cfnbackup/generate.rb', line 47

def initialize_config
  Log.logger.debug("Initialising config, loading global config file")
  # Load the global config file (should always be present in the hardcoded path)
  global_config_path = File.join(File.dirname(__FILE__), '../config/global_config.yml')
  global_config = YAML.load(File.read(global_config_path))
  # Check if a custom config file has been provided with the --config flag
  if @options['config']
    Log.logger.debug("Custom config file path provided, attempting to load")
    # Check if the file/path provided is a valid file and attempt to load it using the YAML object.
    if File.file?(@options['config'])
      custom_config = YAML.load(File.read(@options['config']))
      Log.logger.debug("Custom config file loaded, deep merging with global config")
      # Peform a deep merge on the loaded global config and the custom config
      @config = CfnBackup::Utils.deep_merge(global_config, custom_config)
    else
      abort("Could not find or load file #{@options['config']}")
    end
  else
    # If no custom config was provided no further action is needed
    Log.logger.debug("No custom config file provided, using all default values")
    @config = global_config
  end
  @config['stack_name'] = @stack_name
end

#set_loglevelObject



24
25
26
27
# File 'lib/cfnbackup/generate.rb', line 24

def set_loglevel
  Log.logger.level = Logger::DEBUG if @options['verbose']
  Log.logger.debug("Log level set to DEBUG")
end

#set_stack_nameObject

Sets the stack name to be used in template name & resource names. Defaults to cfnbackup if none provided



30
31
32
33
34
35
36
37
38
# File 'lib/cfnbackup/generate.rb', line 30

def set_stack_name
  if @options['stack_name']
    @stack_name = @options['stack_name']
    Log.logger.debug("Stack name provided, set to #{@stack_name}")
  else
    @stack_name = "cfnbackup"
    Log.logger.debug("Using default stack name #{@stack_name}")
  end
end