Module: KnuckleCluster::Scp

Included in:
KnuckleCluster
Defined in:
lib/knuckle_cluster/scp.rb

Instance Method Summary collapse

Instance Method Details

#generate_agent_scp_string(input, agent) ⇒ Object



47
48
49
50
51
52
# File 'lib/knuckle_cluster/scp.rb', line 47

def generate_agent_scp_string(input, agent)
  split_input = input.split(':')
  location    = split_input.last
  target_ip = bastion ? agent.private_ip : agent.public_ip
  return "#{ssh_username}@#{target_ip}:#{location}"
end

#generate_scp_connection_string(agent:) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/knuckle_cluster/scp.rb', line 62

def generate_scp_connection_string(agent:)
  ip = bastion ? agent.private_ip : agent.public_ip
  command = "scp"
  command += " -i #{rsa_key_location}" if rsa_key_location
  if bastion.is_a? String
    command += " -o ProxyCommand='ssh -qxT #{bastion} nc #{ip} 22'"
  elsif bastion.is_a? Hash
    command += " -o Proxycommand='ssh -qxt #{bastion[:host]} -l#{bastion[:username]} -i #{bastion[:rsa_key_location]} nc #{ip} 22'"
  end
  command
end

#initiate_scp(source:, destination:) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/knuckle_cluster/scp.rb', line 3

def initiate_scp(source:, destination:)
  if source.start_with?('agent') || destination.start_with?('agent')
    agent = select_agent

    if source.start_with?('agent')
      source      = generate_agent_scp_string(source,      agent)
    elsif destination.start_with?('agent')
      destination = generate_agent_scp_string(destination, agent)
    end

    scp_with_agent(source: source, destination: destination, agent: agent)
  elsif source.start_with?('container') || destination.start_with?('container')
    container = select_container
    agent     = container.task.agent
    if source.start_with?('container')
      #This is SCP FROM container
      source = source.split(':').last
      tmp_source_file = '~/tmp_kc.tmp'
      container_id = get_container_id_command(container.name)

      subcommand = "#{'sudo ' if sudo}docker cp \\`#{container_id}\\`:#{source} #{tmp_source_file}"
      run_command_in_agent(agent: agent, command: subcommand)

      scp_source = generate_agent_scp_string(tmp_source_file, agent)
      scp_with_agent(source: scp_source, agent: agent, destination: destination)

      subcommand = "#{'sudo ' if sudo} rm #{tmp_source_file}"
      run_command_in_agent(agent: agent, command: subcommand)

      puts "Done!"
    elsif destination.start_with?('container')
      #SCP TO container
      destination = destination.split(':').last
      tmp_destination_file = '~/tmp_kc.tmp'
      tmp_destination = generate_agent_scp_string(tmp_destination_file, agent)
      scp_with_agent(source: source, agent: agent, destination: tmp_destination)
      container_id = get_container_id_command(container.name)
      subcommand = "#{'sudo ' if sudo}docker cp #{tmp_destination_file} \\`#{container_id}\\`:#{destination} && rm #{tmp_destination_file}"
      run_command_in_agent(agent: agent, command: subcommand)
      puts "Done!"
    end
  end
end

#scp_to_container(source:, destination:) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/knuckle_cluster/scp.rb', line 74

def scp_to_container(source:, destination:)
  container = select_container
  agent     = container.task.agent
  tmp_destination_file = '~/tmp_kc.tmp'
  scp_to_agent(source: source, agent: agent, destination: tmp_destination_file)
  container_id = get_container_id_command(container.name)
  subcommand = "#{'sudo ' if sudo}docker cp #{tmp_destination_file} \\`#{container_id}\\`:#{destination} && rm #{tmp_destination_file}"
  run_command_in_agent(agent: agent, command: subcommand)
  puts "Done!"
end

#scp_with_agent(source:, destination:, agent: nil) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/knuckle_cluster/scp.rb', line 54

def scp_with_agent(source:, destination:, agent: nil)
  command = generate_scp_connection_string(agent: agent)
  command += " #{source}"
  command += " #{destination}"
  system(command)
  puts "Done!"
end