Sha256: 9cf7a7de41c7a689e678c2eaa14a20ceb52b353cdd900760553c93de46ffea8b

Contents?: true

Size: 1.57 KB

Versions: 17

Compression:

Stored size: 1.57 KB

Contents

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?
      CFGS.user.source.sql.adapter   = 'sqlite'
      CFGS.user.source.sql.database  = File.join(Config::Root, 'sqlite.db')
      CFGS.user.source.sql.table     = 'devices'
      CFGS.user.source.sql.map.name  = 'name'
      CFGS.user.source.sql.map.model = 'rancid'
      CFGS.save :user
      raise NoConfig, 'no source sql config, edit ~/.config/oxidized/config'
    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[sql_column.to_sym] }
      keys[:model] = map_model keys[:model] if keys.key? :model

      # map node specific vars
      vars = {}
      @cfg.vars_map.each { |key, sql_column| vars[key.to_sym] = node[sql_column.to_sym] }
      keys[:vars] = vars unless vars.empty?

      nodes << keys
    end
    db.disconnect
    nodes
  end

  private

  def initialize
    super
    @cfg = CFG.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
  end

end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
oxidized-0.9.0 lib/oxidized/source/sql.rb
oxidized-0.8.1 lib/oxidized/source/sql.rb
oxidized-0.8.0 lib/oxidized/source/sql.rb
oxidized-0.7.2 lib/oxidized/source/sql.rb
oxidized-0.7.1 lib/oxidized/source/sql.rb
oxidized-0.7.0 lib/oxidized/source/sql.rb
oxidized-0.6.0 lib/oxidized/source/sql.rb
oxidized-0.5.0 lib/oxidized/source/sql.rb
oxidized-0.4.1 lib/oxidized/source/sql.rb
oxidized-0.4.0 lib/oxidized/source/sql.rb
oxidized-0.3.0 lib/oxidized/source/sql.rb
oxidized-0.2.4 lib/oxidized/source/sql.rb
oxidized-0.2.3 lib/oxidized/source/sql.rb
oxidized-0.2.2 lib/oxidized/source/sql.rb
oxidized-0.2.1 lib/oxidized/source/sql.rb
oxidized-0.2.0 lib/oxidized/source/sql.rb
oxidized-0.1.1 lib/oxidized/source/sql.rb