Skip to content

Instantly share code, notes, and snippets.

@adonaldson
Created October 9, 2015 08:33
Show Gist options
  • Save adonaldson/0ed170a688e339db4666 to your computer and use it in GitHub Desktop.
Save adonaldson/0ed170a688e339db4666 to your computer and use it in GitHub Desktop.
My first ever ruby script - start of a long journey!
#
# Add Virtualhost v0.1 - 13/02/2004
#
# by Andrew Donaldson - [email protected]
#
# Performs three functions
# i) Adds entry to HOSTS file
# ii) Adds entry to httpd.conf
# iii) Restarts apache service
require "ftools"
puts "\n"
puts "Add Virtual Host by Andrew Donaldson v0.1 - 13/02/2004".center(80)
puts ("="*80)
HOSTSFILE = "C:\\WINDOWS\\system32\\drivers\\etc\\hosts";
HTTPCONFFILE = "C:\\apache\\conf\\httpd.conf";
HTDOCS = "c:/apache/htdocs/";
# Get Inputs
puts "Human name for site? (eg: Site name):"
userHname = gets.chomp
puts "Virtual Server name? (eg: sitename-dev):"
userVserver = gets.chomp
puts "Virtual Host to add? (eg: sitename.dev:"
userVhost = gets.chomp
puts "Document root for '" + userHname + "'? (eg: c:/apache/htdocs/sitename):"
userDocroot = gets.chomp
# Make backups
puts "Creating Backup files!"
File.copy(HOSTSFILE,HOSTSFILE + ".bak")
File.copy(HTTPCONFFILE,HTTPCONFFILE + ".bak")
# Check if there is a trailing slash on the document root
# as apache doesn't like this in Vhost declarations
if (userDocroot[-1,1] == "/")
userDocroot.chop
end
# If there are no remaining slashes in the document root
# assume its a folder under HTDOCS
if (userDocroot.include? "/") == false
userDocroot = HTDOCS + userDocroot
end
# Append information to end of httpd.conf
f = File.new(HTTPCONFFILE, "a")
f.puts "\n\n"
f.puts "# " + userHname
f.puts "<VirtualHost " + userVhost + ">"
f.puts "\tServerName " + userVserver
f.puts "\tDocumentRoot \"" + userDocroot + "\""
f.puts "</VirtualHost>"
f.close
# Append information to end of Hosts File
f = File.new(HOSTSFILE, "a") # a = append?
f.puts "\n127.0.0.1\t" + userVhost
f.close
puts "\n" + ("="*80)
puts "Restarting Apache, hold onto your hats!".center(80)
puts ("="*80)
# Stop Apache
system("NET STOP APACHE")
# Start Apache
system("NET START APACHE")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment