github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/collector/host_utility.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package collector 21 22 import ( 23 "bytes" 24 "encoding/json" 25 "fmt" 26 "os/exec" 27 28 "github.com/pkg/errors" 29 pkgcollector "github.com/replicatedhq/troubleshoot/pkg/collect" 30 31 preflightv1beta2 "github.com/1aal/kubeblocks/externalapis/preflight/v1beta2" 32 "github.com/1aal/kubeblocks/pkg/cli/preflight/util" 33 ) 34 35 const UtilityPathFormat = "host-collectors/utility/%s.json" 36 const DefaultHostUtilityName = "Host Utility" 37 const DefaultHostUtilityPath = "utility" 38 39 type HostUtilityInfo struct { 40 Name string `json:"name"` 41 Path string `json:"path"` 42 Error string `json:"error"` 43 } 44 45 type CollectHostUtility struct { 46 HostCollector *preflightv1beta2.HostUtility 47 BundlePath string 48 } 49 50 func (c *CollectHostUtility) Title() string { 51 return util.TitleOrDefault(c.HostCollector.HostCollectorMeta, DefaultHostUtilityName) 52 } 53 54 func (c *CollectHostUtility) IsExcluded() (bool, error) { 55 return util.IsExcluded(c.HostCollector.Exclude) 56 } 57 58 func (c *CollectHostUtility) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { 59 hostCollector := c.HostCollector 60 61 path, err := exec.LookPath(hostCollector.UtilityName) 62 utilityInfo := HostUtilityInfo{ 63 Name: hostCollector.UtilityName, 64 Path: path, 65 } 66 if err != nil { 67 utilityInfo.Error = err.Error() 68 } 69 b, err := json.Marshal(utilityInfo) 70 if err != nil { 71 return nil, errors.Wrap(err, "failed to marshal host utility info") 72 } 73 74 resPath := util.TitleOrDefault(hostCollector.HostCollectorMeta, DefaultHostUtilityPath) 75 76 output := pkgcollector.NewResult() 77 _ = output.SaveResult(c.BundlePath, fmt.Sprintf(UtilityPathFormat, resPath), bytes.NewBuffer(b)) 78 return output, nil 79 }