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, #stdout_lines_to_ignore, #wrap_in

Instance Method Summary collapse

Methods inherited from Node

#cd, #clean, #copy, #create_dir, #create_file, #create_symlink, #current_requisite_number, #git_checkout, #git_merge, #git_pull, #git_push, #log, #move, #path_exist?, #remove, #update_requisite_number!

Constructor Details

#initialize(host:, port: nil, user: "deploy", raise_exceptions: true, path: nil, logfile: '~/deplomat.log', log_to: [:stdout]) ⇒ 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
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/deplomat/remote_node.rb', line 4

def initialize(host:, port: nil, user: "deploy", raise_exceptions: true, path: nil, logfile: '~/deplomat.log', log_to: [:stdout])

  @log_to  = log_to
  @logfile = logfile

  # 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} #{port ? "-p #{port.to_s} " : ""}-o ControlPersist=10m"
  system first_ssh_command

  # get background process id by the full command name
  @pids = []
  Sys::ProcTable.ps.each do |process|
    if process.cmdline.match(first_ssh_command)
      @pids << process.pid.to_i
    end
  end
  @pids.compact!
  if @pids.empty?
    raise Deplomat::ExecutionError.new("ERROR: no ssh pid found for\n\t#{first_ssh_command}.\nThis is weird.")
  elsif @pids.length > 1
    log("Connected with ssh, host #{host}, but looks like another connection has been opened before...", color: "white")
    log("connection pids: #{@pids.join(", ")}. We'll be closing them all when finished.", color: "white")
  else
    log("Connected with ssh, host #{host}, pid #{@pids.first}", color: "white")
  end

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

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

  super(logfile: '~/deplomat.log', path: path, raise_exceptions: raise_exceptions)
end

Instance Method Details

#closeObject



63
64
65
66
67
68
69
70
# File 'lib/deplomat/remote_node.rb', line 63

def close
  begin
    puts "Closing connection(s) to #{@host}, ssh pid(s): #{@pids.join(", ")}."
    @pids.each { |pid| Process.kill 'TERM', pid }
  rescue Errno::ESRCH
    puts "WARNING: no process with #{@pid} found, no connection to close!"
  end
end

#download(what, where, except: nil) ⇒ Object



58
59
60
61
# File 'lib/deplomat/remote_node.rb', line 58

def download(what, where, except: nil)
  except = "--exclude '#{except}'" if except
  local_execute "rsync -arzve 'ssh #{@port ? "-p #{@port.to_s} " : ""}' #{except} --delete #{@user}@#{@host}:#{what} #{where}", nil
end

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



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

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

  log("(#{@host}) --> " + command + "\n", color: "white") if log_command
  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, _raise_exceptions: _raise_exceptions)
end

#local_executeObject



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

alias :local_execute :execute

#upload(what, where, except: nil) ⇒ Object



53
54
55
56
# File 'lib/deplomat/remote_node.rb', line 53

def upload(what, where, except: nil)
  except = "--exclude '#{except}'" if except
  local_execute "rsync -arzve 'ssh #{@port ? "-p #{@port.to_s} " : ""}' #{except} --delete #{what} #{@user}@#{@host}:#{where}", nil
end