|
| 1 | +require 'open3' |
| 2 | +require 'tempfile' |
| 3 | +require 'tmpdir' |
| 4 | +require 'fileutils' |
| 5 | +require 'cloud_controller/blobstore/base_client' |
| 6 | +require 'cloud_controller/blobstore/storage_cli/storage_cli_blob' |
| 7 | + |
| 8 | +module CloudController |
| 9 | + module Blobstore |
| 10 | + class StorageCliClient < BaseClient |
| 11 | + attr_reader :root_dir, :min_size, :max_size |
| 12 | + |
| 13 | + @registry = {} |
| 14 | + |
| 15 | + class << self |
| 16 | + attr_reader :registry |
| 17 | + |
| 18 | + def register(provider, klass) |
| 19 | + registry[provider] = klass |
| 20 | + end |
| 21 | + |
| 22 | + def build(connection_config:, directory_key:, root_dir:, min_size: nil, max_size: nil) |
| 23 | + provider = connection_config[:provider] |
| 24 | + raise 'Missing connection_config[:provider]' if provider.nil? |
| 25 | + |
| 26 | + impl_class = registry[provider] |
| 27 | + raise "No storage CLI client registered for provider #{provider}" unless impl_class |
| 28 | + |
| 29 | + impl_class.new(connection_config:, directory_key:, root_dir:, min_size:, max_size:) |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + def initialize(connection_config:, directory_key:, root_dir:, min_size: nil, max_size: nil) |
| 34 | + @cli_path = cli_path |
| 35 | + @directory_key = directory_key |
| 36 | + @root_dir = root_dir |
| 37 | + @min_size = min_size || 0 |
| 38 | + @max_size = max_size |
| 39 | + config = build_config(connection_config) |
| 40 | + @config_file = write_config_file(config) |
| 41 | + @fork = connection_config.fetch(:fork, false) |
| 42 | + end |
| 43 | + |
| 44 | + def local? |
| 45 | + false |
| 46 | + end |
| 47 | + |
| 48 | + def exists?(blobstore_key) |
| 49 | + key = partitioned_key(blobstore_key) |
| 50 | + _, status = run_cli('exists', key, allow_exit_code_three: true) |
| 51 | + |
| 52 | + if status.exitstatus == 0 |
| 53 | + return true |
| 54 | + elsif status.exitstatus == 3 |
| 55 | + return false |
| 56 | + end |
| 57 | + |
| 58 | + false |
| 59 | + end |
| 60 | + |
| 61 | + def download_from_blobstore(source_key, destination_path, mode: nil) |
| 62 | + FileUtils.mkdir_p(File.dirname(destination_path)) |
| 63 | + run_cli('get', partitioned_key(source_key), destination_path) |
| 64 | + |
| 65 | + File.chmod(mode, destination_path) if mode |
| 66 | + end |
| 67 | + |
| 68 | + def cp_to_blobstore(source_path, destination_key) |
| 69 | + start = Time.now.utc |
| 70 | + log_entry = 'cp-skip' |
| 71 | + size = -1 |
| 72 | + |
| 73 | + logger.info('cp-start', destination_key: destination_key, source_path: source_path, bucket: @directory_key) |
| 74 | + |
| 75 | + File.open(source_path) do |file| |
| 76 | + size = file.size |
| 77 | + next unless within_limits?(size) |
| 78 | + |
| 79 | + run_cli('put', source_path, partitioned_key(destination_key)) |
| 80 | + log_entry = 'cp-finish' |
| 81 | + end |
| 82 | + |
| 83 | + duration = Time.now.utc - start |
| 84 | + logger.info(log_entry, |
| 85 | + destination_key: destination_key, |
| 86 | + duration_seconds: duration, |
| 87 | + size: size) |
| 88 | + end |
| 89 | + |
| 90 | + def cp_file_between_keys(source_key, destination_key) |
| 91 | + if @fork |
| 92 | + run_cli('copy', partitioned_key(source_key), partitioned_key(destination_key)) |
| 93 | + else |
| 94 | + # Azure CLI doesn't support server-side copy yet, so fallback to local copy |
| 95 | + Tempfile.create('blob-copy') do |tmp| |
| 96 | + download_from_blobstore(source_key, tmp.path) |
| 97 | + cp_to_blobstore(tmp.path, destination_key) |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + def delete_all(_=nil) |
| 103 | + # page_size is currently not considered. Azure SDK / API has a limit of 5000 |
| 104 | + pass unless @fork |
| 105 | + |
| 106 | + # Currently, storage-cli does not support bulk deletion. |
| 107 | + run_cli('delete-recursive', @root_dir) |
| 108 | + end |
| 109 | + |
| 110 | + def delete_all_in_path(path) |
| 111 | + pass unless @fork |
| 112 | + |
| 113 | + # Currently, storage-cli does not support bulk deletion. |
| 114 | + run_cli('delete-recursive', partitioned_key(path)) |
| 115 | + end |
| 116 | + |
| 117 | + def delete(key) |
| 118 | + run_cli('delete', partitioned_key(key)) |
| 119 | + end |
| 120 | + |
| 121 | + def delete_blob(blob) |
| 122 | + delete(blob.key) |
| 123 | + end |
| 124 | + |
| 125 | + def blob(key) |
| 126 | + if @fork |
| 127 | + properties = properties(key) |
| 128 | + return nil if properties.nil? || properties.empty? |
| 129 | + |
| 130 | + signed_url = sign_url(partitioned_key(key), verb: 'get', expires_in_seconds: 3600) |
| 131 | + StorageCliBlob.new(key, properties:, signed_url:) |
| 132 | + elsif exists?(key) |
| 133 | + # Azure CLI does not support getting blob properties directly, so fallback to local check |
| 134 | + signed_url = sign_url(partitioned_key(key), verb: 'get', expires_in_seconds: 3600) |
| 135 | + StorageCliBlob.new(key, signed_url:) |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + def files_for(prefix, _ignored_directory_prefixes=[]) |
| 140 | + return nil unless @fork |
| 141 | + |
| 142 | + files, _status = run_cli('list', prefix) |
| 143 | + files.split("\n").map(&:strip).reject(&:empty?).map { |file| StorageCliBlob.new(file) } |
| 144 | + end |
| 145 | + |
| 146 | + def ensure_bucket_exists |
| 147 | + return unless @fork |
| 148 | + |
| 149 | + run_cli('ensure-bucket-exists') |
| 150 | + end |
| 151 | + |
| 152 | + private |
| 153 | + |
| 154 | + def run_cli(command, *args, allow_exit_code_three: false) |
| 155 | + logger.info("[storage_cli_client] Running storage-cli: #{@cli_path} -c #{@config_file} #{command} #{args.join(' ')}") |
| 156 | + |
| 157 | + begin |
| 158 | + stdout, stderr, status = Open3.capture3(@cli_path, '-c', @config_file, command, *args) |
| 159 | + rescue StandardError => e |
| 160 | + raise BlobstoreError.new(e.inspect) |
| 161 | + end |
| 162 | + |
| 163 | + unless status.success? || (allow_exit_code_three && status.exitstatus == 3) |
| 164 | + raise "storage-cli #{command} failed with exit code #{status.exitstatus}, output: '#{stdout}', error: '#{stderr}'" |
| 165 | + end |
| 166 | + |
| 167 | + [stdout, status] |
| 168 | + end |
| 169 | + |
| 170 | + def sign_url(key, verb:, expires_in_seconds:) |
| 171 | + stdout, _status = run_cli('sign', key, verb.to_s.downcase, "#{expires_in_seconds}s") |
| 172 | + stdout.strip |
| 173 | + end |
| 174 | + |
| 175 | + def properties(key) |
| 176 | + stdout, _status = run_cli('properties', partitioned_key(key)) |
| 177 | + # stdout is expected to be in JSON format - raise an error if it is nil, empty or something unexpected |
| 178 | + raise BlobstoreError.new("Properties command returned empty output for key: #{key}") if stdout.nil? || stdout.empty? |
| 179 | + |
| 180 | + begin |
| 181 | + properties = Oj.load(stdout) |
| 182 | + rescue StandardError => e |
| 183 | + raise BlobstoreError.new("Failed to parse json properties for key: #{key}, error: #{e.message}") |
| 184 | + end |
| 185 | + |
| 186 | + properties |
| 187 | + end |
| 188 | + |
| 189 | + def cli_path |
| 190 | + raise NotImplementedError |
| 191 | + end |
| 192 | + |
| 193 | + def build_config(connection_config) |
| 194 | + raise NotImplementedError |
| 195 | + end |
| 196 | + |
| 197 | + def write_config_file(config) |
| 198 | + # TODO: Consider to move the config generation into capi-release |
| 199 | + config_dir = File.join(tmpdir, 'blobstore-configs') |
| 200 | + FileUtils.mkdir_p(config_dir) |
| 201 | + |
| 202 | + config_file_path = File.join(config_dir, "#{@directory_key}.json") |
| 203 | + File.open(config_file_path, 'w', 0o600) do |f| |
| 204 | + f.write(Oj.dump(config.transform_keys(&:to_s))) |
| 205 | + end |
| 206 | + config_file_path |
| 207 | + end |
| 208 | + |
| 209 | + def tmpdir |
| 210 | + VCAP::CloudController::Config.config.get(:directories, :tmpdir) |
| 211 | + rescue StandardError |
| 212 | + # Fallback to a temporary directory if the config is not set (e.g. for cc-deployment-updater |
| 213 | + Dir.mktmpdir('cc_blobstore') |
| 214 | + end |
| 215 | + |
| 216 | + def logger |
| 217 | + @logger ||= Steno.logger('cc.blobstore.storage_cli_client') |
| 218 | + end |
| 219 | + end |
| 220 | + end |
| 221 | +end |
0 commit comments