class Deplomat::RemoteNode

Public Class Methods

new(host:, port: nil, user: "deploy", raise_exceptions: true, path: nil, logfile: '~/deplomat.log', log_to: [:stdout]) click to toggle source
Calls superclass method Deplomat::Node::new
# 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

Public Instance Methods

close() click to toggle source
# File lib/deplomat/remote_node.rb, line 72
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) click to toggle source
# File lib/deplomat/remote_node.rb, line 67
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) click to toggle source
Calls superclass method Deplomat::Node#execute
# 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 login_shell
  super("#{@ssh_command} '#{command}'", nil, message: message, stdout_source: stdout_source, log_command: false, _raise_exceptions: _raise_exceptions)
end
Also aliased as: local_execute
file_exists?(path, log_not_found: true)
Alias for: path_exist?
local_execute(command, path=@current_path, message: [], env_vars: '', login_shell: false, stdout_source: :stdout, log_command: true, _raise_exceptions: @raise_exceptions)
Alias for: execute
path_exist?(path, log_not_found: true) click to toggle source
# File lib/deplomat/remote_node.rb, line 53
def path_exist?(path, log_not_found: true)
  path = adjusted_path(path)
  status = execute("test -f #{path} || test -d #{path}", nil, log_command: true, _raise_exceptions: false)[:status]
  log("NOT FOUND: #{path}") if log_not_found && status == 1
  status == 0
end
Also aliased as: path_exists?, file_exists?
path_exists?(path, log_not_found: true)
Alias for: path_exist?
upload(what, where, except: nil) click to toggle source
# File lib/deplomat/remote_node.rb, line 62
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