Skip to content

Commit

Permalink
Add --domain option to maintenance commands (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelgomesxyz authored Jan 12, 2024
1 parent 69f2567 commit 347b33c
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ _Please add entries here for your pull requests that are not yet released._
### Added

- Added `--clean` option to `run:detached` command to delete workload when disconnecting. [PR 129](https://github.com/shakacode/heroku-to-control-plane/pull/129) by [Rafael Gomes](https://github.com/rafaelgomesxyz).
- Added `--domain` option to `maintenance`, `maintenance:on` and `maintenance:off` commands. [PR 131](https://github.com/shakacode/heroku-to-control-plane/pull/131) by [Rafael Gomes](https://github.com/rafaelgomesxyz).
- Added `default_domain` config to specify domain for `maintenance`, `maintenance:on` and `maintenance:off` commands. [PR 131](https://github.com/shakacode/heroku-to-control-plane/pull/131) by [Rafael Gomes](https://github.com/rafaelgomesxyz).

### Changed

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ apps:
# This is relative to the `.controlplane/` directory.
release_script: release_script

# default_domain is used for commands that require a domain
# including `maintenance`, `maintenance:on`, `maintenance:off`.
default_domain: domain.com

my-app-other:
<<: *common

Expand Down
4 changes: 4 additions & 0 deletions examples/controlplane.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ apps:
# This is relative to the `.controlplane/` directory.
release_script: release_script

# default_domain is used for commands that require a domain
# including `maintenance`, `maintenance:on`, `maintenance:off`.
default_domain: domain.com

my-app-other:
<<: *common

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

def self.domain_option(required: false)
{
name: :domain,
params: {
banner: "DOMAIN_NAME",
desc: "Domain name",
type: :string,
required: required
}
}
end

def self.upstream_token_option(required: false)
{
name: :upstream_token,
Expand Down
9 changes: 7 additions & 2 deletions lib/command/maintenance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module Command
class Maintenance < Base
NAME = "maintenance"
OPTIONS = [
app_option(required: true)
app_option(required: true),
domain_option
].freeze
DESCRIPTION = "Checks if maintenance mode is on or off for an app"
LONG_DESCRIPTION = <<~DESC
Expand All @@ -20,7 +21,11 @@ def call # rubocop:disable Metrics/MethodLength
one_off_workload = config[:one_off_workload]
maintenance_workload = config.current[:maintenance_workload] || "maintenance"

domain_data = cp.find_domain_for([one_off_workload, maintenance_workload])
domain_data = if config.domain
cp.fetch_domain(config.domain)
else
cp.find_domain_for([one_off_workload, maintenance_workload])
end
unless domain_data
raise "Can't find domain. " \
"Maintenance mode is only supported for domains that use path based routing mode " \
Expand Down
9 changes: 7 additions & 2 deletions lib/command/maintenance_off.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module Command
class MaintenanceOff < Base
NAME = "maintenance:off"
OPTIONS = [
app_option(required: true)
app_option(required: true),
domain_option
].freeze
DESCRIPTION = "Disables maintenance mode for an app"
LONG_DESCRIPTION = <<~DESC
Expand All @@ -18,7 +19,11 @@ def call # rubocop:disable Metrics/MethodLength
one_off_workload = config[:one_off_workload]
maintenance_workload = config.current[:maintenance_workload] || "maintenance"

domain_data = cp.find_domain_for([one_off_workload, maintenance_workload])
domain_data = if config.domain
cp.fetch_domain(config.domain)
else
cp.find_domain_for([one_off_workload, maintenance_workload])
end
unless domain_data
raise "Can't find domain. " \
"Maintenance mode is only supported for domains that use path based routing mode " \
Expand Down
9 changes: 7 additions & 2 deletions lib/command/maintenance_on.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module Command
class MaintenanceOn < Base
NAME = "maintenance:on"
OPTIONS = [
app_option(required: true)
app_option(required: true),
domain_option
].freeze
DESCRIPTION = "Enables maintenance mode for an app"
LONG_DESCRIPTION = <<~DESC
Expand All @@ -18,7 +19,11 @@ def call # rubocop:disable Metrics/MethodLength
one_off_workload = config[:one_off_workload]
maintenance_workload = config.current[:maintenance_workload] || "maintenance"

domain_data = cp.find_domain_for([one_off_workload, maintenance_workload])
domain_data = if config.domain
cp.fetch_domain(config.domain)
else
cp.find_domain_for([one_off_workload, maintenance_workload])
end
unless domain_data
raise "Can't find domain. " \
"Maintenance mode is only supported for domains that use path based routing mode " \
Expand Down
14 changes: 14 additions & 0 deletions lib/core/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def location
@location ||= load_location_from_options || load_location_from_env || load_location_from_file
end

def domain
@domain ||= load_domain_from_options || load_domain_from_file
end

def [](key)
ensure_current_config!

Expand Down Expand Up @@ -253,6 +257,16 @@ def load_location_from_file
strip_str_and_validate(current.fetch(:default_location))
end

def load_domain_from_options
strip_str_and_validate(options[:domain])
end

def load_domain_from_file
return unless current&.key?(:default_domain)

strip_str_and_validate(current.fetch(:default_domain))
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
10 changes: 9 additions & 1 deletion lib/core/controlplane.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,18 @@ def find_domain_for(workloads)
route = find_domain_route(domain_data)
next false if route.nil?

workloads.any? { |workload| route["workloadLink"].split("/").last == workload }
workloads.any? { |workload| route["workloadLink"].match?(%r{/org/#{org}/gvc/#{gvc}/workload/#{workload}}) }
end
end

def fetch_domain(domain)
domain_data = api.fetch_domain(org: org, domain: domain)
route = find_domain_route(domain_data)
return nil if route.nil?

domain_data
end

def get_domain_workload(data)
route = find_domain_route(data)
route["workloadLink"].split("/").last
Expand Down
4 changes: 4 additions & 0 deletions lib/core/controlplane_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def delete_volumeset(org:, gvc:, volumeset:)
api_json("/org/#{org}/gvc/#{gvc}/volumeset/#{volumeset}", method: :delete)
end

def fetch_domain(org:, domain:)
api_json("/org/#{org}/domain/#{domain}", method: :get)
end

def list_domains(org:)
api_json("/org/#{org}/domain", method: :get)
end
Expand Down

0 comments on commit 347b33c

Please sign in to comment.