Class: Nanoc::Extra::Deployers::Fog
- Inherits:
-
Nanoc::Extra::Deployer
- Object
- Nanoc::Extra::Deployer
- Nanoc::Extra::Deployers::Fog
- Defined in:
- lib/nanoc/extra/deployers/fog.rb
Overview
A deployer that deploys a site using fog.
Instance Attribute Summary
Attributes inherited from Nanoc::Extra::Deployer
#config, #dry_run, #source_path
Instance Method Summary (collapse)
Methods inherited from Nanoc::Extra::Deployer
Methods included from PluginRegistry::PluginMethods
#all, #identifier, #identifiers, #named, #register
Constructor Details
This class inherits a constructor from Nanoc::Extra::Deployer
Instance Method Details
- (Object) run
25 26 27 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/nanoc/extra/deployers/fog.rb', line 25 def run require 'fog' # Get params, unsetting anything we don't want to pass through to fog. src = File.(source_path) bucket = config.delete(:bucket) || config.delete(:bucket_name) path = config.delete(:path) cdn_id = config.delete(:cdn_id) config.delete(:kind) # Validate params error 'The path requires no trailing slash' if path && path[-1, 1] == '/' # Mock if necessary if self.dry_run? puts 'Dry run - simulation' ::Fog.mock! end # Get connection puts 'Connecting' connection = ::Fog::Storage.new(config) # Get bucket puts 'Getting bucket' begin directory = connection.directories.get(bucket, prefix: path) rescue ::Excon::Errors::NotFound should_create_bucket = true end should_create_bucket = true if directory.nil? # Create bucket if necessary if should_create_bucket puts 'Creating bucket' directory = connection.directories.create(key: bucket, prefix: path) end # Get list of remote files files = directory.files truncated = files.respond_to?(:is_truncated) && files.is_truncated while truncated set = directory.files.all(marker: files.last.key) truncated = set.is_truncated files += set end keys_to_destroy = files.all.map(&:key) keys_to_invalidate = [] # Upload all the files in the output folder to the clouds puts 'Uploading local files' FileUtils.cd(src) do files = Dir['**/*'].select { |f| File.file?(f) } files.each do |file_path| key = path ? File.join(path, file_path) : file_path directory.files.create( key: key, body: File.open(file_path), public: true) keys_to_destroy.delete(key) keys_to_invalidate.push(key) end end # delete extraneous remote files puts 'Removing remote files' keys_to_destroy.each do |key| directory.files.get(key).destroy end # invalidate CDN objects if cdn_id puts 'Invalidating CDN distribution' keys_to_invalidate.concat(keys_to_destroy) cdn = ::Fog::CDN.new(config) # fog cannot mock CDN requests unless self.dry_run? distribution = cdn.get_distribution(cdn_id) # usual limit per invalidation: 1000 objects keys_to_invalidate.each_slice(1000) do |paths| cdn.post_invalidation(distribution, paths) end end end puts 'Done!' end |