2016-05-24 11:55:48 +02:00
|
|
|
package util
|
2016-04-26 20:18:28 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2016-05-17 17:18:26 +02:00
|
|
|
"time"
|
2017-02-21 20:27:01 +01:00
|
|
|
"context"
|
2016-04-26 20:18:28 +02:00
|
|
|
|
|
|
|
"github.com/google/go-github/github"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Adapted from github.com/apex/apex
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// UpdateAvailable contains the full repository information for the latest release of Semaphore
|
2016-05-17 17:18:26 +02:00
|
|
|
var UpdateAvailable *github.RepositoryRelease
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// DoUpgrade checks for an update, and if available downloads the binary and installs it
|
2016-05-24 11:55:48 +02:00
|
|
|
func DoUpgrade(version string) error {
|
2016-04-26 20:18:28 +02:00
|
|
|
fmt.Printf("current release is v%s\n", version)
|
|
|
|
|
2016-05-17 17:18:26 +02:00
|
|
|
if err := CheckUpdate(version); err != nil || UpdateAvailable == nil {
|
2016-04-26 20:18:28 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-17 17:18:26 +02:00
|
|
|
asset := findAsset(UpdateAvailable)
|
2016-04-26 20:18:28 +02:00
|
|
|
if asset == nil {
|
|
|
|
return errors.New("cannot find binary for your system")
|
|
|
|
}
|
|
|
|
|
|
|
|
// create tmp file
|
|
|
|
tmpPath := filepath.Join(os.TempDir(), "semaphore-upgrade")
|
2018-03-27 22:12:47 +02:00
|
|
|
f, err := os.OpenFile(tmpPath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0755) //nolint: gas
|
2016-04-26 20:18:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// download binary
|
|
|
|
fmt.Printf("downloading %s\n", *asset.BrowserDownloadURL)
|
|
|
|
res, err := http.Get(*asset.BrowserDownloadURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-27 22:12:47 +02:00
|
|
|
|
|
|
|
defer res.Body.Close() //nolint: errcheck
|
2016-04-26 20:18:28 +02:00
|
|
|
|
|
|
|
// copy it down
|
|
|
|
_, err = io.Copy(f, res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace it
|
2016-05-23 21:29:38 +02:00
|
|
|
cmdPath := FindSemaphore()
|
|
|
|
if len(cmdPath) == 0 {
|
|
|
|
return errors.New("Cannot find semaphore binary")
|
2016-04-26 20:18:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("replacing %s\n", cmdPath)
|
|
|
|
err = os.Rename(tmpPath, cmdPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("visit https://github.com/ansible-semaphore/semaphore/releases for the changelog")
|
2016-05-17 17:18:26 +02:00
|
|
|
go func() {
|
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
|
2016-04-26 20:18:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// FindSemaphore looks in the PATH for the semaphore variable
|
|
|
|
// if not found it will attempt to find the absolute path of the first
|
|
|
|
// os argument, the semaphore command, and return it
|
2016-05-23 21:29:38 +02:00
|
|
|
func FindSemaphore() string {
|
2018-03-27 22:12:47 +02:00
|
|
|
cmdPath, _ := exec.LookPath("semaphore") //nolint: gas
|
2016-05-23 21:29:38 +02:00
|
|
|
|
|
|
|
if len(cmdPath) == 0 {
|
2018-03-27 22:12:47 +02:00
|
|
|
cmdPath, _ = filepath.Abs(os.Args[0]) // nolint: gas
|
2016-05-23 21:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return cmdPath
|
|
|
|
}
|
|
|
|
|
2016-04-26 20:18:28 +02:00
|
|
|
// findAsset returns the binary for this platform.
|
|
|
|
func findAsset(release *github.RepositoryRelease) *github.ReleaseAsset {
|
|
|
|
for _, asset := range release.Assets {
|
|
|
|
if *asset.Name == fmt.Sprintf("semaphore_%s_%s", runtime.GOOS, runtime.GOARCH) {
|
|
|
|
return &asset
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-05-17 17:18:26 +02:00
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// CheckUpdate uses the github client to check for new tags in the semaphore repo
|
2016-05-17 17:18:26 +02:00
|
|
|
func CheckUpdate(version string) error {
|
|
|
|
// fetch releases
|
|
|
|
gh := github.NewClient(nil)
|
2017-02-21 20:27:01 +01:00
|
|
|
releases, _, err := gh.Repositories.ListReleases(context.TODO(), "ansible-semaphore", "semaphore", nil)
|
2016-05-17 17:18:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateAvailable = nil
|
|
|
|
if (*releases[0].TagName)[1:] != version {
|
2016-06-25 03:04:24 +02:00
|
|
|
UpdateAvailable = releases[0]
|
2016-05-17 17:18:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|