Sha256: bf60058fb386364bf289ca00151f5379c8ba478f957bc7b4ebc82d847306c8bd

Contents?: true

Size: 1.98 KB

Versions: 2

Compression:

Stored size: 1.98 KB

Contents

module Deplomat
  class RemoteNode < Node

    def initialize(host:, port: 22, user: "deploy", raise_exceptions: true)
      super(logfile: '~/deplomat.log')

      # 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

    alias :local_execute :execute
    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

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

    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

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
deplomat-0.1.6 lib/deplomat/remote_node.rb
deplomat-0.1.5 lib/deplomat/remote_node.rb