@@ -21,9 +21,12 @@ import (
2121 "errors"
2222 "fmt"
2323 "io"
24+ "mime"
2425 "net"
26+ "net/http"
2527 "net/url"
2628 "os"
29+ "regexp"
2730 "strconv"
2831 "strings"
2932 "sync"
@@ -176,6 +179,7 @@ func NewRuntimeCommand() *cobra.Command {
176179 cmd .AddCommand (NewRuntimeListCommand ())
177180 cmd .AddCommand (NewRuntimeUninstallCommand ())
178181 cmd .AddCommand (NewRuntimeUpgradeCommand ())
182+ cmd .AddCommand (NewRuntimeLogsCommand ())
179183
180184 cmd .PersistentFlags ().BoolVar (& store .Get ().Silent , "silent" , false , "Disables the command wizard" )
181185
@@ -1950,6 +1954,77 @@ func RunRuntimeUpgrade(ctx context.Context, opts *RuntimeUpgradeOptions) error {
19501954 return nil
19511955}
19521956
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+
19532028func persistRuntime (ctx context.Context , cloneOpts * git.CloneOptions , rt * runtime.Runtime , rtConf * runtime.CommonConfig ) error {
19542029 r , fs , err := cloneOpts .GetRepo (ctx )
19552030 if err != nil {
0 commit comments