Skip to content
Merged
Show file tree
Hide file tree
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
53 changes: 25 additions & 28 deletions lib/puppet/provider/iop_frontend/podman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,56 @@
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
Puppet.info("Deploying new frontend content to '#{resource[:destination]}'")

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
Puppet.info("Removing frontend at '#{resource[:destination]}'")
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')

Expand All @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion lib/puppet/type/iop_frontend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,26 @@
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|
unless value.is_a?(String)
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
Expand Down