forked from juju/charmrepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
legacy.go
372 lines (350 loc) · 10.9 KB
/
legacy.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charmrepo
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/juju/utils"
"gopkg.in/juju/charm.v6-unstable"
)
// LegacyCharmStore is a repository Interface that provides access to the
// legacy Juju charm store.
type LegacyCharmStore struct {
BaseURL string
authAttrs string // a list of attr=value pairs, comma separated
jujuAttrs string // a list of attr=value pairs, comma separated
testMode bool
}
var _ Interface = (*LegacyCharmStore)(nil)
var LegacyStore = &LegacyCharmStore{BaseURL: "https://store.juju.ubuntu.com"}
// WithAuthAttrs return a repository Interface with the authentication token
// list set. authAttrs is a list of attr=value pairs.
func (s *LegacyCharmStore) WithAuthAttrs(authAttrs string) Interface {
authCS := *s
authCS.authAttrs = authAttrs
return &authCS
}
// WithTestMode returns a repository Interface where testMode is set to value
// passed to this method.
func (s *LegacyCharmStore) WithTestMode(testMode bool) Interface {
newRepo := *s
newRepo.testMode = testMode
return &newRepo
}
// WithJujuAttrs returns a repository Interface with the Juju metadata
// attributes set. jujuAttrs is a list of attr=value pairs.
func (s *LegacyCharmStore) WithJujuAttrs(jujuAttrs string) Interface {
jujuCS := *s
jujuCS.jujuAttrs = jujuAttrs
return &jujuCS
}
// Perform an http get, adding custom auth header if necessary.
func (s *LegacyCharmStore) get(url string) (resp *http.Response, err error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
if s.authAttrs != "" {
// To comply with RFC 2617, we send the authentication data in
// the Authorization header with a custom auth scheme
// and the authentication attributes.
req.Header.Add("Authorization", "charmstore "+s.authAttrs)
}
if s.jujuAttrs != "" {
// The use of "X-" to prefix custom header values is deprecated.
req.Header.Add("Juju-Metadata", s.jujuAttrs)
}
return http.DefaultClient.Do(req)
}
// Resolve canonicalizes charm URLs any implied series in the reference.
func (s *LegacyCharmStore) Resolve(ref *charm.Reference) (*charm.URL, error) {
infos, err := s.Info(ref)
if err != nil {
return nil, err
}
if len(infos) == 0 {
return nil, fmt.Errorf("missing response when resolving charm URL: %q", ref)
}
if infos[0].CanonicalURL == "" {
return nil, fmt.Errorf("cannot resolve charm URL: %q", ref)
}
curl, err := charm.ParseURL(infos[0].CanonicalURL)
if err != nil {
return nil, err
}
return curl, nil
}
// Info returns details for all the specified charms in the charm store.
func (s *LegacyCharmStore) Info(curls ...charm.Location) ([]*InfoResponse, error) {
baseURL := s.BaseURL + "/charm-info?"
queryParams := make([]string, len(curls), len(curls)+1)
for i, curl := range curls {
queryParams[i] = "charms=" + url.QueryEscape(curl.String())
}
if s.testMode {
queryParams = append(queryParams, "stats=0")
}
resp, err := s.get(baseURL + strings.Join(queryParams, "&"))
if err != nil {
if url_error, ok := err.(*url.Error); ok {
switch url_error.Err.(type) {
case *net.DNSError, *net.OpError:
return nil, fmt.Errorf("Cannot access the charm store. Are you connected to the internet? Error details: %v", err)
}
}
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
errMsg := fmt.Errorf("Cannot access the charm store. Invalid response code: %q", resp.Status)
body, readErr := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, readErr
}
logger.Errorf("%v Response body: %s", errMsg, body)
return nil, errMsg
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
infos := make(map[string]*InfoResponse)
if err = json.Unmarshal(body, &infos); err != nil {
return nil, err
}
result := make([]*InfoResponse, len(curls))
for i, curl := range curls {
key := curl.String()
info, found := infos[key]
if !found {
return nil, fmt.Errorf("charm store returned response without charm %q", key)
}
if len(info.Errors) == 1 && info.Errors[0] == "entry not found" {
info.Errors[0] = fmt.Sprintf("charm not found: %s", curl)
}
result[i] = info
}
return result, nil
}
// Event returns details for a charm event in the charm store.
//
// If digest is empty, the latest event is returned.
func (s *LegacyCharmStore) Event(curl *charm.URL, digest string) (*EventResponse, error) {
key := curl.String()
query := key
if digest != "" {
query += "@" + digest
}
resp, err := s.get(s.BaseURL + "/charm-event?charms=" + url.QueryEscape(query))
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
events := make(map[string]*EventResponse)
if err = json.Unmarshal(body, &events); err != nil {
return nil, err
}
event, found := events[key]
if !found {
return nil, fmt.Errorf("charm store returned response without charm %q", key)
}
if len(event.Errors) == 1 && event.Errors[0] == "entry not found" {
if digest == "" {
return nil, &NotFoundError{fmt.Sprintf("charm event not found for %q", curl)}
} else {
return nil, &NotFoundError{fmt.Sprintf("charm event not found for %q with digest %q", curl, digest)}
}
}
return event, nil
}
// revisions returns the revisions of the charms referenced by curls.
func (s *LegacyCharmStore) revisions(curls ...charm.Location) (revisions []CharmRevision, err error) {
infos, err := s.Info(curls...)
if err != nil {
return nil, err
}
revisions = make([]CharmRevision, len(infos))
for i, info := range infos {
for _, w := range info.Warnings {
logger.Warningf("charm store reports for %q: %s", curls[i], w)
}
if info.Errors == nil {
revisions[i].Revision = info.Revision
revisions[i].Sha256 = info.Sha256
} else {
// If a charm is not found, we are more concise with the error message.
if len(info.Errors) == 1 && strings.HasPrefix(info.Errors[0], "charm not found") {
revisions[i].Err = fmt.Errorf(info.Errors[0])
} else {
revisions[i].Err = fmt.Errorf("charm info errors for %q: %s", curls[i], strings.Join(info.Errors, "; "))
}
}
}
return revisions, nil
}
// Latest returns the latest revision of the charms referenced by curls, regardless
// of the revision set on each curl.
func (s *LegacyCharmStore) Latest(curls ...*charm.URL) ([]CharmRevision, error) {
baseCurls := make([]charm.Location, len(curls))
for i, curl := range curls {
baseCurls[i] = curl.WithRevision(-1)
}
return s.revisions(baseCurls...)
}
// BranchLocation returns the location for the branch holding the charm at curl.
func (s *LegacyCharmStore) BranchLocation(curl *charm.URL) string {
if curl.User != "" {
return fmt.Sprintf("lp:~%s/charms/%s/%s/trunk", curl.User, curl.Series, curl.Name)
}
return fmt.Sprintf("lp:charms/%s/%s", curl.Series, curl.Name)
}
var branchPrefixes = []string{
"lp:",
"bzr+ssh://bazaar.launchpad.net/+branch/",
"bzr+ssh://bazaar.launchpad.net/",
"http://launchpad.net/+branch/",
"http://launchpad.net/",
"https://launchpad.net/+branch/",
"https://launchpad.net/",
"http://code.launchpad.net/+branch/",
"http://code.launchpad.net/",
"https://code.launchpad.net/+branch/",
"https://code.launchpad.net/",
}
// CharmURL returns the charm URL for the branch at location.
func (s *LegacyCharmStore) CharmURL(location string) (*charm.URL, error) {
var l string
if len(location) > 0 && location[0] == '~' {
l = location
} else {
for _, prefix := range branchPrefixes {
if strings.HasPrefix(location, prefix) {
l = location[len(prefix):]
break
}
}
}
if l != "" {
for len(l) > 0 && l[len(l)-1] == '/' {
l = l[:len(l)-1]
}
u := strings.Split(l, "/")
if len(u) == 3 && u[0] == "charms" {
return charm.ParseURL(fmt.Sprintf("cs:%s/%s", u[1], u[2]))
}
if len(u) == 4 && u[0] == "charms" && u[3] == "trunk" {
return charm.ParseURL(fmt.Sprintf("cs:%s/%s", u[1], u[2]))
}
if len(u) == 5 && u[1] == "charms" && u[4] == "trunk" && len(u[0]) > 0 && u[0][0] == '~' {
return charm.ParseURL(fmt.Sprintf("cs:%s/%s/%s", u[0], u[2], u[3]))
}
}
return nil, fmt.Errorf("unknown branch location: %q", location)
}
// verify returns an error unless a file exists at path with a hex-encoded
// SHA256 matching digest.
func verify(path, digest string) error {
hash, _, err := utils.ReadFileSHA256(path)
if err != nil {
return err
}
if hash != digest {
return fmt.Errorf("bad SHA256 of %q", path)
}
return nil
}
// Get returns the charm referenced by curl.
// CacheDir must have been set, otherwise Get will panic.
func (s *LegacyCharmStore) Get(curl *charm.URL) (charm.Charm, error) {
// The cache location must have been previously set.
if CacheDir == "" {
panic("charm cache directory path is empty")
}
if err := os.MkdirAll(CacheDir, os.FileMode(0755)); err != nil {
return nil, err
}
revInfo, err := s.revisions(curl)
if err != nil {
return nil, err
}
if len(revInfo) != 1 {
return nil, fmt.Errorf("expected 1 result, got %d", len(revInfo))
}
if revInfo[0].Err != nil {
return nil, revInfo[0].Err
}
rev, digest := revInfo[0].Revision, revInfo[0].Sha256
if curl.Revision == -1 {
curl = curl.WithRevision(rev)
} else if curl.Revision != rev {
return nil, fmt.Errorf("store returned charm with wrong revision %d for %q", rev, curl.String())
}
path := filepath.Join(CacheDir, charm.Quote(curl.String())+".charm")
if verify(path, digest) != nil {
store_url := s.BaseURL + "/charm/" + curl.Path()
if s.testMode {
store_url = store_url + "?stats=0"
}
resp, err := s.get(store_url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad status from request for %q: %q", store_url, resp.Status)
}
f, err := ioutil.TempFile(CacheDir, "charm-download")
if err != nil {
return nil, err
}
dlPath := f.Name()
_, err = io.Copy(f, resp.Body)
if cerr := f.Close(); err == nil {
err = cerr
}
if err != nil {
os.Remove(dlPath)
return nil, err
}
if err := utils.ReplaceFile(dlPath, path); err != nil {
return nil, err
}
}
if err := verify(path, digest); err != nil {
return nil, err
}
return charm.ReadCharmArchive(path)
}
// GetBundle is only defined for implementing Interface.
func (s *LegacyCharmStore) GetBundle(curl *charm.URL) (charm.Bundle, error) {
return nil, errors.New("not implemented: legacy API does not support bundles")
}
// LegacyInferRepository returns a charm repository inferred from the provided
// charm or bundle reference. Local references will use the provided path.
func LegacyInferRepository(ref *charm.Reference, localRepoPath string) (repo Interface, err error) {
switch ref.Schema {
case "cs":
repo = LegacyStore
case "local":
if localRepoPath == "" {
return nil, errors.New("path to local repository not specified")
}
repo = &LocalRepository{Path: localRepoPath}
default:
return nil, fmt.Errorf("unknown schema for charm reference %q", ref)
}
return
}