/
bravefile.go
264 lines (224 loc) · 6.74 KB
/
bravefile.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
package shared
import (
"bytes"
"errors"
"fmt"
"net/http"
"strings"
"gopkg.in/yaml.v2"
)
// ImageDescription defines base image type and source
type ImageDescription struct {
Image string `yaml:"image"`
Location string `yaml:"location"`
Architecture string `yaml:"architecture"`
}
// Packages defines system packages to install in container
type Packages struct {
Manager string `yaml:"manager,omitempty"`
System []string `yaml:"system,omitempty"`
}
// RunCommand defines custom commands to run inside continer
type RunCommand struct {
Command string `yaml:"command,omitempty"`
Content string `yaml:"content,omitempty"`
Args []string `yaml:"args,omitempty"`
Env map[string]string `yaml:"env,omitempty"`
Detach bool `yaml:"detach,omitempty"`
}
// CopyCommand defines source and target for files to be copied into container
type CopyCommand struct {
Source string `yaml:"source,omitempty"`
Target string `yaml:"target,omitempty"`
Action string `yaml:"action,omitempty"`
}
// Service defines command to install app
type Service struct {
Name string `yaml:"name,omitempty"`
Image string `yaml:"image,omitempty"`
Version string `yaml:"version,omitempty"`
Profile string `yaml:"profile,omitempty"`
Storage string `yaml:"storage,omitempty"`
Network string `yaml:"network,omitempty"`
Docker string `yaml:"docker,omitempty"`
IP string `yaml:"ip"`
Ports []string `yaml:"ports"`
Resources Resources `yaml:"resources"`
Postdeploy Postdeploy `yaml:"postdeploy,omitempty"`
}
// Postdeploy defines operations to perform after service deployment finish
type Postdeploy struct {
Run []RunCommand `yaml:"run,omitempty"`
Copy []CopyCommand `yaml:"copy,omitempty"`
}
// Resources defines resources allocated to service
type Resources struct {
RAM string `yaml:"ram"`
CPU string `yaml:"cpu"`
GPU string `yaml:"gpu"`
Disk string `yaml:"disk"`
}
// Bravefile describes unit configuration
type Bravefile struct {
Image string `yaml:"image,omitempty"`
Base ImageDescription `yaml:"base,omitempty"`
SystemPackages Packages `yaml:"packages,omitempty"`
Run []RunCommand `yaml:"run,omitempty"`
Copy []CopyCommand `yaml:"copy,omitempty"`
PlatformService Service `yaml:"service,omitempty"`
}
// NewBravefile ..
func NewBravefile() *Bravefile {
return &Bravefile{
PlatformService: Service{
Resources: Resources{
CPU: DefaultUnitCpuLimit,
RAM: DefaultUnitRamLimit,
},
},
}
}
// Load loads Bravefile
func (bravefile *Bravefile) Load(file string) error {
buf, err := ReadFile(file)
if err != nil {
return err
}
err = yaml.Unmarshal(buf.Bytes(), &bravefile)
if err != nil {
return err
}
if bravefile.Image != "" && bravefile.PlatformService.Version != "" {
return fmt.Errorf("bravefile at path %q uses legacy 'version' field in service section and 'image' field in build section "+
"- define version in 'image' field using <image_name>[/version][/arch]", file)
}
if bravefile.Image == "" && bravefile.PlatformService.Image == "" {
return fmt.Errorf("image not defined in bravefile")
}
if bravefile.Image != "" && bravefile.PlatformService.Image != "" {
if bravefile.Image != bravefile.PlatformService.Image {
return fmt.Errorf("two different images defined in same Bravfile: %q and %q", bravefile.Image, bravefile.PlatformService.Image)
}
}
return nil
}
// Validate validates Bravefile for build
func (bravefile *Bravefile) ValidateBuild() error {
if bravefile.Base.Image == "" {
return errors.New("invalid Bravefile: empty Base Image name")
}
if bravefile.Image == "" && bravefile.PlatformService.Image == "" {
return errors.New("invalid Bravefile: empty Service Image name")
}
return nil
}
func (bravefile *Bravefile) IsLegacy() bool {
if bravefile.PlatformService.Version == "" || bravefile.Image != "" {
return false
}
return true
}
func (service *Service) IsLegacy() bool {
if service.Version == "" {
return false
}
return true
}
func (service *Service) ValidateDeploy() error {
if service.Name == "" {
return errors.New("invalid Service: empty Service Name")
}
if service.Image == "" {
return fmt.Errorf("invalid Service %q: empty Image name", service.Name)
}
if strings.ContainsAny(service.Name, "/_. !@£$%^&*(){};`~,?") {
return errors.New("unit names should not contain special characters")
}
if len(service.Ports) > 0 {
for _, p := range service.Ports {
ps := strings.Split(p, ":")
if len(ps) < 2 || len(ps) > 2 || ps[0] == "" || ps[1] == "" {
return fmt.Errorf("invalid port forwarding definition %q. Appropriate format is UNIT_PORT:HOST_PORT", p)
}
}
}
return nil
}
// Merges two Service structs, prioritizing the values present in first struct
func (s *Service) Merge(service *Service) {
if s.Name == "" {
s.Name = service.Name
}
if s.Image == "" {
s.Image = service.Image
}
if s.Version == "" {
s.Version = service.Version
}
if s.Profile == "" {
s.Profile = service.Profile
}
if s.Network == "" {
s.Network = service.Network
}
if s.Storage == "" {
s.Storage = service.Storage
}
if s.Docker == "" {
s.Docker = service.Docker
}
if s.IP == "" {
s.IP = service.IP
}
if len(s.Ports) == 0 {
s.Ports = append(s.Ports, service.Ports...)
}
if s.Resources.CPU == "" {
s.Resources.CPU = service.Resources.CPU
}
if s.Resources.GPU == "" {
s.Resources.GPU = service.Resources.GPU
}
if s.Resources.RAM == "" {
s.Resources.RAM = service.Resources.RAM
}
if s.Resources.Disk == "" {
s.Resources.Disk = service.Resources.Disk
}
if len(s.Postdeploy.Copy) == 0 {
s.Postdeploy.Copy = append(s.Postdeploy.Copy, service.Postdeploy.Copy...)
}
if len(s.Postdeploy.Run) == 0 {
s.Postdeploy.Run = append(s.Postdeploy.Run, service.Postdeploy.Run...)
}
}
// GetBravefileFromGitHub reads bravefile from a github URL
func GetBravefileFromGitHub(name string) (*Bravefile, error) {
var bravefile Bravefile
var baseConfig string
path := strings.SplitN(name, "/", -1)
if len(path) <= 3 {
return nil, fmt.Errorf("failed to retrieve image %q from github", name)
}
user := path[1]
repository := path[2]
project := strings.Join(path[3:], "/")
url := "https://raw.githubusercontent.com/" + user + "/" + repository + "/master/" + project + "/Bravefile"
response, err := http.Get(url)
if err != nil {
return nil, err
}
if response.StatusCode == http.StatusOK {
buf := new(bytes.Buffer)
buf.ReadFrom(response.Body)
baseConfig = buf.String()
}
if len(baseConfig) == 0 {
return nil, errors.New("unable to download valid Bravefile. Check your URL")
}
err = yaml.Unmarshal([]byte(baseConfig), &bravefile)
if err != nil {
return nil, err
}
return &bravefile, nil
}