generated from dronestock/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
183 lines (154 loc) · 4.71 KB
/
plugin.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
package main
import (
"bufio"
"os"
"strings"
"github.com/dronestock/drone"
"github.com/goexl/gox"
"github.com/goexl/gox/args"
"github.com/goexl/gox/field"
)
type plugin struct {
drone.Base
// Kubernetes配置
Kubernetes *kubernetes `default:"${KUBERNETES}" json:"kubernetes,omitempty"`
// 用户名
Username string `default:"${USERNAME=default}" json:"username,omitempty"`
// 密钥
Token string `default:"${TOKEN}" json:"token,omitempty" validate:"required_without_all=Key Password"`
// 密码
// 密钥和密码统一使用密码做内部变量配置
Password string `default:"${PASSWORD}" json:"password,omitempty" validate:"required_without_all=Key Token"`
// 密钥
Key string `default:"${KEY}" json:"key,omitempty" validate:"required_without_all=Token Password"`
// 名称
Name string `default:"${NAME}" json:"name,omitempty" validate:"required"`
// 注册表
Registry string `default:"${REGISTRY}" json:"registry,omitempty" validate:"required"`
// 仓库
Repository string `default:"${REPOSITORY}" json:"repository,omitempty" validate:"required"`
// 标签
Tag string `default:"${TAG=latest}" json:"tag,omitempty"`
// 端口
Port *port `default:"${PORT}" json:"port,omitempty"`
// 端口列表
Ports []*port `default:"${PORTS}" json:"ports,omitempty"`
// 资源限制
Resource *resource `default:"${resource}" json:"resource,omitempty"`
// 配置目录
Dir string `default:"${DIR=.deploy}" json:"dir,omitempty"`
// 无状态服务
Stateless *_stateless `default:"${STATELESS}" json:"stateless,omitempty"`
// 注解
Annotations map[string]string `default:"${ANNOTATIONS}" json:"annotations,omitempty"`
// 环境变量
Environments map[string]string `default:"${ENVIRONMENTS}" json:"environments,omitempty"`
}
func newPlugin() drone.Plugin {
return new(plugin)
}
func (p *plugin) Setup() (err error) {
p.Password = gox.If("" != p.Token, p.Token)
// 统一端口配置
if nil == p.Ports {
p.Ports = make([]*port, 0)
}
if nil != p.Port {
p.Ports = append(p.Ports, p.Port)
}
// 统一注解
if 0 != len(p.Annotations) && nil != p.Stateless {
p.Stateless.Annotations = gox.If(nil == p.Stateless.Annotations, make(map[string]string))
p.copy(p.Annotations, p.Stateless.Annotations)
}
// 统一环境变量
if 0 != len(p.Environments) && nil != p.Stateless {
p.Stateless.Environments = gox.If(nil == p.Stateless.Environments, make(map[string]string))
p.copy(p.Environments, p.Stateless.Environments)
}
return
}
func (p *plugin) Config() drone.Config {
return p
}
func (p *plugin) Steps() drone.Steps {
return drone.Steps{
drone.NewStep(newSetupStep(p)).Name("配置").Build(),
drone.NewStep(newNamespaceStep(p)).Name("命名空间").Build(),
drone.NewStep(newStatelessStep(p)).Name("无状态应用").Build(),
drone.NewStep(newServiceStep(p)).Name("服务").Build(),
}
}
func (p *plugin) Fields() (fields gox.Fields[any]) {
fields = gox.Fields[any]{
field.New("username", p.Username),
field.New("name", p.Name),
field.New("registry", p.Registry),
field.New("repository", p.Repository),
field.New("tag", p.Tag),
field.New("ports", p.Ports),
field.New("dir", p.Dir),
}
if nil != p.Kubernetes {
fields.Add(field.New("kubernetes", p.Kubernetes))
}
if nil != p.Stateless {
fields.Add(field.New("stateless", p.Stateless))
}
if nil != p.Resource {
fields.Add(field.New("resource", p.Resource))
}
if 0 != len(p.Annotations) {
fields.Add(field.New("annotations", p.Annotations))
}
if 0 != len(p.Environments) {
fields.Add(field.New("environments", p.Annotations))
}
return
}
func (p *plugin) KubernetesRepository() string {
return strings.ReplaceAll(p.Repository, slash, dot)
}
func (p *plugin) kubectl(args *args.Args) (err error) {
_, err = p.Command(p.Kubernetes.Binary).Args(args).Build().Exec()
return
}
func (p *plugin) outputs(args *args.Args, outputs *[]string) (err error) {
_, err = p.Command(p.Kubernetes.Binary).Args(args).Collector().TrimRight(enter).Strings(outputs).Build().Exec()
return
}
func (p *plugin) loadKvs(path string, fun kvFun) (err error) {
if _, se := os.Stat(path); nil == se {
err = p.readline(path, p.kv(fun))
}
return
}
func (p *plugin) kv(fun kvFun) lineFun {
return func(line string) {
values := strings.Split(line, equal)
if 2 == len(values) {
fun(values[0], values[1])
}
}
}
func (p *plugin) readline(path string, fun lineFun) (err error) {
if file, oe := os.Open(path); nil != oe {
err = oe
} else {
defer p.close(file)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fun(scanner.Text())
}
err = scanner.Err()
}
return
}
func (p *plugin) close(file *os.File) {
_ = file.Close()
}
func (p *plugin) copy(from map[string]string, to map[string]string) {
for key, value := range from {
to[key] = value
}
}