-
Notifications
You must be signed in to change notification settings - Fork 21
/
media.go
83 lines (74 loc) · 2.38 KB
/
media.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
// Copyright (c) 2016 by António Meireles <antonio.meireles@reformi.st>.
//
// 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 coreos
import (
"bufio"
"fmt"
"net/http"
"strings"
"github.com/blang/semver"
"github.com/helm/helm/log"
)
// Version validation
func Version(version string) string {
if version == defaultVersion {
return version
}
if _, err := semver.Make(version); err != nil {
log.Warn("'%s' is not in a recognizable CoreOS version format. "+
"Using default ('%s') instead", version, defaultVersion)
return defaultVersion
}
return version
}
// Channel validation
func Channel(name string) string {
for _, b := range Channels {
if b == name {
return b
}
}
log.Warn("'%s' is not a recognizable CoreOS image channel. "+
"Using default ('%s')", name, defaultChannel)
return defaultChannel
}
// LatestUpstream returns for the given channel the current shipping version
func LatestUpstream(channel string) (string, error) {
url := fmt.Sprintf("http://%s.release.core-os.net/"+
"amd64-usr/current/version.txt", channel)
response, err := http.Get(url)
// if err we're probably offline
if err != nil {
return "", err
}
defer response.Body.Close()
switch response.StatusCode {
case http.StatusOK, http.StatusNoContent:
default:
return "", fmt.Errorf("failed fetching %s: HTTP status: %s",
url, response.Status)
}
s := bufio.NewScanner(response.Body)
s.Split(bufio.ScanLines)
for s.Scan() {
line := s.Text()
if eq := strings.LastIndex(line, "COREOS_VERSION="); eq >= 0 {
if v := strings.Split(line, "=")[1]; len(v) > 0 {
return v, err
}
}
}
return "", fmt.Errorf("unable to grab 'COREOS_VERSION' from %s (!)", url)
}