forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.go
150 lines (128 loc) · 3.78 KB
/
publish.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
var apiUrl = flag.String("apiUrl", "https://grafana.com/api", "api url")
var apiKey = flag.String("apiKey", "", "api key")
var version = ""
var versionRe = regexp.MustCompile(`grafana-(.*)\.(linux|windows)`)
var builds = []build{}
func main() {
flag.Parse()
if *apiKey == "" {
log.Fatalf("Require apiKey command line parameters")
}
err := filepath.Walk("dist", packageWalker)
if err != nil {
log.Fatalf("Cannot find any packages to publish, %v", err)
}
if version == "" {
log.Fatalf("No version found")
}
if len(builds) == 0 {
log.Fatalf("No builds found")
}
nightly := release{
Version: version,
ReleaseDate: time.Now(),
Stable: false,
Nightly: true,
Beta: false,
WhatsNewUrl: "",
ReleaseNotesUrl: "",
Builds: builds,
}
postRequest("/grafana/versions", nightly, fmt.Sprintf("Create Release %s", nightly.Version))
postRequest("/grafana/versions/"+nightly.Version, nightly, fmt.Sprintf("Update Release %s", nightly.Version))
for _, b := range nightly.Builds {
postRequest(fmt.Sprintf("/grafana/versions/%s/packages", nightly.Version), b, fmt.Sprintf("Create Build %s %s", b.Os, b.Arch))
postRequest(fmt.Sprintf("/grafana/versions/%s/packages/%s/%s", nightly.Version, b.Arch, b.Os), b, fmt.Sprintf("Update Build %s %s", b.Os, b.Arch))
}
}
func packageWalker(path string, f os.FileInfo, err error) error {
if f.Name() == "dist" || strings.Contains(f.Name(), "sha256") || strings.Contains(f.Name(), "latest") {
return nil
}
log.Printf("Finding package file %s", f.Name())
result := versionRe.FindSubmatch([]byte(f.Name()))
if len(result) > 0 {
version = string(result[1])
log.Printf("Version detected: %v", version)
}
shaBytes, err := ioutil.ReadFile(path + ".sha256")
if err != nil {
log.Fatalf("Failed to read sha256 file %v", err)
}
os := ""
if strings.Contains(f.Name(), "linux-x64.tar.gz") {
os = "linux"
}
if strings.HasSuffix(f.Name(), "windows-x64.zip") {
os = "win"
}
if strings.HasSuffix(f.Name(), ".rpm") {
os = "rhel"
}
if strings.HasSuffix(f.Name(), ".deb") {
os = "deb"
}
builds = append(builds, build{
Os: os,
Arch: "amd64",
Url: "https://s3-us-west-2.amazonaws.com/grafana-releases/master/" + f.Name(),
Sha256: string(shaBytes),
})
return nil
}
func postRequest(url string, obj interface{}, desc string) {
jsonBytes, _ := json.Marshal(obj)
req, _ := http.NewRequest(http.MethodPost, (*apiUrl)+url, bytes.NewReader(jsonBytes))
req.Header.Add("Authorization", "Bearer "+(*apiKey))
req.Header.Add("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalf("error: %v", err)
}
if res.StatusCode == http.StatusOK {
log.Printf("Action: %s \t OK", desc)
} else {
if res.Body != nil {
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
if strings.Contains(string(body), "already exists") || strings.Contains(string(body), "Nothing to update") {
log.Printf("Action: %s \t Already exists", desc)
} else {
log.Printf("Action: %s \t Failed - Status: %v", desc, res.Status)
log.Printf("Resp: %s", body)
log.Fatalf("Quitting")
}
}
}
}
type release struct {
Version string `json:"version"`
ReleaseDate time.Time `json:"releaseDate"`
Stable bool `json:"stable"`
Beta bool `json:"beta"`
Nightly bool `json:"nightly"`
WhatsNewUrl string `json:"whatsNewUrl"`
ReleaseNotesUrl string `json:"releaseNotesUrl"`
Builds []build `json:"-"`
}
type build struct {
Os string `json:"os"`
Url string `json:"url"`
Sha256 string `json:"sha256"`
Arch string `json:"arch"`
}