-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
102 lines (89 loc) · 2.79 KB
/
util.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
/*
Copyright 2019 Gravitational, 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 extensions
import (
"os"
"os/exec"
"github.com/gravitational/trace"
)
// ensureSymlinks recreates the provided symlinks.
func ensureSymlinks(symlinks map[string]string) error {
for source, destination := range symlinks {
if err := ensureSymlink(source, destination); err != nil {
return trace.Wrap(err)
}
}
return nil
}
// verifySymlinks verifies a set of symlinks.
func verifySymlinks(symlinks map[string]string) (bool, error) {
for source, destination := range symlinks {
if ok, err := verifySymlink(source, destination); !ok || err != nil {
return false, trace.Wrap(err)
}
}
return true, nil
}
// ensureSymlinks recreates the specified symlink.
func ensureSymlink(source, destination string) error {
log.Debugf("Symlinking %v -> %v.", source, destination)
if err := os.RemoveAll(destination); err != nil {
return trace.ConvertSystemError(err)
}
if err := os.Symlink(source, destination); err != nil {
return trace.ConvertSystemError(err)
}
return nil
}
// verifySymlink verifies that the symlink exists and points to the expected location.
func verifySymlink(source, destination string) (bool, error) {
fileInfo, err := os.Lstat(destination)
err = trace.ConvertSystemError(err)
if err != nil && !trace.IsNotFound(err) {
return false, trace.Wrap(err)
}
if trace.IsNotFound(err) {
return false, nil
}
// If the file exists, it must be a symlink and point to the specified location.
if fileInfo.Mode()&os.ModeSymlink == 0 {
return false, nil
}
actualSource, err := os.Readlink(destination)
if err != nil {
return false, trace.ConvertSystemError(err)
}
if actualSource != source {
return false, nil
}
return true, nil
}
// runCommand executes the specified command.
func runCommand(cmd string, args ...string) error {
command := exec.Command(cmd, args...)
log.Debugf("Executing %v.", command.Args)
if out, err := command.CombinedOutput(); err != nil {
return trace.Wrap(err, "failed to execute %v: %v", command.Args, string(out))
}
return nil
}
// hasDocker returns true if docker binary is available.
func hasDocker() bool {
_, err := exec.LookPath("docker")
return err == nil
}
// hasHelm returns true if helm binary is available.
func hasHelm() bool {
_, err := exec.LookPath("helm")
return err == nil
}