class CfnBackup::Publish

Public Class Methods

source_root() click to toggle source
# File lib/cfnbackup/publish.rb, line 22
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/publish.rb, line 54
def create_build_directory
  @build_dir = "output/#{@stack_name}"
  Log.logger.debug("Creating output directory #{@build_dir}")
  FileUtils.mkdir_p(@build_dir)
end
initialize_config() click to toggle source
# File lib/cfnbackup/publish.rb, line 60
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
  # Load the stack name and source bucket taken from ARGS/Defaults and insert into the final config
  @config['stack_name'] = @stack_name
  @config['source_bucket'] = @source_bucket
end
publish_cloudformation() click to toggle source
# File lib/cfnbackup/publish.rb, line 87
def publish_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, nil, @build_dir)
  compiler = cfhl.render()
  Log.logger.debug("CloudFormation template generated and validated")
  # Publishes the compiled cloudformation to S3 using the source bucket provided, outputting the master stack S3 path
  @template_url = cfhl.publish(compiler)
  say("\n--------- Master Template URL ---------")
  say("#{@template_url}")
  say("---------------------------------------")
end
set_loglevel() click to toggle source
# File lib/cfnbackup/publish.rb, line 26
def set_loglevel
  Log.logger.level = Logger::DEBUG if @options['verbose']
  Log.logger.debug("Log level set to DEBUG")
end
set_source_bucket() click to toggle source

Enforces source bucket to be provided and sets it if it is

# File lib/cfnbackup/publish.rb, line 43
def set_source_bucket
  if !@options['source_bucket']
    Log.logger.debug("No source bucket provided")
    abort("Set source S3 bucket with --source-bucket flag")
  else
    Log.logger.debug("Setting source bucket to #{@options['source_bucket']}")
    @source_bucket = @options['source_bucket']
  end
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/publish.rb, line 32
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