Skip to content

Commit

Permalink
Merge pull request #29 from funbox/develop
Browse files Browse the repository at this point in the history
Version 0.13.0.1
  • Loading branch information
andyone authored Apr 25, 2017
2 parents ef1ce16 + 3c3b749 commit f411f71
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 76 deletions.
7 changes: 5 additions & 2 deletions common/init-exporter.spec
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
Summary: Utility for exporting services described by Procfile to init system
Name: init-exporter
Version: 0.13.0
Release: 0%{?dist}
Release: 1%{?dist}
Group: Development/Tools
License: MIT
URL: https://github.com/funbox/init-exporter
Expand All @@ -69,7 +69,7 @@ Utility for exporting services described by Procfile to init system.
%package converter

Summary: Utility for converting procfiles from v1 to v2 format
Version: 0.5.0
Version: 0.6.0
Release: 0%{?dist}

%description converter
Expand Down Expand Up @@ -132,6 +132,9 @@ rm -rf %{buildroot}
###############################################################################

%changelog
* Mon Apr 24 2017 Anton Novojilov <[email protected]> - 0.13.0-1
- [init-exporter-converter] Replaced text/template by basic procfile rendering

* Mon Apr 24 2017 Anton Novojilov <[email protected]> - 0.13.0-0
- ek package updated to v8
- Improved v2 format validation
Expand Down
134 changes: 60 additions & 74 deletions converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ package converter
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"runtime"
"text/template"

"pkg.re/essentialkaos/ek.v8/arg"
"pkg.re/essentialkaos/ek.v8/fmtc"
Expand All @@ -26,7 +25,7 @@ import (
// App props
const (
APP = "init-exporter-converter"
VER = "0.5.0"
VER = "0.6.0"
DESC = "Utility for converting procfiles from v1 to v2 format"
)

Expand Down Expand Up @@ -55,61 +54,12 @@ const (

// ////////////////////////////////////////////////////////////////////////////////// //

// PROCFILE_TEMPLATE is template used for generation v2 Procfile
const PROCFILE_TEMPLATE = `version: 2
start_on_runlevel: 2
stop_on_runlevel: 5
{{ if .Config.IsRespawnEnabled -}}
respawn:
count: {{ .Config.RespawnCount }}
interval: {{ .Config.RespawnInterval }}
{{ end -}}
limits:
nofile: {{ .Config.LimitFile }}
nproc: {{ .Config.LimitProc }}
{{ if not .HasCustomWorkingDirs -}}
working_directory: {{ .Config.WorkingDir }}
{{ end -}}
{{- $hasCustomWorkingDirs := .HasCustomWorkingDirs -}}
commands:
{{- range .Application.Services }}
{{ .Name }}:
{{ if .HasPreCmd -}}
pre: {{ .PreCmd }}
{{ end -}}
command: {{ .Cmd }}
{{ if .HasPostCmd -}}
post: {{ .PostCmd }}
{{ end -}}
{{ if $hasCustomWorkingDirs -}}
working_directory: {{ .Options.WorkingDir }}
{{ end -}}
{{ if .Options.IsCustomLogEnabled -}}
log: {{ .Options.LogPath }}
{{ end -}}
{{ if .Options.IsEnvSet -}}
env:
{{- range $k, $v := .Options.Env }}
{{ $k }}: {{ $v -}}
{{ end }}
{{ end -}}
{{ end -}}
`

// DEFAULT_WORKING_DIR is path to default working dir
const DEFAULT_WORKING_DIR = "/tmp"

// ////////////////////////////////////////////////////////////////////////////////// //

type templateData struct {
type procData struct {
Config *procfile.Config
Application *procfile.Application
HasCustomWorkingDirs bool
Expand Down Expand Up @@ -207,41 +157,77 @@ func convert(file string) error {

validateApplication(app)

v2data, err := renderTemplate(
"proc_v2", PROCFILE_TEMPLATE,
&templateData{config, app, hasCustomWorkingDirs},
)

if err != nil {
return err
}
data := renderProcfile(&procData{config, app, hasCustomWorkingDirs})

if !arg.GetB(ARG_IN_PLACE) {
fmtc.Println(v2data)
fmt.Printf(data)
return nil
}

return writeData(file, v2data)
return writeData(file, data)
}

// renderTemplate renders template data
func renderTemplate(name, templateData string, data interface{}) (string, error) {
templ, err := template.New(name).Parse(templateData)
// renderProcfile render 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"
}

if err != nil {
return "", fmtc.Errorf("Can't render template: %v", err)
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"
}

var buffer bytes.Buffer
result += "commands:\n"

ct := template.Must(templ, nil)
err = ct.Execute(&buffer, data)
for _, service := range data.Application.Services {
result += " " + service.Name + "\n"

if err != nil {
return "", fmtc.Errorf("Can't render template: %v", err)
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 buffer.String(), nil
return result
}

// getWorkingDir return path to default working dir and flag
Expand Down

0 comments on commit f411f71

Please sign in to comment.