Class: Gaptool::ScpCommand

Inherits:
Clamp::Command
  • Object
show all
Defined in:
lib/gaptool_client/commands.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/gaptool_client/commands.rb', line 402

def execute
  if src.start_with?(':')
    # from remote to local
    signal_usage_error('SRC and DEST cannot be both remote') \
      if dest && dest.start_with?(':')
    source = src[1..-1]
    destf = dest ? dest : File.join(Dir.pwd, File.basename(source))
    destd = File.dirname(destf)
    Gaptool::Helpers.error("Cannot write to #{destd}") \
      unless File.writable?(destd)

    pre_hook = proc do |node|
      download! node['source'], "#{node['destf']}.#{node['instance']}"
    end

  else
    # from local to remote
    signal_usage_error('SRC and DEST cannot be both local') \
      if dest && !dest.start_with?(':')
    begin
      source = File.realpath(src)
    rescue => e
      Gaptool::Helpers.error(e)
    end
    destf = dest ? dest[1..-1] : File.basename(src)
    if destf.end_with?('/')
      destd = destf
      destf = File.join(destf, File.basename(src))
    else
      destd = File.dirname(destf)
    end

    pre_hook = proc do |node|
      if test("[ -w #{node['destd']} ]")
        upload! node['source'], node['destf']
      else
        fail "No such directory #{node['destd']}" \
          unless test("[ -d #{node['destd']} ]")
        tmp = File.join('/tmp', File.basename(node['destf']))
        upload! node['source'], tmp
        execute("sudo mv #{tmp} #{node['destf']}")
      end
    end
  end

  params = exclude_hidden? ? {} : { hidden: true }
  nodes = Gaptool::API.query_nodes(params.merge(instance: instance,
                                                role: role,
                                                environment: environment))
  nodes = nodes.map { |x| x.merge('source' => source, 'destd' => destd, 'destf' => destf) }

  res = Gaptool::SSH.exec(
    nodes, [],
    pre_hooks: [pre_hook], serial: serial?,
    continue_on_errors: continue_on_errors?,
    batch_size: batch_size,
    log_level: Logger::INFO
  )
  exit res
end