Created
July 23, 2009 21:06
-
-
Save MaherSaif/153598 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'fileutils' | |
=begin | |
to add vhost we do the following: | |
1- create directory | |
2- create configration file | |
3- enable vhost with a2ensite | |
4- reloading apache | |
5- add vhost to hosts file | |
=end | |
=begin | |
to remove vhost we need to do the following: | |
1- remove vhost from hosts file /// sed '/$host_name$/d' | |
2- disable vhost with a2disstie // `a2disstie $host_name` | |
3- reloading apache | |
4- ask to remove config and directory. | |
=end | |
=begin | |
to use this script | |
- vhost add|remove hostname | |
=end | |
class Vhost | |
def initialize(host_name) | |
@host_name = host_name | |
@tpl_file = './vhost.tpl' | |
@config = "/etc/apache2/sites-available/#{host_name}" | |
@directory = "/var/www/#{host_name}" | |
@is_new = false | |
end | |
def create_directory() | |
begin | |
if File.directory?(@directory) | |
STDERR.puts "Directory #{@directory} already exists" | |
puts "continue?" | |
wt = STDIN.gets.chop | |
exit(1) if wt == "no" | |
else | |
FileUtils.mkdir(@directory) | |
puts "#{@directory} created successfully" | |
end | |
rescue => err | |
puts "#{err.class} #{err}" | |
exit(1) | |
end | |
end | |
def rm_directory() | |
begin | |
if File.directory?(@directory) | |
STDIN.puts "RM Directory #{@directory}? " | |
wt = STDIN.gets.chop | |
`rm -vrf #{@directory}` if wt == "yes" | |
else | |
puts "#{@directory} Does NOT Exist" | |
end | |
rescue => err | |
puts "#{err.class} #{err}" | |
puts "xherex" | |
exit(1) | |
end | |
end | |
def create_config() | |
begin | |
if File.file?(@config) | |
STDERR.puts "File #{@config} already exists" | |
puts "continue?" | |
wt = STDIN.gets.chop | |
exit(1) if wt == "no" | |
else | |
tpl = File.new(@tpl_file, 'r') | |
vhost = File.new(@config, 'w') | |
tpl.each_line do |line| | |
vhost << line.gsub('{host_name}', "#{@host_name}") | |
end | |
@is_new = true | |
puts "#{@config} created successfully" | |
end | |
rescue => err | |
puts "#{err.class} #{err}" | |
ensure | |
# tpl.close() | |
# vhost.close() | |
end | |
end | |
def rm_config() | |
begin | |
if File.file?(@config) | |
STDERR.puts "RM File #{@config}?" | |
wt = STDIN.gets.chop | |
FileUtils.rm(@config, :verbose => true) if wt == "yes" | |
else | |
puts "#{@config} does NOT Exist" | |
end | |
rescue => err | |
puts "#{err.class} #{err}" | |
ensure | |
# tpl.close() | |
# vhost.close() | |
end | |
end | |
def is_new?() | |
return @is_new | |
end | |
end | |
class Apache | |
def initialize() | |
end | |
def enable_vhost(host_name) | |
`a2ensite #{host_name}` | |
end | |
def disable_vhost(hostname) | |
`a2dissite #{hostname}` | |
end | |
def reload() | |
`service apache2 reload` | |
end | |
end | |
class Dns | |
def self.hosts=(hosts) | |
@hosts = hosts | |
end | |
def self.hosts | |
@hosts | |
end | |
def self.add(host_name) | |
begin | |
host_entry = "127.0.0.1\t#{host_name}\n" | |
self.hosts = File.new('/etc/hosts', 'a+') | |
## array_search | |
##hostsAll = hosts.readline.arraySearch(); | |
hosts << host_entry if ! self.host_name_exists?(host_name) | |
rescue => err | |
puts "#{err.class} #{err}" | |
end | |
end | |
def self.remove(hostname) | |
begin | |
host_entry = "127.0.0.1\t#{hostname}" | |
`sed -i '/#{host_entry}/d' /etc/hosts'` | |
rescue => err | |
puts "#{err.class} #{err}" | |
end | |
end | |
def self.host_name_exists?(host_name) | |
self.hosts.each_line do |line| | |
if line =~/#{host_name}$/ | |
STDERR.puts "hostname #{host_name} alread exists in hosts" | |
return true | |
end | |
end | |
return false | |
end | |
end | |
def usage() | |
STDERR.puts "Usage: #{File.basename($0, '.rb')} add|remove hostname" | |
exit(1) | |
end | |
if $0 == __FILE__ | |
action, hostname = ARGV | |
usage() if hostname.nil? or action.nil? | |
if action == "add" | |
vhost = Vhost.new(hostname) | |
vhost.create_directory() | |
vhost.create_config() | |
apache = Apache.new | |
if vhost.is_new?() | |
apache.enable_vhost(hostname) | |
puts apache.reload() | |
end | |
Dns.add(hostname) | |
elsif action == "remove" | |
vhost = Vhost.new(hostname) | |
vhost.rm_directory() | |
vhost.rm_config() | |
apache = Apache.new | |
apache.disable_vhost(hostname) | |
puts apache.reload() | |
Dns.remove(hostname) | |
else | |
STDERR.puts "#{action} is not an action" | |
exit(1) | |
end | |
puts "" | |
puts "Congratz .. :)" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment