Class: PuppetX::EnterpriseModules::Oracle::SqlplusCommand

Inherits:
Command
  • Object
show all
Includes:
EasyType::Encryption, EasyType::Template, Settings
Defined in:
lib/puppet_x/enterprisemodules/oracle/sqlplus_command.rb

Overview

Docs<<<<<<< HEAD

Direct Known Subclasses

SqlplusDaemon

Instance Attribute 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, included, #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, included, #local_sid_for_db, #open_pdbs, #oracle_major_version, #oracle_managed_files_enabled?, #pdb?, #primary?, #rootdb?, #seeddb?, #sid_for, #value_for_init_param

Constructor Details

#initialize(options = {}) ⇒ SqlplusCommand

Returns a new instance of SqlplusCommand.



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/puppet_x/enterprisemodules/oracle/sqlplus_command.rb', line 38

def initialize(options = {})
  @failonsqlfail     = options.fetch(:failonsqlfail, :true)
  @catch_errors      = options.fetch(:catch_errors) { /SP\d-\d{4}:.*/ }
  @catch_extra_errors = options[:catch_extra_errors]
  @catch_errors      = Regexp.union(@catch_errors, @catch_extra_errors) if @catch_extra_errors
  @parse             = options.fetch(:parse, true)
  super('sqlplus -S /nolog ', options, SQLPLUS_VALID_OPTIONS)
  #
  # Set all values based on settings
  #
  @sys_username           = configuration_value_for(@sid, 'user')
  @encrypted_sys_password = configuration_value_for(@sid, 'password')
  @sys_password           = @sys_password.nil? || @sys_password.empty? ? decrypted_value(@encrypted_sys_password) : ''
  # rubocop:disable Lint/UselessAssignment
  os_user                 = configuration_value_for(@sid, 'os_user')
  # rubocop:enable Lint/UselessAssignment
  encrypted_os_user_pwd   = configuration_value_for(@sid, 'os_user_pwd')
  @os_user_pwd            = @os_user_pwd.nil? || @os_user_pwd.empty? ? decrypted_value(encrypted_os_user_pwd) : ''
  @oracle_home            = configuration_value_for(@sid, 'oracle_home')
  @settings_cdb           = configuration_value_for(@sid, 'cdb')
  @syspriv                = configuration_value_for(@sid, 'syspriv')
  @contained_by           = configuration_value_for(@sid, 'contained_by')
  @daemonized             = options.fetch(:daemonized) { configuration_value_for(@sid, 'daemonized') }
  @daemonized = false if @daemonized.to_s.empty?
  @host_sid = if @contained_by.nil? || @contained_by.empty?
                @sid
              else
                @contained_by
              end
  #
  # Validate values
  #
  fail "Invalid password specified for user #{@sys_username}" if @sys_password.include? '@'

  connect_string = configuration_value_for(@sid, 'connect_string')
  if connect_string.empty?
    @connect_string = ''
  else
    if @contained_by.nil? || @contained_by.empty?
      node = connect_string.split(/\W/).first
      ::Entitlement::Entitlement.entitled?("ora_remote_access##{node}#access")
    end
    @connect_string = "@#{connect_string}"
  end
end

Instance Attribute Details

#catch_errorsObject (readonly)

Returns the value of attribute catch_errors.



36
37
38
# File 'lib/puppet_x/enterprisemodules/oracle/sqlplus_command.rb', line 36

def catch_errors
  @catch_errors
end

#catch_extra_errorsObject (readonly)

Returns the value of attribute catch_extra_errors.



36
37
38
# File 'lib/puppet_x/enterprisemodules/oracle/sqlplus_command.rb', line 36

def catch_extra_errors
  @catch_extra_errors
end

#daemonizedObject (readonly)

Returns the value of attribute daemonized.



36
37
38
# File 'lib/puppet_x/enterprisemodules/oracle/sqlplus_command.rb', line 36

def daemonized
  @daemonized
end

#failonsqlfailObject (readonly)

Returns the value of attribute failonsqlfail.



36
37
38
# File 'lib/puppet_x/enterprisemodules/oracle/sqlplus_command.rb', line 36

def failonsqlfail
  @failonsqlfail
end

#os_userObject (readonly)

Returns the value of attribute os_user.



36
37
38
# File 'lib/puppet_x/enterprisemodules/oracle/sqlplus_command.rb', line 36

def os_user
  @os_user
end

#parseObject (readonly)

Returns the value of attribute parse.



36
37
38
# File 'lib/puppet_x/enterprisemodules/oracle/sqlplus_command.rb', line 36

def parse
  @parse
end

#passwordObject (readonly)

Returns the value of attribute password.



36
37
38
# File 'lib/puppet_x/enterprisemodules/oracle/sqlplus_command.rb', line 36

def password
  @password
end

#sidObject (readonly)

Returns the value of attribute sid.



36
37
38
# File 'lib/puppet_x/enterprisemodules/oracle/sqlplus_command.rb', line 36

def sid
  @sid
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



36
37
38
# File 'lib/puppet_x/enterprisemodules/oracle/sqlplus_command.rb', line 36

def timeout
  @timeout
end

#usernameObject (readonly)

Returns the value of attribute username.



36
37
38
# File 'lib/puppet_x/enterprisemodules/oracle/sqlplus_command.rb', line 36

def username
  @username
end

Instance Method Details

#command_string(arguments = '') ⇒ Object



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

def command_string(arguments = '')
  "su - #{@os_user} -c \"cd #{working_dir};export PERL5LIB=$(ls -d #{@oracle_home}/perl/lib/[0-9]*);export NLS_LANG=english;export ORACLE_SID=#{@host_sid};export ORACLE_HOME=#{@oracle_home};unset TNS_ADMIN;unset TWO_TASK;export PATH=#{@oracle_home}/bin:$PATH; #{@command} #{arguments}\""
end

#execute(arguments, powershell_script = '') ⇒ Object



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
# File 'lib/puppet_x/enterprisemodules/oracle/sqlplus_command.rb', line 88

def execute(arguments, powershell_script = '')
  options = { :failonfail => true, :combine => true }
  value = ''
  command = if Puppet::Util::Platform.windows?
              %(powershell -File "#{powershell_script}")
            else
              command_string(arguments)
            end
  within_time(@timeout) do
    Puppet.debug "sqlplus_command.rb: Executing #{@command} command: #{arguments} as user #{os_user} outside of the daemon"
    value = Puppet::Util::Execution.execute(command, options)
    Puppet.debug "sqlplus_command.rb: Output:\n #{value}"
  end
  value.encode!('UTF-8', :invalid => :replace, :undef => :replace)
  output_file = Tempfile.new(['sql', '.log'])
  ObjectSpace.undefine_finalizer(output_file) # Don't delete the file
  output_file.write(value)
  output_file.close
  FileUtils.chown(@os_user, nil, output_file.path)
  FileUtils.chmod(0o600, output_file.path)
  FileUtils.chown(@os_user, nil, output_file.path)
  FileUtils.chmod(0o600, output_file.path)
  path = output_file.path
  @@created_files << path
  Puppet.debug "sqlplus_command.rb: SQL output saved to #{output_file.path}"
  scan_for_errors(value) if @failonsqlfail == :true
  value
end

#execute_sql_command(command, output_file) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/puppet_x/enterprisemodules/oracle/sqlplus_command.rb', line 117

def execute_sql_command(command, output_file)
  #
  # Do the stuff
  #
  Puppet.debug "sqlplus_command.rb: Executing sql statement :\n #{command}"
  script = command_file(template('ora_config/ora_config/execute.sql.erb', binding))
  if Puppet::Util::Platform.windows?
    # rubocop:disable Lint/UselessAssignment
    powershell_script = command_file(template('ora_config/ora_config/sqlplus_command.ps1.erb', binding), type = '.ps1')
    # rubocop:enable Lint/UselessAssignment
    Puppet.debug "sqlplus_command.rb: Using powershell script: #{powershell_script}"
  end
  if username
    Puppet.debug "sqlplus_command.rb: Connecting to oracle_sid #{@sid} with connect_string \"#{@connect_string}\" as user #{username} and privilege #{@syspriv}"
  else
    Puppet.debug "sqlplus_command.rb: Connecting to oracle_sid #{@sid} with connect_string \"#{@connect_string}\" as user #{@sys_username} and privilege #{@syspriv}"
  end
  execute("@#{script}", powershell_script)
  content = File.read(output_file)
  content.encode!('UTF-8', :invalid => :replace, :undef => :replace)
  content
end