Class: Command::SetupApp

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

Constant Summary collapse

NAME =
"setup-app"
OPTIONS =
[
  app_option(required: true),
  skip_secret_access_binding_option,
  skip_secrets_setup_option,
  skip_post_creation_hook_option
].freeze
DESCRIPTION =
"Creates an app and all its workloads"
LONG_DESCRIPTION =
<<~DESC
  - Creates an app and all its workloads
  - Specify the templates for the app and workloads through `setup_app_templates` in the `.controlplane/controlplane.yml` file
  - Use this for temporary apps like review apps and for first-time bootstrap of persistent staging or production apps; after a persistent app exists, use 'cpflow apply-template' for template updates
  - Configures app to have org-level secrets with default name `"{APP_PREFIX}-secrets"`
    using org-level policy with default name `"{APP_PREFIX}-secrets-policy"` (names can be customized, see docs)
  - Creates identity for secrets if it does not exist
  - Binds the app identity to any configured `shared_secret_grants` policies as part of the secrets setup flow; skipped when `--skip-secrets-setup` or `--skip-secret-access-binding` is provided, or `skip_secrets_setup` is set
  - Use `--skip-secrets-setup` to prevent the automatic setup of secrets,
    or set it through `skip_secrets_setup` in the `.controlplane/controlplane.yml` file
  - Runs a post-creation hook after the app is created if `hooks.post_creation` is specified in the `.controlplane/controlplane.yml` file
  - If the hook exits with a non-zero code, the command will stop executing and also exit with a non-zero code
  - Use `--skip-post-creation-hook` to skip the hook if specified in `controlplane.yml`
DESC
VALIDATIONS =
%w[config templates].freeze

Constants inherited from Base

Base::ACCEPTS_EXTRA_OPTIONS, Base::ALL_VALIDATIONS, Base::DEFAULT_ARGS, Base::EXAMPLES, Base::HIDE, Base::REQUIRES_ARGS, Base::REQUIRES_STARTUP_CHECKS, Base::SUBCOMMAND_NAME, Base::USAGE, 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, #bind_shared_secret_policy_grant, #bind_shared_secret_policy_grants, commit_option, common_options, #cp, cpu_option, detached_option, dir_option, docker_context_option, domain_option, #ensure_docker_running!, #ensure_shared_secret_policy_targets_secret!, entrypoint_option, force_option, #identity_bound_to_policy_with_reveal?, #identity_policy_permissions, image_option, #initialize, interactive_option, location_option, log_method_option, logs_limit_option, logs_since_option, memory_option, org_option, #progress, replica_option, #resolve_shared_secret_policy_grant, #resolve_shared_secret_policy_grants, #run_command_in_latest_image, #run_cpflow_command, run_release_phase_option, #shared_secret_policy_missing_message, #shared_secret_policy_target_links, #shared_secret_policy_targets_secret?, 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/CyclomaticComplexity, Metrics/MethodLength



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/command/setup_app.rb', line 29

def call # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
  templates = config[:setup_app_templates]

  app = cp.fetch_gvc
  if app
    raise "App '#{config.app}' already exists. If you want to update this app, " \
          "either run 'cpflow delete -a #{config.app}' and then re-run this command, " \
          "or run 'cpflow apply-template #{templates.join(' ')} -a #{config.app}'."
  end

  skip_secrets_setup = skip_secrets_setup?

  # Validate shared grants before app resource creation so config/policy
  # drift does not leave a partially-created review app.
  shared_secret_policy_grant_pairs = resolve_shared_secret_policy_grants unless skip_secrets_setup
  create_secret_and_policy_if_not_exist unless skip_secrets_setup

  args = []
  args.push("--add-app-identity") unless skip_secrets_setup
  run_cpflow_command("apply-template", *templates, "-a", config.app, *args)

  bind_identity_to_policy unless skip_secrets_setup
  bind_shared_secret_policy_grants(shared_secret_policy_grant_pairs) unless skip_secrets_setup
  run_post_creation_hook unless config.options[:skip_post_creation_hook]
end