-
Notifications
You must be signed in to change notification settings - Fork 2
/
update.go
executable file
·199 lines (180 loc) · 4.54 KB
/
update.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"text/template"
"time"
)
const (
archiveUrl = `https://my.atlassian.com/download/feeds/archived/confluence.json`
currentUrl = `https://my.atlassian.com/download/feeds/current/confluence.json`
eapUrl = `https://my.atlassian.com/download/feeds/eap/confluence.json`
)
var tmpl = template.Must(template.ParseFiles("Dockerfile.tmpl"))
func main() {
versionDirs, err := getDirs(".")
if err != nil {
fmt.Println("error fetching version dirs:", err)
os.Exit(1)
}
versions, err := getVersions(currentUrl, archiveUrl, eapUrl)
if err != nil {
fmt.Println("error reading atlassian feeds:", err)
os.Exit(1)
}
for _, dir := range versionDirs {
p, ok := versions[dir]
if !ok {
fmt.Println("can't find url for version", dir)
os.Exit(1)
}
if err := update(dir, p); err != nil {
fmt.Printf("error updating %s: %s\n", dir, err)
os.Exit(1)
}
}
}
func update(dir string, pkg Package) (err error) {
f, err := os.OpenFile(filepath.Join(dir, "Dockerfile"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()
if err := tmpl.Execute(f, pkg); err != nil {
return err
}
err = copyFile("docker-entrypoint.sh", filepath.Join(dir, "docker-entrypoint.sh"), 0764)
if err != nil {
return err
}
// If the file already existed the permissions might not be correct to run
// inside the container.
return os.Chmod(filepath.Join(dir, "docker-entrypoint.sh"), 0764)
}
func getDirs(path string) (dirs []string, err error) {
entries, err := ioutil.ReadDir(".")
if err != nil {
return nil, err
}
for _, entry := range entries {
if entry.IsDir() && !strings.HasPrefix(entry.Name(), ".") {
dirs = append(dirs, entry.Name())
}
}
return dirs, nil
}
// getVersions gets the latest packages from the feeds and marks any from the
// latestFeed as Latest.
func getVersions(latestFeed string, otherFeeds ...string) (versions map[string]Package, err error) {
versions = map[string]Package{}
for _, url := range append(otherFeeds, latestFeed) {
newVersions, err := fetchLatestTarVersions(url)
if err != nil {
return nil, err
}
for v, p := range newVersions {
if url == latestFeed {
p.Latest = true
}
versions[v] = p
}
}
return versions, nil
}
// fetchLatestTarVersions reads the atlassian download feed and fetches the
// latest tar.gz entry for each version.
func fetchLatestTarVersions(url string) (versions map[string]Package, err error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
start := bytes.Index(data, []byte("("))
end := bytes.LastIndex(data, []byte(")"))
if !(end > start && start > -1) {
return nil, errors.New("error in jsonp content")
}
var archives []Package
err = json.Unmarshal(data[start+1:end], &archives)
if err != nil {
return nil, err
}
versions = map[string]Package{}
for _, archive := range archives {
filename := path.Base(archive.ZipURL)
if !strings.Contains(filename, ".tar.gz") ||
(strings.Contains(filename, "enterprise") && !strings.Contains(filename, "standalone")) ||
strings.Contains(filename, "cluster") ||
strings.Contains(filename, "war") {
continue
}
majmin := archive.Version.MajorMinor()
v, ok := versions[majmin]
if !ok || time.Time(archive.Released).After(time.Time(v.Released)) {
versions[majmin] = archive
}
}
return versions, nil
}
type Package struct {
ZipURL string `json:"zipUrl"`
Version Version `json:"version"`
Released AtlassianTime `json:"released"`
Latest bool
}
type Version string
var versionSeparator = regexp.MustCompile(`(\.|-)`)
func (v Version) MajorMinor() string {
parts := versionSeparator.Split(string(v), 3)
if len(parts) < 2 {
return "0.0"
}
return parts[0] + "." + parts[1]
}
type AtlassianTime time.Time
func (a *AtlassianTime) UnmarshalJSON(data []byte) error {
var str string
err := json.Unmarshal(data, &str)
if err != nil {
return err
}
t, err := time.Parse("02-Jan-2006", str)
if err != nil {
return err
}
*a = AtlassianTime(t)
return nil
}
func copyFile(src, dst string, perm os.FileMode) (err error) {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, perm)
if err != nil {
return err
}
defer func() {
oErr := out.Close()
if err == nil {
err = oErr
}
}()
_, err = io.Copy(out, in)
return err
}