-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
147 additions
and
86 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,6 @@ lib/puppet/\.DS_Store | |
lib/\.DS_Store | ||
|
||
\.DS_Store | ||
|
||
\.idea/ | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,30 @@ | ||
# file_line_append | ||
# file_line_append | ||
|
||
The module provides a resouce file_line_append which can be used to ensure that a given string is contained within a line in a file. If the string is not contained in the given line, Puppet will append the string to the end of the line to ensure the desired state. | ||
|
||
Example: | ||
|
||
``` | ||
file_line_append { 'sudo_rule': | ||
path => '/etc/sudoers', | ||
line => '%sudo ALL=(ALL)', | ||
string => ' /usr/bin/myscript2' | ||
} | ||
``` | ||
|
||
Match example: | ||
|
||
``` | ||
file_line_append { 'sudo_rule': | ||
path => '/etc/sudoers', | ||
line => '%sudo ALL=(ALL)', | ||
string => '/usr/bin/myscript', | ||
match => '/usr/bin/notworkingscript' | ||
} | ||
``` | ||
|
||
In this code example match will look for a line beginning with %sudo and then look for a match string a replace it with the string value. | ||
|
||
This resource looks like the file_line resource provided by the stdlib module. but it helps you manage lines instead of files and append strings to a given line more easily. | ||
|
||
I made a pull request to the stdlib module to add this resource, while waiting you can use this module. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,57 @@ | ||
Puppet::Type.type(:file_line_append).provide(:ruby) do | ||
|
||
$path = "/etc/systemd/system.conf" | ||
$query = 'DefaultEnvironment' | ||
|
||
def exists? | ||
listvars = init() | ||
exists(resource[:name],resource[:value],listvars) | ||
end | ||
|
||
def create | ||
listvars = init() | ||
listvars = add_env_var(resource[:name],resource[:value],listvars) | ||
newline = $query+"="+listvars.join(" ")+"\n" | ||
file = File.read($path) | ||
new_contents = file.gsub($line, newline) | ||
File.open($path, "w") {|file| file.puts new_contents } | ||
Puppet::Type.type(:file_line_append).provide(:ruby) do | ||
|
||
def exists? | ||
line = getline() | ||
exists(line) | ||
end | ||
|
||
|
||
def create | ||
line = getline() | ||
newline = add_string(line) | ||
file = File.read(resource[:path]) | ||
new_contents = file.gsub(line, newline) | ||
File.open(resource[:path], "w") {|file| file.puts new_contents} | ||
end | ||
|
||
def destroy | ||
listvars = init() | ||
listvars = destroy_env_var(resource[:name],resource[:value],listvars) | ||
newline = $query+"="+listvars.join(" ")+"\n" | ||
file = File.read($path) | ||
new_contents = file.gsub($line, newline) | ||
File.open($path, "w") {|file| file.puts new_contents } | ||
end | ||
|
||
def init() | ||
file = File.readlines($path) | ||
matches = file.select { |name| name[/#{$query}/i] } | ||
matches.push() | ||
$line = matches[0] | ||
if matches[1] | ||
$line = matches[1] | ||
file = File.read($path) | ||
new_contents = file.gsub(matches[0], "") | ||
File.open($path, "w") {|file| file.puts new_contents } | ||
end | ||
vars = $line.split('=',2)[1] | ||
listvars = vars.split(" ") | ||
return listvars | ||
end | ||
|
||
def add_env_var(var,value,listvars) | ||
found = false | ||
listvars = listvars.map do |var_| | ||
if var_.include?(var+"=") | ||
found = true | ||
'"'+var+'='+value+'"' | ||
else | ||
var_ | ||
end | ||
end | ||
if (!found) | ||
listvars.push('"'+var+'='+value+'"') | ||
end | ||
return listvars | ||
end | ||
|
||
def destroy_env_var(var,value,listvars) | ||
listvars.delete('"'+var+'='+value+'"') | ||
return listvars | ||
end | ||
|
||
def exists(var,value,listvars) | ||
if listvars.include?('"'+var+'='+value+'"') | ||
return true | ||
line = getline() | ||
newline = delete_string(line) | ||
file = File.read(resource[:path]) | ||
new_contents = file.gsub(line, newline) | ||
File.open(resource[:path], "w") {|file| file.puts new_contents} | ||
end | ||
|
||
def getline() | ||
file = File.readlines(resource[:path]) | ||
matches = file.select {|line| line.match(resource[:line])} | ||
line = matches[0] | ||
return line | ||
end | ||
|
||
def exists(line) | ||
if line.include? resource[:string] | ||
return true | ||
else | ||
return false | ||
end | ||
end | ||
|
||
def add_string(line) | ||
if (resource[:match]) | ||
line = line.gsub(resource[:match], resource[:string]) | ||
else | ||
return false | ||
if line.include? "\n" | ||
line = line.gsub(/\n/, "") | ||
resource[:string] << "\n" | ||
end | ||
line << resource[:string] | ||
end | ||
end | ||
|
||
return line | ||
end | ||
|
||
def delete_string(line) | ||
line = line.gsub(resource[:string], "") | ||
return line | ||
end | ||
|
||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,63 @@ | ||
Puppet::Type.newtype(:file_line_append) do | ||
|
||
ensurable | ||
|
||
newparam(:name) do | ||
desc "The name of the variable." | ||
|
||
desc <<-EOT | ||
Ensures that a given string is contained within a line in a file. If | ||
the string is not contained in the given line, Puppet will append the string to | ||
the end of the line to ensure the desired state. | ||
Example: | ||
file_line_append { 'sudo_rule': | ||
path => '/etc/sudoers', | ||
line => '%sudo ALL=(ALL)', | ||
string => ' /usr/bin/myscript2' | ||
} | ||
Match example: | ||
file_line_append { 'sudo_rule': | ||
path => '/etc/sudoers', | ||
line => '%sudo ALL=(ALL)', | ||
string => '/usr/bin/myscript', | ||
match => '/usr/bin/notworkingscript' | ||
} | ||
In this code example math will look for a line beginning with %sudo and then look for a match string a replace it with the string value. | ||
EOT | ||
|
||
ensurable do | ||
defaultvalues | ||
defaultto :present | ||
end | ||
|
||
newparam(:name, :namevar => true) do | ||
desc 'An arbitrary name used as the identity of the resource.' | ||
end | ||
|
||
newparam(:string) do | ||
desc "The string to be appended to the line or used to replace matches found by the match attribute." | ||
end | ||
newparam(:path) do | ||
desc 'The file Puppet will ensure contains the line specified by the line parameter.' | ||
validate do |value| | ||
unless Puppet::Util.absolute_path?(value) | ||
raise Puppet::Error, "File paths must be fully qualified, not '#{value}'" | ||
end | ||
newparam(:value) do | ||
desc "The value of the env variable." | ||
end | ||
end | ||
end | ||
end | ||
newparam(:match) do | ||
desc 'an option string to be used to repalce the string found, if not specified, the new string will be appended at the end of the line.' | ||
end | ||
|
||
newparam(:line) do | ||
desc 'The line where the string will be appended.' | ||
end | ||
|
||
validate do | ||
unless self[:path] | ||
raise(Puppet::Error, "path is a required attribute") | ||
end | ||
unless self[:string] | ||
raise(Puppet::Error, "string is a required attribute") | ||
end | ||
unless self[:line] | ||
raise(Puppet::Error, "line is a required attribute") | ||
end | ||
end | ||
|
||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,2 @@ | ||
class file_line_append::init { | ||
|
||
file_line_append {'test file line append': | ||
name => "test", | ||
value => "test" | ||
} | ||
|
||
class file_line_append { | ||
} |