Class: Command::CleanupStaleApps

Inherits:
Base
  • Object
show all
Defined in:
lib/command/cleanup_stale_apps.rb

Constant Summary collapse

CLEANUP_MODE_OPTION =
{
  name: :mode,
  params: {
    banner: "MODE",
    desc: "Action to take on stale apps: `delete` (default) or `stop`",
    type: :string,
    required: false,
    default: "delete",
    valid_regex: /^(delete|stop)$/
  }
}.freeze
NAME =
"cleanup-stale-apps"
OPTIONS =
[
  app_option(required: true),
  skip_confirm_option,
  CLEANUP_MODE_OPTION
].freeze
DESCRIPTION =
"Deletes or stops stale apps based on the latest image's creation date"
LONG_DESCRIPTION =
<<~DESC
  - Acts on stale apps based on the creation date of the latest image, or the GVC if no images exist
  - With `--mode=delete` (default): deletes the whole app (GVC with all workloads, all volumesets and all images), and unbinds the app from the secrets policy as long as both the identity and the policy exist (and are bound)
  - With `--mode=stop`: suspends all workloads via `cpflow ps:stop` — no GVC, volumeset, or image is removed; resume with `cpflow ps:start`
  - `--mode=stop` only suspends workloads listed in `app_workloads` + `additional_workloads`; workloads present in the live GVC but missing from the config are skipped silently
  - `--mode=stop` returns once each workload is marked suspended; it does not wait for the workload to reach a not-ready state
  - Specify the amount of days after an app should be considered stale through `stale_app_image_deployed_days` in the `.controlplane/controlplane.yml` file
  - If `match_if_app_name_starts_with` is `true` in the `.controlplane/controlplane.yml` file, it will act on all stale apps that start with the name
  - Will ask for explicit user confirmation
DESC
EXAMPLES =
<<~EX
  ```sh
  # Deletes stale apps (default).
  cpflow cleanup-stale-apps -a $APP_NAME

  # Stops stale apps instead of deleting them; resume with `cpflow ps:start`.
  cpflow cleanup-stale-apps -a $APP_NAME --mode=stop
  ```
EX

Constants inherited from Base

Base::ACCEPTS_EXTRA_OPTIONS, Base::ALL_VALIDATIONS, Base::DEFAULT_ARGS, Base::HIDE, Base::REQUIRES_ARGS, Base::REQUIRES_STARTUP_CHECKS, Base::SUBCOMMAND_NAME, Base::USAGE, Base::VALIDATIONS, Base::VALIDATIONS_WITHOUT_ADDITIONAL_OPTIONS, Base::VALIDATIONS_WITH_ADDITIONAL_OPTIONS, Base::WITH_INFO_HEADER

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

add_app_identity_option, all_commands, all_options, all_options_by_key_name, app_option, #args_join, commit_option, common_options, #cp, cpu_option, detached_option, dir_option, docker_context_option, domain_option, #ensure_docker_running!, entrypoint_option, image_option, #initialize, interactive_option, location_option, log_method_option, logs_limit_option, logs_since_option, memory_option, org_option, #progress, replica_option, #run_command_in_latest_image, #run_cpflow_command, run_release_phase_option, skip_confirm_option, skip_post_creation_hook_option, skip_pre_deletion_hook_option, skip_secret_access_binding_option, skip_secrets_setup_option, staging_branch_option, #step, #step_finish, terminal_size_option, trace_option, upstream_token_option, use_digest_image_ref_option, use_local_token_option, validations_option, verbose_option, version_option, wait_option, #with_retry, workload_option

Methods included from Helpers

normalize_command_name, normalize_option_name, random_four_digits, strip_str_and_validate

Constructor Details

This class inherits a constructor from Command::Base

Instance Method Details

#callObject

rubocop:disable Metrics/MethodLength



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/command/cleanup_stale_apps.rb', line 44

def call # rubocop:disable Metrics/MethodLength
  return progress.puts("No stale apps found.") if stale_apps.empty?

  progress.puts("Stale apps:")
  stale_apps.each do |app|
    progress.puts("  - #{app[:name]} (#{Shell.color(app[:date].to_s, :red)})")
  end

  return unless confirm_action

  progress.puts
  stale_apps.each do |app|
    process_app(app[:name])
    progress.puts
  end
end