rework a bunch of things

This commit is contained in:
John Bintz 2010-11-29 18:46:04 -05:00
parent 80ad6d501e
commit f0b5676218
6 changed files with 126 additions and 79 deletions

View File

@ -1,7 +1,9 @@
require 'fileutils'
require 'rainbow'
Dir[File.join(File.dirname(__FILE__), '*.rb')].each { |f| require f }
%w{apachify directory logging master modules mpm_prefork performance permissions rewrites ssl}.each do |file|
require "apache/#{file}"
end
module Apache
# The core class of Apache Config Generator.
@ -66,7 +68,7 @@ module Apache
# Build the provided configuration only if the current environment matches one of the conditions
def build_if(target, *conditions, &block)
build(target, &block) if conditions.include? APACHE_ENV
build(target, &block) if environment_ok?(*conditions)
end
# Build the provided configuration
@ -79,7 +81,12 @@ module Apache
end
def build_and_return_if(*conditions, &block)
build_and_return(&block) if conditions.include? APACHE_ENV
build_and_return(&block) if environment_ok?(*conditions)
end
def environment_ok?(*environments)
return true if APACHE_ENV == true
environments.include?(APACHE_ENV)
end
def build(target, &block)

6
lib/apache/rake.rb Normal file
View File

@ -0,0 +1,6 @@
require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)
require 'apache/rake/apache/create'

View File

@ -0,0 +1,53 @@
require 'apache/config'
require 'apache/rake/support'
include Apache::Rake::Support
task :default => 'apache:create'
namespace :apache do
desc "Create all defined configs for the specified environment"
task :create, :environment do |t, args|
if get_environments.empty?
APACHE_ENV = true
else
need_environment if !args[:environment] && !get_default_environment
APACHE_ENV = (args[:environment] || get_default_environment).to_sym
end
config[:source_path] = File.expand_path(config[:source])
config[:dest_path] = File.expand_path(config[:destination])
Apache::Config.rotate_logs_path = config[:rotate_logs_path]
FileUtils.mkdir_p config[:dest_path]
Dir.chdir config[:dest_path]
# using CONFIG is deprecated
CONFIG = config
Dir[File.join(config[:source_path], '**', '*.rb')].each do |file|
puts file.foreground(:green)
require file
end
end
desc "List all possible environments"
task :environments do
puts get_environments * "\n"
end
desc "Set the default environment (currently #{get_default_environment || 'nil'})"
task :default, :environment do |t, args|
need_environment if !args[:environment]
if get_environments.include?(args[:environment])
File.open('.environment', 'w') { |fh| fh.puts args[:environment] }
puts "Calls to apache:create will now use #{args[:environment]} when you don't specify the environment."
else
puts "You need to specify a valid default environment. Here are the possibilities:"
Rake::Task['apache:environments'].invoke
end
end
end

View File

@ -1,76 +0,0 @@
require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)
require 'fileutils'
require 'yaml'
require 'apache/config'
require 'apache/rake/create'
CONFIG = Hash[YAML.load_file('config.yml').collect { |k,v| [ k.to_sym, v ] }]
def get_environments
CONFIG[:source_path] = File.expand_path(CONFIG[:source])
Dir[File.join(CONFIG[:source_path], '**', '*.rb')].collect { |file|
File.readlines(file).find_all { |line| line[%r{(if_environment|build_if)}] }.collect { |line| line.scan(%r{:[a-z_]+}) }
}.flatten.uniq.sort.collect { |name| name[1..-1] }
end
def get_default_environment
File.read('.environment').strip rescue nil
end
def need_environment
puts "You need to specify an environment. Available environments:"
puts
puts get_environments.collect { |env| "rake apache:create[#{env}]" } * "\n"
puts
puts "Additionally, you can set a default environment for this server:"
puts
puts "rake apache:default[#{get_environments.first}]"
exit 1
end
task :default => 'apache:create'
namespace :apache do
desc "Create all defined configs for the specified environment"
task :create, :environment do |t, args|
need_environment if !args[:environment] && !get_default_environment
APACHE_ENV = (args[:environment] || get_default_environment).to_sym
CONFIG[:source_path] = File.expand_path(CONFIG[:source])
CONFIG[:dest_path] = File.expand_path(CONFIG[:destination])
Apache::Config.rotate_logs_path = CONFIG[:rotate_logs_path]
FileUtils.mkdir_p CONFIG[:dest_path]
Dir.chdir CONFIG[:dest_path]
Dir[File.join(CONFIG[:source_path], '**', '*.rb')].each do |file|
puts file.foreground(:green)
require file
end
end
desc "List all possible environments"
task :environments do
puts get_environments * "\n"
end
desc "Set the default environment (currently #{get_default_environment || 'nil'})"
task :default, :environment do |t, args|
need_environment if !args[:environment]
if get_environments.include?(args[:environment])
File.open('.environment', 'w') { |fh| fh.puts args[:environment] }
puts "Calls to apache:create will now use #{args[:environment]} when you don't specify the environment."
else
puts "You need to specify a valid default environment. Here are the possibilities:"
Rake::Task['apache:environments'].invoke
end
end
end

View File

@ -0,0 +1,35 @@
require 'yaml'
require 'fileutils'
module Apache
module Rake
module Support
def config
@config ||= Hash[YAML.load_file('config.yml').collect { |k,v| [ k.to_sym, v ] }]
end
def get_environments
config[:source_path] = File.expand_path(config[:source])
Dir[File.join(config[:source_path], '**', '*.rb')].collect { |file|
File.readlines(file).find_all { |line| line[%r{(if_environment|build_if)}] }.collect { |line| line.scan(%r{:[a-z_]+}) }
}.flatten.uniq.sort.collect { |name| name[1..-1] }
end
def get_default_environment
File.read('.environment').strip rescue nil
end
def need_environment
puts "You need to specify an environment. Available environments:"
puts
puts get_environments.collect { |env| "rake apache:create[#{env}]" } * "\n"
puts
puts "Additionally, you can set a default environment for this server:"
puts
puts "rake apache:default[#{get_environments.first}]"
exit 1
end
end
end
end

View File

@ -11,6 +11,28 @@ describe Apache::Config, "builds configurations" do
Object.send(:const_set, :APACHE_ENV, env)
end
describe '.environment_ok?' do
subject { Apache::Config.environment_ok?(:good) }
context 'empty environment' do
before { set_apache_env(true) }
it { should be_true }
end
context 'bad environment' do
before { set_apache_env(:bad) }
it { should be_false }
end
context 'good environment' do
before { set_apache_env(:good) }
it { should be_true }
end
end
it "should handle indent" do
apache.line_indent = 1