This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
/
step.go
204 lines (188 loc) · 5.5 KB
/
step.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
// Copyright 2019 Drone IO, Inc.
//
// 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 compiler
import (
"strings"
"github.com/drone/drone-runtime/engine"
"github.com/drone/drone-yaml/yaml"
"github.com/drone/drone-yaml/yaml/compiler/image"
"github.com/drone/drone-yaml/yaml/compiler/internal/rand"
)
func createStep(spec *engine.Spec, src *yaml.Container) *engine.Step {
dst := &engine.Step{
Metadata: engine.Metadata{
UID: rand.String(),
Name: src.Name,
Namespace: spec.Metadata.Namespace,
Labels: map[string]string{
"io.drone.step.name": src.Name,
},
},
Detach: src.Detach,
DependsOn: src.DependsOn,
Devices: nil,
Docker: &engine.DockerStep{
Args: src.Command,
Command: src.Entrypoint,
DNS: src.DNS,
DNSSearch: src.DNSSearch,
ExtraHosts: src.ExtraHosts,
Image: image.Expand(src.Image),
Network: src.Network,
Networks: nil, // set in compiler.go
Ports: toPorts(src),
Privileged: src.Privileged,
PullPolicy: toPullPolicy(src),
User: src.User,
},
Envs: map[string]string{},
Files: nil, // set below
IgnoreErr: toIgnoreErr(src),
IgnoreStderr: false,
IgnoreStdout: false,
Resources: toResources(src),
RunPolicy: toRunPolicy(src),
Secrets: nil, // set below
Volumes: nil, // set below
WorkingDir: "", // set in compiler.go
}
// if the user is running a service container with
// no custom commands, we should revert back to the
// user-defined working directory, which may be empty.
if dst.Detach && len(src.Commands) == 0 {
dst.WorkingDir = src.WorkingDir
}
// appends the volumes to the container def.
for _, vol := range src.Volumes {
// the user should never be able to directly
// mount the docker socket. This should be
// restricted by the linter, but we place this
// check here just to be safe.
if vol.Name == "_docker_socket" {
continue
}
mount := &engine.VolumeMount{
Name: vol.Name,
Path: vol.MountPath,
}
dst.Volumes = append(dst.Volumes, mount)
}
// appends the environment variables to the
// container definition.
for key, value := range src.Environment {
// fix https://github.com/drone/drone-yaml/issues/13
if value == nil {
continue
}
if value.Secret != "" {
sec := &engine.SecretVar{
Name: value.Secret,
Env: key,
}
dst.Secrets = append(dst.Secrets, sec)
} else {
dst.Envs[key] = value.Value
}
}
// appends the settings variables to the
// container definition.
for key, value := range src.Settings {
// fix https://github.com/drone/drone-yaml/issues/13
if value == nil {
continue
}
// all settings are passed to the plugin env
// variables, prefixed with PLUGIN_
key = "PLUGIN_" + strings.ToUpper(key)
// if the setting parameter is sources from the
// secret we create a secret enviornment variable.
if value.Secret != "" {
sec := &engine.SecretVar{
Name: value.Secret,
Env: key,
}
dst.Secrets = append(dst.Secrets, sec)
} else {
// else if the setting parameter is opaque
// we inject as a string-encoded environment
// variable.
dst.Envs[key] = encode(value.Value)
}
}
// if the step specifies shell commands we generate a
// script. The script is copied to the container at
// runtime (or mounted as a config map) and then executed
// as the entrypoint.
if len(src.Commands) > 0 {
switch spec.Platform.OS {
case "windows":
setupScriptWin(spec, dst, src)
default:
setupScript(spec, dst, src)
}
}
return dst
}
func createBuildStep(spec *engine.Spec, src *yaml.Container) *engine.Step {
dst := &engine.Step{
Metadata: engine.Metadata{
UID: rand.String(),
Name: src.Name,
Namespace: spec.Metadata.Namespace,
Labels: map[string]string{
"io.drone.step.name": src.Name,
},
},
Detach: src.Detach,
DependsOn: src.DependsOn,
Devices: nil,
Docker: &engine.DockerStep{
Args: []string{"--build"},
DNS: src.DNS,
Image: "drone/docker",
PullPolicy: engine.PullIfNotExists,
},
Envs: map[string]string{},
IgnoreErr: toIgnoreErr(src),
IgnoreStderr: false,
IgnoreStdout: false,
Resources: toResources(src),
RunPolicy: toRunPolicy(src),
}
// if v := src.Build.Args; len(v) > 0 {
// dst.Envs["DOCKER_BUILD_ARGS"] = strings.Join(v, ",")
// }
if v := src.Build.CacheFrom; len(v) > 0 {
dst.Envs["DOCKER_BUILD_CACHE_FROM"] = strings.Join(v, ",")
}
// if len(src.Build.Labels) > 0 {
// dst.Envs["DOCKER_BUILD_LABELS"] = strings.Join(v, ",")
// }
if v := src.Build.Dockerfile; v != "" {
dst.Envs["DOCKER_BUILD_DOCKERFILE"] = v
}
if v := src.Build.Context; v != "" {
dst.Envs["DOCKER_BUILD_CONTEXT"] = v
}
if v := src.Build.Image; v != "" {
alias := image.Trim(v) + ":" + dst.Metadata.UID
dst.Envs["DOCKER_BUILD_IMAGE"] = image.Expand(v)
dst.Envs["DOCKER_BUILD_IMAGE_ALIAS"] = image.Expand(alias)
}
dst.Volumes = append(dst.Volumes, &engine.VolumeMount{
Name: "_docker_socket",
Path: "/var/run/docker.sock",
})
return dst
}