summaryrefslogtreecommitdiff
path: root/lib/oxidized/source
diff options
context:
space:
mode:
Diffstat (limited to 'lib/oxidized/source')
-rw-r--r--lib/oxidized/source/csv.rb89
-rw-r--r--lib/oxidized/source/http.rb101
-rw-r--r--lib/oxidized/source/source.rb13
-rw-r--r--lib/oxidized/source/sql.rb106
4 files changed, 157 insertions, 152 deletions
diff --git a/lib/oxidized/source/csv.rb b/lib/oxidized/source/csv.rb
index 4814bd7..7b771f6 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.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 _node_want = nil
+ 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 6c12f29..55dcd4c 100644
--- a/lib/oxidized/source/http.rb
+++ b/lib/oxidized/source/http.rb
@@ -1,64 +1,67 @@
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 node_want = nil
+ 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.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)
+ req_uri = uri.request_uri
+ if node_want
+ req_uri = "#{req_uri}/#{node_want}"
+ end
+ request = Net::HTTP::Get.new(req_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 13fc39b..c5fe650 100644
--- a/lib/oxidized/source/sql.rb
+++ b/lib/oxidized/source/sql.rb
@@ -1,63 +1,67 @@
module Oxidized
-class SQL < Source
- begin
- require 'sequel'
- rescue LoadError
- raise OxidizedError, 'sequel not found: sudo gem install sequel'
- end
+ 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'
+ 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
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.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 load node_want = nil
+ nodes = []
+ db = connect
+ query = db[@cfg.table.to_sym]
+ query = query.with_sql(@cfg.query) if @cfg.query?
+
+ if node_want
+ query = query.where(@cfg.map.name.to_sym => node_want)
end
- keys[:vars] = vars unless vars.empty?
- nodes << keys
+ 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