Class: Command::Run

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

Constant Summary collapse

NAME =
"run"
USAGE =
"run COMMAND"
REQUIRES_ARGS =
true
DEFAULT_ARGS =
["bash"].freeze
OPTIONS =
[
  app_option(required: true),
  image_option,
  workload_option,
  location_option,
  use_local_token_option,
  terminal_size_option
].freeze
DESCRIPTION =
"Runs one-off **_interactive_** replicas (analog of `heroku run`)"
LONG_DESCRIPTION =
<<~DESC
  - Runs one-off **_interactive_** replicas (analog of `heroku run`)
  - Uses `Standard` workload type and `cpln exec` as the execution method, with CLI streaming
  - If `fix_terminal_size` is `true` in the `.controlplane/controlplane.yml` file, the remote terminal size will be fixed to match the local terminal size (may also be overriden through `--terminal-size`)

  > **IMPORTANT:** Useful for development where it's needed for interaction, and where network connection drops and
  > task crashing are tolerable. For production tasks, it's better to use `cpl run:detached`.
DESC
EXAMPLES =
<<~EX
  ```sh
  # Opens shell (bash by default).
  cpl run -a $APP_NAME

  # Need to quote COMMAND if setting ENV value or passing args.
  cpl run -a $APP_NAME -- 'LOG_LEVEL=warn rails db:migrate'

  # Runs command, displays output, and exits shell.
  cpl run -a $APP_NAME -- ls /
  cpl run -a $APP_NAME -- rails db:migrate:status

  # Runs command and keeps shell open.
  cpl run -a $APP_NAME -- rails c

  # Uses a different image (which may not be promoted yet).
  cpl run -a $APP_NAME --image appimage:123 -- rails db:migrate # Exact image name
  cpl run -a $APP_NAME --image latest -- rails db:migrate       # Latest sequential image

  # Uses a different workload than `one_off_workload` from `.controlplane/controlplane.yml`.
  cpl run -a $APP_NAME -w other-workload -- bash

  # Overrides remote CPLN_TOKEN env variable with local token.
  # Useful when superuser rights are needed in remote container.
  cpl run -a $APP_NAME --use-local-token -- bash
  ```
EX

Constants inherited from Base

Base::ACCEPTS_EXTRA_OPTIONS, Base::HIDE, Base::NO_IMAGE_AVAILABLE, Base::WITH_INFO_HEADER

Instance Attribute Summary collapse

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

all_commands, all_options, all_options_by_key_name, #app_identity, #app_identity_link, #app_image_link, #app_location_link, app_option, #app_secrets, #app_secrets_policy, #args_join, clean_on_failure_option, commit_option, common_options, #cp, domain_option, #ensure_workload_deleted, #extract_image_commit, image_option, #initialize, #latest_image, #latest_image_from, #latest_image_next, location_option, org_option, #perform!, #progress, run_release_phase_option, skip_confirm_option, skip_secret_access_binding_option, #step, #step_error, #step_finish, terminal_size_option, trace_option, upstream_token_option, use_local_token_option, verbose_option, version_option, #wait_for_replica, #wait_for_workload, wait_option, workload_option

Methods included from Helpers

#random_four_digits, #strip_str_and_validate

Constructor Details

This class inherits a constructor from Command::Base

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



54
55
56
# File 'lib/command/run.rb', line 54

def container
  @container
end

#locationObject (readonly)

Returns the value of attribute location.



54
55
56
# File 'lib/command/run.rb', line 54

def location
  @location
end

#workload_cloneObject (readonly)

Returns the value of attribute workload_clone.



54
55
56
# File 'lib/command/run.rb', line 54

def workload_clone
  @workload_clone
end

#workload_to_cloneObject (readonly)

Returns the value of attribute workload_to_clone.



54
55
56
# File 'lib/command/run.rb', line 54

def workload_to_clone
  @workload_to_clone
end

Instance Method Details

#callObject

rubocop:disable Metrics/MethodLength



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/command/run.rb', line 56

def call # rubocop:disable Metrics/MethodLength
  @location = config.location
  @workload_to_clone = config.options["workload"] || config[:one_off_workload]
  @workload_clone = "#{workload_to_clone}-run-#{random_four_digits}"

  step("Cloning workload '#{workload_to_clone}' on app '#{config.options[:app]}' to '#{workload_clone}'") do
    clone_workload
  end

  wait_for_workload(workload_clone)
  wait_for_replica(workload_clone, location)
  run_in_replica
ensure
  progress.puts
  ensure_workload_deleted(workload_clone)
end