Module: Moose::Inventory::DB

Extended by:
DB
Included in:
DB
Defined in:
lib/moose_inventory/db/db.rb,
lib/moose_inventory/db/models.rb,
lib/moose_inventory/db/exceptions.rb

Overview

Module for DB-related functionality

Defined Under Namespace

Classes: Group, Groupvar, Host, Hostvar, MooseDBException

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



19
20
21
# File 'lib/moose_inventory/db/db.rb', line 19

def db
  @db
end

#exceptionsObject (readonly)

Returns the value of attribute exceptions.



21
22
23
# File 'lib/moose_inventory/db/db.rb', line 21

def exceptions
  @exceptions
end

#modelsObject (readonly)

Returns the value of attribute models.



20
21
22
# File 'lib/moose_inventory/db/db.rb', line 20

def models
  @models
end

Class Method Details

.connectObject




223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/moose_inventory/db/db.rb', line 223

def self.connect
  return unless @db.nil?

  adapter = Moose::Inventory::Config._settings[:config][:db][:adapter]
  adapter.downcase!

  case adapter
  when 'sqlite3'
    init_sqlite3

  when 'mysql'
    init_mysql

  when 'postgresql'
    init_postgresql

  else
    fail @exceptions[:moose],
         "database adapter #{adapter} is not yet supported."
  end
end

.create_tablesObject




172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/moose_inventory/db/db.rb', line 172

def self.create_tables # rubocop:disable Metrics/AbcSize
  unless @db.table_exists? :hosts
    @db.create_table(:hosts) do
      primary_key :id
      column :name, :text, unique: true
    end
  end

  unless @db.table_exists? :hostvars
    @db.create_table(:hostvars) do
      primary_key :id
      foreign_key :host_id
      column :name, :text
      column :value, :text
    end
  end

  unless @db.table_exists? :groups
    @db.create_table(:groups) do
      primary_key :id
      column :name, :text, unique: true
    end
  end

  unless @db.table_exists? :groups_groups
    @db.create_table(:groups_groups) do
      primary_key :id
      foreign_key :parent_id, :groups
      foreign_key :child_id,  :groups
    end
  end

  unless @db.table_exists? :groupvars
    @db.create_table(:groupvars) do
      primary_key :id
      foreign_key :group_id
      column :name, :text
      column :value, :text
    end
  end

  unless @db.table_exists? :groups_hosts
    @db.create_table(:groups_hosts) do
      primary_key :id
      foreign_key :host_id,  :hosts
      foreign_key :group_id, :groups
    end
  end
end

.initObject




24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/moose_inventory/db/db.rb', line 24

def self.init
  init_exceptions

  # If we allow init more than once, then the db connection is remade,
  # which changes Sequel:DATABASES[0], thereby invalidating the sequel
  # models.  This causes unexpected behavour. That is to say, because
  # of the way Sequel initializes models, this method is not idempotent.
  # In our single-shot application, this shouldn't be a problem. However,
  # our unit tests like to call init multiple times, which borks things.
  # So, we allow init only once, gated by whether @db is nil. In effect,
  # this means we pool the DB connection for the life of the application.
  # Again, not a problem for our one-shot app, but it may be an issue in
  # long-running code. Personally, I don't like this pooling regime -
  # perhaps I'm not understanding how it's supposed to be used?
  #
  # QUESTION: can the models be refreshed, to make then again valid? What if
  #       we "load" instead of "require" the models?
  # ANSWER: Nope, still borks even if we use a load.
  #
  # @db = nil            # <- fails for unit tests
  return unless @db.nil? # <- works for unit tests

  Sequel::Model.plugin :json_serializer
  connect
  create_tables

  # Make our models work
  Sequel::DATABASES[0] = @db
  require_relative 'models'
  # load( load_dir = File.join(File.dirname(__FILE__), "models.rb") )

  # For convenience
  @models = {}
  @models[:host]     = Moose::Inventory::DB::Host
  @models[:hostvar]  = Moose::Inventory::DB::Hostvar
  @models[:group]    = Moose::Inventory::DB::Group
  @models[:groupvar] = Moose::Inventory::DB::Groupvar

end

.init_exceptionsObject




65
66
67
68
# File 'lib/moose_inventory/db/db.rb', line 65

def self.init_exceptions
  @exceptions ||= {}
  @exceptions[:moose] ||= Moose::Inventory::DB::MooseDBException
end

.init_mysqlObject




270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/moose_inventory/db/db.rb', line 270

def self.init_mysql
  require 'mysql2'

  # Quick check that expected keys are at least present
  config = Moose::Inventory::Config._settings[:config][:db]
  [:host, :database, :user, :password].each do |key|
    if config[key].nil?
      fail @exceptions[:moose],
           "Expected key #{key} missing in mysql configuration"
    end
  end

  @db = Sequel.mysql2(user: config[:user],
                      password: config[:password],
                      host: config[:host],
                      database: config[:database])
end

.init_postgresqlObject




289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/moose_inventory/db/db.rb', line 289

def self.init_postgresql
  require 'pg'

  # Quick check that expected keys are at least present
  config = Moose::Inventory::Config._settings[:config][:db]
  [:host, :database, :user, :password].each do |key|
    if config[key].nil?
      fail @exceptions[:moose],
           "Expected key #{key} missing in postgresql configuration"
    end
  end

  @db = Sequel.postgres(user: config[:user],
                        password: config[:password],
                        host: config[:host],
                        database: config[:database])
end

.init_sqlite3Object




246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/moose_inventory/db/db.rb', line 246

def self.init_sqlite3 # rubocop:disable Metrics/AbcSize
  require 'sqlite3'
  require 'fileutils'

  # Quick check that expected keys are at least present & sensible
  config = Moose::Inventory::Config._settings[:config][:db]
  [:file].each do |key|
    if config[key].nil?
      fail @exceptions[:moose],
           "Expected key #{key} missing in sqlite3 configuration"
    end
  end
  config[:file].empty? && fail("SQLite3 DB 'file' cannot be empty")

  # Make sure the directory exists
  dbfile = File.expand_path(config[:file])
  dbdir = File.dirname(dbfile)
  FileUtils.mkdir_p(dbdir) unless Dir.exist?(dbdir)

  # Create and/or open the database file
  @db = Sequel.sqlite(dbfile)
end

.purgeObject




136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/moose_inventory/db/db.rb', line 136

def self.purge # rubocop:disable Metrics/AbcSize
  adapter = Moose::Inventory::Config._settings[:config][:db][:adapter]
  adapter.downcase!

  if adapter == 'sqlite3'
    # HACK: SQLite3 supposedly supports CASCADE, see
    # https://www.sqlite.org/foreignkeys.html#fk_actions
    # However, when we do a drop_table with :cascade=>true
    # on an sqlite3 database, it throws errors regarding
    # foreign keys constraints. Instead, the following is
    # less efficient, but does work.

    Group.all.each do |g|
      g.remove_all_hosts
      g.remove_all_groupvars
      g.remove_all_children
      g.destroy
    end

    Host.all.each do |h|
      h.remove_all_groups
      h.remove_all_hostvars
      h.destroy
    end

    Groupvar.all.each(&:destroy)
    Hostvar.all.each(&:destroy)

  else
    @db.drop_table(:hosts, :hostvars,
                   :groups, :groupvars, :group_hosts,
                   if_exists: true, cascade: true)
  end
end

.resetObject




124
125
126
127
128
129
# File 'lib/moose_inventory/db/db.rb', line 124

def self.reset
  fail('Database connection has not been established') if @db.nil?
  # @debug << 'reset'
  purge
  create_tables
end

.transactionObject




71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/moose_inventory/db/db.rb', line 71

def self.transaction
  fail('Database connection has not been established') if @db.nil?

  tries = 0

  begin
    @db.transaction(savepoint: true) do
      yield
    end

  rescue Sequel::DatabaseError => e
    # We want to rescue Sqlite3::BusyException.  But, sequel catches that
    # and re-raises it as Sequel::DatabaseError, with a message referencing
    # the original exception class

    # We look into e, to see whether it is a BusyException.  If not,
    # we re-raise immediately.
    raise unless e.message.include?('BusyException')

    # Looks like a BusyException, so we retry, after a random delay.
    tries += 1
    case tries
    when 1..10
      if Moose::Inventory::Config._confopts[:trace] == true
        STDERR.puts e.message
      end
      sleep rand
      retry
    else
      warn('The database appears to be locked by another process, and '\
           " did not become free after #{tries} tries. Giving up. ")

      # TODO: Some useful advice to the user, as to what to do about this error.
      raise
    end

  rescue @exceptions[:moose] => e
    warn 'An error occurred during a transaction, any changes have been rolled back.'

    if Moose::Inventory::Config._confopts[:trace] == true
      STDERR.puts $ERROR_INFO.backtrace
      abort("ERROR: #{e}")
    else
      abort("ERROR: #{e.message}")
    end

  rescue Exception => e
    warn 'An error occurred during a transaction, any changes have been rolled back.'
    raise e
  end
end