Class: Moonshot::Plugins::Backup
- Inherits:
-
Object
- Object
- Moonshot::Plugins::Backup
- Includes:
- CredsHelper
- Defined in:
- lib/plugins/backup.rb
Overview
Moonshot plugin class for deflating and uploading files on given hooks
Instance Attribute Summary collapse
-
#backup_parameters ⇒ Object
Returns the value of attribute backup_parameters.
-
#backup_template ⇒ Object
Returns the value of attribute backup_template.
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#bucket_region ⇒ Object
Returns the value of attribute bucket_region.
-
#buckets ⇒ Object
Returns the value of attribute buckets.
-
#files ⇒ Object
Returns the value of attribute files.
-
#hooks ⇒ Object
Returns the value of attribute hooks.
-
#target_name ⇒ Object
Returns the value of attribute target_name.
Class Method Summary collapse
-
.to_bucket(bucket) ⇒ Backup
Factory method to create preconfigured Backup plugins.
Instance Method Summary collapse
-
#backup(resources) ⇒ Object
Main worker method, creates a tarball of the given files, and uploads to an S3 bucket.
-
#initialize {|_self| ... } ⇒ Backup
constructor
A new instance of Backup.
-
#method_missing(method_name, *args, &block) ⇒ Object
Dynamically responding to hooks supplied in the constructor.
- #respond_to?(method_name, include_private = false) ⇒ Boolean
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.
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_parameters ⇒ Object
Returns the value of attribute backup_parameters.
13 14 15 |
# File 'lib/plugins/backup.rb', line 13 def backup_parameters @backup_parameters end |
#backup_template ⇒ Object
Returns the value of attribute backup_template.
13 14 15 |
# File 'lib/plugins/backup.rb', line 13 def backup_template @backup_template end |
#bucket ⇒ Object
Returns the value of attribute bucket.
13 14 15 |
# File 'lib/plugins/backup.rb', line 13 def bucket @bucket end |
#bucket_region ⇒ Object
Returns the value of attribute bucket_region.
13 14 15 |
# File 'lib/plugins/backup.rb', line 13 def bucket_region @bucket_region end |
#buckets ⇒ Object
Returns the value of attribute buckets.
13 14 15 |
# File 'lib/plugins/backup.rb', line 13 def buckets @buckets end |
#files ⇒ Object
Returns the value of attribute files.
13 14 15 |
# File 'lib/plugins/backup.rb', line 13 def files @files end |
#hooks ⇒ Object
Returns the value of attribute hooks.
13 14 15 |
# File 'lib/plugins/backup.rb', line 13 def hooks @hooks end |
#target_name ⇒ Object
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.
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.
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("#{} in progress.") do |s| tar_out = tar(@files) zip_out = zip(tar_out) upload(zip_out) s.success("#{} succeeded.") rescue StandardError => e s.failure("#{} failed: #{e}") ensure tar_out&.close zip_out&.close end end |
#respond_to?(method_name, include_private = false) ⇒ 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 |