class CfnBackup::Generate

Public Class Methods

source_root() click to toggle source
# File lib/cfnbackup/generate.rb, line 20
def self.source_root
  File.dirname(__FILE__)
end

Public Instance Methods

create_build_directory() click to toggle source

Creates the build dir based on the stack name

# 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_cloudformation() click to toggle source
# 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_config() click to toggle source
# 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_loglevel() click to toggle source
# 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_name() click to toggle source

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

# 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