Skip to content

Commit

Permalink
add-missing-env-var-and-limit-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nir0s committed Apr 26, 2016
1 parent c8fcb27 commit a633b48
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 234 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ install:
script:
- tox -e $TOX_ENV
- sudo tox -e deploy

38 changes: 19 additions & 19 deletions serv/init/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ def __init__(self, lgr=None, **params):
self._validate_service_params()

def _set_default_parameter_values(self):
p = self.params
p['description'] = p.get('description', 'no description given')
p['chdir'] = p.get('chdir', '/')
p['chroot'] = p.get('chroot', '/')
p['user'] = p.get('user', 'root')
p['group'] = p.get('group', 'root')
params = self.params
params['description'] = params.get(
'description', 'no description given')
params['chdir'] = params.get('chdir', '/')
params['chroot'] = params.get('chroot', '/')
params['user'] = params.get('user', 'root')
params['group'] = params.get('group', 'root')

def _validate_service_params(self):
niceness = self.params.get('nice')
Expand All @@ -63,21 +64,21 @@ def _validate_service_params(self):
'limit_stack_size',
]

def _raise_limit_error():
def _raise_limit_error(limit_type, limit):
self.lgr.error('All limits must be integers greater than 0 or '
'ulimited. You provided a {0} with value '
'{1}.'.format('limit_coredump', limit))
'{1}.'.format(limit_type, limit))
sys.exit(1)

for l in limit_params:
limit = self.params.get(l)
for limit_type in limit_params:
limit = self.params.get(limit_type)
if limit not in (None, 'ulimited'):
try:
value = int(limit)
except (ValueError, TypeError):
_raise_limit_error()
_raise_limit_error(limit_type, limit)
if value < 1:
_raise_limit_error()
_raise_limit_error(limit_type, limit)

def generate(self, overwrite):
"""Generates service files.
Expand Down Expand Up @@ -234,13 +235,12 @@ def deploy_service_file(self, source, destination, create_directory=False):

def generate_service_files(self):
files = []
for s in const.TEMPLATES[self.init_sys][self.init_sys_ver].keys():
# remove j2 suffix and then, for instance for:
# systemd['default']['service']
pfx = '_'.join([self.init_sys, self.init_sys_ver])
sfx = s or ''
template = pfx + sfx
self.destination = os.path.join(self.tmp, self.name + sfx)
for file_type in \
const.TEMPLATES[self.init_sys][self.init_sys_ver].keys():
prefix = '_'.join([self.init_sys, self.init_sys_ver])
suffix = file_type or ''
template = prefix + suffix
self.destination = os.path.join(self.tmp, self.name + suffix)
files.append(self.destination)
self.generate_file_from_template(template, self.destination)
return files
20 changes: 13 additions & 7 deletions serv/init/systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,14 @@ def _parse_service_info(svc):
description=svc_info[4]
)

def is_system_exists(self):
@staticmethod
def is_system_exists():
"""Returns True if the init system exists and False if not.
"""
try:
sh.systemctl('--version')
return True
except:
return False
return is_system_exists()

def get_system_version(self):
@staticmethod
def get_system_version():
"""Returns the init system's version if it exists.
"""
try:
Expand All @@ -180,3 +178,11 @@ def validate_platform(self):
self.lgr.error(
'Cannot install SysVinit service on non-Linux systems.')
sys.exit()


def is_system_exists():
try:
sh.systemctl('--version')
return True
except:
return False
27 changes: 19 additions & 8 deletions serv/init/sysv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import subprocess

from serv import utils
from serv.init.base import Base
Expand Down Expand Up @@ -42,7 +43,9 @@ def install(self):

def start(self):
try:
sh.service(self.name, 'start', _bg=True)
subprocess.check_call(
'service {0} start'.format(self.name),
shell=True, stdout=subprocess.PIPE)
except sh.CommandNotFound:
# TODO: cleanup generated files if not found.
self.lgr.warning('service command unavailable. Trying to run '
Expand All @@ -58,7 +61,9 @@ def start(self):

def stop(self):
try:
sh.service(self.name, 'stop', _bg=True)
subprocess.check_call(
'service {0} stop'.format(self.name),
shell=True, stdout=subprocess.PIPE)
except sh.CommandNotFound:
self.lgr.warning('service command unavailable. Trying to run '
'script directly.')
Expand All @@ -67,7 +72,7 @@ def stop(self):
service.stop(_bg=True)
except sh.CommandNotFound as ex:
self.lgr.error('Command not found: {0}'.format(str(ex)))
sys.exit()
sys.exit(1)
except:
self.lgr.info('Service already stopped.')

Expand Down Expand Up @@ -113,11 +118,12 @@ def _parse_service_info(svc):
pid=pid
)

def is_system_exists(self):
# maybe a safer way would be to check if /etc/init.d is not empty.
return os.path.isdir('/etc/init.d')
@staticmethod
def is_system_exists():
return is_system_exists()

def get_system_version(self):
@staticmethod
def get_system_version():
return 'lsb-3.1'

def is_service_exists(self):
Expand Down Expand Up @@ -156,4 +162,9 @@ def validate_platform(self):
if utils.IS_WIN or utils.IS_DARWIN:
self.lgr.error(
'Cannot install SysVinit service on non-Linux systems.')
sys.exit()
sys.exit(1)


def is_system_exists():
# TODO: maybe a safer way would be to check if /etc/init.d is not empty.
return os.path.isdir('/etc/init.d')
129 changes: 0 additions & 129 deletions serv/init/templates/supervisor_default.conf

This file was deleted.

20 changes: 20 additions & 0 deletions serv/init/templates/systemd_default.service
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,25 @@ ExecStart={{ cmd }} {{ args }}
Restart={{ always or 'restart' }}
WorkingDirectory={{ chdir or '/' }}

{% if nice %}
LimitNICE={{ nice }}{% endif %}{% if limit_coredump %}
LimitCORE={{ limit_coredump }}{% endif %}{% if limit_cputime %}
LimitCPU={{ limit_cputime }}{% endif %}{% if limit_data %}
LimitDATA={{ limit_data }}{% endif %}{% if limit_file_size %}
LimitFSIZE={{ limit_file_size }}{% endif %}{% if limit_locked_memory %}
LimitMEMLOCK={{ limit_locked_memory }}{% endif %}{% if limit_open_files %}
LimitNOFILE={{ limit_open_files }}{% endif %}{% if limit_user_processes %}
LimitNPROC={{ limit_user_processes }}{% endif %}{% if limit_physical_memory %}
LimitRSS={{ limit_physical_memory }}{% endif %}{% if limit_stack_size %}
LimitSTACK={{ limit_stack_size }}{% endif %}

#Unsupported by Serv just yet
#LimitAS=
#LimitLOCKS=
#LimitSIGPENDING=
#LimitMSGQUEUE=
#LimitRTPRIO=
#LimitRTTIME=

[Install]
WantedBy=multi-user.target
20 changes: 13 additions & 7 deletions serv/init/upstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,12 @@ def _parse_service_info(svc):
pid=pid
)

def is_system_exists(self):
try:
sh.initctl.version()
return True
except:
return False
@staticmethod
def is_system_exists():
return is_system_exists()

def get_system_version(self):
@staticmethod
def get_system_version():
try:
output = sh.initctl.version()
except:
Expand All @@ -104,3 +102,11 @@ def validate_platform(self):
self.lgr.error(
'Cannot install SysVinit service on non-Linux systems.')
sys.exit()


def is_system_exists():
try:
sh.initctl.version()
return True
except:
return False
Loading

0 comments on commit a633b48

Please sign in to comment.