@@ -21,9 +21,12 @@ import (
21
21
"errors"
22
22
"fmt"
23
23
"io"
24
+ "mime"
24
25
"net"
26
+ "net/http"
25
27
"net/url"
26
28
"os"
29
+ "regexp"
27
30
"strconv"
28
31
"strings"
29
32
"sync"
@@ -176,6 +179,7 @@ func NewRuntimeCommand() *cobra.Command {
176
179
cmd .AddCommand (NewRuntimeListCommand ())
177
180
cmd .AddCommand (NewRuntimeUninstallCommand ())
178
181
cmd .AddCommand (NewRuntimeUpgradeCommand ())
182
+ cmd .AddCommand (NewRuntimeLogsCommand ())
179
183
180
184
cmd .PersistentFlags ().BoolVar (& store .Get ().Silent , "silent" , false , "Disables the command wizard" )
181
185
@@ -1950,6 +1954,77 @@ func RunRuntimeUpgrade(ctx context.Context, opts *RuntimeUpgradeOptions) error {
1950
1954
return nil
1951
1955
}
1952
1956
1957
+ func NewRuntimeLogsCommand () * cobra.Command {
1958
+ cmd := & cobra.Command {
1959
+ Use : "logs [--ingress-host <url>] [--download]" ,
1960
+ Short : "Work with current runtime logs" ,
1961
+ RunE : func (cmd * cobra.Command , _ []string ) error {
1962
+ var err error = nil
1963
+ if isAllRequiredFlagsForDownloadRuntimeLogs () {
1964
+ err = downloadRuntimeLogs ()
1965
+ if err == nil {
1966
+ log .G (cmd .Context ()).Info ("Runtime logs was downloaded successfully" )
1967
+ }
1968
+ }
1969
+ return err
1970
+ },
1971
+ }
1972
+ cmd .Flags ().BoolVar (& store .Get ().IsDownloadRuntimeLogs , "download" , false , "If true, will download logs from all componnents that consist of current runtime" )
1973
+ cmd .Flags ().StringVar (& store .Get ().IngressHost , "ingress-host" , "" , "Set runtime ingress host" )
1974
+ return cmd
1975
+ }
1976
+
1977
+ func isAllRequiredFlagsForDownloadRuntimeLogs () bool {
1978
+ return store .Get ().IsDownloadRuntimeLogs && store .Get ().IngressHost != ""
1979
+ }
1980
+
1981
+ func downloadRuntimeLogs () error {
1982
+ downloadFileUrl := getDownloadFileUrl ()
1983
+ response , err := http .Get (downloadFileUrl )
1984
+ if err != nil {
1985
+ return err
1986
+ }
1987
+ defer response .Body .Close ()
1988
+ fullFilename , err := getFullFilename (response )
1989
+ if err != nil {
1990
+ return err
1991
+ }
1992
+ return downloadFile (response , fullFilename )
1993
+ }
1994
+
1995
+ func getDownloadFileUrl () string {
1996
+ ingressHost := store .Get ().IngressHost
1997
+ appProxyPath := store .Get ().AppProxyIngressPath
1998
+ regularExpression := regexp .MustCompile (`([^:])/{2,}` )
1999
+ url := fmt .Sprintf ("%s/%s/api/applications/logs" , ingressHost , appProxyPath )
2000
+ return regularExpression .ReplaceAllString (url , `$1/` )
2001
+ }
2002
+
2003
+ func getFullFilename (response * http.Response ) (string , error ) {
2004
+ contentDisposition := response .Header .Get ("Content-Disposition" )
2005
+ _ , params , err := mime .ParseMediaType (contentDisposition )
2006
+ if err != nil {
2007
+ return "" , err
2008
+ }
2009
+ filename := params ["filename" ]
2010
+ processWorkingDirectory , err := os .Getwd ()
2011
+ if err != nil {
2012
+ return "" , err
2013
+ }
2014
+ fullFilename := fmt .Sprintf ("%s/%s" , processWorkingDirectory , filename )
2015
+ return fullFilename , err
2016
+ }
2017
+
2018
+ func downloadFile (response * http.Response , fullFilename string ) error {
2019
+ fileDescriptor , err := os .Create (fullFilename )
2020
+ if err != nil {
2021
+ return err
2022
+ }
2023
+ defer fileDescriptor .Close ()
2024
+ _ , err = io .Copy (fileDescriptor , response .Body )
2025
+ return err
2026
+ }
2027
+
1953
2028
func persistRuntime (ctx context.Context , cloneOpts * git.CloneOptions , rt * runtime.Runtime , rtConf * runtime.CommonConfig ) error {
1954
2029
r , fs , err := cloneOpts .GetRepo (ctx )
1955
2030
if err != nil {
0 commit comments