Module: PuppetX::EnterpriseModules::OraInstall::GetHomes

Included in:
Facts
Defined in:
lib/puppet_x/enterprisemodules/ora_install/get_homes.rb

Overview

Docs

Instance Method Summary collapse

Instance Method Details

#cleanup_opatch_residue(homes) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/puppet_x/enterprisemodules/ora_install/get_homes.rb', line 161

def cleanup_opatch_residue(homes)
  # TODO: This is duplicate code from facts.rb. We need to make this more common
  # Some older versions of Opatch leave files and directories arround
  # When running opatch often, this causes some issues. This is to make
  # sure all files created by running Opatch as part of the fact, are remopved
  basename = Time.now.strftime('%Y-%m-%d_%H-%M-*%p')
  FileUtils.rm_f("/OraInstall2018#{basename}")
  homes.each { |home| FileUtils.rm_f("#{home}/cfgtoollogs/opatch/lsinv/lsinventory#{basename}.txt") }
end

#default_database_sidObject



10
11
12
# File 'lib/puppet_x/enterprisemodules/ora_install/get_homes.rb', line 10

def default_database_sid
  Hash[*oratab_content.scan(/^(?!#)(.*):(.*):.*$/).flatten].keys.first
end

#first_non_root_file(dir) ⇒ Object



60
61
62
# File 'lib/puppet_x/enterprisemodules/ora_install/get_homes.rb', line 60

def first_non_root_file(dir)
  Dir.new(dir).each { |file| break file if File.stat("#{dir}/#{file}").uid.nonzero? }
end

#get_homes(path) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/puppet_x/enterprisemodules/ora_install/get_homes.rb', line 68

def get_homes(path)
  return [] if path.nil? || !File.exist?("#{path}/ContentsXML/inventory.xml")

  file = File.read("#{path}/ContentsXML/inventory.xml")
  doc = REXML::Document.new(file)
  homes = []

  doc.elements.each('/INVENTORY/HOME_LIST/HOME') do |element|
    next if element.elements[1]&.name == 'DEPHOMELIST'

    home = element.attributes['LOC']
    if element.attributes['REMOVED'] == 'T'
      Puppet.debug "Home #{home} marked as removed in Oracle inventory; skipping for ora_install_homes fact."
      next
    end

    unless Dir.exist?(home)
      Puppet.warning "Directory #{home} found in oracle inventory, does not exist; skipping for ora_install_homes fact."
      next
    end

    skip_items = %w[plugins jdk OraPlaceHolderDummyHome]
    homes << home unless home.nil? || skip_items.any? { |item| home.include?(item) }
  end

  homes
end

#get_orainst_locObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/puppet_x/enterprisemodules/ora_install/get_homes.rb', line 96

def get_orainst_loc
  if os == 'windows'
    begin
      Win32::Registry::HKEY_LOCAL_MACHINE.open('SOFTWARE\\Oracle').read('inst_loc')[1]
    rescue StandardError
      ''
    end
  else
    return '' unless File.exist?("#{ora_invdir}/oraInst.loc")

    str = ''
    output = File.read("#{ora_invdir}/oraInst.loc")
    output.split(/\r?\n/).each do |item|
      str = item[14, 50] if item =~ /^inventory_loc/
    end
    str

  end
end

#group_for_file(home) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/puppet_x/enterprisemodules/ora_install/get_homes.rb', line 18

def group_for_file(home)
  #
  # To run opatch, we need the owner of the grid and oracle directory. The mechanism
  # we use to get this user, is to fetch the owner of the bin directory in the grid or
  # oracle home. Sometimes however, these directories also contain files with root as owner.
  # since root can never be the user of the grid or oracle processen, we skip those.
  #
  if os == 'windows'
    if File.exist?("#{get_orainst_loc}/oraInst.loc")
      file = File.read("#{get_orainst_loc}/oraInst.loc")
      file.scan(/inst_group=(.*)/).flatten.first
    end
  else
    a_file = first_non_root_file(home)
    if a_file
      gid = File.stat("#{home}/#{a_file}").gid
      Etc.getgrgid(gid).name
    end
  end
end

#installed_patches(resources = nil) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/puppet_x/enterprisemodules/ora_install/get_homes.rb', line 124

def installed_patches(resources = nil)
  if resources
    oracle_homes(resources).collect do |oracle_home|
      patches_in_home(oracle_home, ora_invdir, resources)
    end.flatten
  else
    oracle_homes.collect do |oracle_home|
      patches_in_home(oracle_home, ora_invdir)
    end.flatten
  end
end

#ora_invdirObject



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/puppet_x/enterprisemodules/ora_install/get_homes.rb', line 191

def ora_invdir
  case os
  when 'Linux'
    '/etc'
  when 'SunOS'
    '/var/opt/oracle'
  when 'windows'
    get_orainst_loc
  else
    '/etc'
  end
end

#oracle_home_for(sid) ⇒ Object



14
15
16
# File 'lib/puppet_x/enterprisemodules/ora_install/get_homes.rb', line 14

def oracle_home_for(sid)
  Hash[*oratab_content.scan(/^(?!#)(.*):(.*):.*$/).flatten].select { |k, _v| k == sid }.values.first
end

#oracle_homes(resources = nil) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/puppet_x/enterprisemodules/ora_install/get_homes.rb', line 116

def oracle_homes(resources = nil)
  if resources
    resources.map { |_k, v| v.oracle_product_home_dir }.uniq
  else
    get_homes(get_orainst_loc)
  end
end

#oratab_contentObject



179
180
181
182
183
184
185
# File 'lib/puppet_x/enterprisemodules/ora_install/get_homes.rb', line 179

def oratab_content
  if File.exist?(oratab_file)
    File.read(oratab_file)
  else
    ''
  end
end

#oratab_exists?Boolean

Returns:

  • (Boolean)


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

def oratab_exists?
  if os == 'windows'
    false
  else
    File.exist?(oratab_file)
  end
end

#oratab_fileObject



187
188
189
# File 'lib/puppet_x/enterprisemodules/ora_install/get_homes.rb', line 187

def oratab_file
  "#{ora_invdir}/oratab"
end

#osObject



64
65
66
# File 'lib/puppet_x/enterprisemodules/ora_install/get_homes.rb', line 64

def os
  Facter.value(:kernel)
end

#patches_in_home(oracle_product_home_dir, orainst_dir, resources = nil) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/puppet_x/enterprisemodules/ora_install/get_homes.rb', line 136

def patches_in_home(oracle_product_home_dir, orainst_dir, resources = nil)
  if !resources
    os_user = user_for_file(oracle_product_home_dir)
    if os_user
      Dir.chdir(oracle_product_home_dir) do
        full_command = if os == 'windows'
                         "#{oracle_product_home_dir}\\OPatch\\opatch.bat lspatches -jdk #{oracle_product_home_dir}\\jdk -oh #{oracle_product_home_dir}"
                       else
                         "#{oracle_product_home_dir}/OPatch/opatch lspatches -jdk #{oracle_product_home_dir}/jdk -oh #{oracle_product_home_dir} -invPtrLoc #{orainst_dir}/oraInst.loc"
                       end
        raw_list = Puppet::Util::Execution.execute(full_command, :failonfail => true, :uid => os_user, :combine => true)
        Puppet.debug "\n#{raw_list}"
        cleanup_opatch_residue([oracle_product_home_dir])
        raw_list.scan(/^(\d+);(.*)$/).collect { |e| { :name => "#{oracle_product_home_dir}:#{e[0]}", :comment => e[1] } }
      end
    else
      []
    end
  else
    ora_install_homes = Facter.value(:ora_install_homes) || {}
    patches = ora_install_homes.dig('installed_patches', oracle_product_home_dir)
    patches.nil? ? [] : patches.collect { |p| { :name => "#{oracle_product_home_dir}:#{p}", :comment => '', :ensure => :present } }
  end
end

#user_for_file(home) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/puppet_x/enterprisemodules/ora_install/get_homes.rb', line 39

def user_for_file(home)
  #
  # To run opatch, we need the owner of the grid and oracle directory. The mechanism
  # we use to get this user, is to fetch the owner of the bin directory in the grid or
  # oracle home. Sometimes however, these directories also contain files with root as owner.
  # since root can never be the user of the grid or oracle processen, we skip those.
  #
  if os == 'windows'
    group = group_for_file(home)
    users_in_group = `net localgroup #{group}`
    users = users_in_group.scan(/(^[a-zA-Z]*$)/).flatten
    users.reject!(&:empty?).reject! { |u| u == 'Members' }.first
  else
    a_file = first_non_root_file(home)
    if a_file
      uid = File.stat("#{home}/#{a_file}").uid
      Etc.getpwuid(uid).name
    end
  end
end