vhost_writer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +40 -0
- data/Rakefile +6 -0
- data/bin/vhost_writer +3 -0
- data/lib/vhost_writer/cli.rb +14 -0
- data/lib/vhost_writer/version.rb +3 -0
- data/lib/vhost_writer/writer.rb +39 -0
- data/lib/vhost_writer.rb +2 -0
- data/spec/cli_spec.rb +24 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/test/expected/example.com +8 -0
- data/spec/test/templates/template.erb +8 -0
- data/spec/test/www/example.com/.keep +0 -0
- data/spec/test/www/example.org/.keep +0 -0
- data/spec/test/www/foo.example.com/.keep +0 -0
- data/spec/writer_spec.rb +76 -0
- data/vhost_writer.gemspec +23 -0
- metadata +107 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Brian O'Keefe
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# VhostWriter
|
2
|
+
|
3
|
+
Automatically generate virtual host config files based on a directory of sites and a given ERB template.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
$ gem install vhost_writer
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
`vhost_writer` takes the following:
|
12
|
+
|
13
|
+
* A directory of directories that contain sites
|
14
|
+
* Each site's directory should share the name of its domain (like `example.com`)
|
15
|
+
* A directory to write config files to
|
16
|
+
* The path to a template file written in ERB
|
17
|
+
* Within the template, the `site` variable contains the name of the site.
|
18
|
+
|
19
|
+
### Example
|
20
|
+
|
21
|
+
vhost_writer /srv/www/ /etc/apache2/sites-available/ /home/bok/template.erb
|
22
|
+
|
23
|
+
### Template Example
|
24
|
+
|
25
|
+
<VirtualHost *:80>
|
26
|
+
ServerAdmin admin@<%= site %>
|
27
|
+
ServerName <%= site %>
|
28
|
+
ServerAlias www.<%= site %>
|
29
|
+
DocumentRoot /srv/www/<%= site %>/public_html/
|
30
|
+
ErrorLog /srv/www/<%= site %>/logs/error.log
|
31
|
+
CustomLog /srv/www/<%= site %>/logs/access.log combined
|
32
|
+
</VirtualHost>
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
1. Fork it
|
37
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
38
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
39
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
40
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/vhost_writer
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'vhost_writer'
|
3
|
+
|
4
|
+
module VhostWriter
|
5
|
+
class CLI < Thor
|
6
|
+
desc 'write [SITES_DIR] [CONF_DIR] [TEMPLATE]',
|
7
|
+
'Use the site directories in SITES_DIRECTORY to generate vhost configs in
|
8
|
+
CONFIG_DIRECTORY using TEMPLATE'
|
9
|
+
def write(sites_dir, conf_dir, template)
|
10
|
+
writer = VhostWriter::Writer.new :sites_dir => sites_dir, :conf_dir => conf_dir
|
11
|
+
writer.write_configs_using File.read template
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'erb'
|
2
|
+
|
3
|
+
module VhostWriter
|
4
|
+
class Writer
|
5
|
+
attr_reader :conf_dir, :sites
|
6
|
+
|
7
|
+
def initialize(options={})
|
8
|
+
raise ArgumentError, ':conf_dir is required' unless options.key? :conf_dir
|
9
|
+
raise ArgumentError, 'Either :sites_dir or :sites is required' unless options.key? :sites_dir or options.key? :sites
|
10
|
+
raise ArgumentError, 'Only one of :sites_dir and :sites is allowed' if options.key? :sites_dir and options.key? :sites
|
11
|
+
|
12
|
+
options[:conf_dir] << '/' unless options[:conf_dir] =~ /\/\z/
|
13
|
+
@conf_dir = options[:conf_dir]
|
14
|
+
|
15
|
+
if options.key? :sites_dir
|
16
|
+
@sites = Dir.glob("#{options[:sites_dir]}*").map { |f| File.basename f }
|
17
|
+
else
|
18
|
+
@sites = options[:sites]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def blacklist(site_blacklist)
|
23
|
+
Writer.new :conf_dir => @conf_dir, :sites => @sites - site_blacklist
|
24
|
+
end
|
25
|
+
|
26
|
+
def whitelist(site_whitelist)
|
27
|
+
Writer.new :conf_dir => @conf_dir, :sites => site_whitelist - (@sites - site_whitelist)
|
28
|
+
end
|
29
|
+
|
30
|
+
def write_configs_using(template)
|
31
|
+
@sites.each do |site|
|
32
|
+
File.open("#{@conf_dir}#{site}", 'w') do |f|
|
33
|
+
f.write ERB.new(template).result(binding)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
data/lib/vhost_writer.rb
ADDED
data/spec/cli_spec.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'vhost_writer/cli'
|
3
|
+
|
4
|
+
describe VhostWriter::CLI do
|
5
|
+
describe '#write' do
|
6
|
+
context 'when given parameters' do
|
7
|
+
before { subject.write 'spec/test/www/', 'spec/test/sites-available/', 'spec/test/templates/template.erb' }
|
8
|
+
|
9
|
+
it 'should write config files' do
|
10
|
+
expect(Dir.glob('spec/test/sites-available/*').map { |f| File.basename f }).to eql ['example.com', 'example.org', 'foo.example.com']
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should utilize the specified template' do
|
14
|
+
expect(File.read 'spec/test/sites-available/example.com').to eql File.read 'spec/test/expected/example.com'
|
15
|
+
end
|
16
|
+
|
17
|
+
after { Dir.glob('spec/test/sites-available/*').each { |f| File.delete f } }
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when given a config file' do
|
21
|
+
pending 'not yet implemented'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
RSpec.configure do |config|
|
2
|
+
# Use color in STDOUT
|
3
|
+
config.color_enabled = true
|
4
|
+
|
5
|
+
# Use color not only in STDOUT but also in pagers and files
|
6
|
+
config.tty = true
|
7
|
+
|
8
|
+
# Use the specified formatter
|
9
|
+
config.formatter = :documentation # :progress, :html, :textmate
|
10
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
<VirtualHost *:80>
|
2
|
+
ServerAdmin admin@example.com
|
3
|
+
ServerName example.com
|
4
|
+
ServerAlias www.example.com
|
5
|
+
DocumentRoot /srv/www/example.com/public_html/
|
6
|
+
ErrorLog /srv/www/example.com/logs/error.log
|
7
|
+
CustomLog /srv/www/example.com/logs/access.log combined
|
8
|
+
</VirtualHost>
|
@@ -0,0 +1,8 @@
|
|
1
|
+
<VirtualHost *:80>
|
2
|
+
ServerAdmin admin@<%= site %>
|
3
|
+
ServerName <%= site %>
|
4
|
+
ServerAlias www.<%= site %>
|
5
|
+
DocumentRoot /srv/www/<%= site %>/public_html/
|
6
|
+
ErrorLog /srv/www/<%= site %>/logs/error.log
|
7
|
+
CustomLog /srv/www/<%= site %>/logs/access.log combined
|
8
|
+
</VirtualHost>
|
File without changes
|
File without changes
|
File without changes
|
data/spec/writer_spec.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe VhostWriter::Writer do
|
4
|
+
subject(:writer) { VhostWriter::Writer.new :sites_dir => 'spec/test/www/', :conf_dir => 'spec/test/sites-available'}
|
5
|
+
let(:template) { File.read 'spec/test/templates/template.erb' }
|
6
|
+
let(:all_sites) { ['example.com', 'example.org', 'foo.example.com'] }
|
7
|
+
|
8
|
+
describe '#initialize' do
|
9
|
+
it 'should return a writer object' do
|
10
|
+
expect(writer).to be_an_instance_of VhostWriter::Writer
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'requires a :conf_dir parameter' do
|
14
|
+
expect { VhostWriter::Writer.new :sites_dir => 'foo/bar' }.to raise_error ':conf_dir is required'
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'requires either :sites_dir or :sites' do
|
18
|
+
expect { VhostWriter::Writer.new :conf_dir => 'foo/bar' }.to raise_error 'Either :sites_dir or :sites is required'
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should not accept both :sites_dir and :sites' do
|
22
|
+
expect { VhostWriter::Writer.new :conf_dir => 'foo/bar', :sites_dir => 'bar/baz', :sites => ['example.com', 'example.org']}.to raise_error 'Only one of :sites_dir and :sites is allowed'
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'when conf_dir does not have a trailing slash' do
|
26
|
+
it 'should automatically add a trailing slash' do
|
27
|
+
expect(writer.conf_dir).to eql 'spec/test/sites-available/'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#sites' do
|
33
|
+
it 'should reflect the sites in the sites directory' do
|
34
|
+
expect(writer.sites).to eql all_sites
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#blacklist' do
|
39
|
+
let(:blacklisted_writer) { writer.blacklist ['example.com'] }
|
40
|
+
|
41
|
+
it 'should return a writer object' do
|
42
|
+
expect(blacklisted_writer).to be_an_instance_of VhostWriter::Writer
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should ignore blacklisted sites' do
|
46
|
+
expect(blacklisted_writer.sites).to eql ['example.org', 'foo.example.com']
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '#whitelist' do
|
51
|
+
let(:whitelisted_writer) { writer.whitelist ['example.com'] }
|
52
|
+
|
53
|
+
it 'should return a writer object' do
|
54
|
+
expect(whitelisted_writer).to be_an_instance_of VhostWriter::Writer
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should only operate on whitelisted sites' do
|
58
|
+
expect(whitelisted_writer.sites).to eql ['example.com']
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe '#write_configs_using' do
|
63
|
+
before { writer.write_configs_using template }
|
64
|
+
let(:results_dir_contents) { Dir.glob('spec/test/sites-available/*').map { |f| File.basename f } }
|
65
|
+
|
66
|
+
it 'should write config files' do
|
67
|
+
expect(results_dir_contents).to eql all_sites
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should utilize the provided template' do
|
71
|
+
expect(File.read 'spec/test/sites-available/example.com').to eql File.read 'spec/test/expected/example.com'
|
72
|
+
end
|
73
|
+
|
74
|
+
after { Dir.glob('spec/test/sites-available/*').each { |f| File.delete f } }
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'vhost_writer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'vhost_writer'
|
8
|
+
gem.version = VhostWriter::VERSION
|
9
|
+
gem.authors = ["Brian O'Keefe"]
|
10
|
+
gem.email = ['brian@bokstuff.com']
|
11
|
+
gem.summary = %q(Virtual host configuration generator)
|
12
|
+
gem.description = %q(Automatically generate virtual host config files based on a directory of sites and a given ERB template)
|
13
|
+
gem.homepage = 'https://github.com/brianokeefe/vhost_writer'
|
14
|
+
gem.license = 'MIT'
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split
|
17
|
+
gem.executables = ['vhost_writer']
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ['lib']
|
20
|
+
|
21
|
+
gem.add_dependency 'thor'
|
22
|
+
gem.add_development_dependency 'rspec'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vhost_writer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Brian O'Keefe
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-05-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: thor
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Automatically generate virtual host config files based on a directory
|
47
|
+
of sites and a given ERB template
|
48
|
+
email:
|
49
|
+
- brian@bokstuff.com
|
50
|
+
executables:
|
51
|
+
- vhost_writer
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE.txt
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- bin/vhost_writer
|
61
|
+
- lib/vhost_writer.rb
|
62
|
+
- lib/vhost_writer/cli.rb
|
63
|
+
- lib/vhost_writer/version.rb
|
64
|
+
- lib/vhost_writer/writer.rb
|
65
|
+
- spec/cli_spec.rb
|
66
|
+
- spec/spec_helper.rb
|
67
|
+
- spec/test/expected/example.com
|
68
|
+
- spec/test/templates/template.erb
|
69
|
+
- spec/test/www/example.com/.keep
|
70
|
+
- spec/test/www/example.org/.keep
|
71
|
+
- spec/test/www/foo.example.com/.keep
|
72
|
+
- spec/writer_spec.rb
|
73
|
+
- vhost_writer.gemspec
|
74
|
+
homepage: https://github.com/brianokeefe/vhost_writer
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 1.8.23
|
96
|
+
signing_key:
|
97
|
+
specification_version: 3
|
98
|
+
summary: Virtual host configuration generator
|
99
|
+
test_files:
|
100
|
+
- spec/cli_spec.rb
|
101
|
+
- spec/spec_helper.rb
|
102
|
+
- spec/test/expected/example.com
|
103
|
+
- spec/test/templates/template.erb
|
104
|
+
- spec/test/www/example.com/.keep
|
105
|
+
- spec/test/www/example.org/.keep
|
106
|
+
- spec/test/www/foo.example.com/.keep
|
107
|
+
- spec/writer_spec.rb
|