Class: Command::Run

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

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

INTERACTIVE_COMMANDS =
[
  "bash",
  "rails console",
  "rails c",
  "rails dbconsole",
  "rails db"
].freeze
NAME =
"run"
USAGE =
"run COMMAND"
REQUIRES_ARGS =
true
DEFAULT_ARGS =
["bash"].freeze
OPTIONS =
[
  app_option(required: true),
  image_option,
  log_method_option,
  workload_option,
  location_option,
  use_local_token_option,
  terminal_size_option,
  interactive_option,
  detached_option,
  cpu_option,
  memory_option,
  entrypoint_option
].freeze
DESCRIPTION =
"Runs one-off interactive or non-interactive replicas (analog of `heroku run`)"
LONG_DESCRIPTION =
<<~DESC
  - Runs one-off interactive or non-interactive replicas (analog of `heroku run`)
  - Uses `Cron` workload type and either:
  - - `cpln workload exec` for interactive mode, with CLI streaming
  - - log async fetching for non-interactive mode
  - The Dockerfile entrypoint is used as the command by default, which assumes `exec "${@}"` to be present,
    and the args ["bash", "-c", cmd_to_run] are passed
  - The entrypoint can be overriden through `--entrypoint`, which must be a single command or a script path that exists in the container,
    and the args ["bash", "-c", cmd_to_run] are passed,
    unless the entrypoint is `bash`, in which case the args ["-c", cmd_to_run] are passed
  - Providing `--entrypoint none` sets the entrypoint to `bash` by default
  - 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`)
DESC
EXAMPLES =
<<~EX
  ```sh
  # Opens shell (bash by default).
  cpl run -a $APP_NAME

  # Runs interactive command, keeps shell open, and stops job when exiting.
  cpl run -a $APP_NAME --interactive -- rails c

  # Some commands are automatically detected as interactive, so no need to pass `--interactive`.
  #{INTERACTIVE_COMMANDS.map { |cmd| "cpl run -a $APP_NAME -- #{cmd}" }.join("\n      ")}

  # Runs non-interactive command, outputs logs, exits with the exit code of the command and stops job.
  cpl run -a $APP_NAME -- rails db:migrate

  # Runs non-iteractive command, detaches, exits with 0, and prints commands to:
  # - see logs from the job
  # - stop the job
  cpl run -a $APP_NAME --detached -- rails db:migrate

  # The command needs to be quoted if setting an env variable or passing args.
  cpl run -a $APP_NAME -- 'SOME_ENV_VAR=some_value rails db:migrate'

  # 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

  # Replaces the existing Dockerfile entrypoint with `bash`.
  cpl run -a $APP_NAME --entrypoint none -- rails db:migrate

  # Replaces the existing Dockerfile entrypoint.
  cpl run -a $APP_NAME --entrypoint /app/alternative-entrypoint.sh -- rails db:migrate
  ```
EX
MAGIC_END =
"---cpl run command finished---"

Constants inherited from Base

Base::ACCEPTS_EXTRA_OPTIONS, Base::ALL_VALIDATIONS, Base::HIDE, Base::VALIDATIONS, Base::VALIDATIONS_WITHOUT_ADDITIONAL_OPTIONS, Base::VALIDATIONS_WITH_ADDITIONAL_OPTIONS, Base::WITH_INFO_HEADER

Instance Attribute Summary collapse

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, 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_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, #step, #step_error, #step_finish, terminal_size_option, trace_option, upstream_token_option, use_local_token_option, validations_option, verbose_option, version_option, wait_option, 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 Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



89
90
91
# File 'lib/command/run.rb', line 89

def command
  @command
end

#containerObject (readonly)

Returns the value of attribute container.



89
90
91
# File 'lib/command/run.rb', line 89

def container
  @container
end

#detachedObject (readonly)

Returns the value of attribute detached.



89
90
91
# File 'lib/command/run.rb', line 89

def detached
  @detached
end

#expected_deployed_versionObject (readonly)

Returns the value of attribute expected_deployed_version.



89
90
91
# File 'lib/command/run.rb', line 89

def expected_deployed_version
  @expected_deployed_version
end

#interactiveObject (readonly)

Returns the value of attribute interactive.



89
90
91
# File 'lib/command/run.rb', line 89

def interactive
  @interactive
end

#jobObject (readonly)

Returns the value of attribute job.



89
90
91
# File 'lib/command/run.rb', line 89

def job
  @job
end

#locationObject (readonly)

Returns the value of attribute location.



89
90
91
# File 'lib/command/run.rb', line 89

def location
  @location
end

#original_workloadObject (readonly)

Returns the value of attribute original_workload.



89
90
91
# File 'lib/command/run.rb', line 89

def original_workload
  @original_workload
end

#replicaObject (readonly)

Returns the value of attribute replica.



89
90
91
# File 'lib/command/run.rb', line 89

def replica
  @replica
end

#runner_workloadObject (readonly)

Returns the value of attribute runner_workload.



89
90
91
# File 'lib/command/run.rb', line 89

def runner_workload
  @runner_workload
end

Instance Method Details

#callObject

rubocop:disable Metrics/MethodLength



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/command/run.rb', line 92

def call # rubocop:disable Metrics/MethodLength
  @interactive = config.options[:interactive] || interactive_command?
  @detached = config.options[:detached]
  @log_method = config.options[:log_method]

  @location = config.location
  @original_workload = config.options[:workload] || config[:one_off_workload]
  @runner_workload = "#{original_workload}-runner"

  unless interactive
    @internal_sigint = false

    # Catch Ctrl+C in the main process
    trap("SIGINT") do
      unless @internal_sigint
        print_detached_commands
        exit(ExitCode::INTERRUPT)
      end
    end
  end

  if cp.fetch_workload(runner_workload).nil?
    create_runner_workload
    wait_for_runner_workload_create
  end
  update_runner_workload
  wait_for_runner_workload_update

  start_job
  wait_for_replica_for_job

  progress.puts
  if interactive
    run_interactive
  else
    run_non_interactive
  end
end