-
Notifications
You must be signed in to change notification settings - Fork 21
/
step_includeworkflow.go
146 lines (132 loc) · 4.59 KB
/
step_includeworkflow.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
// Copyright 2017 Google Inc. All Rights Reserved.
//
// 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 daisy
import (
"context"
"fmt"
"path/filepath"
"reflect"
"strings"
)
// IncludeWorkflow defines a Daisy workflow injection step. This step will
// 'include' the workflow found the path given into the parent workflow. Unlike
// a Subworkflow the included workflow will exist in the same namespace
// as the parent and have access to all its resources.
type IncludeWorkflow struct {
Path string
Vars map[string]string `json:",omitempty"`
Workflow *Workflow `json:",omitempty"`
}
func (i *IncludeWorkflow) populate(ctx context.Context, s *Step) DError {
// Typically s.Workflow is instantiated when the parent workflow is read in NewFromFile.
// Workflow could be nil when the parent workflow is constructed manually using Go structs.
if i.Path != "" && i.Workflow == nil {
var err error
if i.Workflow, err = s.w.NewIncludedWorkflowFromFile(i.Path); err != nil {
return newErr("failed to parse duration for step includeworkflow", err)
}
} else {
if i.Workflow == nil {
return Errf(fmt.Sprintf("IncludeWorkflow %q does not have a workflow", s.name))
}
s.w.includeWorkflow(i.Workflow)
}
i.Workflow.id = i.Workflow.parent.id
i.Workflow.username = i.Workflow.parent.username
i.Workflow.ComputeClient = i.Workflow.parent.ComputeClient
i.Workflow.StorageClient = i.Workflow.parent.StorageClient
i.Workflow.CloudLoggingClient = i.Workflow.parent.CloudLoggingClient
i.Workflow.GCSPath = i.Workflow.parent.GCSPath
i.Workflow.Name = i.Workflow.parent.Name
i.Workflow.Project = i.Workflow.parent.Project
i.Workflow.Zone = i.Workflow.parent.Zone
i.Workflow.DefaultTimeout = i.Workflow.parent.DefaultTimeout
i.Workflow.autovars = i.Workflow.parent.autovars
i.Workflow.bucket = i.Workflow.parent.bucket
i.Workflow.scratchPath = i.Workflow.parent.scratchPath
i.Workflow.sourcesPath = i.Workflow.parent.sourcesPath
i.Workflow.logsPath = i.Workflow.parent.logsPath
i.Workflow.outsPath = i.Workflow.parent.outsPath
i.Workflow.externalLogging = i.Workflow.parent.externalLogging
i.Workflow.Logger = i.Workflow.parent.Logger
i.Workflow.Name = s.name
i.Workflow.DefaultTimeout = s.Timeout
var errs DError
Loop:
for k, v := range i.Vars {
for wv := range i.Workflow.Vars {
if k == wv {
i.Workflow.AddVar(k, v)
continue Loop
}
}
errs = addErrs(errs, Errf("unknown workflow Var %q passed to IncludeWorkflow %q", k, s.name))
}
if errs != nil {
return errs
}
var replacements []string
for k, v := range i.Workflow.autovars {
if k == "NAME" {
v = s.name
}
if k == "WFDIR" {
v = i.Workflow.workflowDir
}
replacements = append(replacements, fmt.Sprintf("${%s}", k), v)
}
substitute(reflect.ValueOf(i.Workflow).Elem(), strings.NewReplacer(replacements...))
for k, v := range i.Workflow.Vars {
replacements = append(replacements, fmt.Sprintf("${%s}", k), v.Value)
}
substitute(reflect.ValueOf(i.Workflow).Elem(), strings.NewReplacer(replacements...))
for name, st := range i.Workflow.Steps {
st.name = name
st.w = i.Workflow
if err := st.w.populateStep(ctx, st); err != nil {
return err
}
}
// We do this here, and not in validate, as embedded startup scripts could
// have what we think are daisy variables.
if err := i.Workflow.validateVarsSubbed(); err != nil {
return err
}
if err := i.Workflow.substituteSourceVars(ctx, reflect.ValueOf(i.Workflow).Elem()); err != nil {
return err
}
// Copy Sources up to parent resolving relative paths as we go.
for k, v := range i.Workflow.Sources {
if v == "" {
continue
}
if _, ok := s.w.Sources[k]; ok {
return Errf("source %q already exists in workflow", k)
}
if s.w.Sources == nil {
s.w.Sources = map[string]string{}
}
if _, _, err := splitGCSPath(v); err != nil && !filepath.IsAbs(v) {
v = filepath.Join(i.Workflow.workflowDir, v)
}
s.w.Sources[k] = v
}
return nil
}
func (i *IncludeWorkflow) validate(ctx context.Context, s *Step) DError {
return i.Workflow.validate(ctx)
}
func (i *IncludeWorkflow) run(ctx context.Context, s *Step) DError {
return i.Workflow.run(ctx)
}