|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "runtime" |
| 11 | +) |
| 12 | + |
| 13 | +/* |
| 14 | + * This installs Trivy using the official install script |
| 15 | + */ |
| 16 | +func InstallTrivy(trivyConfig *Runtime, registry string) error { |
| 17 | + log.Println("Installing Trivy") |
| 18 | + |
| 19 | + // Create Trivy installation directory |
| 20 | + trivyFolder := fmt.Sprintf("%s@%s", trivyConfig.Name(), trivyConfig.Version()) |
| 21 | + installDir := filepath.Join(Config.ToolsDirectory(), trivyFolder) |
| 22 | + |
| 23 | + // Check if already installed |
| 24 | + if isTrivyInstalled(trivyConfig) { |
| 25 | + fmt.Printf("Trivy %s is already installed\n", trivyConfig.Version()) |
| 26 | + return nil |
| 27 | + } |
| 28 | + |
| 29 | + // Create installation directory |
| 30 | + err := os.MkdirAll(installDir, 0755) |
| 31 | + if err != nil { |
| 32 | + return fmt.Errorf("failed to create installation directory: %w", err) |
| 33 | + } |
| 34 | + |
| 35 | + // Use the official install script to download and install Trivy |
| 36 | + version := fmt.Sprintf("v%s", trivyConfig.Version()) |
| 37 | + installScriptURL := "https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh" |
| 38 | + |
| 39 | + log.Printf("Installing Trivy %s using the official install script\n", version) |
| 40 | + |
| 41 | + // Create a temporary directory for the installation |
| 42 | + tempDir := filepath.Join(installDir, "temp") |
| 43 | + err = os.MkdirAll(tempDir, 0755) |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("failed to create temporary directory: %w", err) |
| 46 | + } |
| 47 | + |
| 48 | + // Download and run the install script |
| 49 | + cmd := exec.Command("sh", "-c", fmt.Sprintf("curl -sfL %s | sh -s -- -b %s %s", |
| 50 | + installScriptURL, tempDir, version)) |
| 51 | + |
| 52 | + output, err := cmd.CombinedOutput() |
| 53 | + if err != nil { |
| 54 | + return fmt.Errorf("failed to run Trivy install script: %w\nOutput: %s", err, string(output)) |
| 55 | + } |
| 56 | + |
| 57 | + log.Printf("Install script output: %s\n", string(output)) |
| 58 | + |
| 59 | + // Copy the Trivy binary to the final location |
| 60 | + sourcePath := filepath.Join(tempDir, "trivy") |
| 61 | + if runtime.GOOS == "windows" { |
| 62 | + sourcePath += ".exe" |
| 63 | + } |
| 64 | + |
| 65 | + binaryPath := filepath.Join(installDir, "trivy") |
| 66 | + if runtime.GOOS == "windows" { |
| 67 | + binaryPath += ".exe" |
| 68 | + } |
| 69 | + |
| 70 | + // Check if the source binary exists |
| 71 | + if _, err := os.Stat(sourcePath); os.IsNotExist(err) { |
| 72 | + return fmt.Errorf("trivy binary not found at %s after installation", sourcePath) |
| 73 | + } |
| 74 | + |
| 75 | + // Copy the binary to the final location |
| 76 | + source, err := os.Open(sourcePath) |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("failed to open source binary: %w", err) |
| 79 | + } |
| 80 | + defer source.Close() |
| 81 | + |
| 82 | + destination, err := os.Create(binaryPath) |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("failed to create destination binary: %w", err) |
| 85 | + } |
| 86 | + defer destination.Close() |
| 87 | + |
| 88 | + _, err = io.Copy(destination, source) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("failed to copy binary: %w", err) |
| 91 | + } |
| 92 | + |
| 93 | + // Make the copied binary executable |
| 94 | + err = os.Chmod(binaryPath, 0755) |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("failed to make binary executable: %w", err) |
| 97 | + } |
| 98 | + |
| 99 | + // Clean up the temporary directory |
| 100 | + os.RemoveAll(tempDir) |
| 101 | + |
| 102 | + log.Printf("Successfully installed Trivy %s\n", trivyConfig.Version()) |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +// isTrivyInstalled checks if Trivy is already installed |
| 107 | +func isTrivyInstalled(trivyConfig *Runtime) bool { |
| 108 | + trivyFolder := fmt.Sprintf("%s@%s", trivyConfig.Name(), trivyConfig.Version()) |
| 109 | + installDir := filepath.Join(Config.ToolsDirectory(), trivyFolder) |
| 110 | + binaryPath := filepath.Join(installDir, "trivy") |
| 111 | + |
| 112 | + // Add .exe extension for Windows |
| 113 | + if runtime.GOOS == "windows" { |
| 114 | + binaryPath += ".exe" |
| 115 | + } |
| 116 | + |
| 117 | + _, err := os.Stat(binaryPath) |
| 118 | + return err == nil |
| 119 | +} |
0 commit comments