forked from pachyderm/pachyderm
/
parser.go
256 lines (238 loc) · 6.31 KB
/
parser.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
package parse
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
"github.com/pachyderm/pachyderm/src/log"
"github.com/pachyderm/pachyderm/src/pps"
)
var (
versionToParseFunc = map[string]func(string, string, *config) (*pps.Pipeline, error){
"v1": parsePipelineV1,
}
)
type parser struct{}
func newParser() *parser {
return &parser{}
}
func (p *parser) ParsePipeline(dirPath string, contextDirPath string) (*pps.Pipeline, error) {
return parsePipeline(dirPath, contextDirPath)
}
type config struct {
Version string
Include []string
Exclude []string
}
func parsePipeline(dirPath string, contextDirPath string) (*pps.Pipeline, error) {
dirPath = filepath.Clean(dirPath)
config, err := parseConfig(dirPath, contextDirPath)
if err != nil {
return nil, err
}
if config.Version == "" {
return nil, fmt.Errorf("no version specified in pps.yml")
}
parseFunc, ok := versionToParseFunc[config.Version]
if !ok {
return nil, fmt.Errorf("unknown pps specification version: %s", config.Version)
}
return parseFunc(dirPath, contextDirPath, config)
}
func parseConfig(dirPath string, contextDirPath string) (*config, error) {
configFilePath := filepath.Join(dirPath, contextDirPath, "pps.yml")
if err := checkFileExists(configFilePath); err != nil {
return nil, err
}
data, err := ioutil.ReadFile(configFilePath)
if err != nil {
return nil, err
}
config := &config{}
if err := yaml.Unmarshal(data, config); err != nil {
return nil, err
}
return config, nil
}
func parsePipelineV1(dirPath string, contextDirPath string, config *config) (*pps.Pipeline, error) {
filePaths, err := getAllFilePaths(dirPath, contextDirPath, config.Include, config.Exclude)
if err != nil {
return nil, err
}
pipeline := &pps.Pipeline{
NameToElement: make(map[string]*pps.Element),
}
for _, filePath := range filePaths {
element, err := getElementForPipelineFile(dirPath, filePath)
if err != nil {
return nil, err
}
if _, ok := pipeline.NameToElement[element.Name]; ok {
return nil, fmt.Errorf("duplicate element: %s", element.Name)
}
pipeline.NameToElement[element.Name] = element
}
log.Printf("got pipeline %v\n", pipeline)
return pipeline, nil
}
func getAllFilePaths(dirPath string, contextDirPath string, includes []string, excludes []string) ([]string, error) {
var filePaths []string
if err := filepath.Walk(
filepath.Join(dirPath, contextDirPath),
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
filePaths = append(filePaths, path)
}
return nil
},
); err != nil {
return nil, err
}
relFilePaths := make([]string, len(filePaths))
for i, filePath := range filePaths {
relFilePath, err := filepath.Rel(dirPath, filePath)
if err != nil {
return nil, err
}
relFilePaths[i] = relFilePath
}
var filteredRelFilePaths []string
for _, relFilePath := range relFilePaths {
isPipelineFile, err := isPipelineFile(relFilePath, contextDirPath, includes, excludes)
if err != nil {
return nil, err
}
if isPipelineFile {
filteredRelFilePaths = append(filteredRelFilePaths, relFilePath)
}
}
return filteredRelFilePaths, nil
}
func isPipelineFile(filePath string, contextDirPath string, includes []string, excludes []string) (bool, error) {
var err error
if contextDirPath != "" {
filePath, err = filepath.Rel(contextDirPath, filePath)
if err != nil {
return false, err
}
}
isPipelineFileIncluded, err := isPipelineFileIncluded(filePath, includes)
if err != nil {
return false, err
}
isPipelineFileExcluded, err := isPipelineFileExcluded(filePath, excludes)
if err != nil {
return false, err
}
return isPipelineFileIncluded && !isPipelineFileExcluded, nil
}
func isPipelineFileIncluded(filePath string, includes []string) (bool, error) {
if !strings.HasSuffix(filePath, ".yml") {
return false, nil
}
if filePath == "pps.yml" {
return false, nil
}
if len(includes) == 0 {
return true, nil
}
for _, include := range includes {
matched, err := matches(include, filePath)
if err != nil {
return false, err
}
if matched {
return true, nil
}
}
return false, nil
}
func isPipelineFileExcluded(filePath string, excludes []string) (bool, error) {
if !strings.HasSuffix(filePath, ".yml") {
return true, nil
}
for _, exclude := range excludes {
matched, err := matches(exclude, filePath)
if err != nil {
return false, err
}
if matched {
return true, nil
}
}
return false, nil
}
func matches(match string, filePath string) (bool, error) {
if strings.HasPrefix(filePath, match) {
return true, nil
}
matched, err := filepath.Match(match, filePath)
if err != nil {
return false, err
}
return matched, nil
}
func getElementForPipelineFile(dirPath string, relFilePath string) (*pps.Element, error) {
filePath := filepath.Join(dirPath, relFilePath)
data, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
m := make(map[interface{}]interface{})
if err := yaml.Unmarshal(data, &m); err != nil {
return nil, err
}
ppsMetaObj, ok := m["pps"]
if !ok {
return nil, fmt.Errorf("no pps section for %s", relFilePath)
}
ppsMeta := ppsMetaObj.(map[interface{}]interface{})
if ppsMeta["kind"] == "" {
return nil, fmt.Errorf("no kind specified for %s", relFilePath)
}
nameObj, ok := ppsMeta["name"]
if !ok {
return nil, fmt.Errorf("no name specified for %s", relFilePath)
}
name := strings.TrimSpace(nameObj.(string))
if name == "" {
return nil, fmt.Errorf("no name specified for %s", relFilePath)
}
element := &pps.Element{
Name: name,
}
kindObj, ok := ppsMeta["kind"]
if !ok {
return nil, fmt.Errorf("no kind specified for %s", relFilePath)
}
kind := strings.TrimSpace(kindObj.(string))
switch kind {
case "node":
node := &pps.Node{}
if err := yaml.Unmarshal(data, node); err != nil {
return nil, err
}
element.Node = node
case "docker_service":
dockerService := &pps.DockerService{}
if err := yaml.Unmarshal(data, dockerService); err != nil {
return nil, err
}
if dockerService.Build != "" {
dockerService.Build = filepath.Clean(filepath.Join(filepath.Dir(relFilePath), dockerService.Build))
}
element.DockerService = dockerService
default:
return nil, fmt.Errorf("unknown kind %s for %s", kind, relFilePath)
}
return element, nil
}
func checkFileExists(path string) error {
_, err := os.Stat(path)
return err
}