apache-config-generator/lib/apache/rake/create.rb

77 lines
2.3 KiB
Ruby
Raw Normal View History

require 'rubygems'
require 'bundler/setup'
2010-10-26 14:24:45 +00:00
Bundler.require(:default)
2010-05-05 14:44:20 +00:00
require 'fileutils'
2010-09-15 22:05:34 +00:00
require 'yaml'
2010-05-05 14:44:20 +00:00
require 'apache/config'
require 'apache/rake/create'
2010-09-15 22:05:34 +00:00
CONFIG = Hash[YAML.load_file('config.yml').collect { |k,v| [ k.to_sym, v ] }]
2010-09-15 22:08:26 +00:00
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
2010-10-26 14:24:45 +00:00
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'
2010-05-05 14:44:20 +00:00
namespace :apache do
desc "Create all defined configs for the specified environment"
task :create, :environment do |t, args|
2010-10-26 14:24:45 +00:00
need_environment if !args[:environment] && !get_default_environment
2010-09-15 22:08:26 +00:00
2010-10-26 14:24:45 +00:00
APACHE_ENV = (args[:environment] || get_default_environment).to_sym
2010-05-05 14:44:20 +00:00
2010-05-06 14:40:45 +00:00
CONFIG[:source_path] = File.expand_path(CONFIG[:source])
CONFIG[:dest_path] = File.expand_path(CONFIG[:destination])
2010-05-05 14:44:20 +00:00
2010-05-06 14:40:45 +00:00
Apache::Config.rotate_logs_path = CONFIG[:rotate_logs_path]
2010-05-05 16:25:07 +00:00
2010-05-06 14:40:45 +00:00
FileUtils.mkdir_p CONFIG[:dest_path]
Dir.chdir CONFIG[:dest_path]
2010-05-05 14:44:20 +00:00
2010-05-06 14:40:45 +00:00
Dir[File.join(CONFIG[:source_path], '**', '*.rb')].each do |file|
2010-05-11 19:19:16 +00:00
puts file.foreground(:green)
2010-05-05 14:44:20 +00:00
require file
end
end
2010-09-15 22:05:34 +00:00
desc "List all possible environments"
task :environments do
2010-09-15 22:08:26 +00:00
puts get_environments * "\n"
2010-09-15 22:05:34 +00:00
end
2010-10-26 14:24:45 +00:00
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
2010-05-05 14:44:20 +00:00
end