|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "regexp" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/forj-oss/forjj-modules/trace" |
| 14 | +) |
| 15 | + |
| 16 | +// This file manage the relationship between simple-relmgt and github through github-release (https://github.com/aktau/github-release) |
| 17 | + |
| 18 | +// It download the binary from https://github.com/aktau/github-release/releases/download/v{version}/linux-amd64-github-release.tar.bz2 |
| 19 | + |
| 20 | +// Github represents the github-release command used by simple-relmgt |
| 21 | +type Github struct { |
| 22 | + url string |
| 23 | + file string |
| 24 | + version string |
| 25 | + untarCmd string |
| 26 | + packageName string |
| 27 | + packageExtract string |
| 28 | +} |
| 29 | + |
| 30 | +const ( |
| 31 | + defaultVersion = "v0.7.2" |
| 32 | + defaultURLPath = "https://github.com/aktau/github-release/releases/download/%s/%s" |
| 33 | + defaultFilePath = "bin/linux/amd64/github-release" |
| 34 | + defaultFileName = "linux-amd64-github-release.tar.bz2" |
| 35 | + defaultPackageExtract = "tar -xvjf -" |
| 36 | +) |
| 37 | + |
| 38 | +// NewGithub creates the Github object |
| 39 | +func NewGithub() (ret *Github) { |
| 40 | + ret = new(Github) |
| 41 | + |
| 42 | + ret.url = defaultURLPath |
| 43 | + ret.file = defaultFilePath |
| 44 | + ret.version = defaultVersion |
| 45 | + ret.packageName = defaultFileName |
| 46 | + ret.packageExtract = defaultPackageExtract |
| 47 | + |
| 48 | + return |
| 49 | +} |
| 50 | + |
| 51 | +// SetAppVersion define the version of github-release to use. |
| 52 | +func (g *Github) SetAppVersion(version string) { |
| 53 | + if g == nil { |
| 54 | + return |
| 55 | + } |
| 56 | + g.version = version |
| 57 | +} |
| 58 | + |
| 59 | +// SetURLPath define the URL path where versioned package are stored. |
| 60 | +func (g *Github) SetURLPath(urlPath string) (err error) { |
| 61 | + if g == nil { |
| 62 | + return errors.New("Github object is nil") |
| 63 | + } |
| 64 | + |
| 65 | + if found, _ := regexp.Match("%s.*%s", []byte(urlPath)); !found { |
| 66 | + return fmt.Errorf("%s is an invalid package URL base. It must contains '%%s' twice. The first one will get package version, the second will get package file name", urlPath) |
| 67 | + } |
| 68 | + finalURL := fmt.Sprintf(urlPath, g.version, g.packageName) |
| 69 | + |
| 70 | + urlTest := new(url.URL) |
| 71 | + if urlTest == nil { |
| 72 | + return fmt.Errorf("Cannot test the URL %s. Unable to allocate url.URL", urlPath) |
| 73 | + } else if _, err := urlTest.Parse(finalURL); err != nil { |
| 74 | + return fmt.Errorf("The URL '%s' is invalid. %s", finalURL, err) |
| 75 | + } |
| 76 | + |
| 77 | + g.url = urlPath |
| 78 | + return |
| 79 | +} |
| 80 | + |
| 81 | +// CheckGithub verify the binary existence and its version. |
| 82 | +func (g *Github) CheckGithub() error { |
| 83 | + if g == nil { |
| 84 | + return errors.New("Github object is nil") |
| 85 | + } |
| 86 | + if ok, _ := g.checkGithub(); !ok { |
| 87 | + return g.download() |
| 88 | + } |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +// Internal github-release check |
| 93 | +// - file found and executable |
| 94 | +// - returning version requested. |
| 95 | +func (g *Github) checkGithub() (bool, error) { |
| 96 | + if g == nil { |
| 97 | + return false, errors.New("Github object is nil") |
| 98 | + } |
| 99 | + |
| 100 | + gotrace.Trace("Checking %s ...", g.file) |
| 101 | + info, err := os.Stat(g.file) |
| 102 | + if err != nil { |
| 103 | + return false, err |
| 104 | + } |
| 105 | + |
| 106 | + mode := info.Mode().Perm() |
| 107 | + if (mode & 0100) == 0 { |
| 108 | + return false, fmt.Errorf("%s is not executable", g.file) |
| 109 | + } |
| 110 | + |
| 111 | + command := exec.Command(g.file, "--version") |
| 112 | + output, err := command.Output() |
| 113 | + if err != nil { |
| 114 | + return false, err |
| 115 | + } |
| 116 | + |
| 117 | + if !strings.Contains(string(output), g.version) { |
| 118 | + return false, fmt.Errorf("Version %s not detected. Got %s", g.version, string(output)) |
| 119 | + } |
| 120 | + gotrace.Info("OK: Found %s version %s", g.file, g.version) |
| 121 | + return true, nil |
| 122 | +} |
| 123 | + |
| 124 | +// Download the github-release file |
| 125 | +func (g *Github) download() (err error) { |
| 126 | + if g == nil { |
| 127 | + return errors.New("Github object is nil") |
| 128 | + } |
| 129 | + |
| 130 | + finalURL := fmt.Sprintf(g.url, g.version, g.packageName) |
| 131 | + gotrace.Trace("Downloading %s...", finalURL) |
| 132 | + |
| 133 | + // Get the data |
| 134 | + resp, err := http.Get(finalURL) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + defer resp.Body.Close() |
| 139 | + |
| 140 | + // Check server response |
| 141 | + if resp.StatusCode != http.StatusOK { |
| 142 | + return fmt.Errorf("Unable to download '%s'. bad status: %s", finalURL, resp.Status) |
| 143 | + } |
| 144 | + |
| 145 | + cmds := strings.Split(g.packageExtract, " ") |
| 146 | + cmd := cmds[0] |
| 147 | + params := cmds[1:] |
| 148 | + command := exec.Command(cmd, params...) |
| 149 | + |
| 150 | + command.Stdin = resp.Body |
| 151 | + |
| 152 | + if err = command.Start(); err != nil { |
| 153 | + return fmt.Errorf("Unable to run '%s'. %s", g.packageExtract, err) |
| 154 | + } |
| 155 | + |
| 156 | + if err = command.Wait(); err != nil { |
| 157 | + return fmt.Errorf("Issue to run 'curl %s | %s'. %s", finalURL, g.packageExtract, err) |
| 158 | + } |
| 159 | + |
| 160 | + if info, err := os.Stat(g.file); err != nil { |
| 161 | + return fmt.Errorf("Unable to find '%s' from '%s'. %s", g.file, g.packageName, err) |
| 162 | + } else { |
| 163 | + mode := info.Mode().Perm() |
| 164 | + if (mode & 0100) == 0 { |
| 165 | + if err = os.Chmod(g.file, 0755); err != nil { |
| 166 | + return fmt.Errorf("Unable to set '%s' as executable. %s", g.file, err) |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + if _, err := g.checkGithub(); err != nil { |
| 172 | + return fmt.Errorf("Something is wrong. Expected to have an executable %s version %s. %s", g.file, g.version, err) |
| 173 | + } |
| 174 | + |
| 175 | + return |
| 176 | +} |
0 commit comments