forked from juju/charm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charmdir.go
371 lines (335 loc) · 9.31 KB
/
charmdir.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
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package charm
import (
"archive/zip"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
)
// The CharmDir type encapsulates access to data and operations
// on a charm directory.
type CharmDir struct {
Path string
meta *Meta
config *Config
metrics *Metrics
actions *Actions
revision int
}
// Trick to ensure *CharmDir implements the Charm interface.
var _ Charm = (*CharmDir)(nil)
// IsCharmDir report whether the path is likely to represent
// a charm, even it may be incomplete.
func IsCharmDir(path string) bool {
dir := &CharmDir{Path: path}
_, err := os.Stat(dir.join("metadata.yaml"))
return err == nil
}
// ReadCharmDir returns a CharmDir representing an expanded charm directory.
func ReadCharmDir(path string) (dir *CharmDir, err error) {
dir = &CharmDir{Path: path}
file, err := os.Open(dir.join("metadata.yaml"))
if err != nil {
return nil, err
}
dir.meta, err = ReadMeta(file)
file.Close()
if err != nil {
return nil, err
}
file, err = os.Open(dir.join("config.yaml"))
if _, ok := err.(*os.PathError); ok {
dir.config = NewConfig()
} else if err != nil {
return nil, err
} else {
dir.config, err = ReadConfig(file)
file.Close()
if err != nil {
return nil, err
}
}
file, err = os.Open(dir.join("metrics.yaml"))
if err == nil {
dir.metrics, err = ReadMetrics(file)
file.Close()
if err != nil {
return nil, err
}
} else if !os.IsNotExist(err) {
return nil, err
}
file, err = os.Open(dir.join("actions.yaml"))
if _, ok := err.(*os.PathError); ok {
dir.actions = NewActions()
} else if err != nil {
return nil, err
} else {
dir.actions, err = ReadActionsYaml(file)
file.Close()
if err != nil {
return nil, err
}
}
if file, err = os.Open(dir.join("revision")); err == nil {
_, err = fmt.Fscan(file, &dir.revision)
file.Close()
if err != nil {
return nil, errors.New("invalid revision file")
}
}
return dir, nil
}
// join builds a path rooted at the charm's expanded directory
// path and the extra path components provided.
func (dir *CharmDir) join(parts ...string) string {
parts = append([]string{dir.Path}, parts...)
return filepath.Join(parts...)
}
// Revision returns the revision number for the charm
// expanded in dir.
func (dir *CharmDir) Revision() int {
return dir.revision
}
// Meta returns the Meta representing the metadata.yaml file
// for the charm expanded in dir.
func (dir *CharmDir) Meta() *Meta {
return dir.meta
}
// Config returns the Config representing the config.yaml file
// for the charm expanded in dir.
func (dir *CharmDir) Config() *Config {
return dir.config
}
// Metrics returns the Metrics representing the metrics.yaml file
// for the charm expanded in dir.
func (dir *CharmDir) Metrics() *Metrics {
return dir.metrics
}
// Actions returns the Actions representing the actions.yaml file
// for the charm expanded in dir.
func (dir *CharmDir) Actions() *Actions {
return dir.actions
}
// SetRevision changes the charm revision number. This affects
// the revision reported by Revision and the revision of the
// charm archived by ArchiveTo.
// The revision file in the charm directory is not modified.
func (dir *CharmDir) SetRevision(revision int) {
dir.revision = revision
}
// SetDiskRevision does the same as SetRevision but also changes
// the revision file in the charm directory.
func (dir *CharmDir) SetDiskRevision(revision int) error {
dir.SetRevision(revision)
file, err := os.OpenFile(dir.join("revision"), os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
_, err = file.Write([]byte(strconv.Itoa(revision)))
file.Close()
return err
}
// resolveSymlinkedRoot returns the target destination of a
// charm root directory if the root directory is a symlink.
func resolveSymlinkedRoot(rootPath string) (string, error) {
info, err := os.Lstat(rootPath)
if err == nil && info.Mode()&os.ModeSymlink != 0 {
rootPath, err = filepath.EvalSymlinks(rootPath)
if err != nil {
return "", fmt.Errorf("cannot read path symlink at %q: %v", rootPath, err)
}
}
return rootPath, nil
}
// ArchiveTo creates a charm file from the charm expanded in dir.
// By convention a charm archive should have a ".charm" suffix.
func (dir *CharmDir) ArchiveTo(w io.Writer) error {
versionString, vcsType, err := dir.MaybeGenerateVersionString()
if err != nil {
// Just to be safe, ensure version is "" on error.
versionString = ""
logger.Warningf(
"%q version string generation failed : %v\nThis means that the charm version won't show in juju status.",
vcsType, err)
}
return writeArchive(w, dir.Path, dir.revision, versionString, dir.Meta().Hooks())
}
func writeArchive(w io.Writer, path string, revision int, versionString string, hooks map[string]bool) error {
zipw := zip.NewWriter(w)
defer zipw.Close()
// The root directory may be symlinked elsewhere so
// resolve that before creating the zip.
rootPath, err := resolveSymlinkedRoot(path)
if err != nil {
return err
}
zp := zipPacker{zipw, rootPath, hooks}
if revision != -1 {
zp.AddFile("revision", strconv.Itoa(revision))
}
if versionString != "" {
zp.AddFile("version", versionString)
}
return filepath.Walk(rootPath, zp.WalkFunc())
}
type zipPacker struct {
*zip.Writer
root string
hooks map[string]bool
}
func (zp *zipPacker) WalkFunc() filepath.WalkFunc {
return func(path string, fi os.FileInfo, err error) error {
return zp.visit(path, fi, err)
}
}
func (zp *zipPacker) AddFile(filename string, value string) error {
h := &zip.FileHeader{Name: filename}
h.SetMode(syscall.S_IFREG | 0644)
w, err := zp.CreateHeader(h)
if err == nil {
_, err = w.Write([]byte(value))
}
return err
}
func (zp *zipPacker) visit(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
relpath, err := filepath.Rel(zp.root, path)
if err != nil {
return err
}
// Replace any Windows path separators with "/".
// zip file spec 4.4.17.1 says that separators are always "/" even on Windows.
relpath = filepath.ToSlash(relpath)
method := zip.Deflate
hidden := len(relpath) > 1 && relpath[0] == '.'
if fi.IsDir() {
if relpath == "build" {
return filepath.SkipDir
}
if hidden {
return filepath.SkipDir
}
relpath += "/"
method = zip.Store
}
mode := fi.Mode()
if err := checkFileType(relpath, mode); err != nil {
return err
}
if mode&os.ModeSymlink != 0 {
method = zip.Store
}
if hidden || relpath == "revision" {
return nil
}
h := &zip.FileHeader{
Name: relpath,
Method: method,
}
perm := os.FileMode(0644)
if mode&os.ModeSymlink != 0 {
perm = 0777
} else if mode&0100 != 0 {
perm = 0755
}
if filepath.Dir(relpath) == "hooks" {
hookName := filepath.Base(relpath)
if _, ok := zp.hooks[hookName]; ok && !fi.IsDir() && mode&0100 == 0 {
logger.Warningf("making %q executable in charm", path)
perm = perm | 0100
}
}
h.SetMode(mode&^0777 | perm)
w, err := zp.CreateHeader(h)
if err != nil || fi.IsDir() {
return err
}
var data []byte
if mode&os.ModeSymlink != 0 {
target, err := os.Readlink(path)
if err != nil {
return err
}
if err := checkSymlinkTarget(zp.root, relpath, target); err != nil {
return err
}
data = []byte(target)
_, err = w.Write(data)
} else {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(w, file)
}
return err
}
func checkSymlinkTarget(basedir, symlink, target string) error {
if filepath.IsAbs(target) {
return fmt.Errorf("symlink %q is absolute: %q", symlink, target)
}
p := filepath.Join(filepath.Dir(symlink), target)
if p == ".." || strings.HasPrefix(p, "../") {
return fmt.Errorf("symlink %q links out of charm: %q", symlink, target)
}
return nil
}
func checkFileType(path string, mode os.FileMode) error {
e := "file has an unknown type: %q"
switch mode & os.ModeType {
case os.ModeDir, os.ModeSymlink, 0:
return nil
case os.ModeNamedPipe:
e = "file is a named pipe: %q"
case os.ModeSocket:
e = "file is a socket: %q"
case os.ModeDevice:
e = "file is a device: %q"
}
return fmt.Errorf(e, path)
}
// MaybeGenerateVersionString generates charm version string.
// The second return value is the detected vcs type.
func (dir *CharmDir) MaybeGenerateVersionString() (string, string, error) {
var cmdArgs []string
// Verify that it is revision control directory.
if _, err := os.Stat(filepath.Join(dir.Path, ".hg")); err == nil {
cmdArgs = []string{"hg", "id", "-n"}
} else if _, err = os.Stat(filepath.Join(dir.Path, ".bzr")); err == nil {
cmdArgs = []string{"bzr", "version-info"}
} else if _, err = os.Stat(filepath.Join(dir.Path, ".git")); err == nil {
cmdArgs = []string{"git", "describe", "--dirty", "--always"}
} else {
logger.Debugf("charm is not in revision control directory")
// Return empty string.
return "", "", nil
}
vcsType := cmdArgs[0]
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
// version string value is written to stdout if successful.
out, err := cmd.Output()
if err != nil {
return "", vcsType, err
}
output := string(out)
if err == nil {
return output, vcsType, nil
}
// If there's an error, we may be able to get a relevant cause string.
exitErr, ok := err.(*exec.ExitError)
if !ok || len(exitErr.Stderr) == 0 {
return "", vcsType, err
}
return "", vcsType, fmt.Errorf("%s: %s", err, string(exitErr.Stderr))
}