Class: OraUtils::OraTab

Inherits:
Object
  • Object
show all
Defined in:
lib/orabase/utils/ora_tab.rb

Constant Summary collapse

ASM_REGXP =
/^\+ASM\d*$/
NON_ASM_REGXP =
/^(?:(?!\+ASM\d*).)*$/
DEFAULT_CONTENT =
<<-EOD
#
# This file is used by ORACLE utilities.  It is created by root.sh
# and updated by either Database Configuration Assistant while creating
# a database or ASM Configuration Assistant while creating ASM instance.

# A colon, ':', is used as the field terminator.  A new line terminates
# the entry.  Lines beginning with a pound sign, '#', are comments.
#
# Entries are of the form:
#   $ORACLE_SID:$ORACLE_HOME:<N|Y>:
#
# The first and second fields are the system identifier and home
# directory of the database respectively.  The third filed indicates
# to the dbstart utility that the database should , "Y", or should not,
# "N", be brought up at system boot time.
#
# Multiple entries with the same $ORACLE_SID are not allowed.
#
#
EOD

Instance Method Summary collapse

Constructor Details

#initialize(file = default_file) ⇒ OraTab

Returns a new instance of OraTab.



28
29
30
31
# File 'lib/orabase/utils/ora_tab.rb', line 28

def initialize(file = default_file)
  fail "oratab #{file} not found. Probably Oracle not installed" unless File.exists?(file)
  @oratab = file
end

Instance Method Details

#add_new_entry(sid, home, start) ⇒ Object



33
34
35
# File 'lib/orabase/utils/ora_tab.rb', line 33

def add_new_entry(sid, home, start)
  write( append_new_entry(sid, home, start))
end

#append_new_entry(sid, home, start) ⇒ Object



43
44
45
# File 'lib/orabase/utils/ora_tab.rb', line 43

def append_new_entry(sid, home, start)
  "#{oratab_content}\n#{sid}:#{home}:#{start}\n"
end

#asm_sidsObject



84
85
86
# File 'lib/orabase/utils/ora_tab.rb', line 84

def asm_sids
  sids.select{|sid| sid =~ ASM_REGXP}
end

#database_sidsObject



88
89
90
# File 'lib/orabase/utils/ora_tab.rb', line 88

def database_sids
  sids.select{|sid| sid =~ NON_ASM_REGXP}
end

#default_asm_sidObject



112
113
114
# File 'lib/orabase/utils/ora_tab.rb', line 112

def default_asm_sid
  asm_sids.first || ''
end

#default_database_sidObject



108
109
110
# File 'lib/orabase/utils/ora_tab.rb', line 108

def default_database_sid
  database_sids.first || ''
end

#ensure_entry(sid, home, start) ⇒ Object



37
38
39
40
41
# File 'lib/orabase/utils/ora_tab.rb', line 37

def ensure_entry(sid, home, start)
  unless valid_sid?(sid)
    add_new_entry(sid, home, start)
  end
end

#entriesObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/orabase/utils/ora_tab.rb', line 57

def entries
  values = []
  File.open(@oratab) do | oratab|
    oratab.each_line do | line|
      content = [:sid, :home, :start].zip(line.split(':'))
      values << Hash[content] unless comment?(line)
    end
  end
  values
end

#oratab_contentObject



51
52
53
54
55
# File 'lib/orabase/utils/ora_tab.rb', line 51

def oratab_content
  File.open(default_file) {|f| f.read}
rescue Errno::ENOENT
  DEFAULT_CONTENT
end

#running_asm_sidsObject



102
103
104
105
106
# File 'lib/orabase/utils/ora_tab.rb', line 102

def running_asm_sids
  asm_sids.select do |sid|
    `pgrep -f ^asm_pmon_\\\\#{sid}$` != ''
  end
end

#running_database_sidsObject



96
97
98
99
100
# File 'lib/orabase/utils/ora_tab.rb', line 96

def running_database_sids
  database_sids.select do |sid|
    `pgrep -f "^(ora|xe)_pmon_#{sid}$"` != ''
  end
end

#running_sidsObject



92
93
94
# File 'lib/orabase/utils/ora_tab.rb', line 92

def running_sids
  running_database_sids + running_asm_sids
end

#sidsObject



80
81
82
# File 'lib/orabase/utils/ora_tab.rb', line 80

def sids
  entries.collect{|i| i[:sid]}
end

#valid_asm_sid?(sid) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/orabase/utils/ora_tab.rb', line 72

def valid_asm_sid?(sid)
  asm_sids.include?(sid)
end

#valid_database_sid(sid) ⇒ Object



76
77
78
# File 'lib/orabase/utils/ora_tab.rb', line 76

def valid_database_sid(sid)
  database_sids.include?(sid)
end

#valid_sid?(sid) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/orabase/utils/ora_tab.rb', line 68

def valid_sid?(sid)
  sids.include?(sid)
end

#write(content) ⇒ Object



47
48
49
# File 'lib/orabase/utils/ora_tab.rb', line 47

def write(content)
  File.open(default_file, 'w') {|f| f.write(content)}
end