Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve file resource performance #310

Merged
merged 1 commit into from
Apr 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/itamae/resource/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class File < Base
define_attribute :group, type: String
define_attribute :block, type: Proc, default: proc {}

class << self
attr_accessor :sha256sum_available
end

def pre_action
current.exist = run_specinfra(:check_file_is_file, attributes.path)

Expand All @@ -27,6 +31,11 @@ def pre_action
end
end

if exists_and_not_modified?
attributes.modified = false
return
end

send_tempfile
compare_file
end
Expand Down Expand Up @@ -133,6 +142,19 @@ def compare_file
end
end

def exists_and_not_modified?
return false unless current.exist && sha256sum_available?

current_digest = run_command(["sha256sum", attributes.path]).stdout.split(/\s/, 2).first
digest = if content_file
Digest::SHA256.file(content_file).hexdigest
else
Digest::SHA256.hexdigest(attributes.content.to_s)
end

current_digest == digest
end

def show_content_diff
if attributes.modified
Itamae.logger.info "diff:"
Expand Down Expand Up @@ -194,6 +216,12 @@ def send_tempfile
f.unlink if f
end
end

def sha256sum_available?
return self.class.sha256sum_available unless self.class.sha256sum_available.nil?

self.class.sha256sum_available = run_command(["sha256sum", "--version"], error: false).exit_status == 0
end
end
end
end