|
| 1 | +package collect |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + |
| 9 | + troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
| 10 | + "k8s.io/klog/v2" |
| 11 | +) |
| 12 | + |
| 13 | +type CollectHostCopy struct { |
| 14 | + hostCollector *troubleshootv1beta2.HostCopy |
| 15 | + BundlePath string |
| 16 | +} |
| 17 | + |
| 18 | +func (c *CollectHostCopy) Title() string { |
| 19 | + return hostCollectorTitleOrDefault(c.hostCollector.HostCollectorMeta, "copy") |
| 20 | +} |
| 21 | + |
| 22 | +func (c *CollectHostCopy) IsExcluded() (bool, error) { |
| 23 | + return isExcluded(c.hostCollector.Exclude) |
| 24 | +} |
| 25 | + |
| 26 | +func (c *CollectHostCopy) Collect(progressChan chan<- interface{}) (map[string][]byte, error) { |
| 27 | + // 1. Construct subdirectory path in the bundle path to copy files into |
| 28 | + // output.SaveResult() will create the directory if it doesn't exist |
| 29 | + bundleRelPath := filepath.Join("host-collectors", c.Title()) |
| 30 | + bundlePathDest := filepath.Join(c.BundlePath, bundleRelPath) |
| 31 | + |
| 32 | + // 2. Enumerate all files that match the glob pattern |
| 33 | + paths, err := filepath.Glob(c.hostCollector.Path) |
| 34 | + if err != nil { |
| 35 | + klog.Errorf("Invalid glob pattern %q: %v", c.hostCollector.Path, err) |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + |
| 39 | + if len(paths) == 0 { |
| 40 | + klog.V(1).Info("No files found to copy") |
| 41 | + return NewResult(), nil |
| 42 | + } |
| 43 | + |
| 44 | + // 3. Copy content in found host paths to the subdirectory |
| 45 | + klog.V(1).Infof("Copy files from %q to %q", c.hostCollector.Path, bundleRelPath) |
| 46 | + result, err := c.copyFilesToBundle(paths, bundlePathDest) |
| 47 | + if err != nil { |
| 48 | + klog.Errorf("Failed to copy files from %q to %q: %v", c.hostCollector.Path, "<bundle>/"+bundleRelPath, err) |
| 49 | + fileName := fmt.Sprintf("%s/errors.json", c.relBundlePath(bundlePathDest)) |
| 50 | + output := NewResult() |
| 51 | + err := output.SaveResult(c.BundlePath, fileName, marshalErrors([]string{err.Error()})) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + return output, nil |
| 56 | + } |
| 57 | + |
| 58 | + return result, nil |
| 59 | +} |
| 60 | + |
| 61 | +func (c *CollectHostCopy) relBundlePath(path string) string { |
| 62 | + s := strings.ReplaceAll(path, c.BundlePath, "") |
| 63 | + return strings.TrimPrefix(s, "/") |
| 64 | +} |
| 65 | + |
| 66 | +// copyFilesToBundle copies files from the host to the bundle. |
| 67 | +func (c *CollectHostCopy) copyFilesToBundle(paths []string, dstDir string) (CollectorResult, error) { |
| 68 | + result := NewResult() |
| 69 | + |
| 70 | + for _, path := range paths { |
| 71 | + dst := filepath.Join(dstDir, filepath.Base(path)) |
| 72 | + err := c.doCopy(path, dst, result) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return result, nil |
| 79 | +} |
| 80 | + |
| 81 | +func (c *CollectHostCopy) doCopy(src, dst string, result CollectorResult) error { |
| 82 | + srcInfo, err := os.Stat(src) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + if srcInfo.Mode().IsRegular() { |
| 88 | + err := c.copyFile(src, dst, result) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + } else if srcInfo.IsDir() { |
| 93 | + err := c.copyDir(src, dst, result) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + } else { |
| 98 | + klog.V(2).Infof("Skipping non-file, non-directory path: %q", src) |
| 99 | + } |
| 100 | + |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +// copyFile copies a file from src to the bundle |
| 105 | +func (c *CollectHostCopy) copyFile(src, dst string, result CollectorResult) error { |
| 106 | + in, err := os.Open(src) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + defer in.Close() |
| 111 | + |
| 112 | + relDest := c.relBundlePath(dst) |
| 113 | + err = result.SaveResult(c.BundlePath, relDest, in) |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +// copyDir recursively copies a directory tree to the bundle |
| 121 | +func (c *CollectHostCopy) copyDir(src, dst string, result CollectorResult) error { |
| 122 | + // Enunmerate all entries in the source dir |
| 123 | + entries, err := os.ReadDir(src) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + for _, entry := range entries { |
| 129 | + srcPointer := filepath.Join(src, entry.Name()) |
| 130 | + dstPointer := filepath.Join(dst, entry.Name()) |
| 131 | + |
| 132 | + err = c.doCopy(srcPointer, dstPointer, result) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return nil |
| 139 | +} |
0 commit comments