Skip to content

Commit

Permalink
Add --trace option and better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
justin808 committed Dec 30, 2023
1 parent ab53a31 commit 57e95a7
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ _Please add entries here for your pull requests that are not yet released._
- 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).
- Added `--trace` option to all commands for more detailed logs. [PR XX]() by [justin808]()
- Added better error message to check the org name in case of a 403 error. [PR XX]() by [justin808]()

### Changed

Expand Down
8 changes: 2 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ git clone https://github.com/shakacode/heroku-to-control-plane
alias cpl="~/projects/heroku-to-control-plane/bin/cpl"
```

Or set the path of the Ruby gem in your Gemfile.

```ruby
gem 'cpl', path: '~/projects/heroku-to-control-plane'
```

## Linting/Testing

Before committing or pushing code, be sure to:
Expand All @@ -36,6 +30,8 @@ overcommit --install

## Debugging

1. Use the `--verbose` option to see more detailed logs.
2. Use the `--trace` option to see full logging of HTTP requests. Warning, this will display keys to your logs or console.
1. Add a breakpoint (`debugger`) to any line of code you want to debug.
2. Modify the `lib/command/test.rb` file to trigger the code you want to test. To simulate a command, you can use
`Cpl::Cli.start` (e.g., `Cpl::Cli.start(["deploy-image", "-a", "my-app-name"])` would be the same as running
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ npm update -g @controlplane/cli

5. Run `cpln image docker-login --org <your-org>` to ensure that you have access to the Control Plane Docker registry.

6. Install Heroku to Control Plane `cpl` CLI, either as a [Ruby gem](https://rubygems.org/gems/cpl) or a local clone.
For information on the latter, see [CONTRIBUTING.md](CONTRIBUTING.md). You may also install `cpl` in your project's Gemfile.
6. Install Heroku to Control Plane `cpl` CLI as a [Ruby gem](https://rubygems.org/gems/cpl): `gem install cpl`. If you want to hack on the source code, see [CONTRIBUTING.md](CONTRIBUTING.md).

7. You can use [this Dockerfile](https://github.com/shakacode/react-webpack-rails-tutorial/blob/master/.controlplane/Dockerfile) as an example for your project. Ensure that you have Docker running.

Expand Down
14 changes: 13 additions & 1 deletion lib/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def self.all_commands
end

def self.common_options
[org_option, verbose_option]
[org_option, verbose_option, trace_option]
end

def self.org_option(required: false)
Expand Down Expand Up @@ -207,6 +207,17 @@ def self.verbose_option(required: false)
}
end

def self.trace_option(required: false)
{
name: :trace,
params: {
desc: "Shows trace of API calls. WARNING: may contain sensitive data",
type: :boolean,
required: required
}
}
end

def self.all_options
methods.grep(/_option$/).map { |method| send(method.to_s) }
end
Expand Down Expand Up @@ -258,6 +269,7 @@ def latest_image(app = config.app, org = config.org)
end

def latest_image_next(app = config.app, org = config.org, commit: nil)
# debugger
commit ||= config.options[:commit]

@latest_image_next ||= {}
Expand Down
5 changes: 5 additions & 0 deletions lib/core/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ def initialize(args, options, required_options)
ensure_required_options!

Shell.verbose_mode(options[:verbose])
trace_mode = options[:trace]
if trace_mode
ControlplaneApiDirect.set_trace(trace_mode)
Shell.warn("Trace mode is enabled, this will print sensitive information to the console.")
end
end

def org
Expand Down
1 change: 1 addition & 0 deletions lib/core/controlplane.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def image_build(image, dockerfile:, build_args: [], push: true)
# https://docs.controlplane.com/guides/push-image#step-2
# Might need to use `docker buildx build` if compatiblitity issues arise
cmd = "docker build --platform=linux/amd64 -t #{image} -f #{dockerfile}"
cmd += " --progress=plain" if ControlplaneApiDirect.trace

build_args.each { |build_arg| cmd += " --build-arg #{build_arg}" }
cmd += " #{config.app_dir}"
Expand Down
22 changes: 21 additions & 1 deletion lib/core/controlplane_api_direct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ class ControlplaneApiDirect

API_TOKEN_REGEX = /^[\w\-._]+$/.freeze

def self.set_trace(trace)
@trace = trace
end

def self.trace
@trace
end

def call(url, method:, host: :api, body: nil) # rubocop:disable Metrics/MethodLength
trace = ControlplaneApiDirect.trace
uri = URI("#{api_host(host)}#{url}")
request = API_METHODS[method].new(uri)
request["Content-Type"] = "application/json"
Expand All @@ -26,7 +35,11 @@ def call(url, method:, host: :api, body: nil) # rubocop:disable Metrics/MethodLe

Shell.debug(method.upcase, "#{uri} #{body&.to_json}")

response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") { |http| http.request(request) }
http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = uri.scheme == "https"
http.set_debug_output($stdout) if trace

response = http.start { |http| http.request(request) }

case response
when Net::HTTPOK
Expand All @@ -35,6 +48,9 @@ def call(url, method:, host: :api, body: nil) # rubocop:disable Metrics/MethodLe
true
when Net::HTTPNotFound
nil
when Net::HTTPForbidden
org = self.class.parse_org(url)
raise("Double check your org #{org}. #{response} #{response.body}")
else
raise("#{response} #{response.body}")
end
Expand Down Expand Up @@ -65,4 +81,8 @@ def self.reset_api_token
remove_class_variable(:@@api_token) if defined?(@@api_token)
end
# rubocop:enable Style/ClassVars

def self.parse_org(url)
url.match(%r{^/org/([^/]+)})[1]
end
end
3 changes: 2 additions & 1 deletion spec/command/cleanup_images_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
end

it "lists images to delete based on days", vcr: true do
allow(Shell).to receive(:confirm).with("\nAre you sure you want to delete these 6 images?")
allow(Shell).to receive(:confirm).with("\nAre you sure you want to delete these 7 images?")
.and_return(false)

expected_output = <<~OUTPUT
Expand All @@ -94,6 +94,7 @@
- my-app-test-5:511_7ef99dd (#{Shell.color('2023-08-05T02:51:14+00:00', :red)} - #{Shell.color('older than 12 days', :red)})
- my-app-test-5:512_346384f (#{Shell.color('2023-08-06T03:08:27+00:00', :red)} - #{Shell.color('older than 12 days', :red)})
- my-app-test-5:513_ec7930a (#{Shell.color('2023-08-07T13:20:18+00:00', :red)} - #{Shell.color('older than 12 days', :red)})
- my-app-test-5:514_b54466b (#{Shell.color('2023-08-11T09:48:28+00:00', :red)} - #{Shell.color('older than 12 days', :red)})
OUTPUT

output = command_output do
Expand Down
8 changes: 8 additions & 0 deletions spec/core/controlplane_api_direct_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,12 @@
end.to raise_error(RuntimeError, message)
end
end

describe ".parse_org" do
it "returns correct org" do
url = "/org/org1/gvc/gvc1"
org = described_instance.class.parse_org(url)
expect(org).to eq("org1")
end
end
end

0 comments on commit 57e95a7

Please sign in to comment.