/
file.go
269 lines (213 loc) · 6.18 KB
/
file.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
// Package file provides functions to manage docker compose files inside the
// $HOME/.sourced/compose-files directory
package file
import (
"encoding/base64"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
datadir "github.com/src-d/sourced-ce/cmd/sourced/dir"
"github.com/pkg/errors"
goerrors "gopkg.in/src-d/go-errors.v1"
)
// ErrConfigDownload is returned when docker-compose.yml could not be downloaded
var ErrConfigDownload = goerrors.NewKind("docker-compose.yml config file could not be downloaded")
// ErrConfigActivation is returned when docker-compose.yml could not be set as active
var ErrConfigActivation = goerrors.NewKind("docker-compose.yml could not be set as active")
const (
orgName = "src-d"
repoName = "sourced-ce"
composeFileTmpl = "https://raw.githubusercontent.com/%s/%s/%s/docker-compose.yml"
)
var version = "master"
// activeDir is the name of the directory containing the symlink to the
// active docker compose file
const activeDir = "__active__"
// RevOrURL is a revision (tag name, full sha1) or a valid URL to a
// docker-compose.yml file
type RevOrURL = string
// composeFileURL returns the URL to download the raw github docker-compose.yml
// file for the given revision (tag or full sha1)
func composeFileURL(revision string) string {
return fmt.Sprintf(composeFileTmpl, orgName, repoName, revision)
}
// SetVersion sets the version rewritten by the CI build
func SetVersion(v string) {
version = v
}
// InitDefault checks if there is an active docker compose file, and if there
// isn't the file for this release is downloaded.
// The current build version must be set with SetVersion.
// It returns the absolute path to the active docker-compose.yml file
func InitDefault() (string, error) {
activeFilePath, err := path(activeDir)
if err != nil {
return "", err
}
_, err = os.Stat(activeFilePath)
if err == nil {
return activeFilePath, nil
}
if !os.IsNotExist(err) {
return "", err
}
err = ActivateFromRemote(version)
if err != nil {
return "", err
}
return activeFilePath, nil
}
// InitDefaultOverride returns the path of the default docker-compose.override.yml file,
// if it does not exists, it is created.
func InitDefaultOverride() (string, error) {
composeDirPath, err := dir()
if err != nil {
return "", err
}
globalOverridePath := filepath.Join(composeDirPath, activeDir, "docker-compose.override.yml")
_, err = os.Stat(globalOverridePath)
if err == nil {
return globalOverridePath, nil
}
if !os.IsNotExist(err) {
return "", errors.Wrap(err, "could not read the global docker-compose.override.yml file")
}
content := []byte(`version: '3.4'`)
if err := ioutil.WriteFile(globalOverridePath, content, 0644); err != nil {
return "", errors.Wrap(err, "could not create the global docker-compose.override.yml file")
}
return globalOverridePath, nil
}
// ActivateFromRemote downloads the docker-compose.yml file from the given revision
// or URL, and sets it as the active compose file.
func ActivateFromRemote(revOrURL RevOrURL) (err error) {
var url string
if isURL(revOrURL) {
url = revOrURL
} else {
url = composeFileURL(revOrURL)
}
outPath, err := path(revOrURL)
if err != nil {
return err
}
err = datadir.DownloadURL(url, outPath)
if err != nil {
return ErrConfigDownload.Wrap(err)
}
err = SetActive(revOrURL)
if err != nil {
return ErrConfigActivation.Wrap(err)
}
return nil
}
// SetActive makes a symlink from
// $HOME/.sourced/compose-files/__active__/docker-compose.yml to the compose file
// for the given revision or URL.
func SetActive(revOrURL RevOrURL) error {
filePath, err := path(revOrURL)
if err != nil {
return err
}
if _, err := os.Stat(filePath); err != nil {
if os.IsNotExist(err) {
return errors.Wrapf(err, "could not find a docker-compose.yml file in `%s`", filePath)
}
return err
}
activeFilePath, err := path(activeDir)
if err != nil {
return err
}
err = os.MkdirAll(filepath.Dir(activeFilePath), os.ModePerm)
if err != nil {
return errors.Wrapf(err, "error while creating directory for %s", activeFilePath)
}
if _, err := os.Lstat(activeFilePath); err == nil {
if err := os.Remove(activeFilePath); err != nil {
return errors.Wrap(err, "failed to unlink")
}
}
return os.Symlink(filePath, activeFilePath)
}
// Active returns the revision (tag name, full sha1) or the URL of the active
// docker compose file
func Active() (RevOrURL, error) {
activeFilePath, err := path(activeDir)
if err != nil {
return "", err
}
if _, err := os.Stat(activeFilePath); err != nil {
if os.IsNotExist(err) {
return "", nil
}
return "", err
}
dest, err := filepath.EvalSymlinks(activeFilePath)
if err != nil {
return "", err
}
_, name := filepath.Split(filepath.Dir(dest))
return composeName(name), nil
}
// List returns a list of installed docker compose files. Each name is the
// revision (tag name, full sha1) or the URL
func List() ([]RevOrURL, error) {
list := []RevOrURL{}
dir, err := dir()
if err != nil {
return list, err
}
if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
return list, nil
}
return list, err
}
files, err := ioutil.ReadDir(dir)
if err != nil {
return list, err
}
for _, f := range files {
entry := f.Name()
if entry == activeDir {
continue
}
list = append(list, composeName(entry))
}
return list, nil
}
func composeName(rev string) string {
if decoded, err := base64.URLEncoding.DecodeString(rev); err == nil {
return string(decoded)
}
return rev
}
func isURL(revOrURL RevOrURL) bool {
_, err := url.ParseRequestURI(revOrURL)
return err == nil
}
// dir returns the absolute path for $HOME/.sourced/compose-files
func dir() (string, error) {
path, err := datadir.Path()
if err != nil {
return "", err
}
return filepath.Join(path, "compose-files"), nil
}
// path returns the absolute path to
// $HOME/.sourced/compose-files/revOrURL/docker-compose.yml
func path(revOrURL RevOrURL) (string, error) {
composeDirPath, err := dir()
if err != nil {
return "", err
}
subPath := revOrURL
if isURL(revOrURL) {
subPath = base64.URLEncoding.EncodeToString([]byte(revOrURL))
}
dirPath := filepath.Join(composeDirPath, subPath)
return filepath.Join(dirPath, "docker-compose.yml"), nil
}