forked from kubernetes/minikube
/
notify.go
130 lines (112 loc) · 3.59 KB
/
notify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package notify
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/blang/semver"
"github.com/golang/glog"
"github.com/spf13/viper"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/version"
)
const updateLinkPrefix = "https://github.com/kubernetes/minikube/releases/tag/v"
var (
timeLayout = time.RFC1123
lastUpdateCheckFilePath = constants.MakeMiniPath("last_update_check")
githubMinikubeReleasesURL = "https://storage.googleapis.com/minikube/releases.json"
)
func MaybePrintUpdateTextFromGithub(output io.Writer) {
MaybePrintUpdateText(output, githubMinikubeReleasesURL, lastUpdateCheckFilePath)
}
func MaybePrintUpdateText(output io.Writer, url string, lastUpdatePath string) {
if !shouldCheckURLVersion(lastUpdatePath) {
return
}
latestVersion, err := getLatestVersionFromURL(url)
if err != nil {
glog.Errorln(err)
return
}
localVersion, err := version.GetSemverVersion()
if err != nil {
glog.Errorln(err)
return
}
if localVersion.Compare(latestVersion) < 0 {
writeTimeToFile(lastUpdateCheckFilePath, time.Now().UTC())
fmt.Fprintf(output, `There is a newer version of minikube 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"))
}
}
func shouldCheckURLVersion(filePath string) bool {
if !viper.GetBool(config.WantUpdateNotification) {
return false
}
lastUpdateTime := getTimeFromFileIfExists(filePath)
if time.Since(lastUpdateTime).Hours() < viper.GetFloat64(config.ReminderWaitPeriodInHours) {
return false
}
return true
}
type release struct {
Name string
}
type releases []release
func getJson(url string, target *releases) error {
r, err := http.Get(url)
if err != nil {
return err
}
defer r.Body.Close()
return json.NewDecoder(r.Body).Decode(target)
}
func getLatestVersionFromURL(url string) (semver.Version, error) {
var releases releases
if err := getJson(url, &releases); err != nil {
return semver.Version{}, err
}
if len(releases) == 0 {
return semver.Version{}, fmt.Errorf("There were no json releases at the url specified: %s", url)
}
latestVersionString := releases[0].Name
return semver.Make(strings.TrimPrefix(latestVersionString, version.VersionPrefix))
}
func writeTimeToFile(path string, inputTime time.Time) error {
err := ioutil.WriteFile(path, []byte(inputTime.Format(timeLayout)), 0644)
if err != nil {
return fmt.Errorf("Error writing current update time to file: ", err)
}
return nil
}
func getTimeFromFileIfExists(path string) time.Time {
lastUpdateCheckTime, err := ioutil.ReadFile(path)
if err != nil {
return time.Time{}
}
timeInFile, err := time.Parse(timeLayout, string(lastUpdateCheckTime))
if err != nil {
return time.Time{}
}
return timeInFile
}