Skip to content

Instantly share code, notes, and snippets.

@mweppler

mweppler/site-manager.rb

Last active Dec 13, 2015
Embed
What would you like to do?
THIS IS STILL A WORK IN PROGRESS... If this is the first time running site-manager use the --setup switch I got tired of manually setting up a new site that I have to work on. I use Pow for RoR, rack/sinatra apps, static html sites, and proxy to apache via port 81 for anything else. This script makes a few assumptions about your environment, and…
#!/usr/bin/env ruby
###############################################################################
# Required Application Libraries #
###############################################################################
%w{ rubygems optparse ostruct socket yaml }.each { |lib| require lib }
###############################################################################
###############################################################################
# Site Manager #
###############################################################################
##### Application Constants #####
FS = File::SEPARATOR
##### Application Banner #####
banner = <<BANNER
Site Manager w/ Apache & Pow Proxy
BANNER
##### Application Options #####
$options = OpenStruct.new
options = OptionParser.new do |opts|
opts.banner = banner
$options.site_action = nil
opts.on('-a', '--action ACTION', ['add', 'delete'], 'Site action to take (add, delete).') do |action|
$options.site_action = action
end
# $options.config = "#{Dir.pwd}#{FS}config.yml"
# opts.on('-c', '--config [FILE]', 'Configuration file [Optional], defaults to ./config.yml') do |file|
# $options.config = file
# end
$options.site_name = nil
opts.on('-n', '--site-name NAME', 'The name of the site.') do |name|
$options.site_name = name
end
# $options.sudo_password = nil
# opts.on('-p', '--password [PASSWORD]', 'This script requires sudo, so you\' need to pass your password.') do |password|
# $options.sudo_password = password
# end
$options.proxy_port = nil
opts.on('-p', '--proxy-port [PORT]', 'Default proxy port is 81. Pass the port number you use if different.') do |port|
$options.proxy_port = port
end
$options.setup = false
opts.on('-s', '--setup', 'Install pow, create \'/etc/apache2/sites\', restart apache.') do |setup|
$options.setup = true
end
$options.site_type = nil
opts.on('-t', '--site-type [TYPE]', ['apache', 'ror', 'static'], 'The type of site: apache (Apache php, cgi, etc...), ror (Ruby on Rails), static (Static HTML, w/public directory). Type is manditory if you are adding a site.') do |type|
$options.site_type = type
end
opts.on_tail('-h', '--help', 'Show this message') do
print "#{opts}\n"
exit 0
end
end
begin
options.parse!
unless $options.setup
raise OptionParser::MissingArgument, " site action: -a, --action ACTION" if $options.site_action.nil?
raise OptionParser::MissingArgument, " site type: -t, --site-type [TYPE]" if $options.site_action == 'add' && $options.site_type.nil?
raise OptionParser::MissingArgument, " site name: -n, --site-name NAME" if $options.site_name.nil?
end
rescue OptionParser::InvalidArgument
puts "site-manager: #{$!.message}"
puts "site-manager: try 'site-manager --help' for more information."
exit 1
rescue OptionParser::InvalidOption
puts "site-manager: #{$!.message}"
puts "site-manager: try 'site-manager --help' for more information."
exit 1
rescue OptionParser::MissingArgument
puts "site-manager: #{$!.message}"
puts "site-manager: try 'site-manager --help' for more information."
exit 1
end
# setup........................................................................
def setup
pow_directory = Dir.home + FS + '.pow'
setup_pow = false
setup_vhost = false
vhosts_dir = '/etc/apache2/sites'
if Dir.exists? pow_directory
print "#{pow_directory} already exists. Do you still want to install pow? y/N "
if STDIN.gets.chomp == 'y'
setup_pow = true
end
else
setup_pow = true
end
if setup_pow
puts "Installing pow..."
%x[curl get.pow.cx | sh]
else
puts "Skipping pow..."
end
if Dir.exists? vhosts_dir
print "#{vhosts_dir} already exists. Do you want to overwrite it? y/N"
if STDIN.gets.chomp == 'y'
setup_vhost = true
end
else
setup_vhost = true
end
if setup_vhost
puts "Creating /etc/apache2/sites/..."
# Create a directory for your site specific vhost configs
IO.popen("sudo mkdir /etc/apache2/sites", 'r+') do |io|
io.puts ''
io.close_write
io.read
end
puts "\nYou need to add the following entry to your /etc/apache2/httpd.conf file."
puts "# Site specific virtual hosts"
puts "Include /etc/apache2/sites/*.conf"
puts "\nPress enter to continue."
STDIN.gets.chomp
# Restart apache
%x[sudo apachectl restart]
else
puts "Skipping creation of /etc/apache2/sites/..."
end
exit 0
end
# setup and quit
setup if $options.setup
# app variables................................................................
pow_directory = Dir.home + FS + '.pow'
site_directory = Dir.home + FS + 'developer' + FS + 'projects' + FS + $options.site_name
site_public_dir = site_directory + FS + 'public'
site_symlink = Dir.home + FS + 'Sites' + FS + $options.site_name
tmp_vhosts_file = File.dirname(__FILE__) + FS + 'tmp_vhosts'
vhosts_dir = '/etc/apache2/sites'
vhosts_text = <<VHOSTSDATA
### #{$options.site_name.upcase} ###
<Virtualhost *:81>
ServerName #{$options.site_name}.dev
ServerAlias #{$options.site_name}.#{IPSocket.getaddress(Socket.gethostname)}.xip.io
DocumentRoot "#{site_symlink}"
<Directory "#{site_symlink}">
AllowOverride All
</Directory>
</Virtualhost>
VHOSTSDATA
# begin app logic..............................................................
# preform site action (add/delete)
case $options.site_action
when 'add'
# make sure the site exists..................................................
unless Dir.exists? site_directory
puts "'#{site_directory}' does not exist!"
exit 1
end
# make sure the site has a public directory..................................
if $options.site_type == 'static' && !Dir.exists?(site_public_dir)
puts "'#{site_directory}' does not contain a public directory!"
exit 1
end
# make sure the Sites does not already have a symlink for this site_name....
if File.symlink?(site_symlink)
puts "'#{$options.site_name}' is already symlinked in #{site_symlink}!"
exit 1
elsif File.exists? site_symlink
puts "Found a file/directory '#{site_symlink}' where I should'nt have!"
exit 1
end
# make sure the Sites does not already have a symlink for this site_name ....
if Dir.exists? pow_directory + FS + $options.site_name
puts "Pow proxy for '#{$options.site_name}' already exists!"
exit 1
end
# make symlink from ~/developer/projects/site_name to ~/Sites................
if $options.site_type == 'static'
File.symlink site_public_dir, site_symlink
else
File.symlink site_directory, site_symlink
end
# add pow proxy on port [81].................................................
if $options.site_type == 'ror'
File.symlink site_symlink, "#{pow_directory + FS + $options.site_name}"
else
File.open("#{pow_directory + FS + $options.site_name}", 'w') { |file| file.write $options.proxy_port || '81' }
end
# append the vhosts file with the site
File.open(tmp_vhosts_file, 'w') { |file| file << vhosts_text }
IO.popen("sudo mv #{tmp_vhosts_file} #{vhosts_dir + FS + $options.site_name}.conf", 'r+') do |io|
io.puts ''
io.close_write
io.read
end
# update owner, group and permission on the conf file........................
%x[sudo chmod 644 #{vhosts_dir + FS + $options.site_name}.conf]
%x[sudo chown root:wheel #{vhosts_dir + FS + $options.site_name}.conf]
when 'delete'
# remove symlink from Sites..................................................
File.delete site_symlink if File.exists? site_symlink
# remove from .pow/..........................................................
File.delete(pow_directory + FS + $options.site_name) if File.exists? pow_directory + FS + $options.site_name
# remove vhost entry.........................................................
IO.popen("sudo rm #{vhosts_dir + FS + $options.site_name}.conf", 'r+') do |io|
io.puts ''
io.close_write
io.read
end
end
# restart apache..............................................................
%x[sudo apachectl restart]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment