-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage.go
373 lines (306 loc) · 9.75 KB
/
package.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"os/exec"
"strings"
)
// fomConfig contains all configuration needed to create a package using fpm
type FPMConfig struct {
Packages []struct {
// the name of the target package
Name string
// section Source of the fpm config
// defines where and how to source the contents of the package
Source struct {
// source mode specifies how to gather the files contained in the package
//
// "dir":
// use mode dir to source files from a local directory
// a valid configuration using "dir" needs at least one argument containing a path
//
// Mode is REQUIRED
Mode string `yaml:"mode"`
// Excludes is used with mode "dir"
// paths to files that are explicitly not part of the packages source files
Excludes []string `yaml:"excludes"`
Chdir string `yaml:"chdir"`
} `yaml:"source"`
// section Target of the fpm config
Target struct {
// Mode specifies the kind of package to create *REQUIRED*
//
// "deb":
// use mode "deb" to create a debian package
// a valid configuration using "deb" needs flags "name"
Mode string `yaml:"mode"`
// package Version *REQUIRED*
Version string `yaml:"version"`
// package architecture - defaults to local architecture of whatever machine is building the package
Architecture string `yaml:"architecture"`
// Maintainer of the package *OPTIONAL*
// should be an email address
Maintainer string `yaml:"maintainer"`
// Vendor of the package *OPTIONAL*
Vendor string `yaml:"vendor"`
// project URL *OPTIONAL*
// will be displayed in the packages metadata alongside the description
URL string `yaml:"url"`
License string `yaml:"license"`
Description string `yaml:"description"`
Provides []string `yaml:"provides"`
// special file tags
Directories []string `yaml:"directories"`
ConfigFiles []string `yaml:"config_files"`
Systemd []string `yaml:"systemd"`
// dependency management
Depends []string `yaml:"depends"`
Suggests []string `yaml:"suggests"`
NoAutoDepends bool `yaml:"no_auto_depends"`
Conflicts []string `yaml:"conflicts"`
// script tags
BeforeInstall string `yaml:"before_install"`
AfterInstall string `yaml:"after_install"`
BeforeRemove string `yaml:"before_remove"`
AfterRemove string `yaml:"after_remove"`
BeforeUpgrade string `yaml:"before_upgrade"`
AfterUpgrade string `yaml:"after_upgrade"`
SystemdEnable bool `yaml:"systemd_enable"`
SystemdAutoStart bool `yaml:"systemd_auto_start"`
SystemdRestartAfterUpgrade bool `yaml:"systemd_restart_after_upgrade"`
}
Paths []string `yaml:"paths"`
}
}
// function readFile accepts a file path and reads the fpm configuration from that file
func (c *FPMConfig) ReadFile(path string) error {
// read the file from disk
fileContents, err := ioutil.ReadFile(path)
// use ExpandEnv and attempt to insert ${ENVIRONMENT_VARIABLES}
fileContents = []byte(os.ExpandEnv(string(fileContents)))
if err != nil {
return err
}
if err := yaml.Unmarshal(fileContents, c); err != nil {
return err
}
return nil
}
// function contains decides if a given slice contains a given string
// arguments are named h (for haystack) and n (for needle)
// this function is not provided by golang and will be used in the check function below
func contains(h []string, n string) bool {
for _, s := range h {
// test each string in h for equality
if s == n {
// return true if a match is found
return true
}
}
// return false since no string in h matched n
return false
}
// configError to provide structure to the output of the check method
type ConfigError struct {
packageEntry string
field string
message string
}
// method Error provides a message for the ConfigError (and implements the Error interface)
func (c ConfigError) Error() string {
return fmt.Sprintf("error in package %s:\n -> config field %s missing or invalid\n -> %s\n",
c.packageEntry, c.field, c.message)
}
// method check to validate the fpm config
//
// if no error is returned the config is valid
// if there is an error it indicates what exactly is wrong with the config
func (c *FPMConfig) check() error {
if len(c.Packages) == 0 {
fmt.Print("packages.yml specifies no packages to build\n")
}
// check all packages
for i, p := range c.Packages {
// every package needs a name
if p.Name == "" {
return ConfigError{
field: fmt.Sprintf("package[%d].name", i),
message: "name is required",
}
}
// check if source mode is set to a valid mode
validSourceModes := []string{"dir"}
if !contains(validSourceModes, p.Source.Mode) {
return ConfigError{
packageEntry: p.Name,
field: "source.mode",
message: fmt.Sprintf(
"source mode is required and may contain %s", strings.Join(validSourceModes, "|")),
}
}
// checks for source mode "dir"
if p.Source.Mode == "dir" {
// check whether directories were provided
if len(p.Paths) < 1 && p.Source.Chdir == "" {
return ConfigError{
packageEntry: p.Name,
field: "paths",
message: "for mode dir it is required to specify a list of file paths (package.paths) or a chdir (package.source.chdir)",
}
}
}
// check if target mode is set to a valid mode
validTargetModes := []string{"deb"}
if !contains(validTargetModes, p.Target.Mode) {
return ConfigError{
packageEntry: p.Name,
field: "target.mode",
message: fmt.Sprintf(
"target mode is required and may contain %s", strings.Join(validSourceModes, "|")),
}
}
// checks for target mode "deb"
if p.Target.Mode == "deb" {
if p.Target.Version == "" {
return ConfigError{
packageEntry: p.Name,
field: "target.version",
message: "debian packages require a version",
}
}
}
}
return nil
}
// method build will create the packages as specified in packages.yml
func (c *FPMConfig) build() error {
for _, p := range c.Packages {
fmt.Printf("building package %s...\n", p.Name)
// set flags that are always required
args := []string{
"-s", p.Source.Mode,
"-t", p.Target.Mode,
}
// set version from file
args = append(args, "-v", p.Target.Version)
// special flags for the "dir" source mode
if p.Source.Mode == "dir" {
// append all exclude patterns to the command
for _, e := range p.Source.Excludes {
args = append(args, fmt.Sprintf("-x %s", e))
}
if p.Source.Chdir != "" {
args = append(args, "-C", p.Source.Chdir)
}
}
// special flags for the "deb" target mode
if p.Target.Mode == "deb" {
// set package name
args = append(args, "-n", p.Name)
// metadata flags
if p.Target.Maintainer != "" {
args = append(args, "-m", p.Target.Maintainer)
}
if p.Target.URL != "" {
args = append(args, "--url", p.Target.URL)
}
if p.Target.Vendor != "" {
args = append(args, "--vendor", p.Target.Vendor)
}
if p.Target.License != "" {
args = append(args, "--license", p.Target.License)
}
if p.Target.Description != "" {
args = append(args, "--description", p.Target.Description)
}
// tag important files
for _, d := range p.Target.Directories {
args = append(args, "--directories", d)
}
for _, c := range p.Target.ConfigFiles {
args = append(args, "--config-files", c)
}
for _, s := range p.Target.Systemd {
args = append(args, "--deb-systemd", s)
}
if p.Target.Architecture != "" {
args = append(args, "-a", p.Target.Architecture)
}
// append dependencies, suggests and conflicts
for _, d := range p.Target.Depends {
args = append(args, "-d", d)
}
for _, p := range p.Target.Provides {
args = append(args, "--provides", p)
}
for _, s := range p.Target.Suggests {
args = append(args, "--deb-suggests", s)
}
for _, c := range p.Target.Conflicts {
args = append(args, "--conflicts", c)
}
// add scripts
if p.Target.BeforeInstall != "" {
args = append(args, "--before-install", p.Target.BeforeInstall)
}
if p.Target.AfterInstall != "" {
args = append(args, "--after-install", p.Target.AfterInstall)
}
if p.Target.BeforeRemove != "" {
args = append(args, "--before-remove", p.Target.BeforeRemove)
}
if p.Target.AfterRemove != "" {
args = append(args, "--after-remove", p.Target.AfterRemove)
}
if p.Target.BeforeUpgrade != "" {
args = append(args, "--before-upgrade", p.Target.BeforeUpgrade)
}
if p.Target.AfterUpgrade != "" {
args = append(args, "--after-upgrade", p.Target.AfterUpgrade)
}
// handle systemd units
if p.Target.SystemdEnable == true {
args = append(args, "--deb-systemd-enable")
}
if p.Target.SystemdAutoStart == true {
args = append(args, "--deb-systemd-auto-start")
}
if p.Target.SystemdRestartAfterUpgrade == true {
args = append(args, "--deb-systemd-restart-after-upgrade")
}
}
// append arguments
for _, a := range p.Paths {
args = append(args, a)
}
fmt.Printf("%s %s", "fpm", strings.Join(args, " "))
// create the actual command
buildCommand := exec.Command("fpm", args...)
output, err := buildCommand.CombinedOutput()
fmt.Printf(string(output))
// exit with non-zero exit code in case the fpm command fails
if err != nil {
fmt.Printf("FPM command failed\n")
os.Exit(2)
}
// print newlines to separate next package
fmt.Printf("\n\n")
}
return nil
}
// main method
func main() {
c := FPMConfig{}
if err := c.ReadFile("packages.yml"); err != nil {
fmt.Printf(err.Error())
}
if err := c.check(); err != nil {
fmt.Printf(err.Error())
os.Exit(1)
}
if err := c.build(); err != nil {
fmt.Printf(err.Error())
}
}