-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdeployment.go
275 lines (241 loc) · 7.52 KB
/
deployment.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
//==============================================================================
//
// drone-gdm/deployment.go: GDM logic for "Deployments"
//
// Copyright (c) 2017 The New York Times Company
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this library 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 plugin
import (
"bytes"
"fmt"
"gopkg.in/Masterminds/sprig.v2"
"gopkg.in/yaml.v2"
"io/ioutil"
"path"
"strings"
"text/template"
)
type GdmDeploymentCmd struct{}
func NewGdmDeploymentCmd() *GdmDeploymentCmd {
return &GdmDeploymentCmd{}
}
func (command *GdmDeploymentCmd) Name() string {
return "deployments"
}
// On success, return a bool indicating whether or not the named deployment
// already exists.
func (command *GdmDeploymentCmd) Exists(context *GdmPluginContext, spec *GdmConfigurationSpec) (bool, error) {
fmt.Printf("drone-gdm: Checking for existing %s \"%s\"...\n", spec.Group, spec.Name)
var deployExists bool
gcmd := getCmdPrelude(command, spec)
args := append(gcmd, []string{
"list",
"--simple-list",
fmt.Sprintf("--filter=name=%s", spec.Name),
fmt.Sprintf("--project=%s", context.Project),
}...)
result := RunGcloud(context, args...)
if result.Error != nil {
return deployExists, fmt.Errorf("error listing deployments: %s\n", result.Stderr.String())
}
if strings.TrimSpace(result.Stdout.String()) == spec.Name {
fmt.Printf("drone-gdm: \"%s\" exists\n", spec.Name)
deployExists = true
} else {
fmt.Printf("drone-gdm: \"%s\" does not exist\n", spec.Name)
deployExists = false
}
return deployExists, nil
}
func (command *GdmDeploymentCmd) Action(spec *GdmConfigurationSpec, exists bool) (string, error) {
// NOTE: The set of possible actions is as follows:
// - deployment already exists: update or delete
// - deployment does not exist: create
//
// Any other combo of actual state vs desired state is a no-op.
if exists {
switch spec.State {
case "latest":
return "update", nil
case "absent":
return "delete", nil
}
} else {
switch spec.State {
case "latest":
fallthrough
case "present":
return "create", nil
case "absent":
return "", nil
}
}
// Any other combo results in "no action":
return "", nil
}
func (command *GdmDeploymentCmd) Options(context *GdmPluginContext, spec *GdmConfigurationSpec, action string) ([]string, error) {
var err error
var options []string
fileOption, configPath, err := command.getFileOptions(context, spec, action)
if err != nil {
return options, err
}
properties, err := command.getProperties(spec, action)
if err != nil {
return options, err
}
switch action {
case "create":
addOptIfPresent(&options, fileOption, configPath)
addOptIfPresent(&options, "--description", spec.Description)
labels := mapAsOptions(spec.Labels, "=", ",")
addOptIfPresent(&options, "--labels", labels)
addOptIfPresent(&options, "--properties", properties)
if spec.AutoRollback {
options = append(options, "--automatic-rollback-on-error")
}
case "update":
addOptIfPresent(&options, fileOption, configPath)
addOptIfPresent(&options, "--description", spec.Description)
addOptIfPresent(&options, "--properties", properties)
addOptIfPresent(&options, "--create-policy", spec.CreatePolicy)
addOptIfPresent(&options, "--delete-policy", spec.DeletePolicy)
labels := mapAsOptions(spec.Labels, "=", ",")
addOptIfPresent(&options, "--update-labels", labels)
case "delete":
addOptIfPresent(&options, "--delete-policy", spec.DeletePolicy)
}
return options, nil
}
func (command *GdmDeploymentCmd) getFileOptions(context *GdmPluginContext, spec *GdmConfigurationSpec, action string) (string, string, error) {
if action == "delete" {
return "", "", nil
}
pathSpecs := []struct {
param string
val string
}{
{"path", spec.Path},
{"config", spec.Config},
{"template", spec.Template},
}
var configParam string
var configPath string
for _, pathSpec := range pathSpecs {
if pathSpec.val != "" {
if configPath == "" {
configParam = pathSpec.param
configPath = getAdjustedPath(pathSpec.val, context.Dir)
} else {
return "", "", fmt.Errorf(
"Exactly one of \"path\", \"config\", or \"template\" is required for \"%s\". Got: \"%s: %s\" but already had \"%s: %s\"",
spec.Group, pathSpec.param, pathSpec.val, configParam, configPath)
}
}
}
if configPath == "" {
return "", "", fmt.Errorf(
"Exactly one of \"path\", \"config\", or \"template\" is required for \"%s\"",
spec.Group)
}
var err error
var configOption string
switch configParam {
// Compatibility: for "path" parameter, determine option by extension
case "path":
if strings.HasSuffix(configPath, ".yml") || strings.HasSuffix(configPath, ".yaml") {
configPath, err = command.getConfigFile(context, spec, configPath, action)
configOption = "--config"
} else {
configOption = "--template"
}
case "config":
configPath, err = command.getConfigFile(context, spec, configPath, action)
configOption = "--config"
case "template":
configOption = "--template"
}
return configOption, configPath, err
}
func (command *GdmDeploymentCmd) getConfigFile(context *GdmPluginContext, spec *GdmConfigurationSpec, configPath string, action string) (string, error) {
t := template.New(path.Base(configPath))
t.Funcs(template.FuncMap{
"yaml": func(i interface{}) (string, error) {
data, err := yaml.Marshal(i)
return string(data), err
},
}).Funcs(sprig.GenericFuncMap())
t, err := t.ParseFiles(configPath)
if err != nil {
return configPath, fmt.Errorf("Failed to parse configuration yaml: %s", err)
}
gdmVars := make(map[string]interface{})
gdmVars["name"] = spec.Name
gdmVars["status"] = spec.Status
gdmVars["project"] = context.Project
if spec.PassAction {
gdmVars["action"] = action
}
tmplVars := make(map[string]interface{})
tmplVars["drone"] = DroneVars()
tmplVars["plugin"] = PluginVars()
tmplVars["context"] = context.Vars
tmplVars["config"] = spec.Vars
tmplVars["properties"] = spec.Properties
tmplVars["gdm"] = gdmVars
var buff bytes.Buffer
err = t.Execute(&buff, tmplVars)
if err != nil {
return configPath, err
}
//tmpFile, err := ioutil.TempFile(context.TempDir(), "gdm-config")
configDir := path.Dir(configPath)
tmpFile, err := ioutil.TempFile(configDir, "gdm-tmp.*.yml")
if err == nil {
context.trackTempFile(tmpFile.Name())
_, err = tmpFile.Write(buff.Bytes())
if err == nil {
tmpFile.Close()
}
}
return tmpFile.Name(), err
}
func (command *GdmDeploymentCmd) getProperties(spec *GdmConfigurationSpec, action string) (string, error) {
var properties string
if action == "delete" {
return properties, nil
}
noProp := len(spec.Properties)
if spec.PassAction {
noProp += 1
}
if noProp > 0 {
var propPairs []string
for k, v := range spec.Properties {
propVal, err := Y2JMarshal(v)
if err != nil {
return properties, err
}
propPairs = append(propPairs, fmt.Sprintf("%s:%s", k, propVal))
}
if spec.PassAction {
propPairs = append(propPairs, fmt.Sprintf("action:%s", action))
}
properties = strings.Join(propPairs, ",")
}
return properties, nil
}
// EOF