/
storage.go
85 lines (76 loc) · 2.38 KB
/
storage.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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package tools
import (
"errors"
"fmt"
"path/filepath"
"strings"
"github.com/juju/version/v2"
"github.com/juju/juju/environs/storage"
coretools "github.com/juju/juju/tools"
)
var ErrNoTools = errors.New("no agent binaries available")
const (
toolPrefix = "tools/%s/juju-"
toolSuffix = ".tgz"
)
// StorageName returns the name that is used to store and retrieve the
// given version of the juju tools.
func StorageName(vers version.Binary, stream string) string {
return storagePrefix(stream) + vers.String() + toolSuffix
}
func storagePrefix(stream string) string {
return fmt.Sprintf(toolPrefix, stream)
}
// ReadList returns a List of the tools in store with the given major.minor version.
// If minorVersion = -1, then only majorVersion is considered.
// If majorVersion is -1, then all tools tarballs are used.
// If store contains no such tools, it returns ErrNoMatches.
func ReadList(stor storage.StorageReader, toolsDir string, majorVersion, minorVersion int) (coretools.List, error) {
if minorVersion >= 0 {
logger.Debugf("reading v%d.%d agent binaries", majorVersion, minorVersion)
} else {
logger.Debugf("reading v%d.* agent binaries", majorVersion)
}
storagePrefix := storagePrefix(toolsDir)
names, err := storage.List(stor, storagePrefix)
if err != nil {
return nil, err
}
var list coretools.List
var foundAnyTools bool
for _, name := range names {
name = filepath.ToSlash(name)
if !strings.HasPrefix(name, storagePrefix) || !strings.HasSuffix(name, toolSuffix) {
continue
}
var t coretools.Tools
vers := name[len(storagePrefix) : len(name)-len(toolSuffix)]
if t.Version, err = version.ParseBinary(vers); err != nil {
logger.Debugf("failed to parse version %q: %v", vers, err)
continue
}
foundAnyTools = true
// If specified major version value supplied, major version must match.
if majorVersion >= 0 && t.Version.Major != majorVersion {
continue
}
// If specified minor version value supplied, minor version must match.
if minorVersion >= 0 && t.Version.Minor != minorVersion {
continue
}
logger.Debugf("found %s", vers)
if t.URL, err = stor.URL(name); err != nil {
return nil, err
}
list = append(list, &t)
}
if len(list) == 0 {
if foundAnyTools {
return nil, coretools.ErrNoMatches
}
return nil, ErrNoTools
}
return list, nil
}