diff --git a/lib/puppet/provider/iop_frontend/podman.rb b/lib/puppet/provider/iop_frontend/podman.rb index 5a9dd7e..e00fb2e 100644 --- a/lib/puppet/provider/iop_frontend/podman.rb +++ b/lib/puppet/provider/iop_frontend/podman.rb @@ -12,21 +12,15 @@ commands runtime: 'podman' def exists? - desired_checksum = get_image_content_checksum - - unless File.directory?(resource[:destination]) - Puppet.debug("Destination #{resource[:destination]} not found. Resource does not exist.") - return false - end - - current_checksum = get_deployed_checksum - - in_sync = current_checksum == desired_checksum - Puppet.debug("Current checksum: #{current_checksum}, Desired checksum: #{desired_checksum}. In sync: #{in_sync}") + File.exist?(metadata_file) + end - cleanup(nil, @staged_content_path) if in_sync + def image + get_deployed_checksum.to_s + end - in_sync + def image=(value) + create end def create @@ -34,18 +28,15 @@ def create destroy if File.directory?(resource[:destination]) - Puppet.debug("Copying staged content from '#{@staged_content_path}' to '#{resource[:destination]}'") + staged_content_path, new_content_checksum = stage_content + Puppet.debug("Copying staged content from '#{staged_content_path}' to '#{resource[:destination]}'") Dir.mkdir(resource[:destination], 0755) - FileUtils.cp_r(Dir.glob(File.join(@staged_content_path, '*')), resource[:destination]) + FileUtils.cp_r(Dir.glob(File.join(staged_content_path, '*')), resource[:destination]) restore_selinux_context(resource[:destination]) - metadata_file = File.join(resource[:destination], '.iop_frontend_checksum') - Puppet.debug("Writing new checksum #{@new_content_checksum} to #{metadata_file}") - File.write(metadata_file, @new_content_checksum) - - @staged_content_path = nil - @new_content_checksum = nil + Puppet.debug("Writing new checksum #{new_content_checksum} to #{metadata_file}") + File.write(metadata_file, new_content_checksum) end def destroy @@ -53,18 +44,24 @@ def destroy FileUtils.rm_rf(resource[:destination]) end + def content_checksum + path, checksum = stage_content + checksum + end + private + def metadata_file + File.join(resource[:destination], '.iop_frontend_checksum') + end + def get_deployed_checksum - metadata_file = File.join(resource[:destination], '.iop_frontend_checksum') return nil unless File.exist?(metadata_file) File.read(metadata_file).strip end - def get_image_content_checksum - return @new_content_checksum if @new_content_checksum - + def stage_content temp_container_name = "iop-frontend-checker-#{resource.title.gsub(/[^0-9a-zA-Z]/, '-')}" staging_dir = Dir.mktmpdir('iop_frontend_check') @@ -73,10 +70,10 @@ def get_image_content_checksum source_in_container = "#{temp_container_name}:#{resource[:source_path]}/." execute(['podman', 'cp', source_in_container, staging_dir]) - @new_content_checksum = calculate_checksum_for_path(staging_dir) - @staged_content_path = staging_dir + new_content_checksum = calculate_checksum_for_path(staging_dir) + staged_content_path = staging_dir - return @new_content_checksum + return staged_content_path, new_content_checksum rescue Puppet::ExecutionFailure => e Puppet.err("Failed to get content checksum from image '#{resource[:image]}': #{e.message}") cleanup(temp_container_name, staging_dir) diff --git a/lib/puppet/type/iop_frontend.rb b/lib/puppet/type/iop_frontend.rb index 073a097..0494996 100644 --- a/lib/puppet/type/iop_frontend.rb +++ b/lib/puppet/type/iop_frontend.rb @@ -13,7 +13,7 @@ end end - newparam(:image) do + newproperty(:image) do desc 'The full name of the container image to pull, including the tag (e.g., "registry.example.com/my-app:latest").' validate do |value| @@ -21,6 +21,18 @@ raise ArgumentError, "Image must be a string, not '#{value.class}'" end end + + def content_checksum + provider.content_checksum.to_s + end + + def should_to_s(newvalue) + self.class.format_value_for_display(content_checksum) + end + + def insync?(is) + is.to_s == content_checksum + end end newparam(:source_path) do