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:



22
23
24
25
26
# File 'lib/plugins/backup.rb', line 22

def initialize
  yield self if block_given?
  validate_configuration
  @target_name ||= '%<app_name>s_%<timestamp>s_%<user>s.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



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

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

Instance Attribute Details

#backup_parametersObject

Returns the value of attribute backup_parameters.



13
14
15
# File 'lib/plugins/backup.rb', line 13

def backup_parameters
  @backup_parameters
end

#backup_templateObject

Returns the value of attribute backup_template.



13
14
15
# File 'lib/plugins/backup.rb', line 13

def backup_template
  @backup_template
end

#bucketObject

Returns the value of attribute bucket.



13
14
15
# File 'lib/plugins/backup.rb', line 13

def bucket
  @bucket
end

#bucket_regionObject

Returns the value of attribute bucket_region.



13
14
15
# File 'lib/plugins/backup.rb', line 13

def bucket_region
  @bucket_region
end

#bucketsObject

Returns the value of attribute buckets.



13
14
15
# File 'lib/plugins/backup.rb', line 13

def buckets
  @buckets
end

#filesObject

Returns the value of attribute files.



13
14
15
# File 'lib/plugins/backup.rb', line 13

def files
  @files
end

#hooksObject

Returns the value of attribute hooks.



13
14
15
# File 'lib/plugins/backup.rb', line 13

def hooks
  @hooks
end

#target_nameObject

Returns the value of attribute target_name.



13
14
15
# File 'lib/plugins/backup.rb', line 13

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)


32
33
34
35
36
37
38
39
40
41
# File 'lib/plugins/backup.rb', line 32

def self.to_bucket(bucket)
  raise ArgumentError if bucket.nil? || bucket.empty?

  Moonshot::Plugins::Backup.new do |b|
    b.bucket = bucket
    b.backup_parameters = true
    b.backup_template = true
    b.hooks = %i[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)


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/plugins/backup.rb', line 47

def backup(resources)
  raise ArgumentError if resources.nil?

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

  return if @target_bucket.nil?

  resources.ilog.start("#{log_message} in progress.") do |s|
    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
    zip_out&.close
  end
end

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

Returns:

  • (Boolean)


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

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