Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit 85ab0ce

Browse files
committed
Merge pull request #14 from nir0s/tech-debt
add-missing-env-var-and-limit-handling
2 parents 2adc951 + a633b48 commit 85ab0ce

File tree

10 files changed

+210
-234
lines changed

10 files changed

+210
-234
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@ install:
1212
script:
1313
- tox -e $TOX_ENV
1414
- sudo tox -e deploy
15-

serv/init/base.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ def __init__(self, lgr=None, **params):
3838
self._validate_service_params()
3939

4040
def _set_default_parameter_values(self):
41-
p = self.params
42-
p['description'] = p.get('description', 'no description given')
43-
p['chdir'] = p.get('chdir', '/')
44-
p['chroot'] = p.get('chroot', '/')
45-
p['user'] = p.get('user', 'root')
46-
p['group'] = p.get('group', 'root')
41+
params = self.params
42+
params['description'] = params.get(
43+
'description', 'no description given')
44+
params['chdir'] = params.get('chdir', '/')
45+
params['chroot'] = params.get('chroot', '/')
46+
params['user'] = params.get('user', 'root')
47+
params['group'] = params.get('group', 'root')
4748

4849
def _validate_service_params(self):
4950
niceness = self.params.get('nice')
@@ -63,21 +64,21 @@ def _validate_service_params(self):
6364
'limit_stack_size',
6465
]
6566

66-
def _raise_limit_error():
67+
def _raise_limit_error(limit_type, limit):
6768
self.lgr.error('All limits must be integers greater than 0 or '
6869
'ulimited. You provided a {0} with value '
69-
'{1}.'.format('limit_coredump', limit))
70+
'{1}.'.format(limit_type, limit))
7071
sys.exit(1)
7172

72-
for l in limit_params:
73-
limit = self.params.get(l)
73+
for limit_type in limit_params:
74+
limit = self.params.get(limit_type)
7475
if limit not in (None, 'ulimited'):
7576
try:
7677
value = int(limit)
7778
except (ValueError, TypeError):
78-
_raise_limit_error()
79+
_raise_limit_error(limit_type, limit)
7980
if value < 1:
80-
_raise_limit_error()
81+
_raise_limit_error(limit_type, limit)
8182

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

235236
def generate_service_files(self):
236237
files = []
237-
for s in const.TEMPLATES[self.init_sys][self.init_sys_ver].keys():
238-
# remove j2 suffix and then, for instance for:
239-
# systemd['default']['service']
240-
pfx = '_'.join([self.init_sys, self.init_sys_ver])
241-
sfx = s or ''
242-
template = pfx + sfx
243-
self.destination = os.path.join(self.tmp, self.name + sfx)
238+
for file_type in \
239+
const.TEMPLATES[self.init_sys][self.init_sys_ver].keys():
240+
prefix = '_'.join([self.init_sys, self.init_sys_ver])
241+
suffix = file_type or ''
242+
template = prefix + suffix
243+
self.destination = os.path.join(self.tmp, self.name + suffix)
244244
files.append(self.destination)
245245
self.generate_file_from_template(template, self.destination)
246246
return files

serv/init/systemd.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,14 @@ def _parse_service_info(svc):
145145
description=svc_info[4]
146146
)
147147

148-
def is_system_exists(self):
148+
@staticmethod
149+
def is_system_exists():
149150
"""Returns True if the init system exists and False if not.
150151
"""
151-
try:
152-
sh.systemctl('--version')
153-
return True
154-
except:
155-
return False
152+
return is_system_exists()
156153

157-
def get_system_version(self):
154+
@staticmethod
155+
def get_system_version():
158156
"""Returns the init system's version if it exists.
159157
"""
160158
try:
@@ -180,3 +178,11 @@ def validate_platform(self):
180178
self.lgr.error(
181179
'Cannot install SysVinit service on non-Linux systems.')
182180
sys.exit()
181+
182+
183+
def is_system_exists():
184+
try:
185+
sh.systemctl('--version')
186+
return True
187+
except:
188+
return False

serv/init/sysv.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import sys
3+
import subprocess
34

45
from serv import utils
56
from serv.init.base import Base
@@ -42,7 +43,9 @@ def install(self):
4243

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

5962
def stop(self):
6063
try:
61-
sh.service(self.name, 'stop', _bg=True)
64+
subprocess.check_call(
65+
'service {0} stop'.format(self.name),
66+
shell=True, stdout=subprocess.PIPE)
6267
except sh.CommandNotFound:
6368
self.lgr.warning('service command unavailable. Trying to run '
6469
'script directly.')
@@ -67,7 +72,7 @@ def stop(self):
6772
service.stop(_bg=True)
6873
except sh.CommandNotFound as ex:
6974
self.lgr.error('Command not found: {0}'.format(str(ex)))
70-
sys.exit()
75+
sys.exit(1)
7176
except:
7277
self.lgr.info('Service already stopped.')
7378

@@ -113,11 +118,12 @@ def _parse_service_info(svc):
113118
pid=pid
114119
)
115120

116-
def is_system_exists(self):
117-
# maybe a safer way would be to check if /etc/init.d is not empty.
118-
return os.path.isdir('/etc/init.d')
121+
@staticmethod
122+
def is_system_exists():
123+
return is_system_exists()
119124

120-
def get_system_version(self):
125+
@staticmethod
126+
def get_system_version():
121127
return 'lsb-3.1'
122128

123129
def is_service_exists(self):
@@ -156,4 +162,9 @@ def validate_platform(self):
156162
if utils.IS_WIN or utils.IS_DARWIN:
157163
self.lgr.error(
158164
'Cannot install SysVinit service on non-Linux systems.')
159-
sys.exit()
165+
sys.exit(1)
166+
167+
168+
def is_system_exists():
169+
# TODO: maybe a safer way would be to check if /etc/init.d is not empty.
170+
return os.path.isdir('/etc/init.d')

serv/init/templates/supervisor_default.conf

Lines changed: 0 additions & 129 deletions
This file was deleted.

serv/init/templates/systemd_default.service

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,25 @@ ExecStart={{ cmd }} {{ args }}
1111
Restart={{ always or 'restart' }}
1212
WorkingDirectory={{ chdir or '/' }}
1313

14+
{% if nice %}
15+
LimitNICE={{ nice }}{% endif %}{% if limit_coredump %}
16+
LimitCORE={{ limit_coredump }}{% endif %}{% if limit_cputime %}
17+
LimitCPU={{ limit_cputime }}{% endif %}{% if limit_data %}
18+
LimitDATA={{ limit_data }}{% endif %}{% if limit_file_size %}
19+
LimitFSIZE={{ limit_file_size }}{% endif %}{% if limit_locked_memory %}
20+
LimitMEMLOCK={{ limit_locked_memory }}{% endif %}{% if limit_open_files %}
21+
LimitNOFILE={{ limit_open_files }}{% endif %}{% if limit_user_processes %}
22+
LimitNPROC={{ limit_user_processes }}{% endif %}{% if limit_physical_memory %}
23+
LimitRSS={{ limit_physical_memory }}{% endif %}{% if limit_stack_size %}
24+
LimitSTACK={{ limit_stack_size }}{% endif %}
25+
26+
#Unsupported by Serv just yet
27+
#LimitAS=
28+
#LimitLOCKS=
29+
#LimitSIGPENDING=
30+
#LimitMSGQUEUE=
31+
#LimitRTPRIO=
32+
#LimitRTTIME=
33+
1434
[Install]
1535
WantedBy=multi-user.target

serv/init/upstart.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,12 @@ def _parse_service_info(svc):
7979
pid=pid
8080
)
8181

82-
def is_system_exists(self):
83-
try:
84-
sh.initctl.version()
85-
return True
86-
except:
87-
return False
82+
@staticmethod
83+
def is_system_exists():
84+
return is_system_exists()
8885

89-
def get_system_version(self):
86+
@staticmethod
87+
def get_system_version():
9088
try:
9189
output = sh.initctl.version()
9290
except:
@@ -104,3 +102,11 @@ def validate_platform(self):
104102
self.lgr.error(
105103
'Cannot install SysVinit service on non-Linux systems.')
106104
sys.exit()
105+
106+
107+
def is_system_exists():
108+
try:
109+
sh.initctl.version()
110+
return True
111+
except:
112+
return False

0 commit comments

Comments
 (0)