Class: Deplomat::RemoteNode

Inherits:
Node
  • Object
show all
Defined in:
lib/deplomat/remote_node.rb

Instance Attribute Summary

Attributes inherited from Node

#current_path, #log_to, #logfile, #raise_exceptions

Instance Method Summary collapse

Methods inherited from Node

#cd, #clean, #copy, #create_dir, #create_file, #create_symlink, #git_checkout, #git_merge, #git_pull, #git_push, #move, #remove

Constructor Details

#initialize(host:, port: 22, user: "deploy") ⇒ RemoteNode

Returns a new instance of RemoteNode.



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
# File 'lib/deplomat/remote_node.rb', line 4

def initialize(host:, port: 22, user: "deploy")
  super()

  # Create ControlMasters dir, user might not have it
  unless File.exists?("#{ENV['HOME']}/.ssh/controlmasters/")
    Dir.mkdir("#{ENV['HOME']}/.ssh/controlmasters/") 
  end

  # Establish connection
  first_ssh_command = "ssh -MNfS #{ENV['HOME']}/.ssh/controlmasters/%r@%h:%p #{user}@#{host} -p #{port} -o ControlPersist=10m"
  system first_ssh_command

  # get background process id by the full command name
  Sys::ProcTable.ps.each do |process|
    if process.cmdline.match(first_ssh_command)
      @pid = process.pid.to_i 
      puts "Connected with ssh, host #{host}, pid #{@pid}."
      break
    end
  end

  @ssh_command = "ssh -S #{ENV['HOME']}/.ssh/controlmasters/%r@%h:%p #{user}@#{host} -p #{port}"

  @host = host
  @user = user
  @port = port
end

Instance Method Details

#closeObject



45
46
47
48
49
50
51
52
# File 'lib/deplomat/remote_node.rb', line 45

def close
  begin
    puts "Closing connection to #{@host}."
    Process.kill 'KILL', @pid
  rescue Errno::ESRCH
    puts "WARNING: no process with #{@pid} found, no connection to close!"
  end
end

#execute(command, path = @current_path, message: [], env_vars: '', login_shell: false, stdout_source: :stdout) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/deplomat/remote_node.rb', line 33

def execute(command, path=@current_path, message: [], env_vars: '', login_shell: false, stdout_source: :stdout)

  log("(#{@host}) --> " + command + "\n", color: "white")
  command = "#{env_vars} cd #{path} && #{command}"  if path
  command = "bash -l -c \"#{command}\"" if 
  super("#{@ssh_command} '#{command}'", nil, message: message, stdout_source: stdout_source, log_command: false)
end

#local_executeObject



32
# File 'lib/deplomat/remote_node.rb', line 32

alias :local_execute :execute

#upload(what, where) ⇒ Object



41
42
43
# File 'lib/deplomat/remote_node.rb', line 41

def upload(what, where)
  local_execute "rsync -arz #{what} #{@user}@#{@host}:#{where} --port=#{@port}"
end