Class: Moonshot::Plugins::Backup

Inherits:
Object
  • Object
show all
Includes:
CredsHelper
Defined in:
lib/plugins/backup.rb

Overview

Moonshot plugin class for deflating and uploading files on given hooks

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CredsHelper

#as_client, #cd_client, #cf_client, #ec2_client, #iam_client, #s3_client

Constructor Details

#initialize {|_self| ... } ⇒ Backup

Returns a new instance of Backup.

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
# File 'lib/plugins/backup.rb', line 17

def initialize
  yield self if block_given?
  raise ArgumentError \
    if @files.nil? || @files.empty? || @hooks.nil? || !(@bucket.nil? ^ @buckets.nil?)

  @target_name ||= '%{app_name}_%{timestamp}_%{user}.tar.gz'
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Dynamically responding to hooks supplied in the constructor



72
73
74
# File 'lib/plugins/backup.rb', line 72

def method_missing(method_name, *args, &block)
  @hooks.include?(method_name) ? backup(*args) : super
end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



11
12
13
# File 'lib/plugins/backup.rb', line 11

def bucket
  @bucket
end

#bucketsObject

Returns the value of attribute buckets.



11
12
13
# File 'lib/plugins/backup.rb', line 11

def buckets
  @buckets
end

#filesObject

Returns the value of attribute files.



11
12
13
# File 'lib/plugins/backup.rb', line 11

def files
  @files
end

#hooksObject

Returns the value of attribute hooks.



11
12
13
# File 'lib/plugins/backup.rb', line 11

def hooks
  @hooks
end

#target_nameObject

Returns the value of attribute target_name.



11
12
13
# File 'lib/plugins/backup.rb', line 11

def target_name
  @target_name
end

Class Method Details

.to_bucket(bucket) ⇒ Backup

Factory method to create preconfigured Backup plugins. Uploads current template and parameter files.

Parameters:

  • backup (String)

    target bucket name

Returns:

  • (Backup)

    configured backup object

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/plugins/backup.rb', line 29

def self.to_bucket(bucket)
  raise ArgumentError if bucket.nil? || bucket.empty?
  Moonshot::Plugins::Backup.new do |b|
    b.bucket = bucket
    b.files = [
      'cloud_formation/%{app_name}.json',
      'cloud_formation/parameters/%{stack_name}.yml'
    ]
    b.hooks = [:post_create, :post_update]
  end
end

Instance Method Details

#backup(resources) ⇒ Object

Main worker method, creates a tarball of the given files, and uploads to an S3 bucket.

Parameters:

  • resources (Resources)

    injected Moonshot resources

Raises:

  • (ArgumentError)


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

def backup(resources) # rubocop:disable Metrics/AbcSize
  raise ArgumentError if resources.nil?

  @app_name = resources.stack.app_name
  @stack_name = resources.stack.name
  @target_name = render(@target_name)
  @target_bucket = define_bucket

  return if @target_bucket.nil?

  resources.ilog.start("#{log_message} in progress.") do |s|
    begin
      tar_out = tar(@files)
      zip_out = zip(tar_out)
      upload(zip_out)

      s.success("#{log_message} succeeded.")
    rescue StandardError => e
      s.failure("#{log_message} failed: #{e}")
    ensure
      tar_out.close unless tar_out.nil?
      zip_out.close unless zip_out.nil?
    end
  end
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/plugins/backup.rb', line 76

def respond_to?(method_name, include_private = false)
  @hooks.include?(method_name) || super
end