Class: ChkDFront::CliOperations

Inherits:
Object
  • Object
show all
Includes:
Providers
Defined in:
lib/chkdfront/cli_operations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Providers

#alibaba, #amazon, #azure, #get_provider

Constructor Details

#initializeCliOperations

Returns a new instance of CliOperations.



22
23
24
25
26
27
28
29
# 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

Instance Attribute Details

#httpObject

Returns the value of attribute http.



21
22
23
# File 'lib/chkdfront/cli_operations.rb', line 21

def http
  @http
end

#requestObject

Returns the value of attribute request.



21
22
23
# File 'lib/chkdfront/cli_operations.rb', line 21

def request
  @request
end

#responseObject

Returns the value of attribute response.



21
22
23
# File 'lib/chkdfront/cli_operations.rb', line 21

def response
  @response
end

Instance Method Details

#dns_ping(hosts) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# 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) ⇒ Object

Find provider if not given by the user

Parameters:

  • provider_name (Symbol)

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

  • domain_front (String)

    given by ‘options.domain_front’ option.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# 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) ⇒ Object



92
93
94
95
96
97
98
99
100
# 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_bodyObject



50
51
52
53
# File 'lib/chkdfront/cli_operations.rb', line 50

def show_body
  CLI::UI::Frame.divider('Response Body')
  puts @response.body
end

#show_checksObject



46
47
48
# File 'lib/chkdfront/cli_operations.rb', line 46

def show_checks
  puts @http.res_err[:checks]
end

#show_debugObject



54
55
56
57
# 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) ⇒ Object



37
38
39
40
41
42
43
44
# 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_successObject



31
32
33
34
35
# 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 = []) ⇒ Object



86
87
88
89
90
# File 'lib/chkdfront/cli_operations.rb', line 86

def troubleshoot(hosts = [])
  icmp_ping hosts
  http_ping hosts
  dns_ping  hosts
end