Skip to content

Commit

Permalink
Merge pull request #6 from funbox/develop
Browse files Browse the repository at this point in the history
 Version 0.7.0
  • Loading branch information
andyone authored Feb 22, 2017
2 parents 82db9d1 + f1507dc commit a233552
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 77 deletions.
4 changes: 2 additions & 2 deletions Dockerfile.systemd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile.upstart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
79 changes: 47 additions & 32 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 (
Expand Down Expand Up @@ -237,11 +241,17 @@ 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},

{LIMITS_NOFILE, knf.Less, 0},
{LIMITS_NPROC, knf.Less, 0},
{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},

{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},
Expand Down Expand Up @@ -298,12 +308,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),
},
)

Expand Down Expand Up @@ -432,12 +446,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()
Expand Down
18 changes: 15 additions & 3 deletions common/init-exporter.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 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.6.0
Version: 0.7.0
Release: 0%{?dist}
Group: Development/Tools
License: MIT
Expand All @@ -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}
Expand Down Expand Up @@ -105,5 +105,9 @@ rm -rf %{buildroot}
###############################################################################

%changelog
* Wed Feb 22 2017 Anton Novojilov <[email protected]> - 0.7.0-0
- Fixed bug with export to upstart
- Improved working with default values

* Thu Feb 2 2017 Anton Novojilov <[email protected]> - 0.6.0-0
- Initial build
12 changes: 6 additions & 6 deletions export/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"",
Expand All @@ -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",
Expand All @@ -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",
"",
Expand Down
8 changes: 4 additions & 4 deletions export/upstart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}

Expand All @@ -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"),
}

Expand Down
6 changes: 3 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 40 additions & 23 deletions procfile/procfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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") {
Expand All @@ -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") {
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a233552

Please sign in to comment.