class ChkDFront::CliOperations

Attributes

http[RW]
request[RW]
response[RW]

Public Class Methods

new() click to toggle source
# File lib/chkdfront/cli_operations.rb, line 22
def initialize
  @http     = nil
  @request  = nil
  @response = nil

  @regx_domain = /^(?:https?:\/\/)?(?:[^@\n]+@)?([^:\/\n?]+)/ # extract domain from url
  @options = { output: $stdout, format: :pulse_2, success_mark: " ✔ ".green, error_mark: " ✖ ".red, hide_cursor: true }
end

Public Instance Methods

dns_ping(hosts) click to toggle source
# File lib/chkdfront/cli_operations.rb, line 113
def dns_ping(hosts)
  CLI::UI::Frame.divider('NSlookup (CNAME)', color: :reset)
  hosts.map do |host|
    host = host.scan(@regx_domain).join
    spinner = TTY::Spinner.new("[:spinner] nslookup #{host}", @options)
    dns = ChkDFront::Troubleshoot.dns_ping(host)
    dns.empty? ? spinner.error : spinner.success
    puts dns
  end
end
find_provider(provider_name, domain_front) click to toggle source

Find provider if not given by the user @param [Symbol] provider_name

given by 'options.provider' option.
Default is :auto, accepted is :amazon, :azure, :alibaba

@param [String] domain_front

given by 'options.domain_front' option.
# File lib/chkdfront/cli_operations.rb, line 65
def find_provider(provider_name, domain_front)
  @options[:format] = :dots_9
  spinner = TTY::Spinner.new("[:spinner] Auto-detecting", @options)
  case provider_name
  when :auto
    domain = Adomain.domain(domain_front)
    [amazon, azure, alibaba].map do |provider|
      if provider[:dfront].include?(domain)
        # spinner.update(title: "Provider found: #{provider[:name].bold}")
        # spinner.reset
        spinner.success(" | Provider found: #{provider[:name].bold}")
        return provider[:name].downcase.to_sym
      end
    end
  when :amazon || :azure || :alibaba
    provider_name
  else
    spinner.error("Failed to auto detect provider: please use '-p' and choose from: 1, 2 or 3")
  end
end
http_ping(hosts) click to toggle source
# File lib/chkdfront/cli_operations.rb, line 102
def http_ping(hosts)
  CLI::UI::Frame.divider('HTTP Ping', color: :reset)
  hosts.map do |host|
    host = host.scan(@regx_domain).join
    spinner = TTY::Spinner.new("[:spinner] pinging #{host}", @options)
    http = ChkDFront::Troubleshoot.http_ping(host)
    http.ping? ? spinner.success : spinner.error
    puts http.exception unless http.ping?
  end
end
icmp_ping(hosts) click to toggle source
# File lib/chkdfront/cli_operations.rb, line 92
def icmp_ping(hosts)
  CLI::UI::Frame.divider('ICMP Ping', color: :reset)
  hosts.map do |host|
    host = host.scan(@regx_domain).join
    spinner = TTY::Spinner.new("[:spinner] pinging #{host}", @options)
    icmp = ChkDFront::Troubleshoot.icmp_ping(host)
    icmp.ping? ? spinner.success : spinner.error
  end
end
show_body() click to toggle source
# File lib/chkdfront/cli_operations.rb, line 50
def show_body
  CLI::UI::Frame.divider('Response Body')
  puts @response.body
end
show_checks() click to toggle source
# File lib/chkdfront/cli_operations.rb, line 46
def show_checks
  puts @http.res_err[:checks]
end
show_debug() click to toggle source
# File lib/chkdfront/cli_operations.rb, line 54
def show_debug
  CLI::UI::Frame.divider('Debugging')
  puts WordWrap.ww(@http.debug_output, CLI::UI::Terminal.width)
end
show_expected(string) click to toggle source
# File lib/chkdfront/cli_operations.rb, line 37
def show_expected(string)
  CLI::UI::Frame.divider('Expected String')
  if @response.body.include?(string)
    puts CLI::UI.fmt "{{v}} Found: " + "#{string}"
  else
    puts CLI::UI.fmt "{{x}} Not Found: " + "#{string}"
  end
end
show_success() click to toggle source
# File lib/chkdfront/cli_operations.rb, line 31
def show_success
  puts CLI::UI.fmt "{{v}} Front target:    " + "#{@http.front_target.host}"
  puts CLI::UI.fmt "{{v}} Provider header: " + "#{@response['Via']}"
  puts CLI::UI.fmt "{{v}} Domain front:    " + "#{@http.domain_front}"
end
troubleshoot(hosts = []) click to toggle source
# File lib/chkdfront/cli_operations.rb, line 86
def troubleshoot(hosts = [])
  icmp_ping hosts
  http_ping hosts
  dns_ping  hosts
end