/
local.go
182 lines (144 loc) · 3.64 KB
/
local.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
package distro
import (
"context"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/ekristen/cast/pkg/sysinfo"
"gopkg.in/yaml.v3"
cp "github.com/otiai10/copy"
"github.com/sirupsen/logrus"
)
type LocalDistro struct {
Owner string
Repo string
Version string
Name string
Dir string
SaltDir string
Alias string
IsAlias bool
IsLocal bool
Manifest *Manifest
IncludePreReleases bool
ctx context.Context
log *logrus.Entry
data interface{}
}
func NewLocal(ctx context.Context, distro string, version *string, includePreReleases bool, githubToken string, data interface{}) (Distro, error) {
d := &LocalDistro{
Owner: "local",
Repo: path.Base(distro),
}
d.IncludePreReleases = includePreReleases
if version != nil {
d.Version = *version
}
d.Name = fmt.Sprintf("%s_%s", d.Owner, d.Repo)
distroPath, err := filepath.Abs(distro)
if err != nil {
return nil, err
}
d.Dir = distroPath
d.ctx = ctx
d.data = data
d.log = logrus.WithField("component", "distro").WithField("type", "local").WithField("owner", d.Owner).WithField("repo", d.Repo)
if err := d.verifyRelease(); err != nil {
return nil, err
}
d.SaltDir = distroPath
if d.Manifest.Base != "" {
d.SaltDir = filepath.Join(d.SaltDir, d.Manifest.Base)
}
return d, nil
}
func (d *LocalDistro) GetSaltstackPillars() (pillars map[string]string) {
pillars = d.Manifest.Saltstack.Pillars
return d.Manifest.Saltstack.Pillars
}
func (d *LocalDistro) GetName() string {
return d.Name
}
func (d *LocalDistro) GetReleaseName() string {
return "local"
}
func (d *LocalDistro) GetModeState(mode string) (string, error) {
for _, m := range d.Manifest.Modes {
if (mode == "" || mode == "default") && m.Default {
return m.State, nil
} else if mode != "" && m.Name == mode {
return m.State, nil
}
}
return "", fmt.Errorf("unable to resolve state from mode: %s", mode)
}
func (d *LocalDistro) GetCachePath() string {
cachePath := filepath.Join(d.GetName(), d.GetReleaseName())
d.log.Debugf("cache path: %s", cachePath)
return cachePath
}
func (d *LocalDistro) GetCacheSaltStackSourcePath() string {
fileRootPath := filepath.Join("source", d.Manifest.Name)
d.log.Debugf("saltstack file root path: %s", fileRootPath)
return fileRootPath
}
func (d *LocalDistro) Download(dir string) error {
saltstackFileRootPath := filepath.Join(dir, d.GetCacheSaltStackSourcePath())
if err := os.MkdirAll(saltstackFileRootPath, 0755); err != nil {
return err
}
return cp.Copy(d.SaltDir, saltstackFileRootPath)
}
func (d *LocalDistro) verifyRelease() error {
contents, err := ioutil.ReadFile(fmt.Sprintf("%s/.cast.yml", d.Dir))
if err != nil {
return err
}
var cfg LocalConfig
if err := yaml.Unmarshal(contents, &cfg); err != nil {
return err
}
d.Manifest = cfg.Manifest
isSupported := len(d.Manifest.SupportedOS) == 0
if !isSupported {
d.log.Info("checking operating system support")
}
osinfo := sysinfo.GetOSInfo()
for _, s := range d.Manifest.SupportedOS {
mustmatch := 0
match := 0
if s.ID != "" {
mustmatch++
}
if s.Codename != "" {
mustmatch++
}
if s.Release != "" {
mustmatch++
}
if s.ID != "" && strings.EqualFold(s.ID, osinfo.Vendor) {
match++
}
if s.Codename != "" && strings.EqualFold(s.Codename, osinfo.Codename) {
match++
}
if s.Release != "" && strings.EqualFold(s.Release, osinfo.Release) {
match++
}
if match == mustmatch {
isSupported = true
}
}
if !isSupported {
return fmt.Errorf("operating system is not supported")
}
d.log.Info("operating system is supported")
d.log.Info("rendering manifest")
if err := d.Manifest.Render(d.data); err != nil {
return err
}
return nil
}