Module: Aspera::Sync::Operations
- Defined in:
- lib/aspera/sync/operations.rb
Overview
builds command line arg for async and execute it
Constant Summary collapse
- DIRECTIONS =
Sync direction
%i[push pull bidi].freeze
- DEFAULT_DIRECTION =
Default direction for sync
DIRECTIONS.first
- SCP_REMOTE_REGEX =
/\A(?:(?:(?<user>[^@:\s]+)@)?(?<host>[^:\s]+):)?(?<path>.+)\z/- CONF_SCHEMA =
Transfer::CommandLineBuilder.read_schema(Schema::Registry::SYNC_CONF)
Class Method Summary collapse
-
.admin_status(sync_info) ⇒ Hash
Run
asyncadminto get status of sync session. -
.args_to_conf(args) ⇒ Object
Translate
asyncnative command line arguments toconfJSON. -
.direction_sym(sync_info) ⇒ Symbol
Get symbol of sync direction, defaulting to :push.
-
.find_option(schema, path, option) ⇒ Array?
Search given option in JSON Schema tree.
- .list_db_files(local_db_dir) ⇒ Object
-
.local_db_folder(sync_info) ⇒ String?
Find the local database folder based on sync_info.
-
.normalize_target_rate(params) ⇒ Object
Convert
target_rategiven as a string with optional suffix (e.g.100m) into integer bps. -
.parse_status(stdout) ⇒ Object
Parse output of asyncadmin.
-
.remote_certificates(remote) ⇒ Array<String>
Get certificates to use for remote connection.
-
.session_db_file(sync_info) ⇒ String
Path of the database file of the session.
-
.session_db_path(sync_info) ⇒ String
Path of the database file of the session, that may not exist.
- .session_name(sync_info) ⇒ Object
-
.start(sync_info, opt_ts = nil, agent_options: {}) {|direction, local_dir, remote_dir| ... } ⇒ Object
Start the sync process.
-
.tspec_to_sync_info(transfer_spec, sync_info, schema) ⇒ Object
Transfer specification to synchronization information tag
x-ts-namein schema is used to map transfer spec parameters to asyncsync_info. -
.update_remote_dir(sync_info, remote_dir_key, transfer_spec) ⇒ Object
Set
remote_dirin sync parameters based on transfer spec.
Class Method Details
.admin_status(sync_info) ⇒ Hash
Run asyncadmin to get status of sync session
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/aspera/sync/operations.rb', line 197 def admin_status(sync_info) Aspera.assert(PARAM_KEYS.any? { |k| sync_info.key?(k) }, 'At least one of `local` or `sessions` must be present in async parameters', type: ParameterError) arguments = [ASYNC_ADMIN_EXECUTABLE, '--quiet'] if sync_info.key?('local') # `conf` format arguments.push("--name=#{sync_info['name']}") if sync_info.key?('local_db_dir') arguments.push("--local-db-dir=#{sync_info['local_db_dir']}") elsif sync_info.dig('local', 'path') arguments.push("--local-dir=#{sync_info.dig('local', 'path')}") else raise ParameterError, 'Missing either local_db_dir or local.path' end else # `args` format session = sync_info['sessions'].first arguments.push("--name=#{session['name']}") if session.key?('local_db_dir') arguments.push("--local-db-dir=#{session['local_db_dir']}") elsif session.key?('local_dir') arguments.push("--local-dir=#{session['local_dir']}") else raise ParameterError, 'Missing either local_db_dir or local_dir' end end stdout = Environment.secure_execute(*arguments, mode: :capture).first return parse_status(stdout) end |
.args_to_conf(args) ⇒ Object
Translate async native command line arguments to conf JSON
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/aspera/sync/operations.rb', line 334 def args_to_conf(args) result = {} while args.any? option = args.shift if option =~ /^(--[^=]+)=(.*)$/ option = ::Regexp.last_match(1) # "--toto" args.unshift(::Regexp.last_match(2)) end if option.eql?('--preserve-time') || option.eql?('-t') args.unshift('--preserve-creation-time') if Environment.instance.os.eql?(Environment::OS_WINDOWS) option = '--preserve-modification-time' end if option.eql?('--remote') || option.eql?('-r') value = args.first if (m = SCP_REMOTE_REGEX.match(value)) if m[:host] args.shift args.unshift("--host=#{m[:host]}") args.unshift("--user=#{m[:user]}") if m[:user] args.unshift(m[:path]) end end end path, props = find_option(CONF_SCHEMA, [], option) Aspera.assert(!path.nil?) { "Option not found: #{option}" } last_key = path.pop # navigate in the current result to insert the value current = result path.each do |key| current[key] ||= {} current = current[key] end current[last_key] = if props['x-cli-switch'] # a special switch sets the non-default enum value, e.g. `--continuous` sets `mode` to `continuous` props.key?('enum') ? (props['enum'] - [props['default']]).first : true elsif props.key?('x-unit') Units::Duration.new(args.shift.to_s).to_unit(props['x-unit']) else args.shift end end return result end |
.direction_sym(sync_info) ⇒ Symbol
Get symbol of sync direction, defaulting to :push
83 84 85 |
# File 'lib/aspera/sync/operations.rb', line 83 def direction_sym(sync_info) (sync_info['direction'] || DEFAULT_DIRECTION).to_sym end |
.find_option(schema, path, option) ⇒ Array?
Search given option in JSON Schema tree
319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/aspera/sync/operations.rb', line 319 def find_option(schema, path, option) if %w[x-cli-option x-cli-short].any? { |i| schema[i].eql?(option) } Log.log.debug('Special') if schema['x-cli-special'] return [path, schema] end if schema['type'].eql?('object') schema['properties']&.each do |name, props| res = find_option(props, path + [name], option) return res unless res.nil? end end return end |
.list_db_files(local_db_dir) ⇒ Object
277 278 279 280 281 282 283 |
# File 'lib/aspera/sync/operations.rb', line 277 def list_db_files(local_db_dir) private = File.join(local_db_dir, PRIVATE_FOLDER) Dir.children(private).filter_map do |name| db_file = File.join(private, name, ASYNC_DB) [name, db_file] if File.exist?(db_file) end.to_h end |
.local_db_folder(sync_info) ⇒ String?
Find the local database folder based on sync_info
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/aspera/sync/operations.rb', line 229 def local_db_folder(sync_info) Aspera.assert(PARAM_KEYS.any? { |k| sync_info.key?(k) }, 'At least one of `local` or `sessions` must be present in async parameters', type: ParameterError) if sync_info.key?('local') # `conf` format if sync_info.key?('local_db_dir') return sync_info['local_db_dir'] elsif (local_path = sync_info.dig('local', 'path')) return local_path else raise ParameterError, 'Missing either local_db_dir or local.path' end else # `args` format session = sync_info['sessions'].first if session.key?('local_db_dir') return session['local_db_dir'] elsif session.key?('local_dir') return session['local_dir'] else raise ParameterError, 'Missing either local_db_dir or local_dir' end end end |
.normalize_target_rate(params) ⇒ Object
Convert target_rate given as a string with optional suffix (e.g. 100m) into integer bps
289 290 291 |
# File 'lib/aspera/sync/operations.rb', line 289 def normalize_target_rate(params) params['target_rate'] = Units::Decimal.rate_string_to_bps(params['target_rate']) if params.is_a?(Hash) && params['target_rate'].is_a?(String) end |
.parse_status(stdout) ⇒ Object
Parse output of asyncadmin
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/aspera/sync/operations.rb', line 177 def parse_status(stdout) Log.log.trace1 { "stdout=#{stdout}" } result = {} ids = nil stdout.split("\n").each do |line| info = line.split(':', 2).map(&:lstrip) if info[1].eql?('') info[1] = ids = [] elsif info[1].nil? ids.push(info[0]) next end result[info[0]] = info[1] end return result end |
.remote_certificates(remote) ⇒ Array<String>
Get certificates to use for remote connection
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/aspera/sync/operations.rb', line 56 def remote_certificates(remote) certificates_to_use = [] # use web socket secure for session ? if remote['connect_mode']&.eql?('ws') remote.delete('port') remote.delete('fingerprint') # ignore cert for wss ? # if @options[:check_ignore_cb]&.call(remote['host'], remote['ws_port']) # wss_cert_file = TempFileManager.instance.new_file_path_global('wss_cert') # wss_url = "https://#{remote['host']}:#{remote['ws_port']}" # File.write(wss_cert_file, Rest.remote_certificate_chain(wss_url)) # certificates_to_use.push(wss_cert_file) # end # set location for CA bundle to be the one of Ruby, see env var SSL_CERT_FILE / SSL_CERT_DIR # certificates_to_use.concat(@options[:trusted_certs]) if @options[:trusted_certs] else # remove unused parameter (avoid warning) remote.delete('ws_port') # add SSH bypass keys when authentication is token and no auth is provided certificates_to_use.concat(Ascp::Installation.instance.aspera_token_ssh_key_paths(:rsa)) if remote.key?('token') && !remote.key?('pass') end return certificates_to_use end |
.session_db_file(sync_info) ⇒ String
Returns Path of the database file of the session.
271 272 273 274 275 |
# File 'lib/aspera/sync/operations.rb', line 271 def session_db_file(sync_info) db_file = session_db_path(sync_info) Aspera.assert(File.exist?(db_file), type: ParameterError) { "Database file #{db_file} does not exist" } db_file end |
.session_db_path(sync_info) ⇒ String
Returns Path of the database file of the session, that may not exist.
266 |
# File 'lib/aspera/sync/operations.rb', line 266 def session_db_path(sync_info) = File.join(local_db_folder(sync_info), PRIVATE_FOLDER, session_name(sync_info), ASYNC_DB) |
.session_name(sync_info) ⇒ Object
253 254 255 256 257 258 259 260 261 262 |
# File 'lib/aspera/sync/operations.rb', line 253 def session_name(sync_info) Aspera.assert(PARAM_KEYS.any? { |k| sync_info.key?(k) }, 'At least one of `local` or `sessions` must be present in async parameters', type: ParameterError) if sync_info.key?('local') # `conf` format return sync_info['name'] else # `args` format return sync_info['sessions'].first['name'] end end |
.start(sync_info, opt_ts = nil, agent_options: {}) {|direction, local_dir, remote_dir| ... } ⇒ Object
Start the sync process
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/aspera/sync/operations.rb', line 96 def start(sync_info, opt_ts = nil, agent_options: {}) Log.dump(:sync_params_initial, sync_info) Aspera.assert_type(sync_info, Hash) Aspera.assert(PARAM_KEYS.any? { |k| sync_info.key?(k) }, 'At least one of `local` or `sessions` must be present in async parameters', type: ParameterError) exec_spec = ExecSpec.new(exec: :async) if sync_info.key?('local') # `conf` format Aspera.assert_type(sync_info['local'], Hash, type: ParameterError) { 'local' } remote = sync_info['remote'] Aspera.assert_type(remote, Hash, type: ParameterError) { 'remote' } Aspera.assert_type(remote['path'], String, type: ParameterError) { 'remote path' } normalize_target_rate(sync_info['transport']) # get transfer spec if possible, and feed back to new structure if block_given? transfer_spec = yield(direction_sym(sync_info), sync_info['local']['path'], remote['path']) Log.dump(:auth_ts, transfer_spec) transfer_spec.deep_merge!(opt_ts) unless opt_ts.nil? Transfer::Spec.resolve_target_rate(transfer_spec) tspec_to_sync_info(transfer_spec, sync_info, CONF_SCHEMA) update_remote_dir(remote, 'path', transfer_spec) end remote['connect_mode'] ||= transfer_spec['wss_enabled'] ? 'ws' : 'ssh' add_certificates = remote_certificates(remote) if !add_certificates.empty? remote['private_key_paths'] ||= [] remote['private_key_paths'].concat(add_certificates) end # Secrets go in environment variables, not in `--conf64`: the command line is visible to other users (`ps`), and logged CONF_REMOTE_SECRETS_ENV.each do |key, env_var| value = remote.delete(key) exec_spec.env[env_var] = value unless value.nil? end # '--exclusive-mgmt-port=12345', '--arg-err-path=-', conf64 = Base64.strict_encode64(JSON.generate(sync_info)) # A secret that `async` reads only from the configuration (e.g. the proxy password) is in the encoded value: hide it in logs. # The configuration is logged decoded below, with its secrets hidden. Secret::Hider.instance.add_secret_values(conf64) exec_spec.args = ["--conf64=#{conf64}"] Log.dump(:sync_conf, sync_info) agent = Agent::Factory.instance.create(:direct, ) Aspera.assert(agent.respond_to?(:start_and_monitor_process)) { "agent direct (#{agent.class}) cannot run async: it does not provide start_and_monitor_process" } agent.start_and_monitor_process(session: {}, exec_spec: exec_spec) else # `args` format raise StandardError, "Only 'sessions', and optionally 'instance' keys are allowed" unless sync_info.keys.push('instance').uniq.sort.eql?(CMDLINE_PARAMS_KEYS) Aspera.assert_type(sync_info['sessions'], Array, type: ParameterError) { 'sessions' } Aspera.assert_type(sync_info['sessions'].first, Hash, type: ParameterError) { 'session' } sync_info['sessions'].each { |session| normalize_target_rate(session) } if block_given? sync_info['sessions'].each do |session| Aspera.assert_type(session['local_dir'], String, type: ParameterError) { 'local_dir' } Aspera.assert_type(session['remote_dir'], String, type: ParameterError) { 'remote_dir' } transfer_spec = yield(direction_sym(session), session['local_dir'], session['remote_dir']) Log.dump(:auth_ts, transfer_spec) transfer_spec.deep_merge!(opt_ts) unless opt_ts.nil? Transfer::Spec.resolve_target_rate(transfer_spec) tspec_to_sync_info(transfer_spec, session, ARGS_SESSION_SCHEMA) session['private_key_paths'] = Ascp::Installation.instance.aspera_token_ssh_key_paths(:rsa) if transfer_spec.key?('token') update_remote_dir(session, 'remote_dir', transfer_spec) end end if sync_info.key?('instance') Aspera.assert_type(sync_info['instance'], Hash, type: ParameterError) { 'instance' } instance_builder = Transfer::CommandLineBuilder.new(sync_info['instance'], ARGS_INSTANCE_SCHEMA, Transfer::CommandLineConverter) instance_builder.process_params instance_builder.add_to_exec_spec(exec_spec) end sync_info['sessions'].each do |session_params| Aspera.assert_type(session_params, Hash, type: ParameterError) { 'session' } Aspera.assert(session_params.key?('name'), 'session must contain at least: name', type: ParameterError) session_builder = Transfer::CommandLineBuilder.new(session_params, ARGS_SESSION_SCHEMA, Transfer::CommandLineConverter) session_builder.process_params session_builder.add_to_exec_spec(exec_spec) end Environment.secure_execute(Ascp::Installation.instance.path(:async), *exec_spec.args, env: exec_spec.env) end return end |
.tspec_to_sync_info(transfer_spec, sync_info, schema) ⇒ Object
Transfer specification to synchronization information
tag x-ts-name in schema is used to map transfer spec parameters to async sync_info
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/aspera/sync/operations.rb', line 298 def tspec_to_sync_info(transfer_spec, sync_info, schema) Log.dump(:tspec_to_sync_info, transfer_spec) schema['properties'].each do |name, property| if property.key?('x-ts-name') tspec_param = property['x-ts-name'] if transfer_spec.key?(tspec_param) && !sync_info.key?(name) sync_info[name] = property['x-ts-convert'] ? Transfer::CommandLineConverter.send(property['x-ts-convert'], transfer_spec[tspec_param]) : transfer_spec[tspec_param] end end if property['type'].eql?('object') && property.key?('properties') sync_info[name] ||= {} tspec_to_sync_info(transfer_spec, sync_info[name], property) end end end |
.update_remote_dir(sync_info, remote_dir_key, transfer_spec) ⇒ Object
Set remote_dir in sync parameters based on transfer spec
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/aspera/sync/operations.rb', line 39 def update_remote_dir(sync_info, remote_dir_key, transfer_spec) if transfer_spec.dig(*%w[tags aspera node file_id]) # in AoC, use gen4 sync_info[remote_dir_key] = '/' elsif transfer_spec['cookie']&.start_with?('aspera.shares2') # TODO : something more generic, independent of Shares # in Shares, the actual folder on remote end is not always the same as the name of the share remote_key = transfer_spec['direction'].eql?('send') ? 'destination' : 'source' actual_remote = transfer_spec['paths']&.first&.[](remote_key) sync_info[remote_dir_key] = actual_remote if actual_remote end nil end |