Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

create lighttpd conf files for vhosting (See related posts)

Creates lighttpd conf files like suggested on http://blog.lighttpd.net/articles/2005/11/25/simplify-your-configfiles-with-includes
simply add include "incl-domains.conf" to your main lighttpd.conf file
#!/usr/bin/env ruby -w
#
# light_conf_editor.rb
#
# Created by yo on 25.12.05.
#
#
# Description
# Creates lighttpd conf files
#
# usage: light_conf_editor add || remove domainname username
# example: light_conf_editor add bigcurl.com yo
# example: light_conf_editor remove bigcurl.com yo

path_to_lighttpd_conf="/etc/lighttpd"

def remove_include_from_conf(domainname, username, path_to_lighttpd_conf)
  temp_file=""
  File.open("#{path_to_lighttpd_conf}/incl-domains.conf", "a+") unless FileTest.exist?("#{path_to_lighttpd_conf}/incl-domains.conf")
  File.open("#{path_to_lighttpd_conf}/incl-domains.conf", "r") do |file|
    file.each_line do |line| 
        temp_file.concat(line) unless line.include? "include \"domains/#{username}/#{domainname}.conf\""
    end
  end

  File.open("#{path_to_lighttpd_conf}/incl-domains.conf", "w") do |file|
    file.puts(temp_file)
  end

end

def remove_conf_from_folder(domainname, username, path_to_lighttpd_conf)
  File.delete("#{path_to_lighttpd_conf}/domains/#{username}/#{domainname}.conf")  if FileTest.exists?( "#{path_to_lighttpd_conf}/domains/#{username}/#{domainname}.conf" )

end

def add_new_conf_in_folder(include_template, domainname, username, path_to_lighttpd_conf)
  Dir.mkdir( "#{path_to_lighttpd_conf}/domains") unless FileTest.directory?( "#{path_to_lighttpd_conf}/domains/" )
  Dir.mkdir( "#{path_to_lighttpd_conf}/domains/#{username}/") unless FileTest.directory?( "#{path_to_lighttpd_conf}/domains/#{username}")

  aFile = File.new("#{path_to_lighttpd_conf}/domains/#{username}/#{domainname}.conf","w")
  aFile.puts(include_template)
  aFile.close
end

def add_new_include_in_conf(domainname, username, path_to_lighttpd_conf)
  File.open("#{path_to_lighttpd_conf}/incl-domains.conf", "a+") do |file|
    file.puts("include \"domains/#{username}/#{domainname}.conf\"")
  end
end

def show_usage
  print "usage: light_conf_editor add domainname username || remove domainname username\n"
end


if(ARGV.length < 3)
  show_usage
  exit(1)
end

domainname = ARGV[1]

username = ARGV[2]
if(ARGV[0]=="remove")
  remove_include_from_conf(domainname, username, path_to_lighttpd_conf)
  remove_conf_from_folder(domainname, username, path_to_lighttpd_conf)
end


if(ARGV[0]=="add")

  include_template = "$HTTP[\"host\"] =~ \"#{domainname}$\" {
  var.username   = \"#{username}\"
  var.servername = \"#{domainname}\"
  include \"incl-docroot.conf\"

  server.dir-listing        = \"enable\"
  dir-listing.hide-dotfiles = \"enable\"
  }"

  remove_include_from_conf(domainname, username, path_to_lighttpd_conf)
  remove_conf_from_folder(domainname, username, path_to_lighttpd_conf)

  add_new_conf_in_folder(include_template, domainname, username, path_to_lighttpd_conf)
  add_new_include_in_conf(domainname, username, path_to_lighttpd_conf)
  
end



You need to create an account or log in to post comments to this site.


Click here to browse all 5147 code snippets

Related Posts