@@ -21,10 +21,12 @@ import (
2121 "github.com/mholt/archiver"
2222 "github.com/pkg/errors"
2323 "github.com/replicatedhq/troubleshoot/cmd/util"
24+ analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
2425 troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
2526 "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
2627 troubleshootclientsetscheme "github.com/replicatedhq/troubleshoot/pkg/client/troubleshootclientset/scheme"
2728 "github.com/replicatedhq/troubleshoot/pkg/collect"
29+ "github.com/replicatedhq/troubleshoot/pkg/convert"
2830 "github.com/replicatedhq/troubleshoot/pkg/redact"
2931 "github.com/spf13/viper"
3032 spin "github.com/tj/go-spin"
@@ -132,7 +134,63 @@ func runTroubleshoot(v *viper.Viper, arg string) error {
132134
133135 fmt .Printf ("\r %s\r " , cursor .ClearEntireLine ())
134136
135- if len (supportBundleSpec .Spec .AfterCollection ) == 0 {
137+ // upload if needed
138+ fileUploaded := false
139+ if len (supportBundleSpec .Spec .AfterCollection ) > 0 {
140+ for _ , ac := range supportBundleSpec .Spec .AfterCollection {
141+ if ac .UploadResultsTo != nil {
142+ if err := uploadSupportBundle (ac .UploadResultsTo , archivePath ); err != nil {
143+ c := color .New (color .FgHiRed )
144+ c .Printf ("%s\r * Failed to upload support bundle: %v\n " , cursor .ClearEntireLine (), err )
145+ } else {
146+ fileUploaded = true
147+ }
148+ } else if ac .Callback != nil {
149+ if err := callbackSupportBundleAPI (ac .Callback , archivePath ); err != nil {
150+ c := color .New (color .FgHiRed )
151+ c .Printf ("%s\r * Failed to notify API that support bundle has been uploaded: %v\n " , cursor .ClearEntireLine (), err )
152+ }
153+ }
154+ }
155+
156+ }
157+
158+ // perform analysis, if possible
159+ if len (supportBundleSpec .Spec .Analyzers ) > 0 {
160+ tmpDir , err := ioutil .TempDir ("" , "troubleshoot" )
161+ if err != nil {
162+ c := color .New (color .FgHiRed )
163+ c .Printf ("%s\r * Failed to make directory for analysis: %v\n " , cursor .ClearEntireLine (), err )
164+ }
165+
166+ f , err := os .Open (archivePath )
167+ if err != nil {
168+ c := color .New (color .FgHiRed )
169+ c .Printf ("%s\r * Failed to open support bundle for analysis: %v\n " , cursor .ClearEntireLine (), err )
170+
171+ }
172+ if err := analyzer .ExtractTroubleshootBundle (f , tmpDir ); err != nil {
173+ c := color .New (color .FgHiRed )
174+ c .Printf ("%s\r * Failed to extract support bundle for analysis: %v\n " , cursor .ClearEntireLine (), err )
175+ }
176+
177+ analyzeResults , err := analyzer .AnalyzeLocal (tmpDir , supportBundleSpec .Spec .Analyzers )
178+ if err != nil {
179+ c := color .New (color .FgHiRed )
180+ c .Printf ("%s\r * Failed to analyze support bundle: %v\n " , cursor .ClearEntireLine (), err )
181+ }
182+
183+ data := convert .FromAnalyzerResult (analyzeResults )
184+ formatted , err := json .MarshalIndent (data , "" , " " )
185+ if err != nil {
186+ c := color .New (color .FgHiRed )
187+ c .Printf ("%s\r * Failed to format analysis: %v\n " , cursor .ClearEntireLine (), err )
188+ }
189+
190+ fmt .Printf ("%s" , formatted )
191+ }
192+
193+ if ! fileUploaded {
136194 msg := archivePath
137195 if appName := supportBundleSpec .Labels ["applicationName" ]; appName != "" {
138196 f := `A support bundle for %s has been created in this directory
@@ -146,23 +204,6 @@ the %s Admin Console to begin analysis.`
146204 return nil
147205 }
148206
149- fileUploaded := false
150- for _ , ac := range supportBundleSpec .Spec .AfterCollection {
151- if ac .UploadResultsTo != nil {
152- if err := uploadSupportBundle (ac .UploadResultsTo , archivePath ); err != nil {
153- c := color .New (color .FgHiRed )
154- c .Printf ("%s\r * Failed to upload support bundle: %v\n " , cursor .ClearEntireLine (), err )
155- } else {
156- fileUploaded = true
157- }
158- } else if ac .Callback != nil {
159- if err := callbackSupportBundleAPI (ac .Callback , archivePath ); err != nil {
160- c := color .New (color .FgHiRed )
161- c .Printf ("%s\r * Failed to notify API that support bundle has been uploaded: %v\n " , cursor .ClearEntireLine (), err )
162- }
163- }
164- }
165-
166207 fmt .Printf ("\r %s\r " , cursor .ClearEntireLine ())
167208 if fileUploaded {
168209 fmt .Printf ("A support bundle has been created and uploaded to your cluster for analysis. Please visit the Troubleshoot page to continue.\n " )
0 commit comments