Class: Oxidized::HTTP

Inherits:
Source
  • Object
show all
Includes:
Input::CLI
Defined in:
lib/oxidized/input/http.rb,
lib/oxidized/source/http.rb

Instance Attribute Summary

Attributes included from Input::CLI

#node

Instance Method Summary collapse

Methods included from Input::CLI

#connect_cli, #disconnect_cli, #get, #login, #password, #post_login, #pre_logout, #username

Methods inherited from Source

#map_group, #map_model, #node_var_interpolate

Constructor Details

#initializeHTTP

Returns a new instance of HTTP.



3
4
5
6
# File 'lib/oxidized/source/http.rb', line 3

def initialize
  @cfg = Oxidized.config.source.http
  super
end

Instance Method Details

#cmd(callback_or_string) ⇒ Object



32
33
34
35
36
# File 'lib/oxidized/input/http.rb', line 32

def cmd(callback_or_string)
  return cmd_cb callback_or_string if callback_or_string.is_a?(Proc)

  cmd_str callback_or_string
end

#cmd_cb(callback) ⇒ Object



38
39
40
# File 'lib/oxidized/input/http.rb', line 38

def cmd_cb(callback)
  instance_exec(&callback)
end

#cmd_str(string) ⇒ Object



42
43
44
45
# File 'lib/oxidized/input/http.rb', line 42

def cmd_str(string)
  path = string % { password: @node.auth[:password] }
  get_http path
end

#connect(node) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/oxidized/input/http.rb', line 9

def connect(node)
  @node = node
  @secure = false
  @username = nil
  @password = nil
  @headers = {}
  @log = File.open(Oxidized::Config::LOG + "/#{@node.ip}-http", "w") if Oxidized.config.input.debug?
  @node.model.cfg["http"].each { |cb| instance_exec(&cb) }

  return true unless @main_page && defined?()

  begin
    require "mechanize"
  rescue LoadError
    raise OxidizedError, "mechanize not found: sudo gem install mechanize"
  end

  @m = Mechanize.new
  url = URI::HTTP.build host: @node.ip, path: @main_page
  @m_page = @m.get(url.to_s)
  
end

#load(node_want = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/oxidized/source/http.rb', line 20

def load(node_want = nil)
  nodes = []
  uri = URI.parse(@cfg.url)
  data = JSON.parse(read_http(uri, node_want))
  node_data = data
  node_data = string_navigate(data, @cfg.hosts_location) if @cfg.hosts_location?
  node_data = pagination(data, node_want) if @cfg.pagination?

  # at this point we have all the nodes; pagination or not
  node_data.each do |node|
    next if node.empty?

    # map node parameters
    keys = {}
    @cfg.map.each do |key, want_position|
      keys[key.to_sym] = node_var_interpolate string_navigate(node, want_position)
    end
    keys[:model] = map_model keys[:model] if keys.has_key? :model
    keys[:group] = map_group keys[:group] if keys.has_key? :group

    # map node specific vars
    vars = {}
    @cfg.vars_map.each do |key, want_position|
      vars[key.to_sym] = node_var_interpolate string_navigate(node, want_position)
    end
    keys[:vars] = vars unless vars.empty?

    nodes << keys
  end
  nodes
end

#setupObject

Raises:



8
9
10
11
12
13
# File 'lib/oxidized/source/http.rb', line 8

def setup
  Oxidized.setup_logger
  return unless @cfg.url.empty?

  raise NoConfig, 'no source http url config, edit ~/.config/oxidized/config'
end