-
Notifications
You must be signed in to change notification settings - Fork 4
/
devkit.go
78 lines (67 loc) · 2.36 KB
/
devkit.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
/*
(c) Copyright NetFoundry Inc. 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
https://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 devkit
import (
"fmt"
"github.com/openziti/fablab/kernel/lib"
"github.com/openziti/fablab/kernel/model"
"github.com/sirupsen/logrus"
"os"
"path/filepath"
)
type devKit struct {
root func() string
binaries []string
}
func DevKit(root string, binaries []string) model.Stage {
return &devKit{root: func() string { return root }, binaries: binaries}
}
func DevKitF(root func() string, binaries []string) model.Stage {
return &devKit{root: root, binaries: binaries}
}
func (devKit *devKit) Execute(model.Run) error {
cfgRoot := filepath.Join(model.KitBuild(), "cfg")
fi, err := os.Stat(model.ConfigBuild())
if err == nil && fi.IsDir() {
if err := lib.CopyTree(model.ConfigBuild(), cfgRoot); err != nil {
return fmt.Errorf("error copying configuration tree (%s)", err)
}
} else {
logrus.Infof("no [cfg] root, not kitting")
}
pkiRoot := filepath.Join(model.KitBuild(), "pki")
fi, err = os.Stat(model.PkiBuild())
if err == nil && fi.IsDir() {
if err := lib.CopyTree(model.PkiBuild(), pkiRoot); err != nil {
return fmt.Errorf("error copying pki tree (%s)", err)
}
} else {
logrus.Infof("no [pki] root, not kitting")
}
if err := os.MkdirAll(filepath.Join(model.KitBuild(), "bin"), os.ModePerm); err != nil {
return fmt.Errorf("error creating kit bin directory (%s)", err)
}
for _, binary := range devKit.binaries {
srcPath := filepath.Join(devKit.root(), binary)
dstPath := filepath.Join(model.KitBuild(), "bin", binary)
if _, err := lib.CopyFile(srcPath, dstPath); err == nil {
logrus.Infof("[%s] => [%s]", srcPath, dstPath)
} else {
return fmt.Errorf("error copying binary [%s] => [%s] (%w)", srcPath, dstPath, err)
}
if err := os.Chmod(dstPath, 0755); err != nil {
return fmt.Errorf("error setting binary [%s] permissions (%w)", dstPath, err)
}
}
return nil
}