Class: Landlord::VirtualHost

Inherits:
Object
  • Object
show all
Defined in:
lib/landlord/virtual_host.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_name, options = nil) ⇒ VirtualHost

Instance Methods =====================================================



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/landlord/virtual_host.rb', line 41

def initialize(server_name, options = nil)
  @options = {
    :server_name => server_name.freeze
  }
  
  if (options)
    @options.merge!(options)
  end
  
  @options[:virtual_host_path] ||= File.expand_path(
    @options[:server_name],
    Landlord.config.virtual_host_base_dir
  )
end

Class Method Details

.eachObject

Class Methods ========================================================



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/landlord/virtual_host.rb', line 9

def self.each
  Dir.glob(File.expand_path("*.conf", Landlord.config.virtual_host_config_dir)).each do |path|
    config = {
      :server_name => File.basename(path).sub(/\.conf$/, ''),
      :aliases => [ ],
      :redirects => [ ]
    }
    
    File.open(path).readlines.each do |line|
      line.chomp!
      
      if (line.match(/\# VirtualHost (.*)/))
        aliases = $1.split(/\s+/)
        
        server_name = aliases.shift
        
        config[:aliases] += aliases
      elsif (line.match(/\# VirtualHost:Redirect (.*)/))
        redirects = $1.split(/\s+/)
        
        server_name = redirects.shift
        
        config[:redirects] += redirects
      end
    end
    
    yield(config)
  end
end

Instance Method Details

#add!Object



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

def add!
  File.open(self.config_file_path, 'w') do |f|
    f.write(self.config_file)
  end
  
  _virtual_host_path = @options[:virtual_host_path]
  
  Landlord.config.virtual_host_subdirs.each do |dir|
    _path = File.expand_path(
      dir,
      _virtual_host_path
    )
    
    unless (File.exist?(_path))  
      FileUtils.mkdir_p(_path)
      
      if (Landlord.config.chown_user and Landlord.config.chown_group)
        FileUtils.chown_R(
          Landlord.config.chown_user,
          Landlord.config.chown_group,
          _path
        )
      end
    end
  end
end

#config_file(type = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/landlord/virtual_host.rb', line 99

def config_file(type = nil)
  Handlebar::Template.new(
    File.read(self.config_template_path(type))
  ).render(
    :server_name => @options[:server_name],
    :virtual_host_path => @options[:virtual_host_path],
    :server_aliases => @options[:aliases] && @options[:aliases].join(' '),
    :server_redirects => @options[:redirects] && @options[:redirects].collect do |redirect|
      {
        :server_name => @options[:server_name],
        :redirect => redirect
      }
    end,
    :rack_env => @options[:rack_env],
    :rails_env => @options[:rails_env]
  ).sub(/\n+\Z/, "\n")
end

#config_file_path(group = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/landlord/virtual_host.rb', line 60

def config_file_path(group = nil)
  File.expand_path(
    "#{@options[:server_name]}.conf",
    case (group)
    when :disabled
      Landlord.config.disabled_virtual_host_config_dir
    else
      Landlord.config.virtual_host_config_dir
    end
  )
end

#config_template_path(type = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/landlord/virtual_host.rb', line 72

def config_template_path(type = nil)
  conf_file = [
    'virtual_host',
    type || Landlord.config.server_type,
    'conf'
  ].join('.')

  Landlord.config.virtual_host_template or
    [
      File.expand_path(
        conf_file,
        Landlord.config.template_dir
      ),
      File.expand_path(
        File.join(
          '..',
          '..',
          'templates',
          conf_file
        ),
        File.dirname(__FILE__)
      )
    ].find do |path|
      File.exist?(path)
    end
end

#disable!Object



127
128
129
130
131
132
133
# File 'lib/landlord/virtual_host.rb', line 127

def disable!
  _config_file_path = self.config_file_path
  
  if (File.exist?(_config_file_path))
    File.rename(_config_file_path, self.config_file_path(:disabled))
  end
end

#enable!Object



117
118
119
120
121
122
123
124
125
# File 'lib/landlord/virtual_host.rb', line 117

def enable!
  _config_file_path = self.config_file_path
  
  if (File.exist?(_config_file_path))
    # File already exists?!
  else
    File.rename(self.config_file_path(:disabled), _config_file_path)
  end
end

#remove!Object



162
163
164
165
166
167
168
# File 'lib/landlord/virtual_host.rb', line 162

def remove!
  _config_file_path = self.config_file_path

  if (File.exist?(_config_file_path))
    File.unlink(_config_file_path)
  end
end

#server_nameObject



56
57
58
# File 'lib/landlord/virtual_host.rb', line 56

def server_name
  @options[:server_name]
end