class DTK::Client::Operation::Service::Ssh

Constants

DefaultLoginByOSType

Public Class Methods

execute(args = Args.new) click to toggle source
# File lib/client/operation/service/ssh.rb, line 21
def self.execute(args = Args.new)
  wrap_operation(args) do |args|
    service_instance = args.required(:service_instance)
    node_name        = args.required(:node_name)
    remote_user      = args[:remote_user]

    if OsUtil.is_windows?
      OsUtil.print_info "[NOTICE] SSH functionality is currenly not supported on Windows."
      return
    end

    identity_file = get_identity_file(args[:identity_file])
    node_info     = get_node_info_for_ssh_login(node_name, service_instance)

    connect(node_info, identity_file, remote_user)
  end
end

Private Class Methods

connect(node_info, identity_file, remote_user) click to toggle source
# File lib/client/operation/service/ssh.rb, line 99
def self.connect(node_info, identity_file, remote_user)
  unless dns_address = node_info[:dns_address]
    raise Error::Usage, "Not able to resolve instance address, has instance been stopped?"
  end

  unless remote_user ||= node_info[:default_login_user]
    raise Error::Usage, "A default Linux login user could not be computed. Retry the command with a specified login using the '-u LINUX-USER' option."
  end

  connection_string = "#{remote_user}@#{dns_address}"

  ssh_command =
    if identity_file
      # provided PEM key
      "ssh -o \"StrictHostKeyChecking no\" -o \"UserKnownHostsFile /dev/null\" -i #{identity_file} #{connection_string}"
    elsif SSHUtil.ssh_reachable?(remote_user, dns_address)
      # it has PUB key access
      "ssh -o \"StrictHostKeyChecking no\" -o \"UserKnownHostsFile /dev/null\" #{connection_string}"
    end

  unless ssh_command
    raise Error::Usage, "No public key access or PEM provided, please grant access or provide valid PEM key"
  end

  OsUtil.print_info("You are entering SSH terminal (#{connection_string}) ...")
  Kernel.system(ssh_command)
  OsUtil.print_info("You are leaving SSH terminal, and returning to DTK Shell ...")
end
default_login_user?(node_info) click to toggle source
# File lib/client/operation/service/ssh.rb, line 88
def self.default_login_user?(node_info)
  if os_type = node_info['os_type']
    DefaultLoginByOSType[os_type]
  end
end
get_identity_file(identity_file) click to toggle source
# File lib/client/operation/service/ssh.rb, line 41
def self.get_identity_file(identity_file)
  if identity_file
    unless File.exists?(identity_file)
      raise Error::Usage, "Not able to find identity file, '#{identity_file}'"
    end
  elsif default_identity_file = OsUtil.dtk_identity_file_location
    if File.exists?(default_identity_file)
      identity_file = default_identity_file
    end
  end

  identity_file
end
get_node_info_for_ssh_login(node_name, service_instance) click to toggle source
# File lib/client/operation/service/ssh.rb, line 55
      def self.get_node_info_for_ssh_login(node_name, service_instance)
        info_hash = {}

        response = rest_get("#{BaseRoute}/#{service_instance}/nodes")

        if node_name.nil?
          if response.data.nil?
            raise Error::Usage, "Service instance does not contain any nodes"
          end

          if response.data.size == 1
            node_info = response.data.first
          else
            node_names = response.data.map { |node| node['display_name'] }
            raise Error::Usage, "The service instance '#{service_instance}' has more than one node. Please use 'dtk service ssh' command with a node name. Legal names are: #{node_names.join(', ')}"
          end
        else
          unless node_info = response.data.find{ |node| node_name == node['display_name'] }
            raise Error::Usage, "The node '#{node_name}' does not exist"
          end
        end

        if dns_address = node_info['dns_address']
          info_hash.merge!(:dns_address => dns_address)
        end

        if default_login_user = default_login_user?(node_info)
          info_hash.merge!(:default_login_user => default_login_user)
        end
pp info_hash
        info_hash
      end