#!/usr/bin/ruby -w ## Site is a trivial ADT for a web site's most basic configuration. ## Includes driver to create Sites from a space-delimited file. ## For now we pretend there is only Apache 2 but some polymorphic nonsense ## may eventually acknowledge Apache 1.3 or some other software. ## Unit tests are in TestSite. ## Copyleft 2008 adric @ adric . net, Artistic License class Site < Object attr_reader :ip_port, :domain, :user, :docroot, :servername ## Create a new Site from input data tuple thingy. Define a flag ## for using www in servernames and set it to off. def initialize(in_data) @ip_port, @domain, @user = in_data[0], in_data[1], in_data[2] ## undoubtedly there is a more ruby, pretty way of doing that ## default this to off for sanity @www_names=false end ## Change user that owns the Site to specified user. Prob'lly ## a good idea to check if it exists before proceeding. def change_user(to_user) end ## Change Site DocRoot to specified path. Prob'lly ## a good idea to check if it exists before proceeding. def change_docroot(to_path) end ## Change Site's address (ipv4). Range sanity checking for the ## ip at least... def change_ip_port(to_ip,to_port) end ## Add an ip / port to the Site. Range sanity checking for the ## ip at least and see if if we already have it before adding it. def add_ip_port(new_ip,new_port) end ## Switch to using the www.servername.tld form for ServerNames. ## Makes changes, sets boolean flag, notes customer name for later abuse def switch_to_www_servernames() @www_names=true end ## Switch from using the www.servername.tld form for ServerNames. ## Makes changes, sets boolean flag, notes customer name for later abuse def switch_from_www_servernames() @www_names=false end end ## makesites: take a file with space-delimited fields and make Site objects for ## mashing into server configs, take one argument, a file like this: ## username domainname.tld ipv4address ## usage def usage() p 'Site (driver): make Site objects from provided file, takes one argument, a file.' exit end ## check args: if mot one argument, exit with usage ## returns first argument def check_args if ARGV.size != 1 usage end @target = ARGV.pop end ## check file for existence and TODO possibly format ## returns valid filename def check_file(target) @target = target ## if file does not exist, then say so and exit with usage. if not File.exists?(@target) puts '#Argument {@target} does not appaear to be a file.' usage end ## if file does not seem to be formatted correctly, then hint thusly, and exit with usage. ##TODO ## If everything checks out, pass along the target filename. return @target end ## make sites: make Site objects from file argument ## returns list of Sites def make_sites(argument) @target = argument @sites = [] ## for each line in the file, split it up by space and make a Site out of it, and put that ## into the list at the top, and close the file. File.open(@target,'r').each do |line| @sites.push( Site.new( line.split ) ) end return @sites end ## and do stuff, but only if we are run directly if __FILE__ == $0 @target = check_args @target = check_file(@target) @sites = make_sites(@target) p '*** Debug? Sure! ', @sites, '***' end