From e1c6d716d389fcbe943b3b95c167e58310ae8b0c Mon Sep 17 00:00:00 2001 From: Grant Slater Date: Wed, 23 Apr 2025 13:39:32 +0100 Subject: [PATCH] awscli: keep last 5 versions Keep old versions around for a bit else existing running processes can fail. eg: SSL validation failed for https[...][Errno 2] No such file or directory --- cookbooks/awscli/recipes/default.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cookbooks/awscli/recipes/default.rb b/cookbooks/awscli/recipes/default.rb index 195fafed8..90574c3bb 100644 --- a/cookbooks/awscli/recipes/default.rb +++ b/cookbooks/awscli/recipes/default.rb @@ -88,10 +88,12 @@ ruby_block "install-awscli" do FileUtils.rm_f("/opt/awscli/v2/current") FileUtils.ln_sf(install_dir, "/opt/awscli/v2/current") - # Remove old versions, keeping only the current one - Dir.glob("/opt/awscli/v2/*").each do |dir| - next if [install_dir, "/opt/awscli/v2/current"].include?(dir) + # Retain the last 5 versions, including the current one + versions = Dir.glob("/opt/awscli/v2/*").select { |dir| File.directory?(dir) && dir != "/opt/awscli/v2/current" } + versions.sort_by! { |dir| File.mtime(dir) }.reverse! + versions_to_delete = versions[5..] || [] + versions_to_delete.each do |dir| FileUtils.rm_rf(dir) end end -- 2.39.5