From d156ac397beffabe3f0cb045f923050a3ab74887 Mon Sep 17 00:00:00 2001 From: Neil Lathwood Date: Tue, 31 Oct 2017 21:38:54 +0000 Subject: Added support for sending single node request to http source --- lib/oxidized/source/http.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib/oxidized/source') diff --git a/lib/oxidized/source/http.rb b/lib/oxidized/source/http.rb index 6c12f29..e4cb58e 100644 --- a/lib/oxidized/source/http.rb +++ b/lib/oxidized/source/http.rb @@ -15,7 +15,7 @@ class HTTP < Source require "uri" require "json" - def load + def load node=nil nodes = [] uri = URI.parse(@cfg.url) http = Net::HTTP.new(uri.host, uri.port) @@ -28,7 +28,11 @@ class HTTP < Source headers[header] = value end - request = Net::HTTP::Get.new(uri.request_uri, headers) + req_uri = uri.request_uri + if node + req_uri = "#{req_uri}/#{node}" + end + request = Net::HTTP::Get.new(req_uri, headers) if (@cfg.user? && @cfg.pass?) request.basic_auth(@cfg.user,@cfg.pass) end -- cgit v1.2.1 From 29704b5f2721a742d312ced784a8076e27e8063b Mon Sep 17 00:00:00 2001 From: laf Date: Fri, 10 Nov 2017 14:12:13 +0000 Subject: Added sql support for calling for single host --- lib/oxidized/source/http.rb | 6 +++--- lib/oxidized/source/sql.rb | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'lib/oxidized/source') diff --git a/lib/oxidized/source/http.rb b/lib/oxidized/source/http.rb index e4cb58e..73df6f7 100644 --- a/lib/oxidized/source/http.rb +++ b/lib/oxidized/source/http.rb @@ -15,7 +15,7 @@ class HTTP < Source require "uri" require "json" - def load node=nil + def load node_want=nil nodes = [] uri = URI.parse(@cfg.url) http = Net::HTTP.new(uri.host, uri.port) @@ -29,8 +29,8 @@ class HTTP < Source end req_uri = uri.request_uri - if node - req_uri = "#{req_uri}/#{node}" + if node_want + req_uri = "#{req_uri}/#{node_want}" end request = Net::HTTP::Get.new(req_uri, headers) if (@cfg.user? && @cfg.pass?) diff --git a/lib/oxidized/source/sql.rb b/lib/oxidized/source/sql.rb index 13fc39b..7e2824d 100644 --- a/lib/oxidized/source/sql.rb +++ b/lib/oxidized/source/sql.rb @@ -18,11 +18,12 @@ class SQL < Source end end - def load + def load node_want=nil nodes = [] db = connect query = db[@cfg.table.to_sym] query = query.with_sql(@cfg.query) if @cfg.query? + query = qeury.where(@cfg.map.name.to_sym node_want) query.each do |node| # map node parameters keys = {} -- cgit v1.2.1 From d96cfac1f09ff4055f54ab604358290865d41f62 Mon Sep 17 00:00:00 2001 From: laf Date: Fri, 10 Nov 2017 14:44:45 +0000 Subject: updated to check if node_want --- lib/oxidized/source/sql.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib/oxidized/source') diff --git a/lib/oxidized/source/sql.rb b/lib/oxidized/source/sql.rb index 7e2824d..97064fa 100644 --- a/lib/oxidized/source/sql.rb +++ b/lib/oxidized/source/sql.rb @@ -23,7 +23,11 @@ class SQL < Source db = connect query = db[@cfg.table.to_sym] query = query.with_sql(@cfg.query) if @cfg.query? - query = qeury.where(@cfg.map.name.to_sym node_want) + + if node_want + query = query.where(@cfg.map.name.to_sym=>node_want) + end + query.each do |node| # map node parameters keys = {} -- cgit v1.2.1 From 01d7f29905fd9ca712e8b640408425cf2d3ad64d Mon Sep 17 00:00:00 2001 From: Wild Kat Date: Wed, 11 Apr 2018 19:58:30 +0200 Subject: standardize on verbose hash methods --- lib/oxidized/source/csv.rb | 2 +- lib/oxidized/source/http.rb | 2 +- lib/oxidized/source/sql.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/oxidized/source') diff --git a/lib/oxidized/source/csv.rb b/lib/oxidized/source/csv.rb index 4814bd7..b61525e 100644 --- a/lib/oxidized/source/csv.rb +++ b/lib/oxidized/source/csv.rb @@ -36,7 +36,7 @@ class CSV < Source @cfg.map.each do |key, position| keys[key.to_sym] = node_var_interpolate data[position] end - keys[:model] = map_model keys[:model] if keys.key? :model + keys[:model] = map_model keys[:model] if keys.has_key? :model # map node specific vars vars = {} diff --git a/lib/oxidized/source/http.rb b/lib/oxidized/source/http.rb index 6c12f29..56a575b 100644 --- a/lib/oxidized/source/http.rb +++ b/lib/oxidized/source/http.rb @@ -43,7 +43,7 @@ class HTTP < Source want_positions = want_position.split('.') keys[key.to_sym] = node_var_interpolate node.dig(*want_positions) end - keys[:model] = map_model keys[:model] if keys.key? :model + keys[:model] = map_model keys[:model] if keys.has_key? :model # map node specific vars vars = {} diff --git a/lib/oxidized/source/sql.rb b/lib/oxidized/source/sql.rb index 13fc39b..ca3bcc3 100644 --- a/lib/oxidized/source/sql.rb +++ b/lib/oxidized/source/sql.rb @@ -27,7 +27,7 @@ class SQL < Source # map node parameters keys = {} @cfg.map.each { |key, sql_column| keys[key.to_sym] = node_var_interpolate node[sql_column.to_sym] } - keys[:model] = map_model keys[:model] if keys.key? :model + keys[:model] = map_model keys[:model] if keys.has_key? :model # map node specific vars vars = {} -- cgit v1.2.1 From 21e3d6490496573f25ef77fe8172766ac7d1a736 Mon Sep 17 00:00:00 2001 From: Wild Kat Date: Sat, 21 Apr 2018 13:27:05 +0200 Subject: the great makeover - standardize layout, alignment, indentation --- lib/oxidized/source/csv.rb | 89 ++++++++++++++++++------------------ lib/oxidized/source/http.rb | 97 ++++++++++++++++++++------------------- lib/oxidized/source/source.rb | 13 +++--- lib/oxidized/source/sql.rb | 103 +++++++++++++++++++++--------------------- 4 files changed, 149 insertions(+), 153 deletions(-) (limited to 'lib/oxidized/source') diff --git a/lib/oxidized/source/csv.rb b/lib/oxidized/source/csv.rb index b61525e..ae1c0a9 100644 --- a/lib/oxidized/source/csv.rb +++ b/lib/oxidized/source/csv.rb @@ -1,54 +1,53 @@ module Oxidized -class CSV < Source - def initialize - @cfg = Oxidized.config.source.csv - super - end - - def setup - if @cfg.empty? - Oxidized.asetus.user.source.csv.file = File.join(Config::Root, 'router.db') - Oxidized.asetus.user.source.csv.delimiter = /:/ - Oxidized.asetus.user.source.csv.map.name = 0 - Oxidized.asetus.user.source.csv.map.model = 1 - Oxidized.asetus.user.source.csv.gpg = false - Oxidized.asetus.save :user - raise NoConfig, 'no source csv config, edit ~/.config/oxidized/config' + class CSV < Source + def initialize + @cfg = Oxidized.config.source.csv + super end - require 'gpgme' if @cfg.gpg? - end - def load - nodes = [] - file = File.expand_path(@cfg.file) - file = if @cfg.gpg? - crypto = GPGME::Crypto.new password: @cfg.gpg_password - file = crypto.decrypt(File.open(file)).to_s - else - open(file) - end - file.each_line do |line| - next if line.match(/^\s*#/) - data = line.chomp.split(@cfg.delimiter, -1) - next if data.empty? - # map node parameters - keys = {} - @cfg.map.each do |key, position| - keys[key.to_sym] = node_var_interpolate data[position] + def setup + if @cfg.empty? + Oxidized.asetus.user.source.csv.file = File.join(Config::Root, 'router.db') + Oxidized.asetus.user.source.csv.delimiter = /:/ + Oxidized.asetus.user.source.csv.map.name = 0 + Oxidized.asetus.user.source.csv.map.model = 1 + Oxidized.asetus.user.source.csv.gpg = false + Oxidized.asetus.save :user + raise NoConfig, 'no source csv config, edit ~/.config/oxidized/config' end - keys[:model] = map_model keys[:model] if keys.has_key? :model + require 'gpgme' if @cfg.gpg? + end - # map node specific vars - vars = {} - @cfg.vars_map.each do |key, position| - vars[key.to_sym] = node_var_interpolate data[position] - end - keys[:vars] = vars unless vars.empty? + def load + nodes = [] + file = File.expand_path(@cfg.file) + file = if @cfg.gpg? + crypto = GPGME::Crypto.new password: @cfg.gpg_password + file = crypto.decrypt(File.open(file)).to_s + else + open(file) + end + file.each_line do |line| + next if line.match(/^\s*#/) + data = line.chomp.split(@cfg.delimiter, -1) + next if data.empty? + # map node parameters + keys = {} + @cfg.map.each do |key, position| + keys[key.to_sym] = node_var_interpolate data[position] + end + keys[:model] = map_model keys[:model] if keys.has_key? :model - nodes << keys + # map node specific vars + vars = {} + @cfg.vars_map.each do |key, position| + vars[key.to_sym] = node_var_interpolate data[position] + end + keys[:vars] = vars unless vars.empty? + + nodes << keys + end + nodes end - nodes end - -end end diff --git a/lib/oxidized/source/http.rb b/lib/oxidized/source/http.rb index 56a575b..4480cbb 100644 --- a/lib/oxidized/source/http.rb +++ b/lib/oxidized/source/http.rb @@ -1,64 +1,63 @@ module Oxidized -class HTTP < Source - def initialize - @cfg = Oxidized.config.source.http - super - end - - def setup - if @cfg.url.empty? - raise NoConfig, 'no source http url config, edit ~/.config/oxidized/config' + class HTTP < Source + def initialize + @cfg = Oxidized.config.source.http + super end - end - - require "net/http" - require "uri" - require "json" - def load - nodes = [] - uri = URI.parse(@cfg.url) - http = Net::HTTP.new(uri.host, uri.port) - http.use_ssl = true if uri.scheme == 'https' - http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless @cfg.secure - - # map headers - headers = {} - @cfg.headers.each do |header, value| - headers[header] = value + def setup + if @cfg.url.empty? + raise NoConfig, 'no source http url config, edit ~/.config/oxidized/config' + end end - request = Net::HTTP::Get.new(uri.request_uri, headers) - if (@cfg.user? && @cfg.pass?) - request.basic_auth(@cfg.user,@cfg.pass) - end + require "net/http" + require "uri" + require "json" - response = http.request(request) - data = JSON.parse(response.body) - data.each do |node| - next if node.empty? - # map node parameters - keys = {} - @cfg.map.each do |key, want_position| - want_positions = want_position.split('.') - keys[key.to_sym] = node_var_interpolate node.dig(*want_positions) + def load + nodes = [] + uri = URI.parse(@cfg.url) + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true if uri.scheme == 'https' + http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless @cfg.secure + + # map headers + headers = {} + @cfg.headers.each do |header, value| + headers[header] = value end - keys[:model] = map_model keys[:model] if keys.has_key? :model - # map node specific vars - vars = {} - @cfg.vars_map.each do |key, want_position| - want_positions = want_position.split('.') - vars[key.to_sym] = node_var_interpolate node.dig(*want_positions) + request = Net::HTTP::Get.new(uri.request_uri, headers) + if (@cfg.user? && @cfg.pass?) + request.basic_auth(@cfg.user, @cfg.pass) end - keys[:vars] = vars unless vars.empty? - nodes << keys + response = http.request(request) + data = JSON.parse(response.body) + data.each do |node| + next if node.empty? + # map node parameters + keys = {} + @cfg.map.each do |key, want_position| + want_positions = want_position.split('.') + keys[key.to_sym] = node_var_interpolate node.dig(*want_positions) + end + keys[:model] = map_model keys[:model] if keys.has_key? :model + + # map node specific vars + vars = {} + @cfg.vars_map.each do |key, want_position| + want_positions = want_position.split('.') + vars[key.to_sym] = node_var_interpolate node.dig(*want_positions) + end + keys[:vars] = vars unless vars.empty? + + nodes << keys + end + nodes end - nodes end - -end end if RUBY_VERSION < '2.3' diff --git a/lib/oxidized/source/source.rb b/lib/oxidized/source/source.rb index 9b8bc94..3bae0f6 100644 --- a/lib/oxidized/source/source.rb +++ b/lib/oxidized/source/source.rb @@ -11,13 +11,12 @@ module Oxidized end def node_var_interpolate var - case var - when "nil" then nil - when "false" then false - when "true" then true - else var - end + case var + when "nil" then nil + when "false" then false + when "true" then true + else var + end end - end end diff --git a/lib/oxidized/source/sql.rb b/lib/oxidized/source/sql.rb index ca3bcc3..5737c93 100644 --- a/lib/oxidized/source/sql.rb +++ b/lib/oxidized/source/sql.rb @@ -1,63 +1,62 @@ module Oxidized -class SQL < Source - begin - require 'sequel' - rescue LoadError - raise OxidizedError, 'sequel not found: sudo gem install sequel' - end - - def setup - if @cfg.empty? - Oxidized.asetus.user.source.sql.adapter = 'sqlite' - Oxidized.asetus.user.source.sql.database = File.join(Config::Root, 'sqlite.db') - Oxidized.asetus.user.source.sql.table = 'devices' - Oxidized.asetus.user.source.sql.map.name = 'name' - Oxidized.asetus.user.source.sql.map.model = 'rancid' - Oxidized.asetus.save :user - raise NoConfig, 'no source sql config, edit ~/.config/oxidized/config' + class SQL < Source + begin + require 'sequel' + rescue LoadError + raise OxidizedError, 'sequel not found: sudo gem install sequel' end - end - def load - nodes = [] - db = connect - query = db[@cfg.table.to_sym] - query = query.with_sql(@cfg.query) if @cfg.query? - query.each do |node| - # map node parameters - keys = {} - @cfg.map.each { |key, sql_column| keys[key.to_sym] = node_var_interpolate node[sql_column.to_sym] } - keys[:model] = map_model keys[:model] if keys.has_key? :model - - # map node specific vars - vars = {} - @cfg.vars_map.each do |key, sql_column| - vars[key.to_sym] = node_var_interpolate node[sql_column.to_sym] + def setup + if @cfg.empty? + Oxidized.asetus.user.source.sql.adapter = 'sqlite' + Oxidized.asetus.user.source.sql.database = File.join(Config::Root, 'sqlite.db') + Oxidized.asetus.user.source.sql.table = 'devices' + Oxidized.asetus.user.source.sql.map.name = 'name' + Oxidized.asetus.user.source.sql.map.model = 'rancid' + Oxidized.asetus.save :user + raise NoConfig, 'no source sql config, edit ~/.config/oxidized/config' end - keys[:vars] = vars unless vars.empty? + end - nodes << keys + def load + nodes = [] + db = connect + query = db[@cfg.table.to_sym] + query = query.with_sql(@cfg.query) if @cfg.query? + query.each do |node| + # map node parameters + keys = {} + @cfg.map.each { |key, sql_column| keys[key.to_sym] = node_var_interpolate node[sql_column.to_sym] } + keys[:model] = map_model keys[:model] if keys.has_key? :model + + # map node specific vars + vars = {} + @cfg.vars_map.each do |key, sql_column| + vars[key.to_sym] = node_var_interpolate node[sql_column.to_sym] + end + keys[:vars] = vars unless vars.empty? + + nodes << keys + end + db.disconnect + nodes end - db.disconnect - nodes - end - private + private - def initialize - super - @cfg = Oxidized.config.source.sql - end + def initialize + super + @cfg = Oxidized.config.source.sql + end - def connect - Sequel.connect(:adapter => @cfg.adapter, - :host => @cfg.host?, - :user => @cfg.user?, - :password => @cfg.password?, - :database => @cfg.database) - rescue Sequel::AdapterNotFound => error - raise OxidizedError, "SQL adapter gem not installed: " + error.message + def connect + Sequel.connect(:adapter => @cfg.adapter, + :host => @cfg.host?, + :user => @cfg.user?, + :password => @cfg.password?, + :database => @cfg.database) + rescue Sequel::AdapterNotFound => error + raise OxidizedError, "SQL adapter gem not installed: " + error.message + end end - -end end -- cgit v1.2.1 From c65bbfe28eaafa21e7660b51758fafcdafc42bab Mon Sep 17 00:00:00 2001 From: Neil Lathwood Date: Fri, 27 Apr 2018 10:55:47 +0100 Subject: travis fixes --- lib/oxidized/source/http.rb | 2 +- lib/oxidized/source/sql.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/oxidized/source') diff --git a/lib/oxidized/source/http.rb b/lib/oxidized/source/http.rb index 6d7dce3..55dcd4c 100644 --- a/lib/oxidized/source/http.rb +++ b/lib/oxidized/source/http.rb @@ -15,7 +15,7 @@ module Oxidized require "uri" require "json" - def load node_want=nil + def load node_want = nil nodes = [] uri = URI.parse(@cfg.url) http = Net::HTTP.new(uri.host, uri.port) diff --git a/lib/oxidized/source/sql.rb b/lib/oxidized/source/sql.rb index dfcd7b4..8d321a0 100644 --- a/lib/oxidized/source/sql.rb +++ b/lib/oxidized/source/sql.rb @@ -18,7 +18,7 @@ module Oxidized end end - def load node_want=nill + def load node_want = nil nodes = [] db = connect query = db[@cfg.table.to_sym] -- cgit v1.2.1 From 137314377bbc15b651228a0394caf45e6fa8bcbc Mon Sep 17 00:00:00 2001 From: Neil Lathwood Date: Fri, 27 Apr 2018 10:58:19 +0100 Subject: Another travis fix --- lib/oxidized/source/sql.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/oxidized/source') diff --git a/lib/oxidized/source/sql.rb b/lib/oxidized/source/sql.rb index 8d321a0..c5fe650 100644 --- a/lib/oxidized/source/sql.rb +++ b/lib/oxidized/source/sql.rb @@ -25,7 +25,7 @@ module Oxidized query = query.with_sql(@cfg.query) if @cfg.query? if node_want - query = query.where(@cfg.map.name.to_sym=>node_want) + query = query.where(@cfg.map.name.to_sym => node_want) end query.each do |node| -- cgit v1.2.1