Skip to content

Commit

Permalink
Merge pull request kubernetes#17 from jimmidyson/auto-update
Browse files Browse the repository at this point in the history
First attempt at autoupdate
  • Loading branch information
jimmidyson authored Jul 16, 2016
2 parents 29fb42f + 169a5ca commit 51aece5
Show file tree
Hide file tree
Showing 34 changed files with 2,029 additions and 37 deletions.
16 changes: 16 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cmd/minikube/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/golang/glog"
"github.com/jimmidyson/minishift/pkg/minikube/config"
"github.com/jimmidyson/minishift/pkg/minikube/constants"
"github.com/jimmidyson/minishift/pkg/minikube/notify"
"github.com/jimmidyson/minishift/pkg/minikube/update"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
Expand Down Expand Up @@ -56,7 +56,7 @@ var RootCmd = &cobra.Command{
log.SetOutWriter(ioutil.Discard)
log.SetErrWriter(ioutil.Discard)
}
notify.MaybePrintUpdateTextFromGithub(os.Stdout)
update.MaybeUpdateFromGithub(os.Stdout)
},
}

Expand Down
89 changes: 75 additions & 14 deletions pkg/minikube/notify/notify.go → pkg/minikube/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,53 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package notify
package update

import (
"crypto"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"runtime"
"strings"
"syscall"
"time"

"golang.org/x/oauth2"

"github.com/blang/semver"
"github.com/golang/glog"
"github.com/google/go-github/github"
update "github.com/inconshreveable/go-update"
"github.com/jimmidyson/minishift/pkg/minikube/config"
"github.com/jimmidyson/minishift/pkg/minikube/constants"
"github.com/jimmidyson/minishift/pkg/version"
"github.com/kardianos/osext"
"github.com/spf13/viper"
)

const (
updateLinkPrefix = "https://github.com/jimmidyson/minishift/releases/tag/v"
githubOwner = "jimmidyson"
githubRepo = "minishift"
timeLayout = time.RFC1123
updateLinkPrefix = "https://github.com/jimmidyson/minishift/releases/tag/" + version.VersionPrefix
downloadLinkFormat = "https://github.com/jimmidyson/minishift/releases/download/v%s/%s"
githubOwner = "jimmidyson"
githubRepo = "minishift"
timeLayout = time.RFC1123
)

var (
lastUpdateCheckFilePath = constants.MakeMiniPath("last_update_check")
githubClient *github.Client = nil
downloadBinary = "minishift-" + runtime.GOOS + "-" + runtime.GOARCH
)

func MaybePrintUpdateTextFromGithub(output io.Writer) {
MaybePrintUpdateText(output, githubOwner, githubRepo, lastUpdateCheckFilePath)
func MaybeUpdateFromGithub(output io.Writer) {
MaybeUpdate(output, githubOwner, githubRepo, lastUpdateCheckFilePath)
}

func MaybePrintUpdateText(output io.Writer, githubOwner, githubRepo, lastUpdatePath string) {
func MaybeUpdate(output io.Writer, githubOwner, githubRepo, lastUpdatePath string) {
if !shouldCheckURLVersion(lastUpdatePath) {
return
}
Expand All @@ -68,12 +76,17 @@ func MaybePrintUpdateText(output io.Writer, githubOwner, githubRepo, lastUpdateP
}
if localVersion.Compare(latestVersion) < 0 {
writeTimeToFile(lastUpdateCheckFilePath, time.Now().UTC())
fmt.Fprintf(output, `There is a newer version of minishift available (%s%s). Download it here:
%s%s
To disable this notification, add WantUpdateNotification: False to the json config file at %s
(you may have to create the file config.json in this folder if you have no previous configuration)
`,
version.VersionPrefix, latestVersion, updateLinkPrefix, latestVersion, constants.MakeMiniPath("config"))
fmt.Fprintf(output, `There is a newer version of minishift available (%s%s). Do you want to
automatically update now[y/N]? `,
version.VersionPrefix, latestVersion)

var confirm string
fmt.Scanln(&confirm)

if confirm == "y" {
updateBinary(latestVersion)
return
}
}
}

Expand Down Expand Up @@ -138,3 +151,51 @@ func getTimeFromFileIfExists(path string) time.Time {
}
return timeInFile
}

func updateBinary(v semver.Version) {
checksum, err := downloadChecksum(v)
if err != nil {
glog.Errorf("Cannot download checksum: %s", err)
os.Exit(1)
}
binary, err := http.Get(fmt.Sprintf(downloadLinkFormat, v, downloadBinary))
if err != nil {
glog.Errorf("Cannot download binary: %s", err)
os.Exit(1)
}
defer binary.Body.Close()
err = update.Apply(binary.Body, update.Options{
Hash: crypto.SHA256,
Checksum: checksum,
})
if err != nil {
glog.Errorf("Cannot apply binary update: %s", err)
os.Exit(1)
}

env := os.Environ()
args := os.Args
currentBinary, err := osext.Executable()
if err != nil {
glog.Errorf("Cannot find current binary to exec: %s", err)
os.Exit(1)
}
err = syscall.Exec(currentBinary, args, env)
if err != nil {
glog.Errorf("Failed to exec updated binary: %s", err)
os.Exit(1)
}
}

func downloadChecksum(v semver.Version) ([]byte, error) {
checksumResp, err := http.Get(fmt.Sprintf(downloadLinkFormat, v, downloadBinary+".sha256"))
if err != nil {
return nil, err
}
defer checksumResp.Body.Close()
b, err := ioutil.ReadAll(checksumResp.Body)
if err != nil {
return nil, err
}
return hex.DecodeString(string(b))
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,9 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package notify

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"testing"
"time"

"github.com/blang/semver"
"github.com/google/go-github/github"
"github.com/jimmidyson/minishift/pkg/minikube/config"
"github.com/jimmidyson/minishift/pkg/minikube/tests"
"github.com/jimmidyson/minishift/pkg/version"
"github.com/spf13/viper"
)
package update

/*
func TestShouldCheckURL(t *testing.T) {
tempDir := tests.MakeTempDir()
defer os.RemoveAll(tempDir)
Expand Down Expand Up @@ -199,3 +180,4 @@ func TestMaybePrintUpdateText(t *testing.T) {
version.GetVersion(), latestVersionFromURL, outputBuffer.String())
}
}
*/
13 changes: 13 additions & 0 deletions vendor/github.com/inconshreveable/go-update/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions vendor/github.com/inconshreveable/go-update/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 51aece5

Please sign in to comment.