From fa348b8bc5874d69bb8d499ae2bf06e30c46c7f5 Mon Sep 17 00:00:00 2001 From: PhilippPlotnikov Date: Fri, 10 Jun 2022 12:55:37 +0500 Subject: [PATCH 01/12] Add download file --- cmd/commands/runtime.go | 61 +++++++++++++++++++++++++++++++++++++++++ pkg/store/store.go | 2 ++ 2 files changed, 63 insertions(+) diff --git a/cmd/commands/runtime.go b/cmd/commands/runtime.go index 25f031de5..ae51bc426 100644 --- a/cmd/commands/runtime.go +++ b/cmd/commands/runtime.go @@ -21,9 +21,12 @@ import ( "errors" "fmt" "io" + "mime" "net" + "net/http" "net/url" "os" + "regexp" "strconv" "strings" "sync" @@ -175,6 +178,7 @@ func NewRuntimeCommand() *cobra.Command { cmd.AddCommand(NewRuntimeListCommand()) cmd.AddCommand(NewRuntimeUninstallCommand()) cmd.AddCommand(NewRuntimeUpgradeCommand()) + cmd.AddCommand(NewRuntimeLogsCommand()) cmd.PersistentFlags().BoolVar(&store.Get().Silent, "silent", false, "Disables the command wizard") @@ -1901,6 +1905,63 @@ func RunRuntimeUpgrade(ctx context.Context, opts *RuntimeUpgradeOptions) error { return nil } +func NewRuntimeLogsCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "logs [--download]", + Short: "Work with current runtime logs", + RunE: func(cmd *cobra.Command, _ []string) error { + if !store.Get().IsDownloadRuntimeLogs { + return nil + } + downloadFileUrl := getDownloadFileUrl() + response, err := http.Get(downloadFileUrl) + if err != nil { + return err + } + defer response.Body.Close() + fullFileName, err := getFullFilename(response) + err = downloadFile(response, fullFileName) + return err + }, + } + cmd.Flags().BoolVar(&store.Get().IsDownloadRuntimeLogs, "download", false, "If true, will download logs from all componnents that consist of current runtime") + cmd.Flags().StringVar(&store.Get().IngressHost, "ingress-host", "", "Set runtime ingress host") + return cmd +} + +func getDownloadFileUrl() string { + ingressHost := store.Get().IngressHost + appProxyPath := store.Get().AppProxyIngressPath + regularExpression := regexp.MustCompile(`([^:])/{2,}`) + url := fmt.Sprintf("%s/%sapi/applications/logs", ingressHost, appProxyPath) + return regularExpression.ReplaceAllString(url, `$1/`) +} + +func getFullFilename(response *http.Response) (string, error) { + contentDisposition := response.Header.Get("Content-Disposition") + _, params, err := mime.ParseMediaType(contentDisposition) + if err != nil { + return "", err + } + filename := params["filename"] + processWorkingDirectory, err := os.Getwd() + if err != nil { + return "", err + } + fullFileName := fmt.Sprintf("%s/%s", processWorkingDirectory, filename) + return fullFileName, err +} + +func downloadFile(response *http.Response, fullFileName string) error { + fileDescriptor, err := os.Create(fullFileName) + if err != nil { + return err + } + defer fileDescriptor.Close() + _, err = io.Copy(fileDescriptor, response.Body) + return err +} + func persistRuntime(ctx context.Context, cloneOpts *git.CloneOptions, rt *runtime.Runtime, rtConf *runtime.CommonConfig) error { r, fs, err := cloneOpts.GetRepo(ctx) if err != nil { diff --git a/pkg/store/store.go b/pkg/store/store.go index f0b2460ed..bbb796e85 100644 --- a/pkg/store/store.go +++ b/pkg/store/store.go @@ -143,6 +143,8 @@ type Store struct { InstallationFlow string GsCreateFlow string InCluster string + IsDownloadRuntimeLogs bool + IngressHost string } // Get returns the global store From 88bc729da7ffab8c7bdc4f2a25179c68c609dcef Mon Sep 17 00:00:00 2001 From: PhilippPlotnikov Date: Fri, 10 Jun 2022 12:56:55 +0500 Subject: [PATCH 02/12] Add error handler --- cmd/commands/runtime.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/commands/runtime.go b/cmd/commands/runtime.go index ae51bc426..e8b530aff 100644 --- a/cmd/commands/runtime.go +++ b/cmd/commands/runtime.go @@ -1920,6 +1920,9 @@ func NewRuntimeLogsCommand() *cobra.Command { } defer response.Body.Close() fullFileName, err := getFullFilename(response) + if err != nil { + return err + } err = downloadFile(response, fullFileName) return err }, From 1b1c9a86357136debc19eef18e56ab90bcd98e5e Mon Sep 17 00:00:00 2001 From: PhilippPlotnikov Date: Fri, 10 Jun 2022 13:22:33 +0500 Subject: [PATCH 03/12] Refactor --- Makefile | 2 +- cmd/commands/runtime.go | 41 ++++++++++++++++++++++------------ docs/releases/release_notes.md | 4 ++-- manifests/runtime.yaml | 2 +- 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 945d7af81..d776545cb 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION=v0.0.377 +VERSION=v0.0.378 OUT_DIR=dist YEAR?=$(shell date +"%Y") diff --git a/cmd/commands/runtime.go b/cmd/commands/runtime.go index e8b530aff..55cbc5f38 100644 --- a/cmd/commands/runtime.go +++ b/cmd/commands/runtime.go @@ -1907,23 +1907,17 @@ func RunRuntimeUpgrade(ctx context.Context, opts *RuntimeUpgradeOptions) error { func NewRuntimeLogsCommand() *cobra.Command { cmd := &cobra.Command{ - Use: "logs [--download]", + Use: "logs [--ingress-host ] [--download]", Short: "Work with current runtime logs", RunE: func(cmd *cobra.Command, _ []string) error { - if !store.Get().IsDownloadRuntimeLogs { - return nil - } - downloadFileUrl := getDownloadFileUrl() - response, err := http.Get(downloadFileUrl) - if err != nil { - return err - } - defer response.Body.Close() - fullFileName, err := getFullFilename(response) - if err != nil { - return err + var err error = nil + if isAllRequiredFlagsForDownloadRuntimeLogs() { + err = downloadRuntimeLogs() + if err == nil { + green := "\033[32m" + fmt.Printf("%sRuntime logs was downloaded successfully", green) + } } - err = downloadFile(response, fullFileName) return err }, } @@ -1932,6 +1926,25 @@ func NewRuntimeLogsCommand() *cobra.Command { return cmd } +func isAllRequiredFlagsForDownloadRuntimeLogs() bool { + return store.Get().IsDownloadRuntimeLogs && store.Get().IngressHost != "" +} + +func downloadRuntimeLogs() error { + downloadFileUrl := getDownloadFileUrl() + response, err := http.Get(downloadFileUrl) + if err != nil { + return err + } + defer response.Body.Close() + fullFileName, err := getFullFilename(response) + if err != nil { + return err + } + err = downloadFile(response, fullFileName) + return err +} + func getDownloadFileUrl() string { ingressHost := store.Get().IngressHost appProxyPath := store.Get().AppProxyIngressPath diff --git a/docs/releases/release_notes.md b/docs/releases/release_notes.md index 460e1abef..33b6c9cf0 100644 --- a/docs/releases/release_notes.md +++ b/docs/releases/release_notes.md @@ -23,7 +23,7 @@ cf version ```bash # download and extract the binary -curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.377/cf-linux-amd64.tar.gz | tar zx +curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.378/cf-linux-amd64.tar.gz | tar zx # move the binary to your $PATH mv ./cf-linux-amd64 /usr/local/bin/cf @@ -36,7 +36,7 @@ cf version ```bash # download and extract the binary -curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.377/cf-darwin-amd64.tar.gz | tar zx +curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.378/cf-darwin-amd64.tar.gz | tar zx # move the binary to your $PATH mv ./cf-darwin-amd64 /usr/local/bin/cf diff --git a/manifests/runtime.yaml b/manifests/runtime.yaml index 34fd4ae53..cdab4388e 100644 --- a/manifests/runtime.yaml +++ b/manifests/runtime.yaml @@ -5,7 +5,7 @@ metadata: namespace: "{{ namespace }}" spec: defVersion: 1.0.1 - version: 0.0.377 + version: 0.0.378 bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd components: - name: events From 1e9aa029bb40f7a4d5d0ab3c451bf26bdf501710 Mon Sep 17 00:00:00 2001 From: PhilippPlotnikov Date: Fri, 10 Jun 2022 13:28:31 +0500 Subject: [PATCH 04/12] Refactor --- cmd/commands/runtime.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/commands/runtime.go b/cmd/commands/runtime.go index 55cbc5f38..a12beb4bf 100644 --- a/cmd/commands/runtime.go +++ b/cmd/commands/runtime.go @@ -1915,7 +1915,7 @@ func NewRuntimeLogsCommand() *cobra.Command { err = downloadRuntimeLogs() if err == nil { green := "\033[32m" - fmt.Printf("%sRuntime logs was downloaded successfully", green) + fmt.Printf("%sRuntime logs was downloaded successfully\n", green) } } return err From de66cc35fb112c4c62ea449bdf6b18bfa8672134 Mon Sep 17 00:00:00 2001 From: PhilippPlotnikov Date: Fri, 10 Jun 2022 13:34:46 +0500 Subject: [PATCH 05/12] Make codegen and make lint --- docs/commands/cli-v2_runtime.md | 1 + docs/commands/cli-v2_runtime_logs.md | 31 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 docs/commands/cli-v2_runtime_logs.md diff --git a/docs/commands/cli-v2_runtime.md b/docs/commands/cli-v2_runtime.md index 237f6b3b1..affe90cfe 100644 --- a/docs/commands/cli-v2_runtime.md +++ b/docs/commands/cli-v2_runtime.md @@ -28,6 +28,7 @@ cli-v2 runtime [flags] * [cli-v2](cli-v2.md) - cli-v2 is used for installing and managing codefresh installations using gitops * [cli-v2 runtime install](cli-v2_runtime_install.md) - Install a new Codefresh runtime * [cli-v2 runtime list](cli-v2_runtime_list.md) - List all Codefresh runtimes +* [cli-v2 runtime logs](cli-v2_runtime_logs.md) - Work with current runtime logs * [cli-v2 runtime uninstall](cli-v2_runtime_uninstall.md) - Uninstall a Codefresh runtime * [cli-v2 runtime upgrade](cli-v2_runtime_upgrade.md) - Upgrade a Codefresh runtime diff --git a/docs/commands/cli-v2_runtime_logs.md b/docs/commands/cli-v2_runtime_logs.md new file mode 100644 index 000000000..f02ee7c3a --- /dev/null +++ b/docs/commands/cli-v2_runtime_logs.md @@ -0,0 +1,31 @@ +## cli-v2 runtime logs + +Work with current runtime logs + +``` +cli-v2 runtime logs [--ingress-host ] [--download] [flags] +``` + +### Options + +``` + --download If true, will download logs from all componnents that consist of current runtime + -h, --help help for logs + --ingress-host string Set runtime ingress host +``` + +### Options inherited from parent commands + +``` + --auth-context string Run the next command using a specific authentication context + --cfconfig string Custom path for authentication contexts config file (default "/home/user") + --insecure Disable certificate validation for TLS connections (e.g. to g.codefresh.io) + --insecure-ingress-host Disable certificate validation of ingress host (default: false) + --request-timeout duration Request timeout (default 30s) + --silent Disables the command wizard +``` + +### SEE ALSO + +* [cli-v2 runtime](cli-v2_runtime.md) - Manage Codefresh runtimes + From 4b7f30413ee5f3459654c4c46c83bd4c48d689e1 Mon Sep 17 00:00:00 2001 From: PhilippPlotnikov Date: Fri, 10 Jun 2022 13:36:14 +0500 Subject: [PATCH 06/12] Refactor --- cmd/commands/runtime.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/commands/runtime.go b/cmd/commands/runtime.go index a12beb4bf..0ce9ee801 100644 --- a/cmd/commands/runtime.go +++ b/cmd/commands/runtime.go @@ -1949,7 +1949,7 @@ func getDownloadFileUrl() string { ingressHost := store.Get().IngressHost appProxyPath := store.Get().AppProxyIngressPath regularExpression := regexp.MustCompile(`([^:])/{2,}`) - url := fmt.Sprintf("%s/%sapi/applications/logs", ingressHost, appProxyPath) + url := fmt.Sprintf("%s/%s/api/applications/logs", ingressHost, appProxyPath) return regularExpression.ReplaceAllString(url, `$1/`) } From 5e23f97833e66c0625fdc72dbd11962463507f41 Mon Sep 17 00:00:00 2001 From: PhilippPlotnikov Date: Fri, 10 Jun 2022 13:41:41 +0500 Subject: [PATCH 07/12] FileName -> Filename --- cmd/commands/runtime.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/commands/runtime.go b/cmd/commands/runtime.go index 0ce9ee801..897aad711 100644 --- a/cmd/commands/runtime.go +++ b/cmd/commands/runtime.go @@ -1937,11 +1937,11 @@ func downloadRuntimeLogs() error { return err } defer response.Body.Close() - fullFileName, err := getFullFilename(response) + fullFilename, err := getFullFilename(response) if err != nil { return err } - err = downloadFile(response, fullFileName) + err = downloadFile(response, fullFilename) return err } @@ -1964,12 +1964,12 @@ func getFullFilename(response *http.Response) (string, error) { if err != nil { return "", err } - fullFileName := fmt.Sprintf("%s/%s", processWorkingDirectory, filename) - return fullFileName, err + fullFilename := fmt.Sprintf("%s/%s", processWorkingDirectory, filename) + return fullFilename, err } -func downloadFile(response *http.Response, fullFileName string) error { - fileDescriptor, err := os.Create(fullFileName) +func downloadFile(response *http.Response, fullFilename string) error { + fileDescriptor, err := os.Create(fullFilename) if err != nil { return err } From 13b0b57df9017e7674ce98f5e0d7bacdf40b8095 Mon Sep 17 00:00:00 2001 From: PhilippPlotnikov Date: Fri, 10 Jun 2022 14:24:39 +0500 Subject: [PATCH 08/12] Fix --- cmd/commands/runtime.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cmd/commands/runtime.go b/cmd/commands/runtime.go index 897aad711..ab12d8bd4 100644 --- a/cmd/commands/runtime.go +++ b/cmd/commands/runtime.go @@ -1914,8 +1914,7 @@ func NewRuntimeLogsCommand() *cobra.Command { if isAllRequiredFlagsForDownloadRuntimeLogs() { err = downloadRuntimeLogs() if err == nil { - green := "\033[32m" - fmt.Printf("%sRuntime logs was downloaded successfully\n", green) + log.G(cmd.Context()).Info("Runtime logs was downloaded successfully") } } return err @@ -1941,8 +1940,7 @@ func downloadRuntimeLogs() error { if err != nil { return err } - err = downloadFile(response, fullFilename) - return err + return downloadFile(response, fullFilename) } func getDownloadFileUrl() string { From bf0f8976e32fdeed3f89f7d4cf9581e07b92164e Mon Sep 17 00:00:00 2001 From: PhilippPlotnikov Date: Fri, 10 Jun 2022 14:34:14 +0500 Subject: [PATCH 09/12] Bump From c1e2984f17188d2c45aa48f82f420183684836d7 Mon Sep 17 00:00:00 2001 From: PhilippPlotnikov Date: Fri, 10 Jun 2022 17:16:43 +0500 Subject: [PATCH 10/12] Bump From 858b0d618bb25afb5f9bfb1d3a9d58643fc6fb21 Mon Sep 17 00:00:00 2001 From: PhilippPlotnikov Date: Tue, 14 Jun 2022 12:32:39 +0500 Subject: [PATCH 11/12] Bump From cd736f8055a69af1545432d2cabb6874746f83e4 Mon Sep 17 00:00:00 2001 From: PhilippPlotnikov Date: Tue, 14 Jun 2022 13:46:58 +0500 Subject: [PATCH 12/12] Bmp