From 85797e0d40e70d7db66c976e7cbd8636eafd3701 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Fri, 17 Feb 2017 07:30:14 -0500 Subject: [PATCH 1/5] Improved dockerfiles --- Dockerfile.systemd | 4 ++-- Dockerfile.upstart | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Dockerfile.systemd b/Dockerfile.systemd index e201461..aaf0239 100644 --- a/Dockerfile.systemd +++ b/Dockerfile.systemd @@ -3,10 +3,10 @@ FROM centos:centos7 ENV GOPATH /root ENV TARGET /root/src/github.com/funbox/init-exporter -RUN yum install -y http://release.yum.kaos.io/i386/kaos-repo-1.2.0-0.el6.noarch.rpm +RUN yum install -y https://yum.kaos.io/7/release/x86_64/kaos-repo-7.2-0.el7.noarch.rpm RUN yum clean all && yum -y update -RUN yum -y install make go +RUN yum -y install make golang COPY . $TARGET RUN ls $TARGET -al diff --git a/Dockerfile.upstart b/Dockerfile.upstart index e4eec8d..835e068 100644 --- a/Dockerfile.upstart +++ b/Dockerfile.upstart @@ -3,10 +3,10 @@ FROM centos:centos6 ENV GOPATH /root ENV TARGET /root/src/github.com/funbox/init-exporter -RUN yum install -y http://release.yum.kaos.io/i386/kaos-repo-1.2.0-0.el6.noarch.rpm +RUN yum install -y https://yum.kaos.io/6/release/i386/kaos-repo-7.2-0.el6.noarch.rpm RUN yum clean all && yum -y update -RUN yum -y install go +RUN yum -y install make golang COPY . $TARGET RUN ls $TARGET -al From 2fee5a7b00947f60226bfb3e128ae6368a9eee14 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 22 Feb 2017 05:25:12 -0500 Subject: [PATCH 2/5] Improved working with default values + fixed bugs in upstart exporter --- cli/cli.go | 74 +++++++++++++++++++++++---------------- common/init-exporter.conf | 18 ++++++++-- common/init-exporter.spec | 4 +-- export/export_test.go | 12 +++---- export/upstart.go | 8 ++--- procfile/procfile.go | 63 +++++++++++++++++++++------------ readme.md | 20 +++++++++++ 7 files changed, 130 insertions(+), 69 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index 3ff4d29..f864830 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -29,7 +29,7 @@ import ( // App props const ( APP = "init-exporter" - VER = "0.6.0" + VER = "0.7.0" DESC = "Utility for exporting services described by Procfile to init system" ) @@ -47,20 +47,24 @@ const ( // Config properies list const ( - MAIN_RUN_USER = "main:run-user" - MAIN_RUN_GROUP = "main:run-group" - MAIN_PREFIX = "main:prefix" - PATHS_WORKING_DIR = "paths:working-dir" - PATHS_HELPER_DIR = "paths:helper-dir" - PATHS_SYSTEMD_DIR = "paths:systemd-dir" - PATHS_UPSTART_DIR = "paths:upstart-dir" - LIMITS_NPROC = "limits:nproc" - LIMITS_NOFILE = "limits:nofile" - LOG_ENABLED = "log:enabled" - LOG_DIR = "log:dir" - LOG_FILE = "log:file" - LOG_PERMS = "log:perms" - LOG_LEVEL = "log:level" + MAIN_RUN_USER = "main:run-user" + MAIN_RUN_GROUP = "main:run-group" + MAIN_PREFIX = "main:prefix" + PATHS_WORKING_DIR = "paths:working-dir" + PATHS_HELPER_DIR = "paths:helper-dir" + PATHS_SYSTEMD_DIR = "paths:systemd-dir" + PATHS_UPSTART_DIR = "paths:upstart-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" + LOG_ENABLED = "log:enabled" + LOG_DIR = "log:dir" + LOG_FILE = "log:file" + LOG_PERMS = "log:perms" + LOG_LEVEL = "log:level" ) const ( @@ -237,11 +241,14 @@ func validateConfig() { {PATHS_HELPER_DIR, knf.Empty, nil}, {PATHS_SYSTEMD_DIR, knf.Empty, nil}, {PATHS_UPSTART_DIR, knf.Empty, nil}, - {LIMITS_NOFILE, knf.Empty, nil}, - {LIMITS_NPROC, knf.Empty, nil}, + {DEFAULTS_NPROC, knf.Empty, nil}, + {DEFAULTS_NOFILE, knf.Empty, nil}, + {DEFAULTS_RESPAWN_COUNT, knf.Empty, nil}, + {DEFAULTS_RESPAWN_INTERVAL, knf.Empty, nil}, + {DEFAULTS_KILL_TIMEOUT, knf.Empty, nil}, - {LIMITS_NOFILE, knf.Less, 0}, - {LIMITS_NPROC, knf.Less, 0}, + {DEFAULTS_RESPAWN_COUNT, knf.Less, 0}, + {DEFAULTS_RESPAWN_INTERVAL, knf.Less, 0}, {MAIN_RUN_USER, userChecker, nil}, {MAIN_RUN_GROUP, groupChecker, nil}, @@ -298,12 +305,16 @@ func installApplication(appName string) { app, err := procfile.Read( arg.GetS(ARG_PROCFILE), &procfile.Config{ - Name: fullAppName, - User: knf.GetS(MAIN_RUN_USER), - Group: knf.GetS(MAIN_RUN_GROUP), - WorkingDir: knf.GetS(PATHS_WORKING_DIR), - LimitFile: knf.GetI(LIMITS_NOFILE, 0), - LimitProc: knf.GetI(LIMITS_NPROC, 0), + Name: fullAppName, + User: knf.GetS(MAIN_RUN_USER), + Group: knf.GetS(MAIN_RUN_GROUP), + WorkingDir: knf.GetS(PATHS_WORKING_DIR), + IsRespawnEnabled: knf.GetB(DEFAULTS_RESPAWN, false), + RespawnInterval: knf.GetI(DEFAULTS_RESPAWN_INTERVAL), + RespawnCount: knf.GetI(DEFAULTS_RESPAWN_COUNT), + KillTimeout: knf.GetI(DEFAULTS_KILL_TIMEOUT, 0), + LimitFile: knf.GetI(DEFAULTS_NOFILE, 0), + LimitProc: knf.GetI(DEFAULTS_NPROC, 0), }, ) @@ -432,12 +443,13 @@ func showUsage() { // showAbout print version info to console func showAbout() { about := &usage.About{ - App: APP, - Version: VER, - Desc: DESC, - Year: 2006, - Owner: "FB Group", - License: "MIT License", + App: APP, + Version: VER, + Desc: DESC, + Year: 2006, + Owner: "FB Group", + License: "MIT License", + Repository: "funbox/init-exporter", } about.Render() diff --git a/common/init-exporter.conf b/common/init-exporter.conf index 3aeea10..f6f5027 100644 --- a/common/init-exporter.conf +++ b/common/init-exporter.conf @@ -25,14 +25,26 @@ # Path to directory with upstart configs upstart-dir: /etc/init -[limits] +[defaults] - # Number of Processes + # Number of Processes (0 - disabled) nproc: 10240 - # Number of File Descriptors + # Number of File Descriptors (0 - disabled) nofile: 10240 + # Enable or disable respawn by default here + respawn: true + + # Respawn count + respawn-count: 10 + + # Respawn interval + respawn-interval: 15 + + # Kill timeout (0 - disabled) + kill-timeout: 60 + [log] # Enable or disable logging here diff --git a/common/init-exporter.spec b/common/init-exporter.spec index da7d83c..87f110a 100644 --- a/common/init-exporter.spec +++ b/common/init-exporter.spec @@ -42,7 +42,7 @@ Summary: Utility for exporting services described by Procfile to init system Name: init-exporter -Version: 0.6.0 +Version: 0.7.0 Release: 0%{?dist} Group: Development/Tools License: MIT @@ -52,7 +52,7 @@ Source0: %{name}-%{version}.tar.gz BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) -BuildRequires: golang >= 1.5 +BuildRequires: golang >= 1.7 Provides: upstart-exporter = %{version}-%{release} Provides: systemd-exporter = %{version}-%{release} diff --git a/export/export_test.go b/export/export_test.go index 49a7d45..503a5ca 100644 --- a/export/export_test.go +++ b/export/export_test.go @@ -120,8 +120,8 @@ func (s *ExportSuite) TestUpstartExport(c *C) { c.Assert(appUnit[2:], DeepEquals, []string{ - "start on [3]", - "stop on [3]", + "start on runlevel [3]", + "stop on runlevel [3]", "", "pre-start script", "", @@ -137,8 +137,8 @@ func (s *ExportSuite) TestUpstartExport(c *C) { c.Assert(service1Unit[2:], DeepEquals, []string{ - "start on [3]", - "stop on [3]", + "start on starting test_application", + "stop on stopping test_application", "", "respawn", "respawn limit 15 25", @@ -159,8 +159,8 @@ func (s *ExportSuite) TestUpstartExport(c *C) { c.Assert(service2Unit[2:], DeepEquals, []string{ - "start on [3]", - "stop on [3]", + "start on starting test_application", + "stop on stopping test_application", "", "respawn", "", diff --git a/export/upstart.go b/export/upstart.go index 81167cb..dac573b 100644 --- a/export/upstart.go +++ b/export/upstart.go @@ -119,8 +119,8 @@ func (up *UpstartProvider) DisableService(appName string) error { func (up *UpstartProvider) RenderAppTemplate(app *procfile.Application) (string, error) { data := &upstartAppData{ Application: app, - StartLevel: fmt.Sprintf("[%d]", app.StartLevel), - StopLevel: fmt.Sprintf("[%d]", app.StopLevel), + StartLevel: fmt.Sprintf("runlevel [%d]", app.StartLevel), + StopLevel: fmt.Sprintf("runlevel [%d]", app.StopLevel), ExportDate: timeutil.Format(time.Now(), "%Y/%m/%d %H:%M:%S"), } @@ -133,8 +133,8 @@ func (up *UpstartProvider) RenderServiceTemplate(service *procfile.Service) (str data := &upstartServiceData{ Application: service.Application, Service: service, - StartLevel: fmt.Sprintf("[%d]", service.Application.StartLevel), - StopLevel: fmt.Sprintf("[%d]", service.Application.StopLevel), + StartLevel: fmt.Sprintf("starting %s", service.Application.Name), + StopLevel: fmt.Sprintf("stopping %s", service.Application.Name), ExportDate: timeutil.Format(time.Now(), "%Y/%m/%d %H:%M:%S"), } diff --git a/procfile/procfile.go b/procfile/procfile.go index 4cd8ed4..cbb714a 100644 --- a/procfile/procfile.go +++ b/procfile/procfile.go @@ -32,20 +32,19 @@ const ( REGEXP_VALUE_CHECK = `\A[A-Za-z0-9_\-]+\z` ) -const ( - DEFAULT_RESPAWN_INTERVAL = 5 - DEFAULT_RESPAWN_COUNT = 10 -) - // ////////////////////////////////////////////////////////////////////////////////// // type Config struct { - Name string // Application name - User string // Working user - Group string // Working group - WorkingDir string // Working directory - LimitProc int // Global processes limit - LimitFile int // Global descriptors limit + Name string // Application name + User string // Working user + Group string // Working group + WorkingDir string // Working directory + IsRespawnEnabled bool // Global respawn enabled flag + RespawnInterval int // Global respawn interval in seconds + RespawnCount int // Global respawn count + KillTimeout int // Global kill timeout in seconds + LimitProc int // Global processes limit + LimitFile int // Global descriptors limit } type Service struct { @@ -410,14 +409,7 @@ func parseCommands(yaml *simpleyaml.Yaml, commands map[interface{}]interface{}, } mergeServiceOptions(serviceOptions, commonOptions) - - if serviceOptions.LimitFile == 0 && config.LimitFile != 0 { - serviceOptions.LimitFile = config.LimitFile - } - - if serviceOptions.LimitProc == 0 && config.LimitProc != 0 { - serviceOptions.LimitProc = config.LimitProc - } + configureDefaults(serviceOptions, config) service := &Service{ Name: serviceName, @@ -489,8 +481,6 @@ func parseOptions(yaml *simpleyaml.Yaml) (*ServiceOptions, error) { if err != nil { return nil, fmt.Errorf("Can't parse respawn.count value: %v", err) } - } else { - options.RespawnCount = DEFAULT_RESPAWN_COUNT } if yaml.IsPathExist("respawn", "interval") { @@ -499,8 +489,6 @@ func parseOptions(yaml *simpleyaml.Yaml) (*ServiceOptions, error) { if err != nil { return nil, fmt.Errorf("Can't parse respawn.interval value: %v", err) } - } else { - options.RespawnInterval = DEFAULT_RESPAWN_INTERVAL } } else if yaml.IsExist("respawn") { @@ -586,6 +574,35 @@ func mergeServiceOptions(dst, src *ServiceOptions) { } } +// configureDefaults set options default values +func configureDefaults(serviceOptions *ServiceOptions, config *Config) { + if serviceOptions.LimitFile == 0 && config.LimitFile != 0 { + serviceOptions.LimitFile = config.LimitFile + } + + if serviceOptions.LimitProc == 0 && config.LimitProc != 0 { + serviceOptions.LimitProc = config.LimitProc + } + + if serviceOptions.KillTimeout == 0 && config.KillTimeout != 0 { + serviceOptions.KillTimeout = config.KillTimeout + } + + if config.IsRespawnEnabled { + serviceOptions.IsRespawnEnabled = true + } + + if serviceOptions.IsRespawnEnabled { + if serviceOptions.RespawnCount == 0 { + serviceOptions.RespawnCount = config.RespawnCount + } + + if serviceOptions.RespawnInterval == 0 { + serviceOptions.RespawnInterval = config.RespawnInterval + } + } +} + // mergeStringMaps merges two maps func mergeStringMaps(dest, src map[string]string) { for k, v := range src { diff --git a/readme.md b/readme.md index 809102e..11f526b 100644 --- a/readme.md +++ b/readme.md @@ -55,6 +55,26 @@ The export process can be configured through the config `/etc/init-exporter.conf # Path to directory with upstart configs upstart-dir: /etc/init +[defaults] + + # Number of Processes (0 - disabled) + nproc: 10240 + + # Number of File Descriptors (0 - disabled) + nofile: 10240 + + # Enable or disable respawn by default here + respawn: true + + # Respawn count + respawn-count: 10 + + # Respawn interval + respawn-interval: 15 + + # Kill timeout (0 - disabled) + kill-timeout: 60 + [log] # Enable or disable logging here From ac8221085079d66509b65aca65f31fed9d7b1822 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 22 Feb 2017 05:26:13 -0500 Subject: [PATCH 3/5] Updated spec --- common/init-exporter.spec | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/common/init-exporter.spec b/common/init-exporter.spec index 87f110a..1a049c7 100644 --- a/common/init-exporter.spec +++ b/common/init-exporter.spec @@ -105,5 +105,9 @@ rm -rf %{buildroot} ############################################################################### %changelog +* Wed Feb 22 2017 Anton Novojilov - 0.7.0-0 +- Fixed bug with export to upstart +- Improved working with default values + * Thu Feb 2 2017 Anton Novojilov - 0.6.0-0 - Initial build From ee48ccebde32e871bb82dbb5cc028163c44a5367 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 22 Feb 2017 05:28:46 -0500 Subject: [PATCH 4/5] Updated dependencies --- glide.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/glide.lock b/glide.lock index 5a1ccaa..6af03cd 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ hash: 352daf3640af334921e1e295d0738306592c7c4609878e4653936e2e6c7cc749 -updated: 2017-02-02T04:29:34.527741033-05:00 +updated: 2017-02-22T05:27:53.50635214-05:00 imports: - name: pkg.re/essentialkaos/ek.v6 version: c292a0da0a81e93c1161a291370c87369db8fc2c @@ -17,9 +17,9 @@ imports: - timeutil - usage - name: pkg.re/essentialkaos/go-simpleyaml.v1 - version: 6c4951f5cc065c5724d0b2f46a5036cc1f1f57a1 + version: 49b94a0b61131c037bc599d4c69de265c7cc1c4e - name: pkg.re/yaml.v2 - version: 4c78c975fe7c825c6d1466c42be594d1d6f3aba6 + version: a3f3340b5840cee44f372bddb5880fcbc419b46a testImports: - name: pkg.re/check.v1 version: 20d25e2804050c1cd24a7eea1e7a6447dd0e74ec From f1507dcc4fa6ab1835e2864cc4fa19dd363703ba Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 22 Feb 2017 05:33:43 -0500 Subject: [PATCH 5/5] Added validators for default values in main config --- cli/cli.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cli/cli.go b/cli/cli.go index f864830..b513e92 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -247,8 +247,11 @@ func validateConfig() { {DEFAULTS_RESPAWN_INTERVAL, knf.Empty, nil}, {DEFAULTS_KILL_TIMEOUT, knf.Empty, nil}, + {DEFAULTS_NPROC, knf.Less, 0}, + {DEFAULTS_NOFILE, knf.Less, 0}, {DEFAULTS_RESPAWN_COUNT, knf.Less, 0}, {DEFAULTS_RESPAWN_INTERVAL, knf.Less, 0}, + {DEFAULTS_KILL_TIMEOUT, knf.Less, 0}, {MAIN_RUN_USER, userChecker, nil}, {MAIN_RUN_GROUP, groupChecker, nil},