Class: OnlyofficeDigitaloceanWrapper::SshChecker

Inherits:
Object
  • Object
show all
Includes:
LoggerWrapper
Defined in:
lib/onlyoffice_digitalocean_wrapper/ssh_checker.rb

Overview

Class for check if ssh can be connected

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LoggerWrapper

#logger

Constructor Details

#initialize(ip) ⇒ SshChecker



15
16
17
# File 'lib/onlyoffice_digitalocean_wrapper/ssh_checker.rb', line 15

def initialize(ip)
  @ip = ip
end

Instance Attribute Details

#ipString (readonly)



12
13
14
# File 'lib/onlyoffice_digitalocean_wrapper/ssh_checker.rb', line 12

def ip
  @ip
end

Instance Method Details

#ssh_up?Boolean

Check is ssh available for connection right now



39
40
41
42
43
44
45
46
# File 'lib/onlyoffice_digitalocean_wrapper/ssh_checker.rb', line 39

def ssh_up?
  Net::Telnet.new('Host' => @ip,
                  'Timeout' => 1,
                  'Port' => 22)
  true
rescue StandardError
  false
end

#wait_until_ssh_up(timeout: 60) ⇒ void

This method returns an undefined value.

Wait until ssh server on server is up and available for connection



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/onlyoffice_digitalocean_wrapper/ssh_checker.rb', line 22

def wait_until_ssh_up(timeout: 60)
  wait_between_tries = 5
  tries = timeout / wait_between_tries
  tries.times do |try|
    if ssh_up?
      logger.info("SSH on `#{@ip}` is up. Waiting finished")
      return true
    end

    logger.info("SSH on `#{@ip}` is not up. Waited for #{try * wait_between_tries} seconds of #{timeout}")
    sleep wait_between_tries
  end
  raise SshCheckerSshUpTimeout, "SSH on `#{@ip}` is not up for #{timeout} seconds. Could not proceed"
end