Class: Jekyll::Commands::Paspagon

Inherits:
Command
  • Object
show all
Defined in:
lib/jekyll/commands/paspagon.rb

Class Method Summary collapse

Class Method Details

.ensure_logging_bucket(name, client) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jekyll/commands/paspagon.rb', line 69

def ensure_logging_bucket(name, client)
  bucket = Aws::S3::Bucket.new(name: name, client: client)
  bucket.create unless bucket.exists?
  lifecycle_configuration =
    {rules:
     [{expiration:
       {days: 90},
       prefix: '',
       status: 'Enabled'
      }]}
  client.put_bucket_lifecycle_configuration(bucket: name, lifecycle_configuration: lifecycle_configuration)
  client.put_bucket_acl(bucket: name, acl: 'log-delivery-write')
  bucket
end

.init_with_program(jekyll) ⇒ Object



10
11
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
43
44
# File 'lib/jekyll/commands/paspagon.rb', line 10

def init_with_program(jekyll)
  client = Aws::S3::Client.new(region: 'us-west-2')
  jekyll.command(:paspagon_prepare) do |c|
    c.syntax 'paspagon_prepare'
    c.description 'Create and configure S3 buckets for Paspagon'

    c.action do |_, options|
      site_options = configuration_from_options(options)
      site = Jekyll::Site.new(site_options)
      config = PaspagonConfig.new(site)

      logging_bucket_name = config.logging_bucket_name
      logging_bucket = ensure_logging_bucket(logging_bucket_name, client) if logging_bucket_name
      config.buckets.keys.each do |bucket_name|
        prepare_bucket(bucket_name, client, logging_bucket)
      end
    end
  end

  jekyll.command(:paspagon_sync) do |c|
    c.syntax 'paspagon_sync'
    c.description 'Sync paid files with S3'

    c.action do |_, options|
      site_options = configuration_from_options(options)
      site = Jekyll::Site.new(site_options)
      config = PaspagonConfig.new(site)

      paid_dir = config.paid_dest_dir
      Dir["#{paid_dir}/*/"].each do |dir|
        sync_path(dir, client)
      end
    end
  end
end

.prepare_bucket(name, client, logging_bucket) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jekyll/commands/paspagon.rb', line 46

def prepare_bucket(name, client, logging_bucket)
  bucket = Aws::S3::Bucket.new(name, client: client)
  bucket.create unless bucket.exists?
  policy = "{\n  \"Version\": \"2012-10-17\",\n  \"Statement\": {\n    \"Resource\": \"arn:aws:s3:::\#{name}/*\",\n    \"Sid\": \"PaspagonAllow\",\n    \"Effect\": \"Allow\",\n    \"Principal\": {\"AWS\": \"154072225287\"},\n    \"Action\": [\"s3:GetObject\"]\n  }\n}\n JSON\n  client.put_bucket_policy(bucket: name, policy: policy)\n  logging_status =\n    {logging_enabled:\n     {target_bucket: logging_bucket.name,\n      target_prefix: \"\#{name}/\"}}\n  client.put_bucket_logging(bucket: name, bucket_logging_status: logging_status)\nend\n"

.sync_path(path, client) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/jekyll/commands/paspagon.rb', line 84

def sync_path(path, client)
  Dir.chdir(path) do
    local_files =
      Dir.glob('**/*').select { |p| File.file?(p) }.map { |p| [p, [File.mtime(p), File.ctime(p)].max] }.to_h

    bucket_name = File.basename(path)
    bucket = Aws::S3::Bucket.new(bucket_name, client: client)
    cloud_files = bucket.objects.map { |o| [o.key, o.last_modified] }.to_h

    to_delete = (cloud_files.keys - local_files.keys)
    unless to_delete.empty?
      puts('Deleting objects:')
      to_delete.each do |p|
        puts("\t#{p}")
      end
      bucket.delete_objects(delete: {objects: to_delete.map { |p| {key: p} }})
    end

    to_upload = local_files.keys.select { |p| !cloud_files[p] || local_files[p] > cloud_files[p] }
    to_upload.each do |p|
      puts("Uploading #{p} to bucket #{bucket_name}…")
      xattr = Xattr.new(p)
       = xattr.as_json.select { |k, _| k.start_with?('user.x-amz-meta-') }.map { |k, v| [k.sub(/^user.x-amz-meta-/, ''), v] }.to_h
      options =
        {body: File.open(p),
         metadata: ,
         key: p}
      options[:content_disposition] = xattr['user.content-disposition'] if xattr['user.content-disposition']
      options[:content_type] = xattr['user.content-type'] if xattr['user.content-type']
      bucket.put_object(options)
    end
  end
end