Create some vhosts domain structure in ruby script

A guide of my own script that creates a directory structure for a new project.
I have my Apache web server set up to look for .conf (configuration files) in certain directory. Which in this case on
/var/www/_conf/enabled.

createdomain.rb

require 'erb'  
require "pty"  
require "expect"

# the prompt
def prompt(*args)  
    print(*args)
    ARGV.clear
    gets.strip
end

# params
defaultvhosts = "/var/www/vhosts"  
defaultsites_available = "/var/www/_conf/available"  
defaultsites_enabled = "/var/www/_conf/enabled"  
domainname = "#{ARGV[0]}"

#create directory sturcture for domain
create_domain_directories_command = ERB.new <<-EOF  
mkdir #{defaultvhosts}/#{domainname}  
mkdir #{defaultvhosts}/#{domainname}/httpdocs  
mkdir #{defaultvhosts}/#{domainname}/subdomains  
echo "#{domainname}" > #{defaultvhosts}/#{domainname}/httpdocs/index.html  
EOF

#template vhosts for apache
template_q =  
%q{<VirtualHost *:80>
  ServerName <%= domainname %>
  ServerAlias www.<%= domainname %>
  ServerAlias <%= domainname %>.dev

  DocumentRoot "/var/www/vhosts/<%= domainname %>/httpdocs/"

  <Directory "/var/www/vhosts/<%= domainname %>/httpdocs/">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>

  ErrorLog /var/www/log/<%= domainname %>.err
  CustomLog /var/www/log/<%= domainname %>.log combined

</VirtualHost>  
<VirtualHost *:80>  
  ServerName <%= domainname %>
  ServerAlias *.<%= domainname %>
  ServerAlias *.<%= domainname %>.dev
  UseCanonicalName Off
  VirtualDocumentRoot "/var/www/vhosts/<%= domainname %>/subdomains/%1/httpdocs/"
  <Directory />
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>

  ErrorLog /var/www/log/subdomains.<%= domainname %>.err        
  CustomLog /var/www/log/subdomains.<%= domainname %>.log combined
</VirtualHost>  
}

template_vhost_draft = ERB.new(template_q)

#template create vhost symlink
enable_vhost_symlink_command = ERB.new <<-EOF  
ln -nsf #{defaultsites_available}/#{domainname}.conf #{defaultsites_enabled}/#{domainname}.conf  
EOF

def create_domain_structure(defaultvhosts, domainname)  
  if domainname != nil then
    if !File.exists?("#{defaultvhosts}/#{domainname}") && domainname != nil then
      puts "creating directory structure: #{domainname}"
      @create_domain = %x[#{create_domain_directories_command.result}]
      puts "#{domainname} created."
    else
      #puts "structure already exists: #{domainname}"
      if prompt("\e[1;32\mdirectory structure already exists:\e[\m \e[1;34\m#{domainname}\e[\m\e[1;32\m! overwrite it?\e[\m [y/n]: ") == 'y' then
        puts "overwrite directory structure: #{domainname}"
        @create_domain = %x[#{create_domain_directories_command.result}]
        puts "#{domainname} directory structure overwrited."
      else
        puts "#{domainname} structure remain unchanged."
      end
    end
  else
    puts "Domain can't be empty"
  end
end

def create_vhost_file(defaultvhosts, domainname, defaultsites_available)  
  if domainname != nil
    if !File.exists?("#{defaultsites_available}/#{domainname}.conf") then
      puts "creating vhost.conf file"

      my_file = File.new("#{defaultsites_available}/#{domainname}.conf", "w")
      my_file.puts @template_vhost_draft.result
      my_file.close
      puts "#{domainname}.conf created."
    else
      #puts "vhost file already exist: #{domainname}.conf"
      if prompt("\e[1;34\m#{domainname}.conf\e[\m \e[1;32\malready exist. Overwrite it?\e[\m [y/n]: ") == 'y' then
        puts "overwrite #{domainname}.conf file"

        my_file = File.new("#{defaultsites_available}/#{domainname}.conf", "w")
        my_file.puts @template_vhost_draft.result
        my_file.close
        puts "#{domainname}.conf overwrited."
      else
        puts "#{domainname}.conf remain unchanged."
      end
    end
  end
end

def create_vhost_symlink(defaultvhosts, domainname, defaultsites_available, defaultsites_enabled, enable_vhost_symlink_command)  
  if prompt("Do you want to create symlink and enable #{domainname}.conf into Apache?: [y/n]") == 'y' then
    if File.exists?("#{defaultsites_available}/#{domainname}.conf")
      puts "creating symlink #{defaultsites_available}/#{domainname}.conf"
      @create_symlink = %x[#{enable_vhost_symlink_command.result}]
      puts "#{defaultsites_available}/#{domainname}.conf is created"
    else 
      #puts "#{domainname}.conf does not exist!"
      if prompt("#{domainname}.conf does not exist! Do you want to create first? [y/n]") == 'y' then

      else
        puts "#{domainname}.conf is NOT created."
      end
    end
  end
end


def sudo_execute_command(cmd)  
  shell_passwd = "password"

  PTY.spawn(cmd) { | stdin, stdout, pid |
    begin
      stdin.expect(/Password:|/) { 
        stdout.write(shell_passwd+"\n")
        puts stdin.read.lstrip
      }
    rescue Errno::EIO
      # don't care
      puts "lolcat erro"
    end
  }
end

## EXECUTING ###
if (domainname != '') then

  # creating directory structure
  if domainname != nil then
    if !File.exists?("#{defaultvhosts}/#{domainname}") && domainname != nil then
      puts "creating directory structure: #{domainname}"
      @create_domain = %x[#{create_domain_directories_command.result}]
      puts "#{domainname} created."
    else
      #puts "structure already exists: #{domainname}"
      if prompt("\e[1;32\mdirectory structure already exists:\e[\m \e[1;34\m#{domainname}\e[\m\e[1;32\m! overwrite it?\e[\m [y/n]: ") == 'y' then
        puts "overwrite directory structure: #{domainname}"
        @create_domain = %x[#{create_domain_directories_command.result}]
        puts "#{domainname} directory structure overwrited."
      else
        puts "#{domainname} structure remain unchanged."
      end
    end
  else
    puts "Domain can't be empty"
  end


  # create vhost.conf file
  if domainname != nil
    if !File.exists?("#{defaultsites_available}/#{domainname}.conf") then
      puts "creating vhost.conf file"

      my_file = File.new("#{defaultsites_available}/#{domainname}.conf", "w")
      my_file.puts template_vhost_draft.result
      my_file.close
      puts "#{domainname}.conf created."
    else
      #puts "vhost file already exist: #{domainname}.conf"
      if prompt("\e[1;34\m#{domainname}.conf\e[\m \e[1;32\malready exist. Overwrite it?\e[\m [y/n]: ") == 'y' then
        puts "overwrite #{domainname}.conf file"

        my_file = File.new("#{defaultsites_available}/#{domainname}.conf", "w")
        my_file.puts template_vhost_draft.result
        my_file.close
        puts "#{domainname}.conf overwrited."
      else
        puts "#{domainname}.conf remain unchanged."
      end
    end
  end


  # create symlink and enable vhost file
  if prompt("Do you want to create symlink and enable #{domainname}.conf into Apache? [y/n]: ") == 'y' then
    if File.exists?("#{defaultsites_available}/#{domainname}.conf")
      puts "creating symlink #{defaultsites_available}/#{domainname}.conf"
      @create_symlink = %x[#{enable_vhost_symlink_command.result}]
      puts "#{defaultsites_available}/#{domainname}.conf is created"
    else 
      #puts "#{domainname}.conf does not exist!"
      if prompt("\e[1;34\m#{domainname}.conf\e[\m \e[1;32\mdoes not exist! Do you want to create first?\e[\m [y/n]: ") == 'y' then

      else
        puts "#{domainname}.conf is NOT created."
      end
    end
  end

  # restart apache
  restart_apache_command = "sudo /Library/StartupItems/MAMP/MAMP restart"
  sudo_execute_command restart_apache_command
  puts "DONE."
else  
  puts "  No parameters? example: ruby createdomain.rb domainname.com"
end  

command line

To use this:

ruby createdomain.rb <yourdomain.com>  

For example:

ruby createdomain.rb mdstn.com  

That will create:

/var/www/vhosts/
  mdstn.com/
    httpdocs/
    subdomains/

create an Apache configuration in:

/var/www/_conf/available

and a linkage in:

/var/www/_conf/enabled

The enabled directory is where Apache will look for the configurations.

Also this script will prompt if you want to restart the Apache web server.

comments powered by Disqus