This repository has been archived by the owner on May 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userconfig.go
163 lines (127 loc) · 4.31 KB
/
userconfig.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
package main
import (
"database/sql"
log "github.com/Sirupsen/logrus"
"sync"
"time"
)
// sqlite3 cannot use a single connection concurrently - thus the mutex
type userDB struct {
db *sql.DB
mutex sync.Mutex
}
func newUserDB(filename string) (*userDB, error) {
database, err := sql.Open("sqlite3", filename)
if err != nil {
return nil, err
}
_, err = database.Exec("CREATE TABLE IF NOT EXISTS payloads(id TEXT, size INTEGER, sha1 TEXT, sha256 TEXT, ver_build INTEGER, ver_branch INTEGER, ver_patch INTEGER, ver_timestamp INTEGER)")
if err != nil {
return nil, err
}
_, err = database.Exec("CREATE TABLE IF NOT EXISTS channel_payload_rel(payload TEXT, channel TEXT)")
if err != nil {
return nil, err
}
_, err = database.Exec("CREATE TABLE IF NOT EXISTS client(id TEXT, name TEXT)")
if err != nil {
return nil, err
}
_, err = database.Exec("CREATE TABLE IF NOT EXISTS channel_client_rel(client TEXT, channel TEXT)")
if err != nil {
return nil, err
}
return &userDB{db: database}, nil
}
func (u *userDB) Close() error {
return u.db.Close()
}
func (u *userDB) AttachPayloadToChannel(id, channel string) error {
u.mutex.Lock()
defer u.mutex.Unlock()
q, err := u.db.Prepare("INSERT INTO channel_payload_rel (payload,channel) VALUES (?, ?);")
if err != nil {
return err
}
_, err = q.Exec(id, channel)
if err != nil {
return err
}
return nil
}
func (u *userDB) AddPayload(id, sha1, sha256 string, size int64, version payloadVersion) error {
u.mutex.Lock()
defer u.mutex.Unlock()
q, err := u.db.Prepare("INSERT INTO payloads (id,size,sha1,sha256,ver_build,ver_branch,ver_patch,ver_timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?);")
if err != nil {
return err
}
_, err = q.Exec(id, size, sha1, sha256, version.build, version.branch, version.patch, version.timestamp.Unix())
if err != nil {
return err
}
log.Debugf("DB: added payload '%v', size=%v, version=%v.%v.%v+%v, sha1=%v, sha256=%v,", id, size, version.build, version.branch, version.patch, version.timestamp.Unix(), sha1, sha256)
return nil
}
func (u *userDB) GetNewerPayload(currentVersion payloadVersion, channel string) (p payload, err error) {
u.mutex.Lock()
defer u.mutex.Unlock()
q, err := u.db.Prepare("SELECT id,size,sha1,sha256 FROM payloads AS P JOIN channel_payload_rel AS R ON P.id=R.payload WHERE R.channel=? AND ((ver_build > ?) OR (ver_build = ? AND ver_branch > ?) OR (ver_build = ? AND ver_branch = ? AND ver_patch > ?) OR (ver_build = ? AND ver_branch = ? AND ver_patch = ? AND ver_timestamp > ?)) ORDER BY ver_build DESC, ver_branch DESC, ver_patch DESC, ver_timestamp DESC LIMIT 1;")
if err != nil {
return
}
result := q.QueryRow(channel, currentVersion.build, currentVersion.build, currentVersion.branch, currentVersion.build, currentVersion.branch, currentVersion.patch, currentVersion.build, currentVersion.branch, currentVersion.patch, currentVersion.timestamp.Unix())
err = result.Scan(&p.Url, &p.Size, &p.SHA1, &p.SHA256)
return
}
type imageListElement struct {
Id string
Version string
Sha1 string
Sha256 string
Size int64
}
func (u *userDB) ListChannels() ([]string, error) {
u.mutex.Lock()
defer u.mutex.Unlock()
result, err := u.db.Query("SELECT DISTINCT channel FROM channel_payload_rel;")
if err != nil {
return nil, err
}
channels := []string{}
for result.Next() {
var chanName string
err = result.Scan(&chanName)
if err != nil {
return nil, err
}
channels = append(channels, chanName)
}
return channels, nil
}
func (u *userDB) ListImages(channel string) ([]imageListElement, error) {
u.mutex.Lock()
defer u.mutex.Unlock()
q, err := u.db.Prepare("SELECT id,ver_build,ver_branch,ver_patch,ver_timestamp,sha1,sha256,size FROM payloads AS P JOIN channel_payload_rel AS R ON P.id=R.payload WHERE R.channel=? ORDER BY ver_build, ver_branch, ver_patch, ver_timestamp;")
if err != nil {
return nil, err
}
result, err := q.Query(channel)
if err != nil {
return nil, err
}
out := []imageListElement{}
for result.Next() {
var image imageListElement
var ver payloadVersion
var timestamp int64
err = result.Scan(&image.Id, &ver.build, &ver.branch, &ver.patch, ×tamp, &image.Sha1, &image.Sha256, &image.Size)
if err != nil {
return nil, err
}
ver.timestamp = time.Unix(timestamp, 0).UTC()
image.Version = ver.String()
out = append(out, image)
}
return out, nil
}