#!/usr/bin/ruby ## SiteAdder - add some sites, and optionally users from some templates, sloppy ## Uses Site ADT and driver, and etc standard library. ## Copyleft 2008 adric @ adric . net, Artistic License require 'Site' require 'etc' class SiteAdder < Object ## Some Debian specific constants @@SITESA = '/etc/apache2/sites-available' @@A2CTL = '/usr/sbin/apache2ctl' ## don't really want to append to system files directly @@pass='pass_patch' @@shad='shad_patch' ## constructor, perform once per instance setup tasks def initialize ## get usernames from /etc/passwd, once per run @usernames = [] Etc::passwd do |un| @usernames.push(un.name) end end ## check for tmpl, create if needed def check_tmpl() if not File.exists?('tmpl') @template = File.open('tmpl','w') ### heredoc @tmpl = < ServerName domain.tld ServerAlias www.domain.tld *.domain.tld DocumentRoot /web/sites/username/domain.tld CustomLog /var/log/apache2/domain.tld-combined.log combined END_OF_STRING @template.write(@tmpl) @template.close ### can be done much cleaner end end ## do work, was originally just firing off sed def add_site(user, domain, ip_port) @user,@domain,@ip_port = user,domain,ip_port ## if user does not exist, then we'll add it ourselves and make patches for passwd and shadow ## TODO:should probably look up the args order for adduser if not @usernames.find { |user| user == @user } p "echo 'bruce:x:1001:33:,,,:/web/sites/bruce:/bin/bash' | sed -e s,bruce,#{@user}, >> @pass" p "echo 'bruce:justkidding:13967:0:99999:7:::' | sed -e s,bruce,#{@user} >> @shad" ## home folder p "mkdir /web/sites/#{@user}" ## "virt" file p "touch #{@@SITESA}/@user" end ## end add user ## make a virt block, add it to that users file ### FIXME ip_port seems not to be set here? p "sed -e s,domain\.tld,#{@domain}, -e s,username,#{@user}, tmpl >> #{@@SITESA}/#{@user}" ## make folder for the domain in the TODO proper octal mode ? p "mkdir -p /web/sites/#{@user}/#{@domain}" ## chown,chmod it over p "chown -R #{@user}:@@WGROUP /web/sites/#{@user}/#{@domain}" p "chmod -R 2750 /web/sites/#{@user}/#{@domain}" ##FIXME end if __FILE__ == $0 ## go! check_tmpl ### @hash.each_key do |key| do_work(key,@hash[key]) end @sites_to_add.each { |site| add_site( site.user, site.domain, site.ip ) } ## enable the sites and kick apache politely # p "a2ensite #{@user}" # p "#{@@A2CTL} graceful" end end