-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.go
320 lines (297 loc) · 8.66 KB
/
manifest.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
/*
Artisan Core - Automation Manager
Copyright (C) 2022-Present SouthWinds Tech Ltd - www.southwinds.io
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package data
import (
"bytes"
"fmt"
"os"
"southwinds.dev/artisan/core"
"southwinds.dev/artisan/i18n"
"strconv"
"strings"
"text/tabwriter"
"time"
)
type Manifest struct {
// the author of the package
Author string `json:"author,omitempty"`
// the signing and verification authority for the package
Authority []string `json:"authority,omitempty"`
// the package type
Type string `json:"type,omitempty"`
// the license associated to the package
License string `json:"license,omitempty"`
// the target OS for the package
OS string `json:"os"`
// the name of the package file
Ref string `json:"ref"`
// the build profile used
Profile string `json:"profile"`
// runtime image that should be used to execute exported functions in the package
Runtime string `json:"runtime,omitempty"`
// the labels assigned to the package
Labels map[string]string `json:"labels,omitempty"`
// the URI of the package source
Source string `json:"source,omitempty"`
// the path within the source where the project is (for uber repos)
SourcePath string `json:"source_path,omitempty"`
// the commit hash
Commit string `json:"commit,omitempty"`
// repo branch
Branch string `json:"branch,omitempty"`
// the name of the file or folder that has been packaged
Target string `json:"target,omitempty"`
// the timestamp
Time string `json:"time"`
// the size of the package
Size string `json:"size"`
// the Stock Keeping Unit code
SKU string `json:"SKU,omitempty"`
// what functions are available to call?
Functions []*FxInfo `json:"functions,omitempty"`
OpenPolicy string `json:"open_policy,omitempty"`
RunPolicy string `json:"run_policy,omitempty"`
SignPolicy string `json:"sign_policy,omitempty"`
}
func (m *Manifest) Fx(name string) *FxInfo {
for _, fx := range m.Functions {
if fx.Name == name {
return fx
}
}
return nil
}
type Network struct {
Groups []string `yaml:"groups"`
Rules []string `yaml:"rules"`
}
type GroupInfo struct {
Group string
Tags []string
Min int
Max int
IPs []string
}
type GroupsInfo []GroupInfo
func (gs *GroupsInfo) GroupIx(name string) int {
var gsi []GroupInfo = *gs
for ix, info := range gsi {
if strings.EqualFold(info.Group, name) {
return ix
}
}
return -1
}
func (n *Network) parseGroups() (GroupsInfo, error) {
result := make(GroupsInfo, 0)
for _, group := range n.Groups {
parts := strings.Split(group, ":")
groupName := parts[0]
tags := strings.Split(parts[1], ",")
var min, max int
min, err := strconv.Atoi(parts[2])
if err != nil {
return nil, fmt.Errorf("invalid minimum value in network group '%s'", groupName)
}
if strings.EqualFold(parts[3], "*") {
max = 1000
} else {
max, err = strconv.Atoi(parts[3])
if err != nil {
return nil, fmt.Errorf("invalid maximum value in network group '%s'", groupName)
}
}
result = append(result, GroupInfo{
Group: groupName,
Tags: tags,
Min: min,
Max: max,
})
}
return result, nil
}
func parseGroup(group string) (groupName string, tags []string, min, max int, err error) {
parts := strings.Split(group, ":")
groupName = parts[0]
tags = strings.Split(parts[1], ",")
min, err = strconv.Atoi(parts[2])
if err != nil {
return groupName, tags, min, max, fmt.Errorf("invalid minimum value in network group '%s'", groupName)
}
if strings.EqualFold(parts[3], "*") {
max = 1000 // upper limit is 1000 nodes
} else {
max, err = strconv.Atoi(parts[3])
if err != nil {
return groupName, tags, min, max, fmt.Errorf("invalid maximum value in network group '%s'", groupName)
}
}
return
}
func (n *Network) AllocateIPs(ipList ...string) (GroupsInfo, error) {
if hasDuplicates(ipList) {
return nil, fmt.Errorf("IPs in list must be unique")
}
var ipIx int
g, err := n.parseGroups()
if err != nil {
return nil, err
}
// first tries and allocates the minimum requirement
for _, group := range n.Groups {
name, _, min, _, parseErr := parseGroup(group)
if parseErr != nil {
return nil, err
}
infoIx := g.GroupIx(name)
for i := 0; i < min; i++ {
g[infoIx].IPs = append(g[infoIx].IPs, ipList[ipIx])
ipIx++
}
}
// now tries and allocates the maximum requirement
for _, group := range n.Groups {
name, _, min, max, parseErr := parseGroup(group)
if parseErr != nil {
return nil, err
}
infoIx := g.GroupIx(name)
topLimit := max
if max > (len(ipList) - ipIx) {
topLimit = len(ipList) - ipIx
}
for i := 0; i < topLimit; i++ {
if max > min {
g[infoIx].IPs = append(g[infoIx].IPs, ipList[ipIx])
ipIx++
}
}
}
return g, nil
}
type Group struct {
Tags []string `yaml:"tags"`
IPs []string `yaml:"ips"`
}
type Role struct {
Name string `yaml:"name"`
Min int `yaml:"min,omitempty"`
Max int `yaml:"max,omitempty"`
Tag []Tag `yaml:"tag,omitempty"`
}
type Tag struct {
Name string `yaml:"name"`
Min int `yaml:"min,omitempty"`
Max int `yaml:"max,omitempty"`
}
// FxInfo exported function list
type FxInfo struct {
Name string `json:"name"`
Description string `json:"description"`
Input *Input `json:"input,omitempty"`
Credits int `json:"credits,omitempty"`
Runtime string `json:"runtime,omitempty"` // runtime image that should be used to execute functions in the package
Network *Network `json:"network,omitempty"`
}
func (m *Manifest) ToMarkDownBytes(name string) []byte {
var b bytes.Buffer
b.WriteString(fmt.Sprintf("# Package %s Manifest\n", name))
b.WriteString(fmt.Sprintf("*autogenerated using [Artisan CLI](https://github.com/southwinds-io/artisan) on %s*\n", time.Now().Format(time.RFC822Z)))
for _, fx := range m.Functions {
b.WriteString(fmt.Sprintf("## Function: %s\n", fx.Name))
b.WriteString(fmt.Sprintf("%s\n", fx.Description))
if len(fx.Input.Var) > 0 {
b.WriteString(fmt.Sprintf("### Variables:\n"))
b.WriteString(fmt.Sprintf("|name|description|default|\n"))
b.WriteString(fmt.Sprintf("|---|---|---|\n"))
for _, v := range fx.Input.Var {
b.WriteString(fmt.Sprintf("|%s|%s|%s|\n", v.Name, format(v.Description), v.Default))
}
}
if len(fx.Input.Secret) > 0 {
b.WriteString(fmt.Sprintf("### Secrets:\n"))
b.WriteString(fmt.Sprintf("|name|description|\n"))
b.WriteString(fmt.Sprintf("|---|---|\n"))
for _, s := range fx.Input.Secret {
b.WriteString(fmt.Sprintf("|%s|%s|\n", s.Name, format(s.Description)))
}
}
if len(fx.Input.File) > 0 {
b.WriteString(fmt.Sprintf("### Files:\n"))
b.WriteString(fmt.Sprintf("|name|description|path|\n"))
b.WriteString(fmt.Sprintf("|---|---|---|\n"))
for _, f := range fx.Input.File {
b.WriteString(fmt.Sprintf("|%s|%s|%s|\n", f.Name, format(f.Description), f.Path))
}
}
}
return b.Bytes()
}
func (m *Manifest) ListFxs(artHome string) {
// get a table writer for the stdout
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.Debug)
defer func(w *tabwriter.Writer) {
_ = w.Flush()
}(w)
// print the header row
_, err := fmt.Fprintln(w, i18n.String(artHome, i18n.LBL_MAN_FX_HEADER))
core.CheckErr(err, "failed to write table header")
var (
inputs int
groups []string
)
for _, fx := range m.Functions {
if fx.Input != nil {
inputs = len(fx.Input.Env().Slice())
} else {
inputs = 0
}
if fx.Network != nil {
groups = fx.Network.Groups
} else {
groups = []string{}
}
_, err = fmt.Fprintln(w, fmt.Sprintf("%s\t %s\t %v\t %d\t",
fx.Name,
trim(fx.Description, 40),
groups,
inputs))
core.CheckErr(err, "failed to write output")
}
}
func trim(in string, i int) string {
if len(in) < i {
return in
}
return in[:i]
}
func format(content string) string {
return strings.Replace(content, "\n", "<br>", -1)
}
func removeDuplicateValues(strSlice []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range strSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func hasDuplicates(s []string) bool {
s2 := removeDuplicateValues(s)
return len(s) != len(s2)
}