Created
July 15, 2013 16:22
-
-
Save cyberswat/6001309 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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