Class: Naifa::Plugins::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/naifa/plugins/s3.rb,
lib/naifa/plugins/s3/cli.rb,
lib/naifa/plugins/s3/settings.rb

Defined Under Namespace

Classes: CLI

Constant Summary collapse

DEFAULT_SETTINGS =
{
  environments: {
    production: {
      bucket: 's3://production_bucket_name/'
    },
    staging: {
      bucket: 's3://staging_bucket_name/'
    },
    development: {
      bucket: 's3://development_bucket_name/'
    }
  },
  sync: {
    origin: :staging,
    destination: :development,
    sync_options: ['--delete', '--acl public-read']
  }
}.with_indifferent_access.freeze

Class Method Summary collapse

Class Method Details

.build_sync_command(from_bucket, to_bucket, sync_options = []) ⇒ Object



44
45
46
47
48
# File 'lib/naifa/plugins/s3.rb', line 44

def self.build_sync_command(from_bucket, to_bucket, sync_options=[])
  command = "aws s3 sync #{from_bucket} #{to_bucket}"
  command << " #{sync_options.join(' ')}" if sync_options.present?
  command
end

.sync(options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/naifa/plugins/s3.rb', line 12

def self.sync(options={})
  options ||= {}

  sync_settings = options[:sync]
  environments_settings = options[:environments]

  if sync_settings.blank? ||
    sync_settings[:origin].blank? ||
    sync_settings[:destination].blank?

    raise Thor::Error, "Sync settings are not defined"
  end

  origin = sync_settings[:origin]
  destination = sync_settings[:destination]

  if environments_settings.blank? ||
    environments_settings[origin].blank? || environments_settings[origin][:bucket].blank? ||
    environments_settings[destination].blank? || environments_settings[destination][:bucket].blank?

    raise Thor::Error, "Sync environments not set"
  end

  command = build_sync_command(
              environments_settings[origin][:bucket],
              environments_settings[destination][:bucket],
              sync_settings.fetch(:sync_options, [])
            )

  Kernel.system(command)
end