2014-07-12 22:45:33 +00:00
|
|
|
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
// more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-06-01 20:50:14 +00:00
|
|
|
|
2014-09-28 21:10:43 +00:00
|
|
|
// +build !windows,!noupgrade
|
2014-06-16 07:52:14 +00:00
|
|
|
|
2014-07-31 14:01:23 +00:00
|
|
|
package upgrade
|
2014-05-02 08:01:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"compress/gzip"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-05-02 15:04:45 +00:00
|
|
|
"io/ioutil"
|
2014-05-02 08:01:09 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-07-31 14:01:23 +00:00
|
|
|
// Upgrade to the given release, saving the previous binary with a ".old" extension.
|
2014-09-28 21:09:55 +00:00
|
|
|
func upgradeTo(path string, rel Release, archExtra string) error {
|
2014-08-01 14:30:28 +00:00
|
|
|
osName := runtime.GOOS
|
|
|
|
if osName == "darwin" {
|
|
|
|
// We call the darwin release bundles macosx because that makes more
|
|
|
|
// sense for people downloading them
|
|
|
|
osName = "macosx"
|
|
|
|
}
|
2014-08-07 13:57:20 +00:00
|
|
|
expectedRelease := fmt.Sprintf("syncthing-%s-%s%s-%s.", osName, runtime.GOARCH, archExtra, rel.Tag)
|
|
|
|
if debug {
|
|
|
|
l.Debugf("expected release asset %q", expectedRelease)
|
|
|
|
}
|
2014-05-02 08:01:09 +00:00
|
|
|
for _, asset := range rel.Assets {
|
2014-08-07 13:57:20 +00:00
|
|
|
if debug {
|
|
|
|
l.Debugln("considering release", asset)
|
|
|
|
}
|
2014-05-02 08:01:09 +00:00
|
|
|
if strings.HasPrefix(asset.Name, expectedRelease) {
|
|
|
|
if strings.HasSuffix(asset.Name, ".tar.gz") {
|
2014-05-02 15:04:45 +00:00
|
|
|
fname, err := readTarGZ(asset.URL, filepath.Dir(path))
|
2014-05-02 08:01:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:01:23 +00:00
|
|
|
old := path + ".old"
|
2014-05-02 08:01:09 +00:00
|
|
|
err = os.Rename(path, old)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.Rename(fname, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 14:01:23 +00:00
|
|
|
return ErrVersionUnknown
|
2014-07-14 08:45:29 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:01:23 +00:00
|
|
|
// Returns the latest release, including prereleases or not depending on the argument
|
|
|
|
func LatestRelease(prerelease bool) (Release, error) {
|
2014-08-01 14:35:37 +00:00
|
|
|
resp, err := http.Get("https://api.github.com/repos/syncthing/syncthing/releases?per_page=10")
|
2014-07-14 08:45:29 +00:00
|
|
|
if err != nil {
|
2014-07-31 14:01:23 +00:00
|
|
|
return Release{}, err
|
2014-07-14 08:45:29 +00:00
|
|
|
}
|
2014-07-31 08:26:45 +00:00
|
|
|
if resp.StatusCode > 299 {
|
2014-07-31 14:01:23 +00:00
|
|
|
return Release{}, fmt.Errorf("API call returned HTTP error: %s", resp.Status)
|
2014-07-31 08:26:45 +00:00
|
|
|
}
|
2014-07-14 08:45:29 +00:00
|
|
|
|
2014-07-31 14:01:23 +00:00
|
|
|
var rels []Release
|
2014-07-14 08:45:29 +00:00
|
|
|
json.NewDecoder(resp.Body).Decode(&rels)
|
|
|
|
resp.Body.Close()
|
|
|
|
|
2014-07-31 07:04:57 +00:00
|
|
|
if len(rels) == 0 {
|
2014-07-31 14:01:23 +00:00
|
|
|
return Release{}, ErrVersionUnknown
|
2014-07-31 07:04:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 14:01:23 +00:00
|
|
|
if prerelease {
|
|
|
|
// We are a beta version. Use the latest.
|
2014-07-24 12:23:25 +00:00
|
|
|
return rels[0], nil
|
|
|
|
} else {
|
|
|
|
// We are a regular release. Only consider non-prerelease versions for upgrade.
|
|
|
|
for _, rel := range rels {
|
|
|
|
if !rel.Prerelease {
|
|
|
|
return rel, nil
|
|
|
|
}
|
2014-07-24 10:55:41 +00:00
|
|
|
}
|
2014-07-31 14:01:23 +00:00
|
|
|
return Release{}, ErrVersionUnknown
|
2014-07-14 08:45:29 +00:00
|
|
|
}
|
2014-05-02 08:01:09 +00:00
|
|
|
}
|
|
|
|
|
2014-05-02 15:04:45 +00:00
|
|
|
func readTarGZ(url string, dir string) (string, error) {
|
2014-08-07 13:57:20 +00:00
|
|
|
if debug {
|
|
|
|
l.Debugf("loading %q", url)
|
|
|
|
}
|
|
|
|
|
2014-05-02 08:01:09 +00:00
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Accept", "application/octet-stream")
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
gr, err := gzip.NewReader(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
tr := tar.NewReader(gr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through the files in the archive.
|
|
|
|
for {
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
// end of tar archive
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-08-07 13:57:20 +00:00
|
|
|
if debug {
|
|
|
|
l.Debugf("considering file %q", hdr.Name)
|
|
|
|
}
|
2014-05-02 08:01:09 +00:00
|
|
|
|
|
|
|
if path.Base(hdr.Name) == "syncthing" {
|
2014-05-02 15:04:45 +00:00
|
|
|
of, err := ioutil.TempFile(dir, "syncthing")
|
2014-05-02 08:01:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
io.Copy(of, tr)
|
|
|
|
err = of.Close()
|
|
|
|
if err != nil {
|
2014-05-02 15:04:45 +00:00
|
|
|
os.Remove(of.Name())
|
2014-05-02 08:01:09 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-05-02 15:04:45 +00:00
|
|
|
os.Chmod(of.Name(), os.FileMode(hdr.Mode))
|
|
|
|
return of.Name(), nil
|
2014-05-02 08:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("No upgrade found")
|
|
|
|
}
|