forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.go
172 lines (151 loc) · 5.13 KB
/
pipeline.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
package pipeline
import (
"strings"
"encoding/json"
"errors"
"fmt"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/api/handler"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/rancher/pkg/controllers/user/pipeline/utils"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/client/management/v3"
"github.com/robfig/cron"
"github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
)
type Handler struct {
Pipelines v3.PipelineInterface
PipelineLister v3.PipelineLister
PipelineExecutions v3.PipelineExecutionInterface
}
func Formatter(apiContext *types.APIContext, resource *types.RawResource) {
resource.AddAction(apiContext, "activate")
resource.AddAction(apiContext, "deactivate")
resource.AddAction(apiContext, "run")
}
func Validator(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) error {
pipelineSpec := v3.PipelineSpec{}
if err := convert.ToObj(data, &pipelineSpec); err != nil {
return httperror.NewAPIError(httperror.InvalidBodyContent, err.Error())
}
if !validSourceCodeConfig(pipelineSpec) {
return httperror.NewAPIError(httperror.InvalidBodyContent,
"invalid pipeline definition, expected sourceCode step at the start")
}
if pipelineSpec.TriggerCronExpression != "" {
sourceCodeConfig := pipelineSpec.Stages[0].Steps[0].SourceCodeConfig
if sourceCodeConfig.BranchCondition == "all" || sourceCodeConfig.BranchCondition == "except" {
return httperror.NewAPIError(httperror.InvalidBodyContent,
"cron trigger only works for only branch option")
}
_, err := cron.ParseStandard(pipelineSpec.TriggerCronExpression)
if err != nil {
return httperror.NewAPIError(httperror.InvalidBodyContent,
"error parse cron trigger")
}
}
return nil
}
func (h *Handler) CreateHandler(apiContext *types.APIContext, next types.RequestHandler) error {
//update hooks endpoint for webhook
if err := utils.UpdateEndpoint(apiContext.URLBuilder.Current()); err != nil {
return err
}
return handler.CreateHandler(apiContext, next)
}
func (h *Handler) UpdateHandler(apiContext *types.APIContext, next types.RequestHandler) error {
//update hooks endpoint for webhook
if err := utils.UpdateEndpoint(apiContext.URLBuilder.Current()); err != nil {
return err
}
return handler.UpdateHandler(apiContext, next)
}
func (h *Handler) ActionHandler(actionName string, action *types.Action, apiContext *types.APIContext) error {
logrus.Debugf("do pipeline action:%s", actionName)
switch actionName {
case "activate":
return h.changeState(apiContext, "inactive", "active")
case "deactivate":
return h.changeState(apiContext, "active", "inactive")
case "run":
return h.run(apiContext)
}
return httperror.NewAPIError(httperror.InvalidAction, "unsupported action")
}
func (h *Handler) changeState(apiContext *types.APIContext, curState, newState string) error {
parts := strings.Split(apiContext.ID, ":")
ns := parts[0]
name := parts[1]
pipeline, err := h.PipelineLister.Get(ns, name)
if err != nil {
return err
}
if pipeline.Status.PipelineState == curState {
pipeline.Status.PipelineState = newState
if _, err = h.Pipelines.Update(pipeline); err != nil {
return err
}
} else {
return fmt.Errorf("Error resource is not %s", curState)
}
data := map[string]interface{}{}
if err := access.ByID(apiContext, apiContext.Version, apiContext.Type, apiContext.ID, &data); err != nil {
return err
}
apiContext.WriteResponse(http.StatusOK, data)
return nil
}
func (h *Handler) run(apiContext *types.APIContext) error {
parts := strings.Split(apiContext.ID, ":")
ns := parts[0]
name := parts[1]
pipeline, err := h.PipelineLister.Get(ns, name)
if err != nil {
return err
}
runPipelineInput := v3.RunPipelineInput{}
requestBytes, err := ioutil.ReadAll(apiContext.Request.Body)
if err != nil {
return err
}
if string(requestBytes) != "" {
if err := json.Unmarshal(requestBytes, &runPipelineInput); err != nil {
return err
}
}
if !validSourceCodeConfig(pipeline.Spec) {
return errors.New("Error invalid pipeline definition")
}
branch := runPipelineInput.Branch
branchCondition := pipeline.Spec.Stages[0].Steps[0].SourceCodeConfig.BranchCondition
if branchCondition == "except" || branchCondition == "all" {
if branch == "" {
return httperror.NewAPIError(httperror.InvalidBodyContent, "Error branch is not specified for the pipeline to run")
}
} else {
branch = ""
}
userName := apiContext.Request.Header.Get("Impersonate-User")
execution, err := utils.GenerateExecution(h.Pipelines, h.PipelineExecutions, pipeline, utils.TriggerTypeUser, userName, runPipelineInput.Branch)
if err != nil {
return err
}
data := map[string]interface{}{}
if err := access.ByID(apiContext, apiContext.Version, client.PipelineExecutionType, ns+":"+execution.Name, &data); err != nil {
return err
}
apiContext.WriteResponse(http.StatusOK, data)
return err
}
func validSourceCodeConfig(spec v3.PipelineSpec) bool {
if len(spec.Stages) < 1 ||
len(spec.Stages[0].Steps) < 1 ||
spec.Stages[0].Steps[0].SourceCodeConfig == nil {
return false
}
return true
}