class Serverkit::Resources::Line
Ensure a particular line is in a file, or replace an existing line using regexp. @example Example line resource that ensures a line is in /etc/sudoers
- type: line path: /etc/sudoers line: "#includedir /etc/sudoers.d"
Constants
- DEFAULT_STATE
Public Instance Methods
apply()
click to toggle source
@note Override
# File lib/serverkit/resources/line.rb, line 22 def apply if has_correct_file? if validation_script update_remote_file_content_with_validation else update_remote_file_content_without_validation end end end
check()
click to toggle source
@note Override
# File lib/serverkit/resources/line.rb, line 33 def check has_correct_file? && has_correct_line? end
Private Instance Methods
absent?()
click to toggle source
# File lib/serverkit/resources/line.rb, line 39 def absent? state == "absent" end
applied_remote_file_content()
click to toggle source
@return [String]
# File lib/serverkit/resources/line.rb, line 44 def applied_remote_file_content if absent? content.delete(line) elsif insert_after content.insert_after(Regexp.new(insert_after), line) elsif insert_before content.insert_before(Regexp.new(insert_before), line) else content.append(line) end.to_s end
content()
click to toggle source
@return [Serverkit::Resources::Line::Content]
# File lib/serverkit/resources/line.rb, line 57 def content Content.new(get_remote_file_content) end
get_remote_file_content()
click to toggle source
@return [String]
# File lib/serverkit/resources/line.rb, line 62 def get_remote_file_content run_command_from_identifier(:get_file_content, path).stdout end
has_correct_file?()
click to toggle source
# File lib/serverkit/resources/line.rb, line 66 def has_correct_file? check_command_from_identifier(:check_file_is_file, path) end
has_correct_line?()
click to toggle source
# File lib/serverkit/resources/line.rb, line 70 def has_correct_line? if present? && !has_matched_line? false elsif !present? && has_matched_line? false else true end end
has_matched_line?()
click to toggle source
# File lib/serverkit/resources/line.rb, line 80 def has_matched_line? if pattern content.match(Regexp.new(pattern)) else content.match(line) end end
present?()
click to toggle source
# File lib/serverkit/resources/line.rb, line 88 def present? state == "present" end
update_remote_file_content_with_validation()
click to toggle source
Create a new temp file on remote side, then validate it with given script, and move it to right file path if it succeeded. Note that ‘%{path}` in validation script will be replaced with the path to temp file.
# File lib/serverkit/resources/line.rb, line 95 def update_remote_file_content_with_validation temp_path = create_remote_temp_file(applied_remote_file_content) if check_command(format(validation_script, path: temp_path)) move_remote_file_keeping_destination_metadata(temp_path, path) end run_command_from_identifier(:remove_file, temp_path) end
update_remote_file_content_without_validation()
click to toggle source
Create or update remote file
# File lib/serverkit/resources/line.rb, line 104 def update_remote_file_content_without_validation update_remote_file_content(content: applied_remote_file_content, path: path) end