From 04c88761a2841334ad4d82cc2195d19af9cfbdd3 Mon Sep 17 00:00:00 2001 From: Rafael Gomes Date: Fri, 19 Jan 2024 09:55:23 -0300 Subject: [PATCH 01/12] fix: ensure that profile is switched back to default --- lib/command/copy_image_from_upstream.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/command/copy_image_from_upstream.rb b/lib/command/copy_image_from_upstream.rb index be052dff..08b7840d 100644 --- a/lib/command/copy_image_from_upstream.rb +++ b/lib/command/copy_image_from_upstream.rb @@ -38,6 +38,7 @@ def call # rubocop:disable Metrics/MethodLength pull_image_from_upstream push_image_to_app ensure + cp.profile_switch("default") delete_upstream_profile end From 9deb4ecf6514eaf68bbe2dbaf64843eae921a52b Mon Sep 17 00:00:00 2001 From: Rafael Gomes Date: Fri, 19 Jan 2024 09:56:16 -0300 Subject: [PATCH 02/12] docs: update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2ccaa6f..76298ffb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,10 @@ Changes since the last non-beta release. _Please add entries here for your pull requests that are not yet released._ +### Fixed + +- Fixed issue where cpln profile was not switched back to `default` if an error happened while running `copy-image-from-upstream` command. [PR 135](https://github.com/shakacode/heroku-to-control-plane/pull/135) by [Rafael Gomes](https://github.com/rafaelgomesxyz). + ### Added - 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). From 765ebfd5586f0b5b39a37e254bc29b593c65ed1a Mon Sep 17 00:00:00 2001 From: Rafael Gomes Date: Fri, 19 Jan 2024 10:01:59 -0300 Subject: [PATCH 03/12] fix: allow using upstream that has match_if_app_name_starts_with set to true --- lib/command/copy_image_from_upstream.rb | 2 +- lib/core/config.rb | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/command/copy_image_from_upstream.rb b/lib/command/copy_image_from_upstream.rb index 08b7840d..a59f7f31 100644 --- a/lib/command/copy_image_from_upstream.rb +++ b/lib/command/copy_image_from_upstream.rb @@ -29,7 +29,7 @@ def call # rubocop:disable Metrics/MethodLength ensure_docker_running! @upstream = config[:upstream] - @upstream_org = config.apps[@upstream.to_sym][:cpln_org] || ENV.fetch("CPLN_ORG_UPSTREAM", nil) + @upstream_org = config.find_app_config(@upstream)&.dig(:cpln_org) || ENV.fetch("CPLN_ORG_UPSTREAM", nil) ensure_upstream_org! create_upstream_profile diff --git a/lib/core/config.rb b/lib/core/config.rb index fec91235..7cde41fe 100644 --- a/lib/core/config.rb +++ b/lib/core/config.rb @@ -102,6 +102,13 @@ def current end end + def find_app_config(app_name1) + @app_configs ||= {} + @app_configs[app_name1] ||= apps.find do |app_name2, app_config| + app_matches?(app_name1, app_name2, app_config) + end&.last + end + private def ensure_current_config! @@ -127,13 +134,6 @@ def app_matches?(app_name1, app_name2, app_options) ) end - def find_app_config(app_name1) - @app_configs ||= {} - @app_configs[app_name1] ||= apps.find do |app_name2, app_config| - app_matches?(app_name1, app_name2, app_config) - end&.last - end - def ensure_app! return if app From d1fd8812aabc1238741615e81742125c06f8bf5b Mon Sep 17 00:00:00 2001 From: Rafael Gomes Date: Fri, 19 Jan 2024 10:03:37 -0300 Subject: [PATCH 04/12] docs: update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76298ffb..7110a31f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ _Please add entries here for your pull requests that are not yet released._ ### Fixed - Fixed issue where cpln profile was not switched back to `default` if an error happened while running `copy-image-from-upstream` command. [PR 135](https://github.com/shakacode/heroku-to-control-plane/pull/135) by [Rafael Gomes](https://github.com/rafaelgomesxyz). +- Fixed issue that didn't allow using upstream with `match_if_app_name_starts_with` set to `true` in `copy-image-from-upstream` command. [PR 136](https://github.com/shakacode/heroku-to-control-plane/pull/136) by [Rafael Gomes](https://github.com/rafaelgomesxyz). ### Added From 06c1f5881c5e8e9e7251471974538c08c5866478 Mon Sep 17 00:00:00 2001 From: Rafael Gomes Date: Fri, 19 Jan 2024 10:07:49 -0300 Subject: [PATCH 05/12] feat: change precedence for CPLN_ORG_UPSTREAM --- lib/command/copy_image_from_upstream.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/command/copy_image_from_upstream.rb b/lib/command/copy_image_from_upstream.rb index a59f7f31..9eb86325 100644 --- a/lib/command/copy_image_from_upstream.rb +++ b/lib/command/copy_image_from_upstream.rb @@ -29,7 +29,7 @@ def call # rubocop:disable Metrics/MethodLength ensure_docker_running! @upstream = config[:upstream] - @upstream_org = config.find_app_config(@upstream)&.dig(:cpln_org) || ENV.fetch("CPLN_ORG_UPSTREAM", nil) + @upstream_org = ENV.fetch("CPLN_ORG_UPSTREAM", nil) || config.find_app_config(@upstream)&.dig(:cpln_org) ensure_upstream_org! create_upstream_profile From c2130de2159f8f77c26ac745aba1f89fceae4402 Mon Sep 17 00:00:00 2001 From: Rafael Gomes Date: Fri, 19 Jan 2024 10:09:26 -0300 Subject: [PATCH 06/12] docs: update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7110a31f..acca9486 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ _Please add entries here for your pull requests that are not yet released._ ### Changed - `build-image` command now accepts extra options and passes them to `docker build`. [PR 126](https://github.com/shakacode/heroku-to-control-plane/pull/126) by [Rafael Gomes](https://github.com/rafaelgomesxyz). +- `CPLN_ORG_UPSTREAM` env var now takes precedence over config from `controlplane.yml` in `copy-image-from-upstream` command. [PR 137](https://github.com/shakacode/heroku-to-control-plane/pull/137) by [Rafael Gomes](https://github.com/rafaelgomesxyz). ## [1.2.0] - 2024-01-03 From 329808ae04b0d6b975d9d1272fc7bbc26faef2ee Mon Sep 17 00:00:00 2001 From: Rafael Gomes Date: Fri, 19 Jan 2024 10:10:18 -0300 Subject: [PATCH 07/12] test: fix specs --- spec/command/copy_image_from_upstream_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/command/copy_image_from_upstream_spec.rb b/spec/command/copy_image_from_upstream_spec.rb index 1061c5c3..b5059c17 100644 --- a/spec/command/copy_image_from_upstream_spec.rb +++ b/spec/command/copy_image_from_upstream_spec.rb @@ -8,6 +8,7 @@ allow(ENV).to receive(:fetch).with("CPLN_ENDPOINT", "https://api.cpln.io").and_return("https://api.cpln.io") allow(ENV).to receive(:fetch).with("CPLN_TOKEN", nil).and_return("token") allow(ENV).to receive(:fetch).with("CPLN_ORG", nil).and_return(nil) + allow(ENV).to receive(:fetch).with("CPLN_ORG_UPSTREAM", nil).and_return(nil) allow(ENV).to receive(:fetch).with("CPLN_APP", nil).and_return(nil) allow_any_instance_of(Config).to receive(:config_file_path).and_return("spec/fixtures/config.yml") allow_any_instance_of(described_class).to receive(:ensure_docker_running!) From 77e441cad460503bf3f442ef21deada55eb3469a Mon Sep 17 00:00:00 2001 From: Rafael Gomes Date: Fri, 19 Jan 2024 10:15:39 -0300 Subject: [PATCH 08/12] feat: allow specifying upstream through env var --- docs/commands.md | 2 +- lib/command/copy_image_from_upstream.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/commands.md b/docs/commands.md index 9bb48a12..523617ba 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -87,7 +87,7 @@ cpl config -a $APP_NAME ### `copy-image-from-upstream` - Copies an image (by default the latest) from a source org to the current org -- The source org must be specified through `upstream` in the `.controlplane/controlplane.yml` file +- The source app must be specified either through the `CPLN_UPSTREAM` env var or `upstream` in the `.controlplane/controlplane.yml` file - Additionally, the token for the source org must be provided through `--upstream-token` or `-t` - A `cpln` profile will be temporarily created to pull the image from the source org diff --git a/lib/command/copy_image_from_upstream.rb b/lib/command/copy_image_from_upstream.rb index 9eb86325..f2d8d045 100644 --- a/lib/command/copy_image_from_upstream.rb +++ b/lib/command/copy_image_from_upstream.rb @@ -11,7 +11,7 @@ class CopyImageFromUpstream < Base DESCRIPTION = "Copies an image (by default the latest) from a source org to the current org" LONG_DESCRIPTION = <<~DESC - Copies an image (by default the latest) from a source org to the current org - - The source org must be specified through `upstream` in the `.controlplane/controlplane.yml` file + - The source app must be specified either through the `CPLN_UPSTREAM` env var or `upstream` in the `.controlplane/controlplane.yml` file - Additionally, the token for the source org must be provided through `--upstream-token` or `-t` - A `cpln` profile will be temporarily created to pull the image from the source org DESC @@ -28,7 +28,7 @@ class CopyImageFromUpstream < Base def call # rubocop:disable Metrics/MethodLength ensure_docker_running! - @upstream = config[:upstream] + @upstream = ENV.fetch("CPLN_UPSTREAM", nil) || config[:upstream] @upstream_org = ENV.fetch("CPLN_ORG_UPSTREAM", nil) || config.find_app_config(@upstream)&.dig(:cpln_org) ensure_upstream_org! From 438835457bc1fbd6ef4c7fd6ebbd34bb3879cb65 Mon Sep 17 00:00:00 2001 From: Rafael Gomes Date: Fri, 19 Jan 2024 10:16:48 -0300 Subject: [PATCH 09/12] docs: update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index acca9486..d85a6161 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ _Please add entries here for your pull requests that are not yet released._ - 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). +- Added option to specify upstream for `copy-image-from-upstream` command through `CPLN_UPSTREAM` env var. [PR 138](https://github.com/shakacode/heroku-to-control-plane/pull/138) by [Rafael Gomes](https://github.com/rafaelgomesxyz). ### Changed From 0089299d7a6625f3c6333ba563391f2bf4c31901 Mon Sep 17 00:00:00 2001 From: Rafael Gomes Date: Fri, 19 Jan 2024 10:16:55 -0300 Subject: [PATCH 10/12] test: fix specs --- spec/command/copy_image_from_upstream_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/command/copy_image_from_upstream_spec.rb b/spec/command/copy_image_from_upstream_spec.rb index b5059c17..9ce39fb6 100644 --- a/spec/command/copy_image_from_upstream_spec.rb +++ b/spec/command/copy_image_from_upstream_spec.rb @@ -10,6 +10,7 @@ allow(ENV).to receive(:fetch).with("CPLN_ORG", nil).and_return(nil) allow(ENV).to receive(:fetch).with("CPLN_ORG_UPSTREAM", nil).and_return(nil) allow(ENV).to receive(:fetch).with("CPLN_APP", nil).and_return(nil) + allow(ENV).to receive(:fetch).with("CPLN_UPSTREAM", nil).and_return(nil) allow_any_instance_of(Config).to receive(:config_file_path).and_return("spec/fixtures/config.yml") allow_any_instance_of(described_class).to receive(:ensure_docker_running!) allow_any_instance_of(Controlplane).to receive(:profile_exists?).and_return(false) From 72e277a8b9efedf982da3a8a7293aa3ff758311d Mon Sep 17 00:00:00 2001 From: Rafael Gomes Date: Fri, 19 Jan 2024 13:42:38 -0300 Subject: [PATCH 11/12] feat: updates to info command --- lib/command/info.rb | 37 +++++++++++-------------------------- lib/core/config.rb | 14 +++++++------- 2 files changed, 18 insertions(+), 33 deletions(-) diff --git a/lib/command/info.rb b/lib/command/info.rb index f2bf0d9b..1dbbdaf5 100644 --- a/lib/command/info.rb +++ b/lib/command/info.rb @@ -32,7 +32,7 @@ def call @missing_apps_workloads = {} @missing_apps_starting_with = {} - if config.app && !config.current[:match_if_app_name_starts_with] + if config.app && !config.should_app_start_with?(config.app) single_app_info else multiple_apps_info @@ -41,20 +41,8 @@ def call private - def app_matches?(app, app_name, app_options) - app == app_name.to_s || (app_options[:match_if_app_name_starts_with] && app.start_with?(app_name.to_s)) - end - - def find_app_options(app) - @app_options ||= {} - @app_options[app] ||= config.apps.find do |app_name, app_options| - app_matches?(app, app_name, app_options) - end&.last - end - def find_workloads(app) - app_options = find_app_options(app) - return [] if app_options.nil? + app_options = config.find_app_config(app) (app_options[:app_workloads] + app_options[:additional_workloads] + [app_options[:one_off_workload]]).uniq end @@ -75,21 +63,19 @@ def fetch_app_workloads(org) # rubocop:disable Metrics/MethodLength end if config.app - result.select { |app, _| app_matches?(app, config.app, config.current) } + result.select { |app, _| config.app_matches?(app, config.app, config.current) } else - result.reject { |app, _| find_app_options(app).nil? } + result.reject { |app, _| config.find_app_config(app).nil? } end end - def orgs # rubocop:disable Metrics/MethodLength + def orgs result = [] if config.org result.push(config.org) else - config.apps.each do |app_name, app_options| - next if config.app && !app_matches?(config.app, app_name, app_options) - + config.apps.each do |_, app_options| org = app_options[:cpln_org] result.push(org) if org && !result.include?(org) end @@ -102,7 +88,7 @@ def apps(org) result = [] config.apps.each do |app_name, app_options| - next if config.app && !app_matches?(config.app, app_name, app_options) + next if config.app && !config.app_matches?(config.app, app_name, app_options) app_org = app_options[:cpln_org] result.push(app_name.to_s) if app_org == org @@ -113,7 +99,7 @@ def apps(org) end def any_app_starts_with?(app) - @app_workloads.keys.find { |app_name| app_matches?(app_name, app, config.apps[app.to_sym]) } + @app_workloads.keys.find { |app_name| config.app_matches?(app_name, app, config.apps[app.to_sym]) } end def check_any_app_starts_with(app) @@ -184,8 +170,7 @@ def print_missing_apps_starting_with "(replace 'whatever' with whatever suffix you want):" @missing_apps_starting_with.each do |app, _workloads| - app_with_suffix = "#{app}#{app.end_with?('-') ? '' : '-'}whatever" - puts " - `cpl setup-app -a #{app_with_suffix}`" + puts " - `cpl setup-app -a #{app}-whatever`" end end @@ -197,7 +182,7 @@ def single_app_info @defined_workloads = find_workloads(config.app) @available_workloads = fetch_workloads(config.app) - workloads = (@defined_workloads + @available_workloads).uniq.sort + workloads = (@defined_workloads + @available_workloads).uniq workloads.each do |workload| print_workload(config.app, workload) end @@ -217,7 +202,7 @@ def multiple_apps_info # rubocop:disable Metrics/MethodLength @defined_workloads = find_workloads(app) @available_workloads = @app_workloads[app] || [] - workloads = (@defined_workloads + @available_workloads).uniq.sort + workloads = (@defined_workloads + @available_workloads).uniq workloads.each do |workload| print_workload(app, workload) end diff --git a/lib/core/config.rb b/lib/core/config.rb index 7cde41fe..4c1f4936 100644 --- a/lib/core/config.rb +++ b/lib/core/config.rb @@ -102,6 +102,13 @@ def current end end + def app_matches?(app_name1, app_name2, app_options) + app_name1 && app_name2 && + (app_name1.to_s == app_name2.to_s || + (app_options[:match_if_app_name_starts_with] && app_name1.to_s.start_with?(app_name2.to_s)) + ) + end + def find_app_config(app_name1) @app_configs ||= {} @app_configs[app_name1] ||= apps.find do |app_name2, app_config| @@ -127,13 +134,6 @@ def ensure_config_app!(app_name, app_options) raise "Can't find config for app '#{app_name}' in 'controlplane.yml'." unless app_options end - def app_matches?(app_name1, app_name2, app_options) - app_name1 && app_name2 && - (app_name1.to_s == app_name2.to_s || - (app_options[:match_if_app_name_starts_with] && app_name1.to_s.start_with?(app_name2.to_s)) - ) - end - def ensure_app! return if app From 234dc39fd0e122932a168fb7abf8fffe8173d755 Mon Sep 17 00:00:00 2001 From: Rafael Gomes Date: Fri, 19 Jan 2024 13:48:12 -0300 Subject: [PATCH 12/12] docs: update CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d85a6161..bb679c70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,8 @@ _Please add entries here for your pull requests that are not yet released._ - `build-image` command now accepts extra options and passes them to `docker build`. [PR 126](https://github.com/shakacode/heroku-to-control-plane/pull/126) by [Rafael Gomes](https://github.com/rafaelgomesxyz). - `CPLN_ORG_UPSTREAM` env var now takes precedence over config from `controlplane.yml` in `copy-image-from-upstream` command. [PR 137](https://github.com/shakacode/heroku-to-control-plane/pull/137) by [Rafael Gomes](https://github.com/rafaelgomesxyz). +- `info` command now works properly for apps with `match_if_app_name_starts_with` set to `true`.[PR 139](https://github.com/shakacode/heroku-to-control-plane/pull/139) by [Rafael Gomes](https://github.com/rafaelgomesxyz). +- `info` command now lists workloads in the same order as `controlplane.yml`. [PR 139](https://github.com/shakacode/heroku-to-control-plane/pull/139) by [Rafael Gomes](https://github.com/rafaelgomesxyz). ## [1.2.0] - 2024-01-03