Skip to content

Commit

Permalink
Accept --location option (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahangarha authored Dec 20, 2023
1 parent 2de4779 commit c10625a
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ _Please add entries here for your pull requests that are not yet released._
- Added option to set the app with a `CPLN_APP` env var. [PR 88](https://github.com/shakacode/heroku-to-control-plane/pull/88) by [Rafael Gomes](https://github.com/rafaelgomesxyz).
- Show `org` and `app` on every command excluding `info`, `version`, `maintenance`, `env`, `ps`, and `latest_image`. [PR 94](https://github.com/shakacode/heroku-to-control-plane/pull/94) by [Mostafa Ahangarha](https://github.com/ahangarha).
- Added option to only use `CPLN_ORG` and `CPLN_APP` env vars if `allow_org_override_by_env` and `allow_app_override_by_env` configs are set to `true` in `controlplane.yml`. [PR 109](https://github.com/shakacode/heroku-to-control-plane/pull/109) by [Rafael Gomes](https://github.com/rafaelgomesxyz).
- Added `CPLN_LOCATION` env variable and `--location` option for `apply-template`, `ps`, `run`, `run:detached`. [PR 105](https://github.com/shakacode/heroku-to-control-plane/pull/105) by [Mostafa Ahangarha](https://github.com/ahangarha).

### Changed

Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ aliases:
# (provided that `allow_org_override_by_env` is set to `true`).
cpln_org: my-org-staging

# Example apps use only one location. Control Plane offers the ability to use multiple locations.
# Control Plane offers the ability to use multiple locations.
# default_location is used for commands that require a location
# including `ps`, `run`, `run:detached`, `apply-template`.
# This can be overridden with option --location=<location> and
# CPLN_LOCATION environment variable.
default_location: aws-us-east-2

# Allows running the command `cpl setup-app`
Expand Down
7 changes: 6 additions & 1 deletion examples/controlplane.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ aliases:
# (provided that `allow_org_override_by_env` is set to `true`).
cpln_org: my-org-staging

# Example apps use only one location. Control Plane offers the ability to use multiple locations.
# Control Plane offers the ability to use multiple locations.
# default_location is used for commands that require a location
# including `ps`, `run`, `run:detached`, `apply-template`.
# This can be overridden with option --location=<location> and
# CPLN_LOCATION environment variable.
# TODO: Allow specification of multiple locations.
default_location: aws-us-east-2

# Allows running the command `cpl setup-app`
Expand Down
3 changes: 2 additions & 1 deletion lib/command/apply_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ApplyTemplate < Base # rubocop:disable Metrics/ClassLength
REQUIRES_ARGS = true
OPTIONS = [
app_option(required: true),
location_option,
skip_confirm_option
].freeze
DESCRIPTION = "Applies application-specific configs from templates"
Expand Down Expand Up @@ -126,7 +127,7 @@ def confirm_workload(template)
def apply_template(filename)
data = File.read(filename)
.gsub("APP_GVC", config.app)
.gsub("APP_LOCATION", config[:default_location])
.gsub("APP_LOCATION", config.location)
.gsub("APP_ORG", config.org)
.gsub("APP_IMAGE", latest_image)

Expand Down
13 changes: 13 additions & 0 deletions lib/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ def self.commit_option(required: false)
}
end

def self.location_option(required: false)
{
name: :location,
params: {
aliases: ["-l"],
banner: "LOCATION_NAME",
desc: "Location name",
type: :string,
required: required
}
}
end

def self.upstream_token_option(required: false)
{
name: :upstream_token,
Expand Down
5 changes: 4 additions & 1 deletion lib/command/ps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Ps < Base
NAME = "ps"
OPTIONS = [
app_option(required: true),
location_option,
workload_option
].freeze
DESCRIPTION = "Shows running replicas in app"
Expand All @@ -25,12 +26,14 @@ class Ps < Base
def call
cp.fetch_gvc!

location = config.location

workloads = [config.options[:workload]] if config.options[:workload]
workloads ||= config[:app_workloads] + config[:additional_workloads]
workloads.each do |workload|
cp.fetch_workload!(workload)

result = cp.workload_get_replicas(workload, location: config[:default_location])
result = cp.workload_get_replicas(workload, location: location)
result["items"].each { |replica| puts replica }
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/command/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Run < Base
app_option(required: true),
image_option,
workload_option,
location_option,
use_local_token_option,
terminal_size_option
].freeze
Expand Down Expand Up @@ -56,7 +57,7 @@ class Run < Base
attr_reader :location, :workload, :one_off, :container

def call # rubocop:disable Metrics/MethodLength
@location = config[:default_location]
@location = config.location
@workload = config.options["workload"] || config[:one_off_workload]
@one_off = "#{workload}-run-#{rand(1000..9999)}"

Expand Down
3 changes: 2 additions & 1 deletion lib/command/run_detached.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class RunDetached < Base # rubocop:disable Metrics/ClassLength
app_option(required: true),
image_option,
workload_option,
location_option,
use_local_token_option
].freeze
DESCRIPTION = "Runs one-off **_non-interactive_** replicas (close analog of `heroku run:detached`)"
Expand Down Expand Up @@ -47,7 +48,7 @@ class RunDetached < Base # rubocop:disable Metrics/ClassLength
attr_reader :location, :workload, :one_off, :container

def call # rubocop:disable Metrics/MethodLength
@location = config[:default_location]
@location = config.location
@workload = config.options["workload"] || config[:one_off_workload]
@one_off = "#{workload}-runner-#{rand(1000..9999)}"

Expand Down
18 changes: 18 additions & 0 deletions lib/core/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def app
@app ||= load_app_from_options || load_app_from_env
end

def location
@location ||= load_location_from_options || load_location_from_env || load_location_from_file
end

def [](key)
ensure_current_config!

Expand Down Expand Up @@ -230,6 +234,20 @@ def load_org_from_file
strip_str_and_validate(current[:cpln_org])
end

def load_location_from_options
strip_str_and_validate(options[:location])
end

def load_location_from_env
strip_str_and_validate(ENV.fetch("CPLN_LOCATION", nil))
end

def load_location_from_file
return unless current&.key?(:default_location)

strip_str_and_validate(current.fetch(:default_location))
end

def warn_deprecated_options(app_options)
deprecated_option_keys = new_option_keys.select { |old_key| app_options.key?(old_key) }
return if deprecated_option_keys.empty?
Expand Down

0 comments on commit c10625a

Please sign in to comment.