apache_config 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +14 -0
- data/VERSION +1 -0
- data/lib/apache_config/node.rb +37 -0
- data/lib/apache_config/virtual_host.rb +25 -0
- data/lib/apache_config/write_back_array.rb +13 -0
- data/lib/apache_config.rb +65 -0
- data/test/apache_config_test.rb +45 -0
- metadata +68 -0
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |gemspec|
|
4
|
+
gemspec.name = "apache_config"
|
5
|
+
gemspec.summary = "Apache config parser and printer"
|
6
|
+
gemspec.description = "Parses Apache config files, allowing for changes and then can output again"
|
7
|
+
gemspec.email = "jan@openminds.be"
|
8
|
+
gemspec.homepage = "http://github.com/defv/apache_config"
|
9
|
+
gemspec.authors = ["Jan De Poorter"]
|
10
|
+
end
|
11
|
+
Jeweler::GemcutterTasks.new
|
12
|
+
rescue LoadError
|
13
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
14
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Apache
|
2
|
+
class Node
|
3
|
+
attr_accessor :name, :content, :children, :parent
|
4
|
+
|
5
|
+
def initialize name, content = nil
|
6
|
+
@name, @content = name, content
|
7
|
+
end
|
8
|
+
|
9
|
+
def [] name
|
10
|
+
r = Array(children).select { |child| child.name == name }
|
11
|
+
case r.size
|
12
|
+
when 0
|
13
|
+
self << Node.new(name)
|
14
|
+
when 1
|
15
|
+
r.first
|
16
|
+
else
|
17
|
+
r
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def << node
|
22
|
+
@children ||= []
|
23
|
+
@children << node
|
24
|
+
node.parent = self
|
25
|
+
|
26
|
+
node
|
27
|
+
end
|
28
|
+
|
29
|
+
def isRoot?
|
30
|
+
parent.nil?
|
31
|
+
end
|
32
|
+
|
33
|
+
def hasChildren?
|
34
|
+
children && children.any?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class VirtualHost
|
2
|
+
def initialize(config)
|
3
|
+
@config = config
|
4
|
+
end
|
5
|
+
|
6
|
+
def domain
|
7
|
+
@config['ServerName'].content
|
8
|
+
end
|
9
|
+
|
10
|
+
def domain=(dom)
|
11
|
+
@config['ServerName'].content = dom
|
12
|
+
end
|
13
|
+
|
14
|
+
def aliases
|
15
|
+
Apache::WriteBackArray.new(@config['ServerAlias'], @config['ServerAlias'].content.to_s.split(/\s+/))
|
16
|
+
end
|
17
|
+
|
18
|
+
def domains
|
19
|
+
[domain] + aliases
|
20
|
+
end
|
21
|
+
|
22
|
+
def document_root
|
23
|
+
@config['Documentroot'].content
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Apache
|
2
|
+
autoload :Node, 'apache_config/node'
|
3
|
+
autoload :VirtualHost, 'apache_config/virtual_host'
|
4
|
+
autoload :WriteBackArray, 'apache_config/write_back_array'
|
5
|
+
|
6
|
+
class Config
|
7
|
+
TabSize = " "
|
8
|
+
Comment = /^\s*#\s?(.*?)\s*$/
|
9
|
+
Blank = /^\s+$/
|
10
|
+
Directive = /^\s*(\w+)(?:\s+(.*?)|)$/
|
11
|
+
SectionOpen = /^\s*<\s*(\w+)(?:\s+([^>]+)|\s*)>$/
|
12
|
+
SectionClose = /^\s*<\/\s*(\w+)\s*>$/
|
13
|
+
|
14
|
+
def initialize(path)
|
15
|
+
@path = path
|
16
|
+
@config = parse_config(path)
|
17
|
+
end
|
18
|
+
|
19
|
+
def virtual_hosts
|
20
|
+
@config.children.map { |vh| VirtualHost.new(vh) }
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_config(root = nil, indent = "")
|
24
|
+
element = root || @config
|
25
|
+
content = ""
|
26
|
+
|
27
|
+
case
|
28
|
+
when element.isRoot?
|
29
|
+
element.children.map { |c| to_config(c) }.join
|
30
|
+
when element.hasChildren?
|
31
|
+
"#{indent}<#{element.name} #{element.content}>\n" +
|
32
|
+
element.children.map {|c| to_config(c, indent + TabSize) }.join +
|
33
|
+
"#{indent}</#{element.name}>\n"
|
34
|
+
when element.name == :comment
|
35
|
+
"#{indent}# #{element.content}\n"
|
36
|
+
when element.name == :blank
|
37
|
+
"#{indent}\n"
|
38
|
+
else
|
39
|
+
"#{indent}#{element.name} #{element.content}\n"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
def parse_config(path)
|
45
|
+
current_child = Node.new("ROOT")
|
46
|
+
|
47
|
+
File.open(path).read.split(/\n/).each do |line|
|
48
|
+
case line
|
49
|
+
when Comment
|
50
|
+
current_child << Node.new(:comment, $1)
|
51
|
+
when Blank
|
52
|
+
current_child << Node.new(:blank)
|
53
|
+
when Directive
|
54
|
+
current_child << Node.new($1, $2)
|
55
|
+
when SectionOpen
|
56
|
+
current_child = current_child << Node.new($1, $2)
|
57
|
+
when SectionClose
|
58
|
+
current_child = current_child.parent
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
current_child
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require 'rubygems'
|
3
|
+
require 'fakefs'
|
4
|
+
|
5
|
+
require "apache_config"
|
6
|
+
|
7
|
+
class TestApacheConfig < Test::Unit::TestCase
|
8
|
+
Config =<<APACHE
|
9
|
+
<VirtualHost *>
|
10
|
+
ServerName example.com
|
11
|
+
ServerAlias www.example.com
|
12
|
+
Documentroot /var/www/default/public
|
13
|
+
Errorlog /var/log/apache2/default.errorlog
|
14
|
+
LogLevel Warn
|
15
|
+
CustomLog /var/log/apache2/default.customlog combined
|
16
|
+
|
17
|
+
<Directory "/var/www/default/public">
|
18
|
+
Options Indexes Includes FollowSymLinks IncludesNOEXEC
|
19
|
+
AllowOverride AuthConfig FileInfo Indexes Options Limit
|
20
|
+
Order allow,deny
|
21
|
+
Allow from all
|
22
|
+
# Foo
|
23
|
+
</Directory>
|
24
|
+
</VirtualHost>
|
25
|
+
APACHE
|
26
|
+
|
27
|
+
def setup
|
28
|
+
File.open('/etc/apache2/example.conf', 'w') do |f|
|
29
|
+
f.write Config
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Quite the integration test you have here..
|
34
|
+
def test_apache_parsed_config_outputs_the_same
|
35
|
+
config = Apache::Config.new('/etc/apache2/example.conf')
|
36
|
+
assert_equal Config, config.to_config
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_adding_to_virtual_hosts
|
40
|
+
config = Apache::Config.new('/etc/apache2/example.conf')
|
41
|
+
config.virtual_hosts.first.aliases << 'foo.be'
|
42
|
+
|
43
|
+
assert_match "ServerAlias www.example.com foo.be", config.to_config
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: apache_config
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Jan De Poorter
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-11 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Parses Apache config files, allowing for changes and then can output again
|
22
|
+
email: jan@openminds.be
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- Rakefile
|
31
|
+
- VERSION
|
32
|
+
- lib/apache_config.rb
|
33
|
+
- lib/apache_config/node.rb
|
34
|
+
- lib/apache_config/virtual_host.rb
|
35
|
+
- lib/apache_config/write_back_array.rb
|
36
|
+
- test/apache_config_test.rb
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: http://github.com/defv/apache_config
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options:
|
43
|
+
- --charset=UTF-8
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
segments:
|
51
|
+
- 0
|
52
|
+
version: "0"
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.3.6
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Apache config parser and printer
|
67
|
+
test_files:
|
68
|
+
- test/apache_config_test.rb
|