def apply
@applied_status = :failed
result = true
fqdn = get_item( 'cli::network::hostname' ).value
@host_yaml = File.join( File.dirname( @template_file ), "#{fqdn}.yaml" )
if !File.exists?(@template_file) and !File.exists?(@host_yaml) and File.exists?(@alt_file)
FileUtils.cp(@alt_file, @template_file)
end
debug( "Creating #{File.basename(@host_yaml)} from #{File.basename(@template_file)} template" )
if File.exists?(@template_file)
if File.exists?( @host_yaml )
diff = `diff #{@host_yaml} #{@template_file}`
if diff.empty?
@applied_status = :succeeded
FileUtils.rm_rf(@template_file)
else
@applied_status = :deferred
@applied_status_detail =
"Manual merging of #{File.basename(@template_file)} into pre-existing" +
" #{File.basename(@host_yaml)} may be required"
message = %Q{\nWARNING: #{File.basename( @host_yaml )} already exists, but differs from the template.
Review and consider updating:
#{diff}}
warn( message, [:YELLOW] )
pause(:warn)
backup_host_yaml
end
else
File.rename( @template_file, @host_yaml )
FileUtils.chmod(0640, @host_yaml)
begin
FileUtils.chown(nil, @group, @host_yaml)
@applied_status = :succeeded
rescue Errno::EPERM, ArgumentError
error( "\nERROR: Could not change #{@host_yaml} to #{@group} group", [:RED])
end
end
else
if File.exists?(@host_yaml)
@applied_status = :unnecessary
@applied_status_detail = "Template already moved to #{File.basename(@host_yaml)}"
message = "#{File.basename(@host_yaml)} creation not required:\n" +
" #{@applied_status_detail}"
debug( message, [:MAGENTA] )
backup_host_yaml
else
error( "\nERROR: Creation of #{File.basename(@host_yaml)} not possible. Neither template file " +
"#{File.basename(@template_file)} or\n#{File.basename(@host_yaml)} exist.", [:RED] )
end
end
end