diff --git a/Gemfile.lock b/Gemfile.lock index c57f739d..5cc977c1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - uffizzi-cli (0.3.8) + uffizzi-cli (0.4.0) thor tty-spinner diff --git a/README.md b/README.md index 5363149f..696049c6 100644 --- a/README.md +++ b/README.md @@ -176,6 +176,16 @@ $ uffizzi config delete OPTION Deletes specified option. +### disconnect ### + +``` +$ uffizzi disconnect CREDENTIAL_TYPE +``` + +Deletes credential of specified type + +Supported credential types - `docker-hub`, `acr`, `ecr`, `gcr` + ## Git workflow for the app: 1. Clone the repository and checkout to `develop` branch diff --git a/lib/uffizzi.rb b/lib/uffizzi.rb index 7864bc9e..78a93968 100644 --- a/lib/uffizzi.rb +++ b/lib/uffizzi.rb @@ -3,6 +3,8 @@ require 'io/console' require 'tty-spinner' +require 'thor' +require 'uffizzi/error' require 'uffizzi/shell' require 'uffizzi/version' require 'uffizzi/clients/api/api_client' @@ -11,8 +13,6 @@ require_relative '../config/uffizzi' module Uffizzi - class Error < StandardError; end - class << self def ui @ui ||= Uffizzi::UI::Shell.new diff --git a/lib/uffizzi/cli.rb b/lib/uffizzi/cli.rb index d19e34ad..47331ed9 100644 --- a/lib/uffizzi/cli.rb +++ b/lib/uffizzi/cli.rb @@ -8,6 +8,10 @@ class CLI < Thor require_relative 'cli/common' class_option :help, type: :boolean, aliases: ['-h', 'help'] + def self.exit_on_failure? + true + end + desc 'version', 'show version' def version require_relative 'version' @@ -54,5 +58,11 @@ def connect(credential_type, credential_file_path = nil) require_relative 'cli/connect' Connect.new.run(credential_type, credential_file_path) end + + desc 'disconect CREDENTIAL_TYPE', 'Disonnect credentials from Uffizzi' + def disconnect(credential_type) + require_relative 'cli/disconnect' + Disconnect.new.run(credential_type) + end end end diff --git a/lib/uffizzi/cli/disconnect.rb b/lib/uffizzi/cli/disconnect.rb new file mode 100644 index 00000000..350ff36c --- /dev/null +++ b/lib/uffizzi/cli/disconnect.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +require 'uffizzi' + +module Uffizzi + class CLI::Disconnect + include ApiClient + + def run(credential_type) + connection_type = case credential_type + when 'docker-hub' + Uffizzi.configuration.credential_types[:dockerhub] + when 'acr' + Uffizzi.configuration.credential_types[:azure] + when 'ecr' + Uffizzi.configuration.credential_types[:amazon] + when 'gcr' + Uffizzi.configuration.credential_types[:google] + else + raise Uffizzi::Error.new('Unsupported credential type.') + end + + response = delete_credential(ConfigFile.read_option(:hostname), connection_type) + + if ResponseHelper.no_content?(response) + Uffizzi.ui.say("Successfully disconnected #{connection_name(credential_type)} connection") + else + ResponseHelper.handle_failed_response(response) + end + end + + private + + def connection_name(credential_type) + { + 'docker-hub' => 'DockerHub', + 'acr' => 'ACR', + 'ecr' => 'ECR', + 'gcr' => 'GCR', + }[credential_type] + end + end +end diff --git a/lib/uffizzi/cli/preview/service.rb b/lib/uffizzi/cli/preview/service.rb index cdb1425b..28cc5cc2 100644 --- a/lib/uffizzi/cli/preview/service.rb +++ b/lib/uffizzi/cli/preview/service.rb @@ -9,7 +9,23 @@ module Uffizzi class CLI::Preview::Service < Thor include ApiClient - desc 'list', 'list' + desc 'uffizzi preview service logs [LOGS_TYPE] [DEPLOYMENT_ID] [CONTAINER_NAME]', 'logs' + def logs(logs_type, deployment_name, container_name = args) + return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in? + return Uffizzi.ui.say('This command needs project to be set in config file') unless Uffizzi::AuthHelper.project_set? + + deployment_id = PreviewService.read_deployment_id(deployment_name) + response = service_logs_response(logs_type, deployment_id, container_name) + return Uffizzi.ui.say(response[:errors]) if response[:errors] + + if ResponseHelper.ok?(response) + handle_succeed_logs_response(response, container_name) + else + ResponseHelper.handle_failed_response(response) + end + end + + desc 'uffizzi preview service logs [DEPLOYMENT_ID]', 'list' def list(deployment_name) return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in? return Uffizzi.ui.say('This command needs project to be set in config file') unless Uffizzi::AuthHelper.project_set? @@ -20,7 +36,7 @@ def list(deployment_name) response = fetch_deployment_services(hostname, project_slug, deployment_id) if ResponseHelper.ok?(response) - handle_succeed_response(response, deployment_name) + handle_succeed_list_response(response, deployment_name) else ResponseHelper.handle_failed_response(response) end @@ -28,7 +44,19 @@ def list(deployment_name) private - def handle_succeed_response(response, deployment_name) + def service_logs_response(logs_type, deployment_id, container_name) + project_slug = ConfigFile.read_option(:project) + hostname = ConfigFile.read_option(:hostname) + + case logs_type + when 'container' + fetch_deployment_service_logs(hostname, project_slug, deployment_id, container_name) + else + raise Uffizzi::Error.new('Unknown log type') + end + end + + def handle_succeed_list_response(response, deployment_name) services = response[:body][:containers] || [] return Uffizzi.ui.say("There are no services associated with the preview #{deployment_name}") if services.empty? @@ -36,5 +64,14 @@ def handle_succeed_response(response, deployment_name) Uffizzi.ui.say(service) end end + + def handle_succeed_logs_response(response, container_name) + logs = response[:body][:logs] || [] + return Uffizzi.ui.say("The service #{container_name} has no logs") if logs.empty? + + logs.each do |log| + Uffizzi.ui.say(log) + end + end end end diff --git a/lib/uffizzi/clients/api/api_client.rb b/lib/uffizzi/clients/api/api_client.rb index 57acb2cd..a60bcf33 100644 --- a/lib/uffizzi/clients/api/api_client.rb +++ b/lib/uffizzi/clients/api/api_client.rb @@ -41,6 +41,20 @@ def fetch_deployment_services(hostname, project_slug, deployment_id) build_response(response) end + def delete_credential(hostname, credential_type) + uri = delete_credential_uri(hostname, credential_type) + response = Uffizzi::HttpClient.make_delete_request(uri) + + build_response(response) + end + + def fetch_deployment_service_logs(hostname, project_slug, deployment_id, container_name) + uri = preview_service_logs_uri(hostname, project_slug, deployment_id, container_name) + response = Uffizzi::HttpClient.make_get_request(uri) + + build_response(response) + end + def set_compose_file(hostname, params, project_slug) uri = compose_file_uri(hostname, project_slug) response = Uffizzi::HttpClient.make_post_request(uri, params) diff --git a/lib/uffizzi/clients/api/api_routes.rb b/lib/uffizzi/clients/api/api_routes.rb index eea86260..b6cd2f7d 100644 --- a/lib/uffizzi/clients/api/api_routes.rb +++ b/lib/uffizzi/clients/api/api_routes.rb @@ -55,4 +55,12 @@ def credentials_uri(hostname) def preview_services_uri(hostname, project_slug, deployment_id) "#{hostname}/api/cli/v1/projects/#{project_slug}/deployments/#{deployment_id}/containers" end + + def delete_credential_uri(hostname, credential_type) + "#{hostname}/api/cli/v1/account/credentials/#{credential_type}" + end + + def preview_service_logs_uri(hostname, project_slug, deployment_id, container_name) + "#{hostname}/api/cli/v1/projects/#{project_slug}/deployments/#{deployment_id}/containers/#{container_name}/logs" + end end diff --git a/lib/uffizzi/error.rb b/lib/uffizzi/error.rb new file mode 100644 index 00000000..bff31faf --- /dev/null +++ b/lib/uffizzi/error.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +module Uffizzi + class Error < Thor::Error; end +end diff --git a/lib/uffizzi/version.rb b/lib/uffizzi/version.rb index 1aa9f7f5..24b870b4 100644 --- a/lib/uffizzi/version.rb +++ b/lib/uffizzi/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Uffizzi - VERSION = '0.3.8' + VERSION = '0.4.0' end diff --git a/man/uffizzi-preview_service_logs b/man/uffizzi-preview_service_logs new file mode 100644 index 00000000..afddbc15 --- /dev/null +++ b/man/uffizzi-preview_service_logs @@ -0,0 +1,62 @@ +.\" generated with Ronn-NG/v0.9.1 +.\" http://github.com/apjanke/ronn-ng/tree/0.9.1 +.TH "PREVIEW" "" "March 2022" "" +.SH "NAME" +\fBpreview\fR \- manage previews +.SH "NAME" +.nf +uffizzi preview service logs \- show the logs for a container service +of a preview +.fi +.SH "SYNOPSIS" +.nf +uffizzi preview service logs LOG_TYPE [PREVIEW_ID] [SERVICE] [UFFIZZI_WIDE_FLAG \|\.\|\.\|\.] +.fi +.SH "DESCRIPTION" +.nf +Shows the logs for a given container service of a given preview\. + +This command can fail for the following reasons: + \- There is no preview with the given PREVIEW_ID + \- There is no service with the name SERVICE + +For more information on service logs, see: +https://docs\.uffizzi\.com/cli +.fi +.SH "LOG_TYPE" +.nf +LOG_TYPE is one of the following: + +build + The build logs of a service\. + +container + The container logs of a service\. +.fi +.SH "POSITIONAL ARGUMENTS" +.nf +[PREVIEW_ID] + The ID of the preview that includes the service you want to + show logs for\. + +[SERVICE] + The name of the service you want to show logs for\. +.fi +.SH "UFFIZZI WIDE FLAGS" +.nf +These flags are available to all commands: \-\-project\. Run $ uffizzi +help for details\. +.fi +.SH "EXAMPLES" +.nf +The following command shows build logs for the service web\-app of the +preview with ID deployment\-14: + + $ uffizzi preview service logs build deployment\-14 web\-app + +The following command shows container logs for the service postgres\-db of +the preview with ID deployment\-14: + + $ uffizzi preview service logs container deployment\-14 postgres\-db +.fi + diff --git a/man/uffizzi-preview_service_logs.html b/man/uffizzi-preview_service_logs.html new file mode 100644 index 00000000..aefb4a8e --- /dev/null +++ b/man/uffizzi-preview_service_logs.html @@ -0,0 +1,142 @@ + + +
+ + +
+ preview
- manage previews
+
uffizzi preview service logs - show the logs for a container service
+of a preview
+
+
+uffizzi preview service logs LOG_TYPE [PREVIEW_ID] [SERVICE] [UFFIZZI_WIDE_FLAG ...]
+
+
+Shows the logs for a given container service of a given preview.
+
+This command can fail for the following reasons:
+ - There is no preview with the given PREVIEW_ID
+ - There is no service with the name SERVICE
+
+For more information on service logs, see:
+https://docs.uffizzi.com/cli
+
+
+LOG_TYPE is one of the following:
+
+build
+ The build logs of a service.
+
+container
+ The container logs of a service.
+
+
+[PREVIEW_ID]
+ The ID of the preview that includes the service you want to
+ show logs for.
+
+[SERVICE]
+ The name of the service you want to show logs for.
+
+
+These flags are available to all commands: --project. Run $ uffizzi
+help for details.
+
+
+The following command shows build logs for the service web-app of the
+preview with ID deployment-14:
+
+ $ uffizzi preview service logs build deployment-14 web-app
+
+The following command shows container logs for the service postgres-db of
+the preview with ID deployment-14:
+
+ $ uffizzi preview service logs container deployment-14 postgres-db
+
+
+