28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/middleman-cloudfront/commands.rb', line 28
def invalidate(options = nil, files = nil)
if options.nil?
app_instance = ::Middleman::Application.server.inst
unless app_instance.respond_to?(:cloudfront_options)
raise Error, "ERROR: You need to activate the cloudfront extension in config.rb.\n\nThe example configuration is:\nactivate :cloudfront do |cf|\n cf.access_key_id = 'I'\n cf.secret_access_key = 'love'\n cf.distribution_id = 'cats'\n cf.filter = /\\.html/i # default /.*/\n cf.after_build = true # default is false\nend\n TEXT\n end\n options = app_instance.cloudfront_options\n end\n options.filter ||= /.*/\n [:distribution_id, :filter].each do |key|\n raise StandardError, \"Configuration key \#{key} is missing.\" if options.public_send(key).nil?\n end\n\n puts \"## Invalidating files on CloudFront\"\n\n fog_options = {\n :provider => 'AWS'\n }\n\n if options.access_key_id && options.secret_access_key\n fog_options.merge!({\n :aws_access_key_id => options.access_key_id,\n :aws_secret_access_key => options.secret_access_key\n })\n else\n fog_options.merge!({:use_iam_profile => true})\n end\n\n cdn = Fog::CDN.new(fog_options)\n\n distribution = cdn.distributions.get(options.distribution_id)\n\n # CloudFront limits the amount of files which can be invalidated by one request to 1000.\n # If there are more than 1000 files to invalidate, do so sequentially and wait until each validation is ready.\n # If there are max 1000 files, create the invalidation and return immediately.\n files = normalize_files(files || list_files(options.filter))\n return if files.empty?\n\n if files.count <= INVALIDATION_LIMIT\n puts \"Invalidating \#{files.count} files. It might take 10 to 15 minutes until all files are invalidated.\"\n puts 'Please check the AWS Management Console to see the status of the invalidation.'\n invalidation = distribution.invalidations.create(:paths => files)\n raise StandardError, %(Invalidation status is \#{invalidation.status}. Expected \"InProgress\") unless invalidation.status == 'InProgress'\n else\n slices = files.each_slice(INVALIDATION_LIMIT)\n puts \"Invalidating \#{files.count} files in \#{slices.count} batch(es). It might take 10 to 15 minutes per batch until all files are invalidated.\"\n slices.each_with_index do |slice, i|\n puts \"Invalidating batch \#{i + 1}...\"\n invalidation = distribution.invalidations.create(:paths => slice)\n invalidation.wait_for { ready? } unless i == slices.count - 1\n end\n end\nend\n"
|