|
1 | 1 | package lib
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "log"
|
| 6 | + "os" |
5 | 7 | "os/exec"
|
| 8 | + "path/filepath" |
6 | 9 | "strings"
|
7 | 10 | )
|
8 | 11 |
|
@@ -56,3 +59,171 @@ func ParsePhaseFromYAML(yamlOutput string) string {
|
56 | 59 | }
|
57 | 60 | return ""
|
58 | 61 | }
|
| 62 | + |
| 63 | +type CLISetup struct { |
| 64 | + repoURL string |
| 65 | + installArgs []string |
| 66 | + namespace string |
| 67 | +} |
| 68 | + |
| 69 | +func NewOADPCLISetup() *CLISetup { |
| 70 | + return &CLISetup{ |
| 71 | + repoURL: "https://github.com/migtools/oadp-cli.git", |
| 72 | + installArgs: []string{"build"}, |
| 73 | + namespace: "openshift-adp", |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func (c *CLISetup) Install() error { |
| 78 | + tmpDir, err := c.createTempDir() |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + defer os.RemoveAll(tmpDir) |
| 83 | + |
| 84 | + cloneDir := filepath.Join(tmpDir, "oadp-cli") |
| 85 | + |
| 86 | + steps := []struct { |
| 87 | + name string |
| 88 | + fn func() error |
| 89 | + }{ |
| 90 | + {"Cloning repository", func() error { return c.cloneRepo(cloneDir) }}, |
| 91 | + {"Building and installing", func() error { return c.buildAndInstall(cloneDir) }}, |
| 92 | + {"Verifying installation", func() error { return c.verifyInstallation() }}, |
| 93 | + {"Configuring namespace", func() error { return c.configureNamespace() }}, |
| 94 | + } |
| 95 | + |
| 96 | + for _, step := range steps { |
| 97 | + log.Printf("OADP CLI Setup: %s...", step.name) |
| 98 | + if err := step.fn(); err != nil { |
| 99 | + return fmt.Errorf("%s failed: %w", step.name, err) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + log.Print("OADP CLI setup completed successfully") |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +func (c *CLISetup) createTempDir() (string, error) { |
| 108 | + tmpDir, err := os.MkdirTemp("", "oadp-cli-*") |
| 109 | + if err != nil { |
| 110 | + return "", fmt.Errorf("failed to create temp directory: %w", err) |
| 111 | + } |
| 112 | + return tmpDir, nil |
| 113 | +} |
| 114 | + |
| 115 | +func (c *CLISetup) cloneRepo(cloneDir string) error { |
| 116 | + return runCommand("git", []string{"clone", c.repoURL, cloneDir}, "") |
| 117 | +} |
| 118 | + |
| 119 | +func (c *CLISetup) buildAndInstall(cloneDir string) error { |
| 120 | + // Build the binary |
| 121 | + log.Print("Building OADP CLI...") |
| 122 | + if err := runCommand("make", c.installArgs, cloneDir); err != nil { |
| 123 | + return fmt.Errorf("build failed: %w", err) |
| 124 | + } |
| 125 | + |
| 126 | + // Verify the binary was created |
| 127 | + binaryPath := filepath.Join(cloneDir, "kubectl-oadp") |
| 128 | + if _, err := os.Stat(binaryPath); err != nil { |
| 129 | + return fmt.Errorf("kubectl-oadp binary not found at %s: %w", binaryPath, err) |
| 130 | + } |
| 131 | + |
| 132 | + // Try multiple target locations, starting with user-writable paths |
| 133 | + targetPaths := []string{ |
| 134 | + fmt.Sprintf("%s/bin/kubectl-oadp", os.Getenv("HOME")), |
| 135 | + "/usr/local/bin/kubectl-oadp", |
| 136 | + } |
| 137 | + |
| 138 | + var targetPath string |
| 139 | + var moveErr error |
| 140 | + |
| 141 | + for _, tp := range targetPaths { |
| 142 | + targetPath = tp |
| 143 | + log.Printf("Attempting to move binary from %s to %s", binaryPath, targetPath) |
| 144 | + |
| 145 | + // Create directory if it doesn't exist (for ~/bin) |
| 146 | + if targetPath == fmt.Sprintf("%s/bin/kubectl-oadp", os.Getenv("HOME")) { |
| 147 | + binDir := filepath.Dir(targetPath) |
| 148 | + if err := os.MkdirAll(binDir, 0755); err != nil { |
| 149 | + log.Printf("Failed to create directory %s: %v", binDir, err) |
| 150 | + continue |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + if err := runCommand("mv", []string{binaryPath, targetPath}, ""); err != nil { |
| 155 | + log.Printf("Failed to move to %s: %v", targetPath, err) |
| 156 | + moveErr = err |
| 157 | + continue |
| 158 | + } |
| 159 | + |
| 160 | + // Success! |
| 161 | + moveErr = nil |
| 162 | + break |
| 163 | + } |
| 164 | + |
| 165 | + if moveErr != nil { |
| 166 | + return fmt.Errorf("failed to move binary to any location: %w", moveErr) |
| 167 | + } |
| 168 | + |
| 169 | + // Make it executable |
| 170 | + if err := runCommand("chmod", []string{"+x", targetPath}, ""); err != nil { |
| 171 | + return fmt.Errorf("failed to make binary executable: %w", err) |
| 172 | + } |
| 173 | + |
| 174 | + // If we installed to ~/bin, ensure it's in PATH |
| 175 | + if targetPath == fmt.Sprintf("%s/bin/kubectl-oadp", os.Getenv("HOME")) { |
| 176 | + homeBin := fmt.Sprintf("%s/bin", os.Getenv("HOME")) |
| 177 | + currentPath := os.Getenv("PATH") |
| 178 | + if !strings.Contains(currentPath, homeBin) { |
| 179 | + newPath := fmt.Sprintf("%s:%s", homeBin, currentPath) |
| 180 | + os.Setenv("PATH", newPath) |
| 181 | + log.Printf("Added %s to PATH", homeBin) |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + log.Printf("Successfully installed kubectl-oadp to %s", targetPath) |
| 186 | + return nil |
| 187 | +} |
| 188 | + |
| 189 | +func (c *CLISetup) verifyInstallation() error { |
| 190 | + // Check current PATH |
| 191 | + cmd := exec.Command("bash", "-c", "echo $PATH") |
| 192 | + if output, err := cmd.CombinedOutput(); err == nil { |
| 193 | + log.Printf("Current PATH: %s", string(output)) |
| 194 | + } |
| 195 | + |
| 196 | + // Try to find kubectl-oadp binary |
| 197 | + cmd = exec.Command("which", "kubectl-oadp") |
| 198 | + if output, err := cmd.CombinedOutput(); err == nil { |
| 199 | + log.Printf("kubectl-oadp found at: %s", string(output)) |
| 200 | + } else { |
| 201 | + log.Printf("kubectl-oadp not found in PATH: %v", err) |
| 202 | + } |
| 203 | + |
| 204 | + // List kubectl plugins |
| 205 | + cmd = exec.Command("kubectl", "plugin", "list") |
| 206 | + if output, err := cmd.CombinedOutput(); err == nil { |
| 207 | + log.Printf("Available kubectl plugins: %s", string(output)) |
| 208 | + } |
| 209 | + |
| 210 | + return runCommand("kubectl", []string{"oadp", "version"}, "") |
| 211 | +} |
| 212 | + |
| 213 | +func (c *CLISetup) configureNamespace() error { |
| 214 | + return runCommand("kubectl", []string{"oadp", "client", "config", "set", fmt.Sprintf("namespace=%s", c.namespace)}, "") |
| 215 | +} |
| 216 | + |
| 217 | +func runCommand(name string, args []string, dir string) error { |
| 218 | + cmd := exec.Command(name, args...) |
| 219 | + if dir != "" { |
| 220 | + cmd.Dir = dir |
| 221 | + } |
| 222 | + |
| 223 | + output, err := cmd.CombinedOutput() |
| 224 | + if err != nil { |
| 225 | + return fmt.Errorf("command '%s %s' failed: %v\nOutput: %s", |
| 226 | + name, strings.Join(args, " "), err, string(output)) |
| 227 | + } |
| 228 | + return nil |
| 229 | +} |
0 commit comments