Class: CfnBackup::Publish

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

colors, logger, logger=

Class Method Details

.source_rootObject



22
23
24
# File 'lib/cfnbackup/publish.rb', line 22

def self.source_root
  File.dirname(__FILE__)
end

Instance Method Details

#create_build_directoryObject

Creates the build dir based on the stack name



54
55
56
57
58
# 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_configObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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_cloudformationObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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_loglevelObject



26
27
28
29
# 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_bucketObject

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



43
44
45
46
47
48
49
50
51
# 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_nameObject

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



32
33
34
35
36
37
38
39
40
# 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