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
Source
# File lib/serverkit/resources/line.rb, line 24 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
@note Override
Source
# File lib/serverkit/resources/line.rb, line 35 def check has_correct_file? && has_correct_line? end
@note Override
Private Instance Methods
Source
# File lib/serverkit/resources/line.rb, line 46 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
@return [String]
Source
# File lib/serverkit/resources/line.rb, line 59 def content Content.new(get_remote_file_content) end
@return [Serverkit::Resources::Line::Content]
Source
# File lib/serverkit/resources/line.rb, line 64 def get_remote_file_content run_command_from_identifier(:get_file_content, path).stdout end
@return [String]
Source
# File lib/serverkit/resources/line.rb, line 68 def has_correct_file? check_command_from_identifier(:check_file_is_file, path) end
Source
# File lib/serverkit/resources/line.rb, line 72 def has_correct_line? if present? && !has_matched_line? false elsif !present? && has_matched_line? false else true end end
Source
# File lib/serverkit/resources/line.rb, line 82 def has_matched_line? if pattern content.match(Regexp.new(pattern)) else content.match(line) end end
Source
# File lib/serverkit/resources/line.rb, line 90 def present? state == "present" end
Source
# File lib/serverkit/resources/line.rb, line 97 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
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.
Source
# File lib/serverkit/resources/line.rb, line 106 def update_remote_file_content_without_validation update_remote_file_content(content: applied_remote_file_content, path: path) end
Create or update remote file