Module: Aspera::Transfer::Sync
- Defined in:
- lib/aspera/transfer/sync.rb
Overview
builds command line arg for async
Constant Summary collapse
- DIRECTIONS =
sync direction, default is push
%i[push pull bidi].freeze
Class Method Summary collapse
- .admin_status(sync_params, session_name) ⇒ Object
- .parse_status(stdout) ⇒ Object
- .remote_certificates(remote) ⇒ Object
- .start(sync_params, &block) ⇒ Object
-
.update_remote_dir(sync_params, remote_dir_key, transfer_spec) ⇒ Object
Set remote_dir in sync parameters based on transfer spec.
Class Method Details
.admin_status(sync_params, session_name) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/aspera/transfer/sync.rb', line 236 def admin_status(sync_params, session_name) command_line = [ASYNC_ADMIN_EXECUTABLE, '--quiet'] if sync_params.key?('local') Aspera.assert(!sync_params['name'].nil?){'Missing session name'} Aspera.assert(session_name.nil? || session_name.eql?(sync_params['name'])){'Session not found'} command_line.push("--name=#{sync_params['name']}") if sync_params.key?('local_db_dir') command_line.push("--local-db-dir=#{sync_params['local_db_dir']}") elsif sync_params.dig('local', 'path') command_line.push("--local-dir=#{sync_params.dig('local', 'path')}") else raise 'Missing either local_db_dir or local.path' end elsif sync_params.key?('sessions') session = session_name.nil? ? sync_params['sessions'].first : sync_params['sessions'].find{|s|s['name'].eql?(session_name)} raise "Session #{session_name} not found in #{sync_params['sessions'].map{|s|s['name']}.join(',')}" if session.nil? raise 'Missing session name' if session['name'].nil? command_line.push("--name=#{session['name']}") if session.key?('local_db_dir') command_line.push("--local-db-dir=#{session['local_db_dir']}") elsif session.key?('local_dir') command_line.push("--local-dir=#{session['local_dir']}") else raise 'Missing either local_db_dir or local_dir' end else raise 'At least one of `local` or `sessions` must be present in async parameters' end Log.log.debug{"execute: #{command_line.join(' ')}"} stdout, stderr, status = Open3.capture3(*command_line) Log.log.debug{"status=#{status}, stderr=#{stderr}"} Log.log.trace1{"stdout=#{stdout}"} raise "Sync failed: #{status.exitstatus} : #{stderr}" unless status.success? return parse_status(stdout) end |
.parse_status(stdout) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/aspera/transfer/sync.rb', line 219 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) ⇒ Object
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 |
# File 'lib/aspera/transfer/sync.rb', line 109 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 if remote.key?('token') && !remote.key?('pass') certificates_to_use.concat(Ascp::Installation.instance.aspera_token_ssh_key_paths) end end return certificates_to_use end |
.start(sync_params, &block) ⇒ Object
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/aspera/transfer/sync.rb', line 137 def start(sync_params, &block) Aspera.assert_type(sync_params, Hash) env_args = { args: [], env: {} } if sync_params.key?('local') remote = sync_params['remote'] # async native JSON format (v2) Aspera.assert_type(remote, Hash) # get transfer spec if possible, and feed back to new structure if block transfer_spec = yield((sync_params['direction'] || 'push').to_sym, sync_params['local']['path'], remote['path']) # async native JSON format Aspera.assert_type(sync_params['local'], Hash) # translate transfer spec to async parameters TS_TO_PARAMS_V2.each do |ts_param, sy_path| next unless transfer_spec.key?(ts_param) sy_dig = sy_path.split('.') param = sy_dig.pop hash = sy_dig.empty? ? sync_params : sync_params[sy_dig.first] hash = sync_params[sy_dig.first] = {} if hash.nil? hash[param] = transfer_spec[ts_param] end update_remote_dir(remote, 'path', transfer_spec) end remote['connect_mode'] ||= remote.key?('ws_port') ? 'ws' : 'ssh' add_certificates = remote_certificates(remote) if !add_certificates.empty? remote['private_key_paths'] ||= [] remote['private_key_paths'].concat(add_certificates) end Aspera.assert_type(sync_params, Hash) env_args[:args] = ["--conf64=#{Base64.strict_encode64(JSON.generate(sync_params))}"] elsif sync_params.key?('sessions') # ascli JSON format (v1) if block sync_params['sessions'].each do |session| transfer_spec = yield((session['direction'] || 'push').to_sym, session['local_dir'], session['remote_dir']) PARAMS_VX_SESSION.each do |async_param, behavior| if behavior.key?(:ts) tspec_param = behavior[:ts].is_a?(TrueClass) ? async_param : behavior[:ts].to_s session[async_param] ||= transfer_spec[tspec_param] if transfer_spec.key?(tspec_param) end end session['private_key_paths'] = Ascp::Installation.instance.aspera_token_ssh_key_paths if transfer_spec.key?('token') update_remote_dir(session, 'remote_dir', transfer_spec) end end raise StandardError, "Only 'sessions', and optionally 'instance' keys are allowed" unless sync_params.keys.push('instance').uniq.sort.eql?(PARAMS_VX_KEYS) Aspera.assert_type(sync_params['sessions'], Array) Aspera.assert_type(sync_params['sessions'].first, Hash) if sync_params.key?('instance') Aspera.assert_type(sync_params['instance'], Hash) instance_builder = CommandLineBuilder.new(sync_params['instance'], PARAMS_VX_INSTANCE) instance_builder.process_params instance_builder.add_env_args(env_args) end sync_params['sessions'].each do |session_params| Aspera.assert_type(session_params, Hash) Aspera.assert(session_params.key?('name')){'session must contain at least name'} session_builder = CommandLineBuilder.new(session_params, PARAMS_VX_SESSION) session_builder.process_params session_builder.add_env_args(env_args) end else raise 'At least one of `local` or `sessions` must be present in async parameters' end Log.log.debug{Log.dump(:sync_params, sync_params)} Log.log.debug{"execute: #{env_args[:env].map{|k, v| "#{k}=\"#{v}\""}.join(' ')} \"#{ASYNC_EXECUTABLE}\" \"#{env_args[:args].join('" "')}\""} res = system(env_args[:env], [ASYNC_EXECUTABLE, ASYNC_EXECUTABLE], *env_args[:args]) Log.log.debug{"result=#{res}"} case res when true then return nil when false then raise "failed: #{$CHILD_STATUS}" when nil then raise "not started: #{$CHILD_STATUS}" else Aspera.error_unexpected_value(res) end end |
.update_remote_dir(sync_params, remote_dir_key, transfer_spec) ⇒ Object
Set remote_dir in sync parameters based on transfer spec
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/aspera/transfer/sync.rb', line 96 def update_remote_dir(sync_params, remote_dir_key, transfer_spec) if transfer_spec.dig(*%w[tags aspera node file_id]) # in AoC, use gen4 sync_params[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 actual_remote = transfer_spec['paths']&.first&.[]('source') sync_params[remote_dir_key] = actual_remote if actual_remote end nil end |