|
| 1 | +require 'cloud_controller/blobstore/base_client' |
| 2 | +require 'cloud_controller/blobstore/errors' |
| 3 | +require 'cloud_controller/blobstore/local/local_blob' |
| 4 | +require 'fileutils' |
| 5 | +require 'digest' |
| 6 | + |
| 7 | +module CloudController |
| 8 | + module Blobstore |
| 9 | + class LocalClient < BaseClient |
| 10 | + attr_reader :root_dir |
| 11 | + |
| 12 | + def initialize( |
| 13 | + directory_key:, |
| 14 | + base_path:, |
| 15 | + root_dir: nil, |
| 16 | + min_size: nil, |
| 17 | + max_size: nil, |
| 18 | + use_temp_storage: false |
| 19 | + ) |
| 20 | + @directory_key = directory_key |
| 21 | + @use_temp_storage = use_temp_storage |
| 22 | + @root_dir = root_dir |
| 23 | + @min_size = min_size || 0 |
| 24 | + @max_size = max_size |
| 25 | + |
| 26 | + setup_storage_path(base_path) |
| 27 | + end |
| 28 | + |
| 29 | + def local? |
| 30 | + true |
| 31 | + end |
| 32 | + |
| 33 | + def exists?(key) |
| 34 | + File.exist?(file_path(key)) |
| 35 | + end |
| 36 | + |
| 37 | + def download_from_blobstore(source_key, destination_path, mode: nil) |
| 38 | + FileUtils.mkdir_p(File.dirname(destination_path)) |
| 39 | + FileUtils.cp(file_path(source_key), destination_path) |
| 40 | + File.chmod(mode, destination_path) if mode |
| 41 | + rescue Errno::ENOENT |
| 42 | + raise FileNotFound.new("Could not find object '#{source_key}'") |
| 43 | + end |
| 44 | + |
| 45 | + def cp_to_blobstore(source_path, destination_key) |
| 46 | + start = Time.now.utc |
| 47 | + log_entry = 'blobstore.cp-skip' |
| 48 | + |
| 49 | + logger.info('blobstore.cp-start', destination_key: destination_key, source_path: source_path, bucket: @directory_key) |
| 50 | + |
| 51 | + size = File.size(source_path) |
| 52 | + if within_limits?(size) |
| 53 | + destination = file_path(destination_key) |
| 54 | + FileUtils.mkdir_p(File.dirname(destination)) |
| 55 | + FileUtils.cp(source_path, destination) |
| 56 | + log_entry = 'blobstore.cp-finish' |
| 57 | + end |
| 58 | + |
| 59 | + duration = Time.now.utc - start |
| 60 | + logger.info(log_entry, destination_key: destination_key, duration_seconds: duration, size: size) |
| 61 | + rescue Errno::ENOENT => e |
| 62 | + raise FileNotFound.new("Could not find source file '#{source_path}': #{e.message}") |
| 63 | + end |
| 64 | + |
| 65 | + def cp_file_between_keys(source_key, destination_key) |
| 66 | + source = file_path(source_key) |
| 67 | + destination = file_path(destination_key) |
| 68 | + |
| 69 | + raise FileNotFound.new("Could not find object '#{source_key}'") unless File.exist?(source) |
| 70 | + |
| 71 | + FileUtils.mkdir_p(File.dirname(destination)) |
| 72 | + FileUtils.cp(source, destination) |
| 73 | + end |
| 74 | + |
| 75 | + def delete(key) |
| 76 | + path = file_path(key) |
| 77 | + FileUtils.rm_f(path) |
| 78 | + cleanup_empty_parent_directories(path) |
| 79 | + end |
| 80 | + |
| 81 | + def blob(key) |
| 82 | + path = file_path(key) |
| 83 | + return unless File.exist?(path) |
| 84 | + |
| 85 | + LocalBlob.new(key: partitioned_key(key), file_path: path) |
| 86 | + end |
| 87 | + |
| 88 | + def delete_blob(blob) |
| 89 | + path = File.join(@base_path, blob.key) |
| 90 | + FileUtils.rm_f(path) |
| 91 | + cleanup_empty_parent_directories(path) |
| 92 | + end |
| 93 | + |
| 94 | + def delete_all(_=nil) |
| 95 | + FileUtils.rm_rf(@base_path) |
| 96 | + FileUtils.mkdir_p(@base_path) |
| 97 | + end |
| 98 | + |
| 99 | + def delete_all_in_path(path) |
| 100 | + dir = File.join(@base_path, path) |
| 101 | + FileUtils.rm_rf(dir) if File.directory?(dir) |
| 102 | + end |
| 103 | + |
| 104 | + def files_for(prefix, _ignored_directory_prefixes=[]) |
| 105 | + pattern = File.join(@base_path, prefix, '**', '*') |
| 106 | + Enumerator.new do |yielder| |
| 107 | + Dir.glob(pattern).each do |file_path| |
| 108 | + next unless File.file?(file_path) |
| 109 | + |
| 110 | + relative_path = file_path.sub("#{@base_path}/", '') |
| 111 | + yielder << LocalBlob.new(key: relative_path, file_path: file_path) |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + def ensure_bucket_exists |
| 117 | + FileUtils.mkdir_p(@base_path) |
| 118 | + end |
| 119 | + |
| 120 | + private |
| 121 | + |
| 122 | + def setup_storage_path(base_path) |
| 123 | + if use_temp_storage? |
| 124 | + @base_path = Dir.mktmpdir(['cc-blobstore-', "-#{@directory_key}"]) |
| 125 | + logger.info('storage-mode', mode: 'temp', directory_key: @directory_key, path: @base_path) |
| 126 | + register_cleanup_hook |
| 127 | + else |
| 128 | + raise ArgumentError.new('local_blobstore_path is required for persistent storage') if base_path.nil? |
| 129 | + |
| 130 | + @base_path = File.join(base_path, @directory_key) |
| 131 | + FileUtils.mkdir_p(@base_path) |
| 132 | + logger.info('storage-mode', mode: 'persistent', directory_key: @directory_key, path: @base_path) |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + def file_path(key) |
| 137 | + File.join(@base_path, partitioned_key(key)) |
| 138 | + end |
| 139 | + |
| 140 | + def use_temp_storage? |
| 141 | + @use_temp_storage |
| 142 | + end |
| 143 | + |
| 144 | + def register_cleanup_hook |
| 145 | + # Register cleanup handler for temp storage mode |
| 146 | + at_exit do |
| 147 | + cleanup_temp_storage |
| 148 | + end |
| 149 | + end |
| 150 | + |
| 151 | + def cleanup_temp_storage |
| 152 | + return unless use_temp_storage? && @base_path && File.directory?(@base_path) |
| 153 | + |
| 154 | + logger.info('temp-storage-cleanup', directory_key: @directory_key, path: @base_path) |
| 155 | + FileUtils.rm_rf(@base_path) |
| 156 | + rescue StandardError => e |
| 157 | + logger.error('temp-storage-cleanup-failed', error: e.message, path: @base_path) |
| 158 | + end |
| 159 | + |
| 160 | + def logger |
| 161 | + @logger ||= Steno.logger('cc.blobstore.local_client') |
| 162 | + end |
| 163 | + |
| 164 | + def cleanup_empty_parent_directories(path) |
| 165 | + dir = File.dirname(path) |
| 166 | + # Walk up the directory tree, removing empty directories until we hit the base path |
| 167 | + while dir != @base_path && dir.start_with?(@base_path) |
| 168 | + break unless File.directory?(dir) |
| 169 | + break unless Dir.empty?(dir) |
| 170 | + |
| 171 | + FileUtils.rmdir(dir) |
| 172 | + dir = File.dirname(dir) |
| 173 | + end |
| 174 | + end |
| 175 | + end |
| 176 | + end |
| 177 | +end |
0 commit comments