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/sql.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/oxidized/source/sql.rb') 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/sql.rb') 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/sql.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/oxidized/source/sql.rb') 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/sql.rb | 103 ++++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 52 deletions(-) (limited to 'lib/oxidized/source/sql.rb') 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/sql.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/oxidized/source/sql.rb') 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/sql.rb') 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