/
urls.go
executable file
·85 lines (77 loc) · 2.81 KB
/
urls.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 (
"fmt"
"net/url"
"strings"
"launchpad.net/juju-core/environs"
"launchpad.net/juju-core/environs/simplestreams"
"launchpad.net/juju-core/environs/storage"
)
// SupportsCustomSources represents an environment that
// can host tools metadata at provider specific sources.
type SupportsCustomSources interface {
GetToolsSources() ([]simplestreams.DataSource, error)
}
// GetMetadataSources returns the sources to use when looking for
// simplestreams tools metadata. If env implements SupportsCustomSurces,
// the sources returned from that method will also be considered.
// The sources are configured to not use retries.
func GetMetadataSources(env environs.ConfigGetter) ([]simplestreams.DataSource, error) {
return GetMetadataSourcesWithRetries(env, false)
}
// GetMetadataSourcesWithRetries returns the sources to use when looking for
// simplestreams tools metadata. If env implements SupportsCustomSurces,
// the sources returned from that method will also be considered.
// The sources are configured to use retries according to the value of allowRetry.
func GetMetadataSourcesWithRetries(env environs.ConfigGetter, allowRetry bool) ([]simplestreams.DataSource, error) {
var sources []simplestreams.DataSource
config := env.Config()
if userURL, ok := config.ToolsURL(); ok {
verify := simplestreams.VerifySSLHostnames
if !config.SSLHostnameVerification() {
verify = simplestreams.NoVerifySSLHostnames
}
sources = append(sources, simplestreams.NewURLDataSource("tools-metadata-url", userURL, verify))
}
if custom, ok := env.(SupportsCustomSources); ok {
customSources, err := custom.GetToolsSources()
if err != nil {
return nil, err
}
sources = append(sources, customSources...)
}
defaultURL, err := ToolsURL(DefaultBaseURL)
if err != nil {
return nil, err
}
if defaultURL != "" {
sources = append(sources, simplestreams.NewURLDataSource("default simplestreams", defaultURL, simplestreams.VerifySSLHostnames))
}
for _, source := range sources {
source.SetAllowRetry(allowRetry)
}
return sources, nil
}
// ToolsURL returns a valid tools URL constructed from source.
// source may be a directory, or a URL like file://foo or http://foo.
func ToolsURL(source string) (string, error) {
if source == "" {
return "", nil
}
// If source is a raw directory, we need to append the file:// prefix
// so it can be used as a URL.
defaultURL := source
u, err := url.Parse(source)
if err != nil {
return "", fmt.Errorf("invalid default tools URL %s: %v", defaultURL, err)
}
if u.Scheme == "" {
defaultURL = "file://" + defaultURL
if !strings.HasSuffix(defaultURL, "/"+storage.BaseToolsPath) {
defaultURL = fmt.Sprintf("%s/%s", defaultURL, storage.BaseToolsPath)
}
}
return defaultURL, nil
}