Class: Passenger::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/passenger/config.rb

Constant Summary collapse

VERSION =
'0.8.4'
@@marker_string =
"#----- #{File.basename($0, '.rb')} marker line -- DO NOT DELETE -----#"
@@apachectl =
'apachectl'
@@passenger_config =
'passenger-config'
@@user_re =
/^\s*User\s+(\S+)/i
@@group_re =
/^\s*Group\s+(\S+)/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p) ⇒ Config

Returns a new instance of Config.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/passenger/config.rb', line 29

def initialize(p)
  self.root = p[:root]
  @domain = (p[:domain] || '.dev').sub(/^\.*/,'.')
  @hosts = p[:hosts] || '/etc/hosts'
  @ip = p[:ip] || '127.0.0.1'

  @check_for_user = lambda {|l|
    if m = l.match(@@user_re)
      @user = m[1]
    end
  }
  @check_for_group = lambda {|l|
    if m = l.match(@@user_re)
      @group = m[1]
    end
  }

  read_conf
  check_root_perms

  setup_vhost
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



17
18
19
# File 'lib/passenger/config.rb', line 17

def domain
  @domain
end

#hostsObject (readonly)

Returns the value of attribute hosts.



19
20
21
# File 'lib/passenger/config.rb', line 19

def hosts
  @hosts
end

#ipObject (readonly)

Returns the value of attribute ip.



18
19
20
# File 'lib/passenger/config.rb', line 18

def ip
  @ip
end

Instance Method Details

#consolidateObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/passenger/config.rb', line 52

def consolidate
  _hosts = list_hosts
  _vhosts = list_vhosts

  _hosts.each_index do |i|
    case _hosts[i] <=> _vhosts[i]
    when 0
      next
    when 1
      puts "Missing from #{hosts}: #{_vhosts[i]}"
      _hosts.insert(i, _vhosts[i])
    when -1
      puts "Missing from #{passenger_conf}: #{_hosts[i]}"
      _vhosts.insert(i, _hosts[i])
    end
  end
end

#update_apacheObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/passenger/config.rb', line 94

def update_apache
  File.open(passenger_conf, 'r+') do |f|
    f.flock File::LOCK_EX
    catch(:out) do
      f.each("\n\n") do |ir|
        if ir == @vhost_entry
          f.seek 0, IO::SEEK_END
          throw :out
        end
      end
      f.puts @vhost_entry
    end
  end
  system("#{@@apachectl} restart")
end

#update_hostsObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/passenger/config.rb', line 70

def update_hosts
  _had_domain_line = false
  File.open(hosts, 'r+') do |f|
    f.flock File::LOCK_EX
    _buffer = []
    f.each do |l|
      if l.match(domain_re)		# add it to the same line that the other #{domain} entries are on
        _had_domain_line = true
        unless l.match(host_re)	# already exists - don't re-add
          l = "#{l.chomp} #{host}\n"
        end
      end
      _buffer << l
    end

    unless _had_domain_line			# if there was no #{domain} line then just add it to the end
      _buffer << "#{ip} #{host}\n"
    end
    f.seek 0
    f.truncate 0
    f << _buffer
  end
end