Class: AwsS3WebsiteSync::Plan

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_s3_website_sync/plan.rb

Class Method Summary collapse

Class Method Details

.create_changeset(output_changeset_path, diff_delete_keys, diff_create_or_update, ignore_files) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/aws_s3_website_sync/plan.rb', line 27

def self.create_changeset output_changeset_path, diff_delete_keys, diff_create_or_update, ignore_files
  diff_delete = diff_delete_keys.map{|t| {path: t, action: 'delete'} }
  data = diff_delete + diff_create_or_update

  data.each do |t|
    if ignore_files.include?(t[:path])
      t[:action] = 'ignore'
    end
  end

  formatted_data = JSON.pretty_generate data

  FileUtils.mkdir_p File.dirname(output_changeset_path)
  File.write output_changeset_path, formatted_data
end

.create_or_update(paths, keys) ⇒ Object

Get the difference of files changed



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/aws_s3_website_sync/plan.rb', line 51

def self.create_or_update paths, keys
  $logger.info "Plan.create_update"
  results = []
  paths.each do |p|
    if k = keys.find{|t|t[:path] == p[:path] }
      # existing entry
      if k[:md5] == p[:md5]
        results.push p.merge(action: 'no_change')
      else
        results.push p.merge(action: 'update')
      end
    else
      results.push p.merge(action: 'create')
    end
  end
  results
end

.delete(paths, keys) ⇒ Object

Get the differece between the local build directory and S3 bucket return: [Array] a list of keys that should be deleted



45
46
47
48
# File 'lib/aws_s3_website_sync/plan.rb', line 45

def self.delete paths, keys
  $logger.info "Plan.delete"
  keys.map{|t|t[:path]} - paths.map{|t|t[:path]}
end

.run(output_changeset_path:, build_dir:, aws_access_key_id:, aws_secret_access_key:, s3_bucket:, aws_default_region:, ignore_files:) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/aws_s3_website_sync/plan.rb', line 3

def self.run(
    output_changeset_path:,
    build_dir:,
    aws_access_key_id:,
    aws_secret_access_key:,
    s3_bucket:,
    aws_default_region:,
    ignore_files:
  )
  paths = AwsS3WebsiteSync::List.local build_dir
  keys  = AwsS3WebsiteSync::List.remote aws_access_key_id, aws_secret_access_key, s3_bucket, aws_default_region

  # Files we should delete
  diff_delete = AwsS3WebsiteSync::Plan.delete paths, keys

  # Ignore files we plan to delete for create_or_update
  create_or_update_keys = keys.reject{|t| diff_delete.any?(t[:path]) }

  # Files we should create or update
  diff_create_or_update = AwsS3WebsiteSync::Plan.create_or_update paths, create_or_update_keys

  AwsS3WebsiteSync::Plan.create_changeset output_changeset_path, diff_delete, diff_create_or_update, ignore_files
end