-
Notifications
You must be signed in to change notification settings - Fork 0
/
init-exporter-converter.go
417 lines (324 loc) · 9.92 KB
/
init-exporter-converter.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package main
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2006-2023 FUNBOX //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"fmt"
"os"
"runtime"
"github.com/essentialkaos/ek/v12/fmtc"
"github.com/essentialkaos/ek/v12/knf"
"github.com/essentialkaos/ek/v12/options"
"github.com/essentialkaos/ek/v12/terminal/tty"
"github.com/essentialkaos/ek/v12/usage"
"github.com/essentialkaos/ek/v12/usage/completion/bash"
"github.com/essentialkaos/ek/v12/usage/completion/fish"
"github.com/essentialkaos/ek/v12/usage/completion/zsh"
"github.com/essentialkaos/ek/v12/usage/man"
"github.com/essentialkaos/ek/v12/usage/update"
"github.com/essentialkaos/go-simpleyaml/v2"
"github.com/funbox/init-exporter/procfile"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// App props
const (
APP = "init-exporter-converter"
VER = "0.12.1"
DESC = "Utility for converting procfiles from v1 to v2 format"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// Supported arguments
const (
OPT_CONFIG = "c:config"
OPT_IN_PLACE = "i:in-place"
OPT_NO_COLOR = "nc:no-color"
OPT_HELP = "h:help"
OPT_VER = "v:version"
OPT_COMPLETION = "completion"
OPT_GENERATE_MAN = "generate-man"
)
// Config properties
const (
MAIN_PREFIX = "main:prefix"
PATHS_WORKING_DIR = "paths:working-dir"
DEFAULTS_NPROC = "defaults:nproc"
DEFAULTS_NOFILE = "defaults:nofile"
DEFAULTS_RESPAWN = "defaults:respawn"
DEFAULTS_RESPAWN_COUNT = "defaults:respawn-count"
DEFAULTS_RESPAWN_INTERVAL = "defaults:respawn-interval"
DEFAULTS_KILL_TIMEOUT = "defaults:kill-timeout"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// DEFAULT_WORKING_DIR is path to default working dir
const DEFAULT_WORKING_DIR = "/tmp"
// ////////////////////////////////////////////////////////////////////////////////// //
type procData struct {
Config *procfile.Config
Application *procfile.Application
HasCustomWorkingDirs bool
}
// ////////////////////////////////////////////////////////////////////////////////// //
var optMap = options.Map{
OPT_CONFIG: {},
OPT_IN_PLACE: {Type: options.BOOL},
OPT_NO_COLOR: {Type: options.BOOL},
OPT_HELP: {Type: options.BOOL},
OPT_VER: {Type: options.BOOL},
OPT_COMPLETION: {},
OPT_GENERATE_MAN: {Type: options.BOOL},
}
var colorTagApp string
var colorTagVer string
var gitrev string
// ////////////////////////////////////////////////////////////////////////////////// //
func main() {
runtime.GOMAXPROCS(1)
preConfigureUI()
args, errs := options.Parse(optMap)
if len(errs) != 0 {
printError(errs[0].Error())
os.Exit(1)
}
configureUI()
switch {
case options.Has(OPT_COMPLETION):
os.Exit(printCompletion())
case options.Has(OPT_GENERATE_MAN):
printMan()
os.Exit(0)
case options.GetB(OPT_VER):
genAbout(gitrev).Print()
os.Exit(0)
case options.GetB(OPT_HELP) || len(args) == 0:
genUsage().Print()
os.Exit(0)
}
process(args.Strings())
}
// preConfigureUI preconfigures UI based on information about user terminal
func preConfigureUI() {
if !tty.IsTTY() {
fmtc.DisableColors = true
}
}
// configureUI configures user interface
func configureUI() {
if options.GetB(OPT_NO_COLOR) {
fmtc.DisableColors = true
}
switch {
case fmtc.IsTrueColorSupported():
colorTagApp, colorTagVer = "{#BCCF00}", "{#BCCF00}"
case fmtc.Is256ColorsSupported():
colorTagApp, colorTagVer = "{#148}", "{#148}"
default:
colorTagApp, colorTagVer = "{g}", "{g}"
}
}
// process starts data processing
func process(files []string) {
var err error
var hasErrors bool
if options.Has(OPT_CONFIG) {
err = knf.Global(options.GetS(OPT_CONFIG))
if err != nil {
printErrorAndExit(err.Error())
}
}
for _, file := range files {
err = convert(file)
if err != nil {
hasErrors = true
printError(err.Error())
}
}
if hasErrors {
os.Exit(1)
}
}
// convert reads procfile in v1 format and print v2 data or save it to file
func convert(file string) error {
var hasCustomWorkingDirs bool
config := &procfile.Config{
Name: "",
WorkingDir: knf.GetS(PATHS_WORKING_DIR, "/tmp"),
IsRespawnEnabled: knf.GetB(DEFAULTS_RESPAWN, true),
RespawnInterval: knf.GetI(DEFAULTS_RESPAWN_INTERVAL, 15),
RespawnCount: knf.GetI(DEFAULTS_RESPAWN_COUNT, 10),
KillTimeout: knf.GetI(DEFAULTS_KILL_TIMEOUT, 60),
LimitFile: knf.GetI(DEFAULTS_NOFILE, 10240),
LimitProc: knf.GetI(DEFAULTS_NPROC, 10240),
}
app, err := procfile.Read(file, config)
if err != nil {
return err
}
if app.ProcVersion != 1 {
return fmt.Errorf("Given procfile already converted to v2 format")
}
config.WorkingDir, hasCustomWorkingDirs = getWorkingDir(app)
err = validateApplication(app)
if err != nil {
return err
}
yamlData := renderProcfile(&procData{config, app, hasCustomWorkingDirs})
err = validateYaml(yamlData)
if err != nil {
return fmt.Errorf("Can't convert given procfile to YAML: %v", err)
}
if !options.GetB(OPT_IN_PLACE) {
fmt.Print(yamlData)
return nil
}
return writeData(file, yamlData)
}
// renderProcfile renders procfile
func renderProcfile(data *procData) string {
var result string
result += "version: 2\n"
result += "\n"
result += "start_on_runlevel: 2\n"
result += "stop_on_runlevel: 5\n"
result += "\n"
if data.Config.IsRespawnEnabled {
result += "respawn:\n"
result += fmt.Sprintf(" count: %d\n", data.Config.RespawnCount)
result += fmt.Sprintf(" interval: %d\n", data.Config.RespawnInterval)
result += "\n"
}
result += "limits:\n"
result += fmt.Sprintf(" nofile: %d\n", data.Config.LimitFile)
result += fmt.Sprintf(" nproc: %d\n", data.Config.LimitProc)
result += "\n"
if !data.HasCustomWorkingDirs {
result += "working_directory: " + data.Config.WorkingDir + "\n"
result += "\n"
}
result += "commands:\n"
for _, service := range data.Application.Services {
result += " " + service.Name + ":\n"
if service.HasPreCmd() {
result += " pre: " + service.PreCmd + "\n"
}
result += " command: " + service.Cmd + "\n"
if service.HasPostCmd() {
result += " post: " + service.PostCmd + "\n"
}
if data.HasCustomWorkingDirs {
result += " working_directory: " + service.Options.WorkingDir + "\n"
}
if service.Options.IsCustomLogEnabled() {
result += " log: " + service.Options.LogFile + "\n"
}
if service.Options.IsEnvSet() {
result += " env:\n"
for k, v := range service.Options.Env {
result += fmt.Sprintf(" %s: %s\n", k, v)
}
}
result += "\n"
}
return result
}
// getWorkingDir returns path to default working dir and flag
// if custom working dirs is used
func getWorkingDir(app *procfile.Application) (string, bool) {
var dir = DEFAULT_WORKING_DIR
for _, service := range app.Services {
if dir == DEFAULT_WORKING_DIR {
if service.Options.WorkingDir != "" {
dir = service.Options.WorkingDir
}
continue
}
if dir != service.Options.WorkingDir {
return DEFAULT_WORKING_DIR, true
}
}
return dir, false
}
// validateApplication validates application and all services
func validateApplication(app *procfile.Application) error {
errs := app.Validate()
if len(errs) == 0 {
return nil
}
return errs[0]
}
// validateYaml validates rendered yaml
func validateYaml(data string) error {
_, err := simpleyaml.NewYaml([]byte(data))
return err
}
// writeData writes procfile data to file
func writeData(file, data string) error {
return os.WriteFile(file, []byte(data), 0644)
}
// printError prints error message to console
func printError(f string, a ...interface{}) {
fmtc.Fprintf(os.Stderr, "{r}"+f+"{!}\n", a...)
}
// printErrorAndExit print error message and exit with exit code 1
func printErrorAndExit(f string, a ...interface{}) {
printError(f, a...)
os.Exit(1)
}
// ////////////////////////////////////////////////////////////////////////////////// //
// printCompletion prints completion for given shell
func printCompletion() int {
info := genUsage()
switch options.GetS(OPT_COMPLETION) {
case "bash":
fmt.Print(bash.Generate(info, "init-exporter-converter"))
case "fish":
fmt.Print(fish.Generate(info, "init-exporter-converter"))
case "zsh":
fmt.Print(zsh.Generate(info, optMap, "init-exporter-converter"))
default:
return 1
}
return 0
}
// printMan prints man page
func printMan() {
fmt.Println(man.Generate(genUsage(), genAbout("")))
}
// genUsage generates usage info
func genUsage() *usage.Info {
info := usage.NewInfo("", "procfile…")
info.AppNameColorTag = "{*}" + colorTagApp
info.AddOption(OPT_CONFIG, "Path to init-exporter config", "file")
info.AddOption(OPT_IN_PLACE, "Edit procfile in place")
info.AddOption(OPT_NO_COLOR, "Disable colors in output")
info.AddOption(OPT_HELP, "Show this help message")
info.AddOption(OPT_VER, "Show version")
info.AddExample(
"-i config/Procfile.production",
"Convert Procfile.production to version 2 in-place",
)
info.AddExample(
"-c /etc/init-exporter.conf Procfile.*",
"Convert all procfiles to version 2 with defaults from init-exporter config and print result to console",
)
return info
}
// genAbout generates info about version
func genAbout(gitRev string) *usage.About {
about := &usage.About{
App: APP,
Version: VER,
Desc: DESC,
Year: 2006,
Owner: "FunBox",
License: "MIT License",
UpdateChecker: usage.UpdateChecker{"funbox/init-exporter-converter", update.GitHubChecker},
AppNameColorTag: "{*}" + colorTagApp,
VersionColorTag: colorTagVer,
}
if gitRev != "" {
about.Build = "git:" + gitRev
}
return about
}