Module: PuppetX::EnterpriseModules::Oracle::Access

Includes:
EasyType::Helpers, Settings
Included in:
OracleFeature, Puppet::Parameter::OracleProfileProperty, Resources::Generic, TitleParser
Defined in:
lib/puppet_x/enterprisemodules/oracle/access.rb

Overview

Docs

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Settings

#asm_sid?, #asm_sids, #configuration, #configuration_for, #configuration_value_for, #container_db?, #database_sid?, #database_sids, #default_asm_sid, #default_database_sid, #default_sids, #local_pdb?, #mgmt_sid?, #mgmt_sids, #normal_db?, #num_default_asm_sids, #num_default_database_sids, #read_from_yaml, #registered_sids, #remote_sid?, #running_asm_sids, #running_database_sids, #running_db?, #running_mgmt_sids, #running_mt_database_sids, #running_nopdb_database_sids, #running_normal_database_sids, #running_pdb?, #running_primary_database_sids, #running_sids, #settings_file, #valid_asm_sid?, #valid_database_sid, #valid_sid?

Methods included from Information

#cached_sid_value, #cluster?, #cluster_instances, #containerdb?, #database_properties, #database_version, #db_create_file_dest, #db_domain, #db_for, #diagnostic_dest, #local_sid_for_db, #open_pdbs, #oracle_major_version, #oracle_managed_files_enabled?, #pdb?, #primary?, #rootdb?, #seeddb?, #sid_for, #value_for_init_param

Class Method Details

.included(parent) ⇒ Object



25
26
27
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 25

def self.included(parent)
  parent.extend(Access)
end

Instance Method Details

#add_sid_to(elements, sid) ⇒ Object



315
316
317
318
319
320
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 315

def add_sid_to(elements, sid)
  elements.collect do |e|
    e['SID'] = sid
    e
  end
end

#for_version(hash) ⇒ Object



29
30
31
32
33
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 29

def for_version(hash)
  hash.collect do |version, query|
    [lambda { |sid| oracle_version?(sid, version) }, query]
  end
end

#hex_to_raw(raw) ⇒ Object

This is the reverse of Oracle’s rawtohex TODO: See if we can make this more efficient



39
40
41
42
43
44
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 39

def hex_to_raw(raw)
  raw.chars.each_slice(2).collect do |slice|
    txt = slice[0] + slice[1]
    txt.hex.chr
  end.join
end

#instanceObject



363
364
365
366
367
368
369
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 363

def instance
  if resource[:instances].any?
    resource[:instances].select { |_inst, host| host.include? Facter.value('hostname') }.to_h.keys[0]
  else
    resource.name
  end
end

#module_nameObject



21
22
23
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 21

def module_name
  'ora_config'
end

#ora_autorequire(type, property) ⇒ Object



333
334
335
336
337
338
339
340
341
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 333

def ora_autorequire(type, property)
  autorequire(type) do
    if property.is_a?(Array)
      property.collect { |p| resource_list_for(p) }.flatten
    else
      resource_list_for(property)
    end
  end
end

#oracle_version?(sid, version) ⇒ Boolean

Check if the oracle version of the specified sid is equal to the specified version.

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 50

def oracle_version?(sid, version)
  return true if version.to_s == 'default'

  if version.instance_of?(String) || version.instance_of?(Symbol)
    oracle_major_version(sid) == version.to_s.to_i
  elsif version.instance_of?(Regexp)
    database_version(sid) =~ version
  else
    fail 'Invalid type specified for sql version query'
  end
end

#resource_list_for(property) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 343

def resource_list_for(property)
  value = property.is_a?(Proc) ? instance_exec(&property) : send(property)
  current_sid = self[:sid]
  if value.nil? || value.empty?
    nil
  elsif value.is_a?(::Array)
    if current_sid
      value.collect { |element| "#{element}@#{current_sid}" }.flatten
    else
      value
    end
  else
    current_sid ? "#{value}@#{current_sid}" : value
  end
end

#rman(command, options = {}) ⇒ Object

Use this function to execute Oracle statements

Parameters:

  • command (String)

    this is the commands to be given



271
272
273
274
275
276
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 271

def rman(command, options = {})
  @rman = Rman.new(options)
  sid = @rman.sid
  Puppet.debug "executing rman command '#{command}' on #{sid}"
  { 'OUTPUT' => @rman.execute(command), 'SID' => sid }
end

#rman_on_all_database_sids(command, parameters = {}) ⇒ Object

Use this function to execute Oracle statements on all running sid. This includes asm database

Parameters:

  • command (String)

    this is the commands to be given



198
199
200
201
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 198

def rman_on_all_database_sids(command, parameters = {})
  sids = running_database_sids
  rman_on_sids(sids, command, parameters)
end

#rman_on_sids(sids, command, parameters = {}) ⇒ Array

Run the sql commmand on all specified sids. If the sql is a String, The string will be used as the sql statement.

The command, can also be an array. In that case, every pair contains a landa followed by an sql sattement e.g

[
    lamda { a check},         'select * from tab',
    lamda { an other  check}, 'select * from users',
]

The check will be executed on every sid. You can use this for example to use different statements on different versions of oracle

Parameters:

  • sids (Array)

    Array of sids to run the command on

  • command (String)

    Sql statement to run on all sids

    Array

    Array of lambda’s and sql statements.

Returns:

  • (Array)

    The returned results



225
226
227
228
229
230
231
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 225

def rman_on_sids(sids, command, parameters = {})
  results = []
  sids.each do |sid|
    results += [rman(command, { :sid => sid }.merge(parameters))]
  end
  results
end

#safe_sql(command, options) ⇒ Object

Execute a user specified sql and ensure that after this sql, we are still connected to the correct container.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 283

def safe_sql(command, options)
  #
  # There are two cases where we don't want/need the check and just execute the sql:
  # 1) On an ASM sid
  # 2) When Oracle is not running
  #
  # regarding case 2, the exec might contain a start command we would like to execure without
  # any other checks
  #
  return sql(command, options) if asm_sid?(sid_from_resource) || (normal_db?(sid_from_resource) && !running_db?(sid_from_resource))

  #
  # First ensure the correct rootdb information, when we know we are connected to the correct database.
  #
  rootdb?(sid_from_resource)
  output = sql(command, options)
  #
  # In ora_exec you can run arbitrary sql scripts. Also scripts that can connect to
  # an other container. This can confuse the daemon. Therefor after we execute a script, we always connect to the correct
  # container again.
  begin
    sql(template('ora_config/ora_exec/ensure_correct_container.sql.erb', binding), options) if rootdb?(sid_from_resource) || containerdb?(sid_from_resource)
  rescue RuntimeError => e
    #
    # If the database was shutdown in the ora_exec command, the reconnect will get a ORA-01034: ORACLE not available
    # error. So if this error occurs, just let it be. All other errors will be raised again
    #
    raise unless e.message.include?('ORA-01034')
  end
  output
end

#select_statement(versioned_command, sid) ⇒ Object

Returns the first valid sql statement in the versioned command



183
184
185
186
187
188
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 183

def select_statement(versioned_command, sid)
  versioned_command.each do |version_proc, statement|
    return statement if version_proc.call(sid)
  end
  fail 'no valid version found'
end

#sid_from(source) ⇒ Object



359
360
361
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 359

def sid_from(source)
  source.sid.nil? ? default_database_sid : source.sid
end

#sid_from_resourceObject



329
330
331
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 329

def sid_from_resource
  sid_from(resource)
end

#sql(command, options = {}) ⇒ Object

Use this function to execute Oracle statements

Parameters:

  • command (String)

    this is the commands to be given



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 245

def sql(command, options = {})
  options[:timeout] ||= resource[:timeout] if timeout_specified
  parse = options.fetch(:parse, true)
  @sql = Sql.new(options)
  sid = @sql.sid
  statement = if versioned_statement?(command)
                select_statement(command, sid)
              else
                command
              end
  Puppet.debug "executing #{statement} on #{sid}"
  results = @sql.execute(statement)
  if parse
    add_sid_to(convert_csv_data_to_hash(results, [], :col_sep => ',', :converters => lambda { |f| f ? f.strip : nil }), sid)
  else
    results
  end
end

#sql_on_all_asm_sids(command, parameters = {}) ⇒ Object

Use this function to execute Oracle statements on all running asm sids.

Parameters:

  • command (String)

    this is the commands to be given



120
121
122
123
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 120

def sql_on_all_asm_sids(command, parameters = {})
  sids = running_asm_sids
  sql_on_sids(sids, command, parameters)
end

#sql_on_all_database_sids(command, parameters = {}) ⇒ Object

Use this function to execute Oracle statements on all running databases. This excludes asm database

Parameters:

  • command (String)

    this is the commands to be given



70
71
72
73
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 70

def sql_on_all_database_sids(command, parameters = {})
  sids = running_database_sids
  sql_on_sids(sids, command, parameters)
end

#sql_on_all_mgmt_sids(command, parameters = {}) ⇒ Object

Use this function to execute Oracle statements on all running mgmtdb sids.

Parameters:

  • command (String)

    this is the commands to be given



131
132
133
134
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 131

def sql_on_all_mgmt_sids(command, parameters = {})
  sids = running_mgmt_sids
  sql_on_sids(sids, command, parameters)
end

#sql_on_all_mt_sids(command, parameters = {}) ⇒ Object

Use this function to execute Oracle statements on all running multitenant databases. This excludes asm and normal databases

Parameters:

  • command (String)

    this is the commands to be given



109
110
111
112
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 109

def sql_on_all_mt_sids(command, parameters = {})
  sids = running_mt_database_sids
  sql_on_sids(sids, command, parameters)
end

#sql_on_all_normal_sids(command, parameters = {}) ⇒ Object

Use this function to execute Oracle statements on all running normal databases. This excludes asm and multitenant databases

Parameters:

  • command (String)

    this is the commands to be given



96
97
98
99
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 96

def sql_on_all_normal_sids(command, parameters = {})
  sids = running_normal_database_sids
  sql_on_sids(sids, command, parameters)
end

#sql_on_all_primary_database_sids(command, parameters = {}) ⇒ Object

Use this function to execute Oracle statements on all running databases. This excludes asm database

Parameters:

  • command (String)

    this is the commands to be given



83
84
85
86
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 83

def sql_on_all_primary_database_sids(command, parameters = {})
  sids = running_primary_database_sids
  sql_on_sids(sids, command, parameters)
end

#sql_on_all_sids(command, parameters = {}) ⇒ Object

Use this function to execute Oracle statements on all running sid. This includes asm database

Parameters:

  • command (String)

    this is the commands to be given



144
145
146
147
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 144

def sql_on_all_sids(command, parameters = {})
  sids = running_sids
  sql_on_sids(sids, command, parameters)
end

#sql_on_sids(sids, command, parameters = {}) ⇒ Array

Run the sql commmand on all specified sids. If the sql is a String, The string will be used as the sql statement.

The command, can also be an array. In that case, every pair contains a landa followed by an sql sattement e.g

[
    lamda { a check},         'select * from tab',
    lamda { an other  check}, 'select * from users',
]

The check will be executed on every sid. You can use this for example to use different statements on different versions of oracle

Parameters:

  • sids (Array)

    Array of sids to run the command on

  • command (String)

    Sql statement to run on all sids

    Array

    Array of lambda’s and sql statements.

Returns:

  • (Array)

    The returned results



171
172
173
174
175
176
177
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 171

def sql_on_sids(sids, command, parameters = {})
  results = []
  sids.each do |sid|
    results += sql(command, { :sid => sid }.merge(parameters))
  end
  results
end

#timeout_specifiedObject

This is a little hack to get a specified timeout value



323
324
325
326
327
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 323

def timeout_specified
  resource.timeout
rescue NameError
  nil
end

#versioned_statement?(command) ⇒ Boolean

Returns:

  • (Boolean)


234
235
236
# File 'lib/puppet_x/enterprisemodules/oracle/access.rb', line 234

def versioned_statement?(command)
  command.is_a?(::Array)
end