vh 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +52 -0
- data/Rakefile +1 -0
- data/bin/vh +17 -0
- data/lib/vh.rb +31 -0
- data/lib/vh/apache_config.rb +66 -0
- data/lib/vh/apache_handler.rb +19 -0
- data/lib/vh/base_config.rb +52 -0
- data/lib/vh/hosts_config.rb +47 -0
- data/lib/vh/version.rb +3 -0
- data/vh.gemspec +21 -0
- data/vh.rb +83 -0
- metadata +76 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 José Nahuel Cuesta Luengo
|
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,52 @@
|
|
1
|
+
# vh
|
2
|
+
|
3
|
+
Virtual Hosts manager for the CLI -- a dead simple script to add new virtual hosts
|
4
|
+
that will ease the local development of web apps with Apache.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'vh'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install vh
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Running `vh` is pretty straight-forward:
|
23
|
+
|
24
|
+
vh path hostname
|
25
|
+
|
26
|
+
Where:
|
27
|
+
|
28
|
+
* `path` is the path to the `DocumentRoot` of the Virtual Host, and
|
29
|
+
* `hostname` is the hostname that will be used locally for the VH.
|
30
|
+
|
31
|
+
## License
|
32
|
+
|
33
|
+
```
|
34
|
+
Copyright (c) 2012 José Nahuel Cuesta Luengo
|
35
|
+
|
36
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
37
|
+
of this software and associated documentation files (the "Software"), to deal
|
38
|
+
in the Software without restriction, including without limitation the rights
|
39
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
40
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
41
|
+
so, subject to the following conditions:
|
42
|
+
|
43
|
+
The above copyright notice and this permission notice shall be included in all
|
44
|
+
copies or substantial portions of the Software.
|
45
|
+
|
46
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
47
|
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
48
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
49
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
50
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
51
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
52
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/vh
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "vh"
|
4
|
+
require "pathname"
|
5
|
+
|
6
|
+
# Validate required arguments
|
7
|
+
if ARGV.length < 2
|
8
|
+
puts 'Missing arguments.'
|
9
|
+
puts 'Usage:'
|
10
|
+
puts ' vh path hostname'
|
11
|
+
exit 1
|
12
|
+
end
|
13
|
+
|
14
|
+
path = Pathname.new(ARGV[0]).realpath.to_s
|
15
|
+
host = ARGV[1]
|
16
|
+
|
17
|
+
Vh.add path, host
|
data/lib/vh.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require "vh/apache_config"
|
2
|
+
require "vh/hosts_config"
|
3
|
+
require "vh/apache_handler"
|
4
|
+
|
5
|
+
# Virtual Hosts Manager driver
|
6
|
+
module Vh
|
7
|
+
# Adds a virtual host to the currently-existing ones.
|
8
|
+
#
|
9
|
+
# Example:
|
10
|
+
# >> Vh.add "~/dev/project/web", "project.local"
|
11
|
+
#
|
12
|
+
# @param [String|Pathname] path The local path for the web root.
|
13
|
+
# @param [String] hostname The host name for the virtual host.
|
14
|
+
#
|
15
|
+
# @return [Vh]
|
16
|
+
def self.add(path, hostname)
|
17
|
+
# Add the host to the Apache configuration
|
18
|
+
apache_config = ApacheConfig.new path, hostname
|
19
|
+
apache_config.run
|
20
|
+
|
21
|
+
# Add the hostname alias to the hosts file
|
22
|
+
hosts_config = HostsConfig.new hostname
|
23
|
+
hosts_config.run
|
24
|
+
|
25
|
+
# Restart Apache
|
26
|
+
apache_handler = ApacheHandler.new
|
27
|
+
apache_handler.run
|
28
|
+
|
29
|
+
return self
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require "vh/base_config"
|
2
|
+
|
3
|
+
module Vh
|
4
|
+
# Apache configuration manager
|
5
|
+
class ApacheConfig < BaseConfig
|
6
|
+
DEFAULT_CONFIG_PATH = '/etc/apache2/extra/httpd-vhosts.conf'
|
7
|
+
|
8
|
+
# Initializes a new instance of this class.
|
9
|
+
#
|
10
|
+
# @param [String|Pathname] path The path to the web root.
|
11
|
+
# @param [String] host The host name to be added.
|
12
|
+
# @param [String] config_path The path to the configuration path
|
13
|
+
# (optional, defaults to `DEFAULT_CONFIG_PATH`).
|
14
|
+
def initialize(path, host, config_path = DEFAULT_CONFIG_PATH)
|
15
|
+
@path = path
|
16
|
+
@host = host
|
17
|
+
@config_path = config_path
|
18
|
+
end
|
19
|
+
|
20
|
+
# Performs any necessary checks prior to modifying the configuration file
|
21
|
+
# and returns a `Boolean` value indicating whether the checks were successful or not.
|
22
|
+
#
|
23
|
+
# @return [Boolean]
|
24
|
+
def check?
|
25
|
+
# Check if the path exists and is a directory
|
26
|
+
unless File.directory? @path
|
27
|
+
puts "#{@path} is not a valid path."
|
28
|
+
return false
|
29
|
+
end
|
30
|
+
|
31
|
+
# Check if the configuration file is writable
|
32
|
+
unless check_writable?
|
33
|
+
puts "The Apache configuration file is not writable by the current user."
|
34
|
+
return false
|
35
|
+
end
|
36
|
+
|
37
|
+
# Check if there is not already a virtual host defined with the same host name
|
38
|
+
unless check_content? /ServerName\s+"#{@host}"/i
|
39
|
+
puts "There already is a server named '#{@host}' on the Apache configuration file."
|
40
|
+
return false
|
41
|
+
end
|
42
|
+
|
43
|
+
# Success! :)
|
44
|
+
return true
|
45
|
+
end
|
46
|
+
|
47
|
+
# Generates the new content that will be appended to the configuration file.
|
48
|
+
#
|
49
|
+
# @return [String]
|
50
|
+
def new_config_content
|
51
|
+
return <<-VHOST
|
52
|
+
|
53
|
+
# vh - #{@host}
|
54
|
+
<Directory "#{@path}">
|
55
|
+
Allow From All
|
56
|
+
AllowOverride All
|
57
|
+
</Directory>
|
58
|
+
<VirtualHost *:80>
|
59
|
+
ServerName "#{@host}"
|
60
|
+
DocumentRoot "#{@path}"
|
61
|
+
</VirtualHost>
|
62
|
+
# /vh - #{@host}
|
63
|
+
VHOST
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Vh
|
2
|
+
# Apache server handler
|
3
|
+
class ApacheHandler
|
4
|
+
DEFAULT_APACHE_COMMAND = "apachectl"
|
5
|
+
|
6
|
+
# Initializes a new instance of this class.
|
7
|
+
#
|
8
|
+
# @param [String] apache_command The command to use to handle Apache
|
9
|
+
# (Optional, defaults to DEFAULT_APACHE_COMMAND).
|
10
|
+
def initialize(apache_command = DEFAULT_APACHE_COMMAND)
|
11
|
+
@apachectl = apache_command
|
12
|
+
end
|
13
|
+
|
14
|
+
# Runs this handler - Restarts Apache server
|
15
|
+
def run
|
16
|
+
`#{@apachectl} restart`
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Vh
|
2
|
+
# Base class for all configuration managers
|
3
|
+
class BaseConfig
|
4
|
+
# Checks if the configuration path stored in `@config_path`
|
5
|
+
# is writable and returns a boolean value representing the
|
6
|
+
# outcome of that check.
|
7
|
+
#
|
8
|
+
# @return [Boolean]
|
9
|
+
def check_writable?
|
10
|
+
File.writable? @config_path
|
11
|
+
end
|
12
|
+
|
13
|
+
# Checks if the configuration file at `@config_path`
|
14
|
+
# does not match a `regex` and returns `true` if
|
15
|
+
# there is no match or `false` otherwise.
|
16
|
+
#
|
17
|
+
# @param [Regexp] regex The regular expression to check `@config_path` against.
|
18
|
+
#
|
19
|
+
# @return [Boolean]
|
20
|
+
def check_content?(regex)
|
21
|
+
open @config_path do | config |
|
22
|
+
config.grep(regex).length == 0
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Appends `content` to the configuration file at `@config_path`
|
27
|
+
#
|
28
|
+
# @param [String] content The content to append to the configuration file.
|
29
|
+
def append_to_config(content)
|
30
|
+
open @config_path, 'a+' do | config |
|
31
|
+
config << content
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Runs the manager: mainly, append the new configuration generated
|
36
|
+
# by {#new_config} if the checks are OK.
|
37
|
+
#
|
38
|
+
# (see #append_to_config)
|
39
|
+
# (see #check?)
|
40
|
+
# (see #new_config)
|
41
|
+
def run
|
42
|
+
append_to_config new_config_content if check?
|
43
|
+
end
|
44
|
+
|
45
|
+
# Generates the new content that will be appended to the configuration file.
|
46
|
+
#
|
47
|
+
# @return [String]
|
48
|
+
def new_config_content
|
49
|
+
raise NotImplementedError
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "vh/base_config"
|
2
|
+
|
3
|
+
module Vh
|
4
|
+
# Hosts aliases configuration manager
|
5
|
+
class HostsConfig < BaseConfig
|
6
|
+
DEFAULT_CONFIG_PATH = '/etc/hosts'
|
7
|
+
|
8
|
+
# Initializes a new instance of this class.
|
9
|
+
#
|
10
|
+
# @param [String] hostname The hostname to be added.
|
11
|
+
# @param [String] ip The IP address to use (optional, defaults to `'127.0.0.1'`).
|
12
|
+
# @param [String] config_path The path to the configuration path (optional, defaults to `DEFAULT_CONFIG_PATH`).
|
13
|
+
def initialize(hostname, ip = '127.0.0.1', config_path = DEFAULT_CONFIG_PATH)
|
14
|
+
@hostname = hostname
|
15
|
+
@ip_address = ip
|
16
|
+
@config_path = config_path
|
17
|
+
end
|
18
|
+
|
19
|
+
# Performs any necessary checks prior to modifying the configuration file
|
20
|
+
# and returns a `Boolean` value indicating whether the checks were successful or not.
|
21
|
+
#
|
22
|
+
# @return [Boolean]
|
23
|
+
def check?
|
24
|
+
# Check if the configuration file is writable
|
25
|
+
unless check_writable?
|
26
|
+
puts "You don't have the required permissions to modify hosts configuration file."
|
27
|
+
return false
|
28
|
+
end
|
29
|
+
|
30
|
+
# Check if there is not already a host defined with the same name
|
31
|
+
unless check_content? /\W#{@hostname}\W/i
|
32
|
+
puts "There already is a host named '#{@hostname}'."
|
33
|
+
return false
|
34
|
+
end
|
35
|
+
|
36
|
+
# Success! :)
|
37
|
+
return true
|
38
|
+
end
|
39
|
+
|
40
|
+
# Generates the new content that will be appended to the configuration file.
|
41
|
+
#
|
42
|
+
# @return [String]
|
43
|
+
def new_config_content
|
44
|
+
return "\n#{@ip_address} #{@hostname} # vh"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/vh/version.rb
ADDED
data/vh.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'vh/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "vh"
|
8
|
+
gem.version = Vh::VERSION
|
9
|
+
gem.authors = ["José Nahuel Cuesta Luengo"]
|
10
|
+
gem.email = ["nahuelcuestaluengo@gmail.com"]
|
11
|
+
gem.description = %q{CLI tool for managing Virtual Hosts on a local environment.}
|
12
|
+
gem.summary = %q{Virtual Hosts manager.}
|
13
|
+
gem.homepage = "https://ncuesta.github.com/vh"
|
14
|
+
|
15
|
+
gem.add_development_dependency "rspec"
|
16
|
+
|
17
|
+
gem.files = `git ls-files`.split($/)
|
18
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
19
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
end
|
data/vh.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
##
|
4
|
+
# vh.rb
|
5
|
+
# Add virtual hosts to the apache configuration and the hosts file.
|
6
|
+
#
|
7
|
+
# Copyright (c) 2012 José Nahuel Cuesta Luengo
|
8
|
+
##
|
9
|
+
|
10
|
+
require 'pathname'
|
11
|
+
|
12
|
+
# Validate required arguments
|
13
|
+
if ARGV.length < 2
|
14
|
+
puts 'Missing arguments.'
|
15
|
+
puts 'Usage:'
|
16
|
+
puts ' vh.rb path hostname'
|
17
|
+
exit 1
|
18
|
+
end
|
19
|
+
|
20
|
+
path = Pathname.new(ARGV[0]).realpath
|
21
|
+
server_name = ARGV[1]
|
22
|
+
|
23
|
+
# Check the validity of path
|
24
|
+
if not File.directory? path
|
25
|
+
puts "'#{path}' is not a valid path. Please correct that and try again."
|
26
|
+
exit 2
|
27
|
+
end
|
28
|
+
|
29
|
+
# Check if the server is not already defined
|
30
|
+
vhosts_path = '/etc/apache2/extra/httpd-vhosts.conf'
|
31
|
+
add = true
|
32
|
+
open vhosts_path do | f |
|
33
|
+
if f.grep(/ServerName\s+"#{server_name}"/i).length > 0
|
34
|
+
puts "There already exists a server named '#{server_name}' in the virtual hosts file. Skipping..."
|
35
|
+
add = false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Append the vhost to the vhosts file
|
40
|
+
if add
|
41
|
+
puts 'Adding virtual host to the virtual hosts file...'
|
42
|
+
open vhosts_path, 'a+' do | f |
|
43
|
+
f.puts <<-VHOST
|
44
|
+
|
45
|
+
# vh - #{server_name}
|
46
|
+
<Directory "#{path}">
|
47
|
+
Allow From All
|
48
|
+
AllowOverride All
|
49
|
+
</Directory>
|
50
|
+
<VirtualHost *:80>
|
51
|
+
ServerName "#{server_name}"
|
52
|
+
DocumentRoot "#{path}"
|
53
|
+
</VirtualHost>
|
54
|
+
# /vh - #{server_name}
|
55
|
+
VHOST
|
56
|
+
end
|
57
|
+
puts 'Done'
|
58
|
+
end
|
59
|
+
|
60
|
+
# Append the new host to the hosts file
|
61
|
+
hosts_path = '/etc/hosts'
|
62
|
+
add = true
|
63
|
+
open hosts_path do | f |
|
64
|
+
if f.grep(/\W#{server_name}\W/i).length > 0
|
65
|
+
puts "There is already a host named '#{server_name}'. Skipping..."
|
66
|
+
add = false
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
if add
|
71
|
+
puts 'Adding host alias to hosts file...'
|
72
|
+
open hosts_path, 'a+' do | f |
|
73
|
+
f.puts <<-HOST
|
74
|
+
127.0.0.1 #{server_name} # vh
|
75
|
+
HOST
|
76
|
+
end
|
77
|
+
puts 'Done'
|
78
|
+
end
|
79
|
+
|
80
|
+
# Restart apache
|
81
|
+
puts 'Restarting apache...'
|
82
|
+
system 'apachectl restart'
|
83
|
+
puts 'Done'
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vh
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- José Nahuel Cuesta Luengo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
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
|
+
description: CLI tool for managing Virtual Hosts on a local environment.
|
31
|
+
email:
|
32
|
+
- nahuelcuestaluengo@gmail.com
|
33
|
+
executables:
|
34
|
+
- vh
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- bin/vh
|
44
|
+
- lib/vh.rb
|
45
|
+
- lib/vh/apache_config.rb
|
46
|
+
- lib/vh/apache_handler.rb
|
47
|
+
- lib/vh/base_config.rb
|
48
|
+
- lib/vh/hosts_config.rb
|
49
|
+
- lib/vh/version.rb
|
50
|
+
- vh.gemspec
|
51
|
+
- vh.rb
|
52
|
+
homepage: https://ncuesta.github.com/vh
|
53
|
+
licenses: []
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.8.24
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Virtual Hosts manager.
|
76
|
+
test_files: []
|