Skip to content

Commit

Permalink
Merge pull request #19 from funbox/develop
Browse files Browse the repository at this point in the history
Version 0.11.0
  • Loading branch information
andyone authored Apr 7, 2017
2 parents e45c3d9 + 1509273 commit 7198532
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 67 deletions.
52 changes: 34 additions & 18 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
// App props
const (
APP = "init-exporter"
VER = "0.10.0"
VER = "0.11.0"
DESC = "Utility for exporting services described by Procfile to init system"
)

Expand Down Expand Up @@ -152,13 +152,11 @@ func checkForRoot() {
user, err = system.CurrentUser()

if err != nil {
printError(err.Error())
os.Exit(1)
printErrorAndExit(err.Error())
}

if !user.IsRoot() {
printError("This utility must have superuser privileges (root)")
os.Exit(1)
printErrorAndExit("This utility must have superuser privileges (root)")
}
}

Expand Down Expand Up @@ -277,7 +275,7 @@ func validateConfig() {
printError("Errors while config validation:")

for _, err := range errs {
printError(" %v\n", err)
printError(" - %v", err)
}

os.Exit(1)
Expand Down Expand Up @@ -328,15 +326,7 @@ func installApplication(appName string) {
printErrorAndExit(err.Error())
}

if app.ProcVersion == 1 && !knf.GetB(PROCFILE_VERSION1, true) {
printError("Proc format version 1 support is disabled")
os.Exit(1)
}

if app.ProcVersion == 2 && !knf.GetB(PROCFILE_VERSION2, true) {
printError("Proc format version 2 support is disabled")
os.Exit(1)
}
validateApplication(app)

if arg.GetB(ARG_DRY_START) {
os.Exit(0)
Expand All @@ -345,8 +335,9 @@ func installApplication(appName string) {
err = getExporter().Install(app)

if err == nil {
log.Aux("User %s (%d) installed service %s", user.RealName, user.RealUID, app.Name)
log.Info("User %s (%d) installed service %s", user.RealName, user.RealUID, app.Name)
} else {
log.Error(err.Error())
printErrorAndExit(err.Error())
}
}
Expand All @@ -359,12 +350,38 @@ func uninstallApplication(appName string) {
err := getExporter().Uninstall(app)

if err == nil {
log.Aux("User %s (%d) uninstalled service %s", user.RealName, user.RealUID, app.Name)
log.Info("User %s (%d) uninstalled service %s", user.RealName, user.RealUID, app.Name)
} else {
log.Error(err.Error())
printErrorAndExit(err.Error())
}
}

// validateApplication validate application and all services
func validateApplication(app *procfile.Application) {
if app.ProcVersion == 1 && !knf.GetB(PROCFILE_VERSION1, true) {
printErrorAndExit("Proc format version 1 support is disabled")
}

if app.ProcVersion == 2 && !knf.GetB(PROCFILE_VERSION2, true) {
printErrorAndExit("Proc format version 2 support is disabled")
}

errs := app.Validate()

if len(errs) == 0 {
return
}

printError("Errors while application validation:")

for _, err := range errs {
printError(" - %v", err)
}

os.Exit(1)
}

// checkProviderTargetDir check permissions on target dir
func checkProviderTargetDir(dir string) error {
if !fsutil.CheckPerms("DRWX", dir) {
Expand Down Expand Up @@ -436,7 +453,6 @@ func printWarn(f string, a ...interface{}) {

// printErrorAndExit print error mesage and exit with exit code 1
func printErrorAndExit(f string, a ...interface{}) {
log.Crit(f, a...)
printError(f, a...)
os.Exit(1)
}
Expand Down
10 changes: 8 additions & 2 deletions common/init-exporter.spec
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

Summary: Utility for exporting services described by Procfile to init system
Name: init-exporter
Version: 0.10.0
Version: 0.11.0
Release: 0%{?dist}
Group: Development/Tools
License: MIT
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.3.0
Version: 0.4.0
Release: 0%{?dist}

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

%changelog
* Fri Apr 07 2017 Anton Novojilov <[email protected]> - 0.11.0-0
- Added application validation before installation
- [converter] Added application validation before procfile converting
- [converter] Improved converter for better support of procfiles for local run
- Code refactoring

* Tue Apr 04 2017 Anton Novojilov <[email protected]> - 0.10.0-0
- Added kill signal definition feature for v2 and all exporters
- Added reload signal definition feature for v2 and all exporters
Expand Down
35 changes: 30 additions & 5 deletions converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// App props
const (
APP = "init-exporter-converter"
VER = "0.3.0"
VER = "0.4.0"
DESC = "Utility for converting procfiles from v1 to v2 format"
)

Expand Down Expand Up @@ -104,6 +104,9 @@ commands:
{{ end -}}
`

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

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

type templateData struct {
Expand Down Expand Up @@ -202,6 +205,8 @@ func convert(file string) error {

config.WorkingDir, hasCustomWorkingDirs = getWorkingDir(app)

validateApplication(app)

v2data, err := renderTemplate(
"proc_v2", PROCFILE_TEMPLATE,
&templateData{config, app, hasCustomWorkingDirs},
Expand Down Expand Up @@ -242,22 +247,42 @@ func renderTemplate(name, templateData string, data interface{}) (string, error)
// getWorkingDir return path to default working dir and flag
// if custom working dirs is used
func getWorkingDir(app *procfile.Application) (string, bool) {
var dir = ""
var dir = DEFAULT_WORKING_DIR

for _, service := range app.Services {
if dir == "" {
dir = service.Options.WorkingDir
if dir == DEFAULT_WORKING_DIR {
if service.Options.WorkingDir != "" {
dir = service.Options.WorkingDir
}

continue
}

if dir != service.Options.WorkingDir {
return "/tmp", true
return DEFAULT_WORKING_DIR, true
}
}

return dir, false
}

// validateApplication validate application and all services
func validateApplication(app *procfile.Application) {
errs := app.Validate()

if len(errs) == 0 {
return
}

printError("Errors while application validation:")

for _, err := range errs {
printError(" - %v", err)
}

os.Exit(1)
}

// writeData write procfile data to file
func writeData(file, data string) error {
return ioutil.WriteFile(file, []byte(data), 0644)
Expand Down
11 changes: 7 additions & 4 deletions export/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,14 @@ func (s *ExportSuite) TestUpstartExport(c *C) {
c.Assert(service1Helper[4:], DeepEquals,
[]string{
"[[ -r /etc/profile.d/rbenv.sh ]] && source /etc/profile.d/rbenv.sh", "",
"cd /srv/service/service1-dir && exec env STAGING=true /bin/echo 'service1:pre' &>>/srv/service/service1-dir/log/service1.log && exec env STAGING=true /bin/echo 'service1' &>>/srv/service/service1-dir/log/service1.log && exec env STAGING=true /bin/echo 'service1:post' &>>/srv/service/service1-dir/log/service1.log",
"cd /srv/service/service1-dir && exec env \"STAGING=true\" /bin/echo 'service1:pre' &>>/srv/service/service1-dir/log/service1.log && exec env \"STAGING=true\" /bin/echo 'service1' &>>/srv/service/service1-dir/log/service1.log && exec env \"STAGING=true\" /bin/echo 'service1:post' &>>/srv/service/service1-dir/log/service1.log",
""},
)

c.Assert(service2Helper[4:], DeepEquals,
[]string{
"[[ -r /etc/profile.d/rbenv.sh ]] && source /etc/profile.d/rbenv.sh", "",
"cd /srv/service/working-dir && exec /bin/echo 'service2'",
"cd /srv/service/working-dir && exec env $(cat /srv/service/working-dir/shared/env.vars | xargs) /bin/echo 'service2'",
""},
)

Expand Down Expand Up @@ -323,7 +323,8 @@ func (s *ExportSuite) TestSystemdExport(c *C) {
"User=service",
"Group=service",
"WorkingDirectory=/srv/service/service1-dir",
"Environment=STAGING=true",
"Environment=\"STAGING=true\"",
"",
fmt.Sprintf("ExecStart=/bin/bash %s/test_application-service1.sh &>>/var/log/test_application/service1.log", helperDir),
"",
""},
Expand Down Expand Up @@ -357,6 +358,7 @@ func (s *ExportSuite) TestSystemdExport(c *C) {
"Group=service",
"WorkingDirectory=/srv/service/working-dir",
"",
"EnvironmentFile=/srv/service/working-dir/shared/env.vars",
fmt.Sprintf("ExecStart=/bin/bash %s/test_application-service2.sh &>>/var/log/test_application/service2.log", helperDir),
"ExecReload=/bin/kill -SIGUSR2 $MAINPID",
""},
Expand Down Expand Up @@ -410,7 +412,7 @@ func createTestApp(helperDir, targetDir string) *procfile.Application {
Options: &procfile.ServiceOptions{
Env: map[string]string{"STAGING": "true"},
WorkingDir: "/srv/service/service1-dir",
LogPath: "log/service1.log",
LogFile: "log/service1.log",
KillTimeout: 10,
KillSignal: "SIGQUIT",
Count: 2,
Expand All @@ -426,6 +428,7 @@ func createTestApp(helperDir, targetDir string) *procfile.Application {
Cmd: "/bin/echo 'service2'",
Application: app,
Options: &procfile.ServiceOptions{
EnvFile: "shared/env.vars",
WorkingDir: "/srv/service/working-dir",
ReloadSignal: "SIGUSR2",
IsRespawnEnabled: true,
Expand Down
1 change: 1 addition & 0 deletions export/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ User={{.Application.User}}
Group={{.Application.Group}}
WorkingDirectory={{.Service.Options.WorkingDir}}
{{ if .Service.Options.IsEnvSet }}Environment={{.Service.Options.EnvString}}{{ end }}
{{ if .Service.Options.IsEnvFileSet }}EnvironmentFile={{.Service.Options.FullEnvFilePath}}{{ end }}
ExecStart=/bin/bash {{.Service.HelperPath}} &>>/var/log/{{.Application.Name}}/{{.Service.Name}}.log
{{ if .Service.Options.IsReloadSignalSet }}ExecReload=/bin/kill -{{.Service.Options.ReloadSignal}} $MAINPID{{ end }}
`
Expand Down
Loading

0 comments on commit 7198532

Please sign in to comment.