get rid of the symlink thing and just recreate the config directory each time

This commit is contained in:
John Bintz 2011-01-06 11:17:46 -05:00
parent d373738451
commit 2770636def
7 changed files with 33 additions and 124 deletions

View File

@ -9,7 +9,7 @@ GEM
specs: specs:
autotest (4.4.2) autotest (4.4.2)
diff-lcs (1.1.2) diff-lcs (1.1.2)
mocha (0.9.9) mocha (0.9.10)
rake rake
nokogiri (1.4.3.1) nokogiri (1.4.3.1)
rainbow (1.1) rainbow (1.1)

View File

@ -92,15 +92,17 @@ module Apache
def build(target, &block) def build(target, &block)
config = build_and_return(&block) config = build_and_return(&block)
if !disabled?
FileUtils.mkdir_p File.split(target).first FileUtils.mkdir_p File.split(target).first
File.open(target, 'w') { |file| file.puts generate_config_file(config) * "\n" } File.open(target, 'w') { |file| file.puts generate_config_file(config) * "\n" }
@was_written = true @was_written = true
end
config config
end end
# If included in a configuration, will not generate the symlink in the Rake task # If included in a configuration, will not generate the config file in the Rake task
def disable_symlink! def disable!
@is_disabled = true @is_disabled = true
end end
@ -113,9 +115,7 @@ module Apache
end end
def generate_config_file(config) def generate_config_file(config)
output = [ "# Generated by apache-config-generator #{Time.now.to_s}", config ] [ "# Generated by apache-config-generator #{Time.now.to_s}", config ].flatten
output.unshift('# disabled') if @is_disabled
output.flatten
end end
# Reset the current settings # Reset the current settings

View File

@ -27,6 +27,7 @@ namespace :apache do
Apache::Config.rotate_logs_path = config[:rotate_logs_path] Apache::Config.rotate_logs_path = config[:rotate_logs_path]
FileUtils.rm_rf config[:destination_path]
FileUtils.mkdir_p config[:destination_path] FileUtils.mkdir_p config[:destination_path]
Dir.chdir config[:destination_path] Dir.chdir config[:destination_path]
@ -47,8 +48,6 @@ namespace :apache do
puts output puts output
end end
end end
symlink_configs!
end end
desc "List all possible environments" desc "List all possible environments"

View File

@ -28,7 +28,7 @@ module Apache
end end
def config_paths! def config_paths!
[ :source, :destination, :symlink ].each do |which| [ :source, :destination ].each do |which|
begin begin
@config[:"#{which}_path"] = File.expand_path(@config[which]) @config[:"#{which}_path"] = File.expand_path(@config[which])
rescue StandardError rescue StandardError
@ -39,8 +39,6 @@ module Apache
end end
def get_environments def get_environments
config[:source_path] = File.expand_path(config[:source])
Dir[File.join(config[:source_path], '**', '*.rb')].collect { |file| 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_]+}) } 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] } }.flatten.uniq.sort.collect { |name| name[1..-1] }
@ -60,23 +58,6 @@ module Apache
puts "rake apache:default[#{get_environments.first}]" puts "rake apache:default[#{get_environments.first}]"
exit 1 exit 1
end end
def symlink_configs!
raise Errno::ENOENT if !File.directory?(config[:destination_path])
FileUtils.rm_rf(config[:symlink_path])
FileUtils.mkdir_p(config[:symlink_path])
Dir[File.join(config[:destination_path], '**/*')].find_all { |file| File.file?(file) }.each do |file|
if line = File.read(file).first
if !line['# disabled']
target = file.gsub(config[:destination_path], config[:symlink_path])
FileUtils.mkdir_p(File.split(target).first)
FileUtils.ln_sf(file, target)
end
end
end
end
end end
end end
end end

View File

@ -1,4 +1,3 @@
source: "source" source: "source"
destination: "conf.d-original" destination: "conf.d"
symlink: "conf.d"
rotate_logs_path: '/path/to/apache/bin/rotatelogs' rotate_logs_path: '/path/to/apache/bin/rotatelogs'

View File

@ -33,32 +33,38 @@ describe Apache::Config, "builds configurations" do
end end
end end
describe '.disable_symlink!' do describe '.disable!' do
context 'is enabled by default' do context 'is enabled by default' do
it { apache.instance_variable_get(:@is_disabled).should be_false } it { apache.instance_variable_get(:@is_disabled).should be_false }
it { apache.disabled?.should be_false } it { apache.disabled?.should be_false }
context 'writes config' do
before {
FileUtils.expects(:mkdir_p).once
File.expects(:open).once
apache.build("here") { cats }
}
it { apache.written?.should == true }
end
end end
context 'disable' do context 'disable' do
before { apache.disable_symlink! } before { apache.disable! }
it { apache.instance_variable_get(:@is_disabled).should be_true } it { apache.instance_variable_get(:@is_disabled).should be_true }
it { apache.disabled?.should be_true } it { apache.disabled?.should be_true }
context 'does not write config' do
before {
FileUtils.expects(:mkdir_p).never
File.expects(:open).never
apache.build("here") { disable!; cats }
}
it { apache.written?.should == false }
end end
end end
describe '.generate_config_file' do
subject { apache.generate_config_file(%w{config}) }
context 'with symlink' do
its(:first) { should_not == '# disabled' }
end
context 'without symlink' do
before { apache.disable_symlink! }
its(:first) { should == '# disabled' }
end
end end
it "should handle indent" do it "should handle indent" do

View File

@ -6,14 +6,12 @@ describe Apache::Rake::Support do
let(:source) { '/source' } let(:source) { '/source' }
let(:destination) { '/destination/available' } let(:destination) { '/destination/available' }
let(:symlink) { '/destination/enabled' }
describe 'config_paths!' do describe 'config_paths!' do
before { before {
@config = { @config = {
:source => 'cats', :source => 'cats',
:destination => 'dogs', :destination => 'dogs'
:symlink => 'cows'
} }
} }
@ -21,79 +19,5 @@ describe Apache::Rake::Support do
its([:source_path]) { should == File.expand_path('cats') } its([:source_path]) { should == File.expand_path('cats') }
its([:destination_path]) { should == File.expand_path('dogs') } its([:destination_path]) { should == File.expand_path('dogs') }
its([:symlink_path]) { should == File.expand_path('cows') }
end
describe 'symlink_configs!' do
before {
@config = {
:source_path => source,
:destination_path => destination,
:symlink_path => symlink
}
}
subject { symlink_configs! }
context 'source does not exist' do
before { File.expects(:directory?).with(destination).returns(false) }
it { expect { subject }.to raise_error(Errno::ENOENT) }
end
context 'source does exist' do
before {
File.expects(:directory?).with(destination).returns(true)
FileUtils.expects(:rm_rf).with(symlink)
FileUtils.expects(:mkdir_p).with(symlink)
Dir.expects(:[]).with(File.join(destination, '**/*')).returns(dir_return)
}
context 'with no configs' do
let(:dir_return) { [] }
before { FileUtils.expects(:ln_sf).never }
it { subject }
end
context 'with one config' do
let(:filename) { File.join(destination, 'dogs/cats') }
let(:dir_return) { [ filename ] }
before { File.expects(:file?).with(filename).returns(is_file_result) }
context 'is a directory' do
let(:is_file_result) { false }
it { subject }
end
context 'is a file' do
let(:is_file_result) { true }
before { File.expects(:read).with(filename).returns(read_result) }
context 'config should not be symlinked' do
let(:read_result) { ['# disabled'] }
before { FileUtils.expects(:ln_sf).never }
it { subject }
end
context 'config should be symlinked' do
let(:read_result) { ['# whatever'] }
before {
FileUtils.expects(:mkdir_p).with(File.join(symlink, 'dogs'))
FileUtils.expects(:ln_sf).with(filename, filename.gsub(destination, symlink))
}
it { subject }
end
end
end
end
end end
end end