Module: BindLogAnalyzer::Connector

Included in:
Base
Defined in:
lib/bind_log_analyzer/connector.rb

Overview

The module which provides connection facility

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.connectObject

Establishes the connection to the database



32
33
34
# File 'lib/bind_log_analyzer/connector.rb', line 32

def self.connect
  ActiveRecord::Base.establish_connection(@database_params)
end

.establish_connection(database_params, logger) ⇒ Object

Setups the database params calling #setup_db_confs and log level calling #set_log_level then connects to the database

Parameters:

  • database_params (Hash, String)

    The path to the database configurations file or a hash containing such informations

  • logfile (String)

    The path to the file containing the Bind’s logs to analyze



53
54
55
56
57
# File 'lib/bind_log_analyzer/connector.rb', line 53

def self.establish_connection(database_params, logger)
  BindLogAnalyzer::Connector.setup_db_confs(database_params, logger)  
  BindLogAnalyzer::Connector.connect
  ActiveRecord::Base.logger = logger
end

.setup_db_confs(database_params, logger) ⇒ Object

Analyzes the database_params param and extracts the database parameters. Raises BindLogAnalyzer::DatabaseConfsNotValid if it can’t find any useful information

Parameters:

  • database_params (Hash, String)

    The path to the database configurations file or a hash containing such informations



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bind_log_analyzer/connector.rb', line 62

def self.setup_db_confs(database_params, logger)
  if database_params
    if database_params.instance_of?(Hash)
      logger.debug "Setting up database with confs: #{database_params}"
      @database_params = database_params
    else
      # Load the yaml file
      if FileTest.exists?(database_params)
        logger.debug "Setting up database using file #{database_params}"
        @database_params = YAML::load(File.open(database_params))['database']
      else
        logger.fatal "The indicated YAML file doesn't exist or is invalid"
        raise BindLogAnalyzer::DatabaseConfsNotValid, "The indicated YAML file doesn't exist or is invalid"
      end
    end
  else
    # Tries to find the yaml file or prints an error
    filename = './database.yml'
    if FileTest.exists?(filename)
        logger.info "No database configurations provided, now trying using #{filename}..."
        @database_params = YAML::load(File.open(filename))['database']
    else
      logger.fatal "Can't find valid database configurations"
      raise BindLogAnalyzer::DatabaseConfsNotValid, "Can't find valid database configurations"
    end
  end
end

Instance Method Details

#connected?true, false

Shows the status of the connection to the database

Returns:

  • (true, false)

    The status of the connection to the database



38
39
40
# File 'lib/bind_log_analyzer/connector.rb', line 38

def connected?
  ActiveRecord::Base.connected?
end

#load_environmentObject

Loads the ActiveRecord models



43
44
45
46
47
48
# File 'lib/bind_log_analyzer/connector.rb', line 43

def load_environment
  Dir.glob('./lib/models/*').each do |r|
    @log.debug "Requiring model #{r}"
    require r
  end
end

#migrate_tablesObject

Launches ActiveRecord migrations



25
26
27
28
29
# File 'lib/bind_log_analyzer/connector.rb', line 25

def migrate_tables
  ActiveRecord::Migration.verbose = true
  migrations_dir = File.join(File.dirname(__FILE__), '..', '..', 'db/migrate')
  ActiveRecord::Migrator.migrate migrations_dir
end

#setup_db(database_params, setup_database = false) ⇒ Object

Main connection method which connects to the database, launches migrations if requested, loads the Log ActiveRecord model and setups the logger.

Parameters:

  • database_params (Hash)

    The database params and credentials

  • setup_database (true, false) (defaults to: false)

    If true launches the migrations of the database



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bind_log_analyzer/connector.rb', line 12

def setup_db(database_params, setup_database = false)
  BindLogAnalyzer::Connector.setup_db_confs(database_params, @log)

  BindLogAnalyzer::Connector.connect

  migrate_tables if setup_database

  self.load_environment

  ActiveRecord::Base.logger = @log
end