This repository has been archived by the owner on Feb 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtorcx.go
286 lines (248 loc) · 7.47 KB
/
torcx.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
// Copyright 2017 CoreOS Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internal
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/coreos/go-semver/semver"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
const TORCX_STORE = "/var/lib/torcx/store"
type profileList struct {
LowerProfileNames []string `json:"lower_profile_names"`
UserProfileName *string `json:"user_profile_name"`
CurrentProfilePath *string `json:"current_profile_path"`
NextProfileName *string `json:"next_profile_name"`
Profiles []string `json:"profiles"`
}
type profileListBox struct {
Kind string `json:"kind"`
Value profileList `json:"value"`
}
type imageEntry struct {
Name string `json:"name"`
Reference string `json:"reference"`
Filepath string `json:"filepath"`
}
type imageListBox struct {
Kind string `json:"kind"`
Value []imageEntry `json:"value"`
}
// InstallAddon fetches, verify and store an addon image
func (a *App) InstallAddon(name string, reference string, osVersions []string) error {
logrus.Infof("Installing %s:%s for os versions %v", name, reference, osVersions)
for _, osVersion := range osVersions {
if a.AddonInStore(name, reference, osVersion) {
logrus.Debugf("Skipping osVersion %s, already installed", osVersion)
continue
}
manif, err := a.GetPackageManifest(osVersion)
if err != nil {
return err // should not happen; it is already in cache
}
loc, err := manif.LocationFor(name, reference)
if err != nil {
return err // should not happen, strategy caught this case
}
if loc.Path != "" {
logrus.Debugf("Skipping osVersion %s, already in store", osVersion)
continue
}
path, err := a.FetchAddon(loc)
if err != nil {
return errors.Wrapf(err, "failed to fetch addon")
}
err = a.copyToStore(path, name, reference, osVersion)
if err != nil {
return errors.Wrapf(err, "copy to store failed")
}
}
logrus.Debugf("fetch phase complete, adding to profile")
err := a.UseAddon(name, reference)
if err != nil {
return errors.Wrapf(err, "failed to enable addon")
}
a.DockerRequiresReboot = true
return nil
}
// AddonInStore returns true if the referenced addon is already
// in the store
func (a *App) AddonInStore(name, reference, osVersion string) bool {
il := imageListBox{}
a.torcxCmd(&il, []string{
"image", "list",
"-n", osVersion,
name,
})
for _, entry := range il.Value {
if entry.Name == name && entry.Reference == reference {
return true
}
}
return false
}
// copyToStore moves an already downloaded addon to the store
func (a *App) copyToStore(path, name, reference, osVersion string) error {
srcfd, err := os.Open(path)
if err != nil {
return err
}
defer srcfd.Close()
if strings.HasPrefix(path, "/tmp/") {
defer os.Remove(path)
}
var destPath string
if err := os.MkdirAll(filepath.Join(a.Conf.torcxStoreDir, osVersion), 0755); err != nil {
return err
}
if osVersion != "" {
destPath = fmt.Sprintf("%s/%s/%s:%s.torcx.tgz",
a.Conf.torcxStoreDir, osVersion, name, reference)
} else {
destPath = fmt.Sprintf("%s/%s:%s.torcx.tgz", a.Conf.torcxStoreDir, name, reference)
}
destfd, err := os.OpenFile(destPath, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return nil
}
defer destfd.Close()
logrus.Debugf("copying to store: src %s dst %s", path, destPath)
if _, err := io.Copy(destfd, srcfd); err != nil {
return err
}
return destfd.Sync()
}
// UseAddon selects the addon for installation on next boot.
// When run on a fresh machine, this will create a profile
// of our choosing, otherwise will use the already-enabled version.
func (a *App) UseAddon(name string, reference string) error {
profileName, err := a.profileName()
if err != nil {
return errors.Wrap(err, "could not determine / create torcx profile")
}
// Add this addon to the profile
err = a.torcxCmd(nil, []string{
"profile", "use-image",
"--allow=missing",
"--name", profileName,
name + ":" + reference,
})
if err != nil {
return errors.Wrap(err, "could not add image to profile")
}
err = a.torcxCmd(nil, []string{
"profile", "set-next", profileName})
if err != nil {
return errors.Wrap(err, "could not set-next profile")
}
return nil
}
// TorcxGC removes versioned stores that we know we won't need.
// All versioned stores older than a given version are removed.
func (a *App) TorcxGC(minOSVersion string) error {
minOSVers, err := semver.NewVersion(minOSVersion)
if err != nil {
return errors.Wrapf(err, "didn't understand minOSVersion")
}
// Only consider directories that look like CL versions
versionRE := regexp.MustCompile(`^\d+\.\d+\.\d+$`)
// List the user store
entries, err := ioutil.ReadDir(a.Conf.torcxStoreDir)
if err != nil {
return errors.Wrap(err, "failed to list torcx store")
}
// Each torcx store directory holds addons and os version sub-stores
// Remove all unwanted stores
for _, entry := range entries {
if !entry.IsDir() {
continue
}
if !versionRE.MatchString(entry.Name()) {
continue
}
vers := semver.New(entry.Name())
if vers == nil {
continue
}
if !vers.LessThan(*minOSVers) {
continue
}
p := filepath.Join(a.Conf.torcxStoreDir, entry.Name())
logrus.Debugf("Removing unneeded torcx store directory %s", p)
if err := os.RemoveAll(p); err != nil {
return errors.Wrap(err, "failed to remove old torcx addons")
}
}
return nil
}
// profileName determines which profile name to use.
// If this is an untouched machine, we want to create
// a new profile. If there is alread an existing profile,
// we should use that instead
func (a *App) profileName() (string, error) {
plb := profileListBox{}
err := a.torcxCmd(&plb, []string{"profile", "list"})
if err != nil {
return "", err
}
// If the next-profile name isn't default, just use it
if plb.Value.NextProfileName != nil && *plb.Value.NextProfileName != "vendor" {
logrus.Debugf("non-default torcx profile %s already active, using", *plb.Value.NextProfileName)
return *plb.Value.NextProfileName, nil
}
// Otherwise, create our profile if it doesn't exist
exists := false
for _, profileName := range plb.Value.Profiles {
if profileName == a.Conf.ProfileName {
exists = true
break
}
}
if !exists {
logrus.Debugf("creating torcx profile %s", a.Conf.ProfileName)
err = a.torcxCmd(nil, []string{
"profile", "new",
"--name", a.Conf.ProfileName})
if err != nil {
return "", err
}
}
return a.Conf.ProfileName, nil
}
// torcxCmd executes a torcx command. If result is not nil, attempt to
// json-unmarshal stdout in to the result
func (a *App) torcxCmd(result interface{}, args []string) error {
logrus.Debug("executing: ", a.Conf.TorcxBin, " ", args)
cmd := exec.Command(a.Conf.TorcxBin, args...)
out, err := cmd.Output()
if err != nil {
switch e := err.(type) {
case *exec.ExitError:
logrus.Debugf("torcx exited with non-zero status code, stderr: %s", string(e.Stderr))
}
return err
}
if result != nil {
return json.Unmarshal(out, result)
}
return nil
}