forked from coreos/core-admin
/
new-version.go
151 lines (121 loc) · 3.17 KB
/
new-version.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
package main
import (
"github.com/coreos/core-admin/update/types"
"bytes"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/xml"
"fmt"
"hash"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
)
var cmdNewVersion = &Command{
UsageLine: "new-version -k [key] -a [app-id] -v [version] -t [track] -p [url path] [filename]",
Short: "update the version database for a given file",
Long: `
Takes a file path and some meta data and update the information used in the datastore.
`,
}
var versionD = cmdNewVersion.Flag.Bool("d", false, "dry run, print out the xml payload")
var versionK = cmdNewVersion.Flag.String("k", "", "api key for the admin user")
var versionA = cmdNewVersion.Flag.String("a", "", "application id")
var versionV = cmdNewVersion.Flag.String("v", "", "version ")
var versionT = cmdNewVersion.Flag.String("t", "", "track")
var versionP = cmdNewVersion.Flag.String("p", "", "url path")
func init() {
cmdNewVersion.Run = runNewVersion
}
func calculateHashes(filename string, pkg *types.Package) {
var (
writers []io.Writer
hashes []hash.Hash
)
push := func(h hash.Hash) {
writers = append(writers, h)
hashes = append(hashes, h)
}
push(sha256.New())
push(sha1.New())
in, err := os.Open(filename)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
io.Copy(io.MultiWriter(writers...), in)
formatHash := func(hash hash.Hash) string {
return base64.StdEncoding.EncodeToString(hash.Sum(nil))
}
pkg.Sha256Sum = formatHash(hashes[0])
pkg.Sha1Sum = formatHash(hashes[1])
}
func runNewVersion(cmd *Command, args []string) {
dryRun := *versionD
key := *versionK
appId := *versionA
version := *versionV
track := *versionT
path := *versionP
if dryRun == false && key == "" {
fmt.Printf("key or dry-run required")
os.Exit(-1)
}
if appId == "" || version == "" || track == "" || path == "" {
fmt.Printf("one of the required fields was not present\n")
os.Exit(-1)
}
if len(args) != 1 {
fmt.Printf("update file name not provided\n")
os.Exit(-1)
}
file := args[0]
fileBase := filepath.Base(file)
fi, err := os.Stat(file)
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(-1)
}
fileSize := strconv.FormatInt(fi.Size(), 10)
app := types.App{Id: appId, Version: version, Track: track}
pkg := types.Package{Name: fileBase, Size: fileSize, Path: path}
ver := types.Version{App: &app, Package: &pkg}
calculateHashes(file, ver.Package)
raw, err := xml.MarshalIndent(ver, "", " ")
if err != nil {
fmt.Printf(err.Error())
os.Exit(-1)
}
body := []byte(xml.Header)
body = append(body, raw...)
adminURL, _ := url.Parse(updateURL.String())
adminURL.Path = "/admin/version"
req, _ := http.NewRequest("POST", adminURL.String(), bytes.NewBuffer(body))
req.Header.Set("Content-Type", "text/xml")
req.SetBasicAuth("admin", key)
if dryRun || *debug {
req.Write(os.Stdout)
}
if dryRun {
return
}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf(err.Error())
os.Exit(-1)
}
body, _ = ioutil.ReadAll(resp.Body)
os.Stdout.Write(body)
fmt.Printf("\n")
if resp.StatusCode != 200 {
fmt.Printf("Error: bad return code %s\n", resp.Status)
os.Exit(-1)
}
return
}