|
| 1 | +package configurer |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/Mirantis/launchpad/pkg/util/fileutil" |
| 13 | + log "github.com/sirupsen/logrus" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + downloadedInstallers = map[string]string{} // Global list of downloaded installers, to prevent repetition. |
| 18 | + ErrInstallerDownloadFailed = errors.New("could not download installer") |
| 19 | +) |
| 20 | + |
| 21 | +func GetInstaller(source string) (string, error) { |
| 22 | + path, ok := downloadedInstallers[source] |
| 23 | + if ok { |
| 24 | + return path, nil |
| 25 | + } |
| 26 | + |
| 27 | + if path == "" { |
| 28 | + return "", fmt.Errorf("%w; skipping failed installer download", ErrInstallerDownloadFailed) |
| 29 | + } |
| 30 | + |
| 31 | + path, getErr := downloadInstaller(source) |
| 32 | + if getErr != nil { |
| 33 | + return "", fmt.Errorf("%w, installer download failed; %s", ErrInstallerDownloadFailed, getErr.Error()) |
| 34 | + } |
| 35 | + downloadedInstallers[source] = path |
| 36 | + return path, nil |
| 37 | +} |
| 38 | + |
| 39 | +// Run does all the prep work on the hosts in parallel. |
| 40 | +func downloadInstaller(path string) (string, error) { |
| 41 | + winScript, err := getScript(path) |
| 42 | + if err != nil { |
| 43 | + return "", fmt.Errorf("failed to get Windows installer script: %w", err) |
| 44 | + } |
| 45 | + f, err := os.CreateTemp("", "installerWindows") |
| 46 | + if err != nil { |
| 47 | + return "", fmt.Errorf("failed to create temporary file for windows installer script: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + _, err = f.WriteString(winScript) |
| 51 | + if err != nil { |
| 52 | + return "", fmt.Errorf("failed to write to temporary file for windows installer script: %w", err) |
| 53 | + } |
| 54 | + |
| 55 | + return f.Name(), nil |
| 56 | +} |
| 57 | + |
| 58 | +func parseURL(uri string) (*url.URL, error) { |
| 59 | + if !strings.Contains(uri, "://") { |
| 60 | + return &url.URL{Path: uri, Scheme: "file"}, nil |
| 61 | + } |
| 62 | + |
| 63 | + u, err := url.ParseRequestURI(uri) |
| 64 | + if err != nil { |
| 65 | + return nil, fmt.Errorf("failed to parse installer URL: %w", err) |
| 66 | + } |
| 67 | + return u, nil |
| 68 | +} |
| 69 | + |
| 70 | +var errInvalidScript = fmt.Errorf("invalid container runtime install script") |
| 71 | + |
| 72 | +func getScript(uri string) (string, error) { |
| 73 | + u, err := parseURL(uri) |
| 74 | + if err != nil { |
| 75 | + return "", err |
| 76 | + } |
| 77 | + |
| 78 | + var data string |
| 79 | + |
| 80 | + if u.Scheme == "file" { |
| 81 | + data, err = readFile(u.Path) |
| 82 | + } else { |
| 83 | + data, err = downloadFile(uri) |
| 84 | + } |
| 85 | + |
| 86 | + log.Debugf("read %d bytes from %s", len(data), uri) |
| 87 | + |
| 88 | + if err != nil { |
| 89 | + return "", err |
| 90 | + } |
| 91 | + |
| 92 | + if len(data) < 10 { |
| 93 | + // cant fit an installer into that! |
| 94 | + return "", fmt.Errorf("%w: script is too short", errInvalidScript) |
| 95 | + } |
| 96 | + |
| 97 | + if !strings.HasPrefix(data, "#") { |
| 98 | + log.Warnf("possibly invalid container runtime install script in %s", uri) |
| 99 | + } |
| 100 | + |
| 101 | + return data, nil |
| 102 | +} |
| 103 | + |
| 104 | +func downloadFile(url string) (string, error) { |
| 105 | + log.Infof("downloading container runtime install script from %s", url) |
| 106 | + resp, err := http.Get(url) //nolint:gosec // "G107: Url provided to HTTP request as taint input" -- user-provided URL is ok here |
| 107 | + if err != nil { |
| 108 | + return "", fmt.Errorf("failed to download container runtime install script: %w", err) |
| 109 | + } |
| 110 | + defer resp.Body.Close() |
| 111 | + |
| 112 | + body, err := io.ReadAll(resp.Body) |
| 113 | + if err != nil { |
| 114 | + return "", fmt.Errorf("failed to read response body: %w", err) |
| 115 | + } |
| 116 | + return string(body), nil |
| 117 | +} |
| 118 | + |
| 119 | +func readFile(path string) (string, error) { |
| 120 | + log.Infof("reading container runtime install script from %s", path) |
| 121 | + |
| 122 | + data, err := fileutil.LoadExternalFile(path) |
| 123 | + return string(data), err |
| 124 | +} |
0 commit comments