Skip to content

Instantly share code, notes, and snippets.

@cyberswat
Created May 30, 2013 14:43
Show Gist options
  • Save cyberswat/5678391 to your computer and use it in GitHub Desktop.
Save cyberswat/5678391 to your computer and use it in GitHub Desktop.
A sample LWRP per #drupal-colorado conversation
require 'fileutils'
require 'tempfile'
action :ensure do
t_file = Tempfile.new('/root/.profile_temp')
::File.open(new_resource.profile, 'r') do |f|
f.each_line do |line|
if line.match(/PATH=/)
if ::File.readlines(new_resource.profile).grep(/:#{new_resource.path.gsub('/','\/')}/).length > 0
t_file.puts line
else
new_line = line.split("\n")[0]
t_file.puts "#{new_line}:#{new_resource.path}\n"
end
else
t_file.puts line
end
end
end
FileUtils.mv(t_file.path, new_resource.profile)
FileUtils.chmod(0600, new_resource.profile)
end
action :purge do
t_file = Tempfile.new('/root/.profile_temp')
::File.open(new_resource.profile, 'r') do |f|
f.each_line do |line|
if line.match(/PATH=/)
t_file.puts line.sub(":#{new_resource.path}",'')
else
t_file.puts line
end
end
end
FileUtils.mv(t_file.path, new_resource.profile)
FileUtils.chmod(0600, new_resource.profile)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment