# frozen_string_literal: true module Groundskeeper # Wraps ssh executable. class Ssh COMMAND = "ssh" DEFAULT_CONNECT_TIMEOUT_IN_S = 5 # ssh arguments CONNECT_WITH_TIMEOUT = "-o ConnectTimeout=#{DEFAULT_CONNECT_TIMEOUT_IN_S}".freeze REMOTE_ECHO = "%s %s@%s \"echo 'groundskeeper'\"" attr_reader :domain_name, :ssh, :username def self.build(username, domain_name) new Executable.new(COMMAND), username, domain_name end def initialize(ssh, username, domain_name) @ssh = ssh @username = username @domain_name = domain_name end def can_connect? ssh.execute(format(REMOTE_ECHO, options: CONNECT_WITH_TIMEOUT, username: username, domain_name: domain_name)).strip == "groundskeeper" end end end