Skip to content

Commit 7295d08

Browse files
Frédéric Mohiermohierf
Frédéric Mohier
authored andcommitted
Check daemons configured directories (eg. logdir, workdir...)
1 parent 80bbc7c commit 7295d08

File tree

5 files changed

+99
-28
lines changed

5 files changed

+99
-28
lines changed

alignak/daemon.py

+73-28
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ def get_all_groups():
201201
from alignak.http.daemon import HTTPDaemon, PortNotFree
202202
from alignak.stats import statsmgr
203203
from alignak.modulesmanager import ModulesManager
204-
from alignak.property import StringProp, BoolProp, PathProp
205-
from alignak.property import IntegerProp, FloatProp, ListProp
204+
from alignak.property import (StringProp, BoolProp, PathProp,
205+
IntegerProp, FloatProp, ListProp)
206206
from alignak.misc.common import setproctitle, SIGNALS_TO_NAMES_DICT
207207
from alignak.version import VERSION
208208

@@ -244,7 +244,8 @@ class Daemon(object): # pylint: disable=too-many-instance-attributes
244244
'env_filename':
245245
StringProp(default=u''),
246246

247-
'log_loop': # Set True to log the daemon loop activity
247+
# Set True to log the daemon loop activity
248+
'log_loop':
248249
BoolProp(default=False),
249250

250251
'pid_filename':
@@ -262,6 +263,10 @@ class Daemon(object): # pylint: disable=too-many-instance-attributes
262263
'bindir': # Default is empty
263264
PathProp(default=''),
264265

266+
# Create missing directories
267+
'create_directories':
268+
BoolProp(default=False),
269+
265270
# Interface the daemon will listen to
266271
'host':
267272
StringProp(default=u'127.0.0.1'),
@@ -387,6 +392,8 @@ def __init__(self, name, **kwargs):
387392
388393
:param kwargs: list of key/value pairs from the daemon command line and configuration
389394
"""
395+
self.use_ssl = False
396+
390397
self.alignak_env = None
391398
self.legacy_cfg_files = []
392399
self.modules_manager = None
@@ -406,6 +413,9 @@ def __init__(self, name, **kwargs):
406413
self.name = name
407414
self.host_name = socket.getfqdn()
408415
self.address = '127.0.0.1'
416+
self.pre_log.append(("INFO",
417+
"Daemon '%s' running on host: %s"
418+
% (self.name, self.host_name)))
409419

410420
# Check if /dev/shm exists and usable...
411421
self.check_shm()
@@ -516,7 +526,7 @@ def __init__(self, name, **kwargs):
516526
print("Daemon '%s' did not correctly read Alignak environment file: %s"
517527
% (self.name, args['<cfg_file>']))
518528
print("Exception: %s\n%s" % (exp, traceback.format_exc()))
519-
self.exit_on_exception(EnvironmentFile("Exception: %s" % (exp)))
529+
self.exit_on_exception(EnvironmentFile("Exception: %s" % exp))
520530

521531
# Stop me if it I am disabled in the configuration
522532
if not self.active:
@@ -585,6 +595,13 @@ def __init__(self, name, **kwargs):
585595
self.exit_on_error("Configured user/group (%s/%s) are not valid."
586596
% (self.user, self.group), exit_code=3)
587597

598+
# Check the configured daemon directories, and create some if configuration allows
599+
self.workdir = self.check_dir(self.workdir, self.create_directories)
600+
self.etcdir = self.check_dir(self.etcdir, False)
601+
self.vardir = self.check_dir(self.vardir, False)
602+
self.logdir = self.check_dir(self.logdir, self.create_directories)
603+
self.bindir = self.check_dir(self.bindir, False)
604+
588605
# Alignak logger configuration file
589606
if os.getenv('ALIGNAK_LOGGER_CONFIGURATION', None):
590607
self.logger_configuration = os.getenv('ALIGNAK_LOGGER_CONFIGURATION', None)
@@ -783,46 +800,72 @@ def scheme(self):
783800
_scheme = 'https'
784801
return _scheme
785802

786-
def check_dir(self, dirname):
803+
def check_dir(self, dir_name, create):
787804
"""Check and create directory
788805
789-
:param dirname: file name
790-
:type dirname; str
806+
If the directory name is a relative, this function will consider it
807+
relative to the working directory. It will return the full path of the
808+
directory
791809
792-
:return: None
810+
:param create: create if it does not exist
811+
:type create: bool
812+
813+
:param dir_name: file name
814+
:type dir_name; str
815+
816+
:return: str, the real directory full path
793817
"""
794818
try:
795-
os.makedirs(dirname)
796-
dir_stat = os.stat(dirname)
797-
print("Created the directory: %s, stat: %s" % (dirname, dir_stat))
798-
if not dir_stat.st_uid == self.uid:
799-
os.chown(dirname, self.uid, self.gid)
800-
os.chmod(dirname, 0o775)
801-
dir_stat = os.stat(dirname)
819+
if dir_name in [self.workdir]:
820+
if dir_name != os.path.abspath(dir_name):
821+
# It is not an absolute path, so make it absolute!
822+
dir_name = os.path.abspath(dir_name)
823+
else:
824+
if dir_name != os.path.abspath(dir_name):
825+
# It is not an absolute path, so it is relative to the working dir...
826+
dir_name = os.path.abspath(os.path.join(self.workdir, dir_name))
827+
828+
if not os.path.exists(dir_name):
829+
if create:
830+
os.makedirs(dir_name)
831+
dir_stat = os.stat(dir_name)
832+
print("I created the directory: %s, stat: %s" % (dir_name, dir_stat))
833+
else:
834+
print("The directory: %s does not exist" % dir_name)
835+
return dir_name
836+
837+
dir_stat = os.stat(dir_name)
838+
if create and not dir_stat.st_uid == self.uid:
839+
os.chown(dir_name, self.uid, self.gid)
840+
os.chmod(dir_name, 0o775)
841+
dir_stat = os.stat(dir_name)
802842
print("Changed directory ownership and permissions: %s, stat: %s"
803-
% (dirname, dir_stat))
843+
% (dir_name, dir_stat))
844+
845+
self.pre_log.append(("INFO",
846+
"Daemon '%s' directory %s did not exist, I created it. "
847+
"I set ownership for this directory to %s:%s."
848+
% (self.name, dir_name, self.user, self.group)))
804849

805850
self.pre_log.append(("DEBUG",
806851
"Daemon '%s' directory %s checking... "
807852
"User uid: %s, directory stat: %s."
808-
% (self.name, dirname, os.getuid(), dir_stat)))
853+
% (self.name, dir_name, os.getuid(), dir_stat)))
854+
855+
return dir_name
809856

810-
self.pre_log.append(("INFO",
811-
"Daemon '%s' directory %s did not exist, I created it. "
812-
"I set ownership for this directory to %s:%s."
813-
% (self.name, dirname, self.user, self.group)))
814857
except OSError as exp:
815-
if exp.errno == errno.EEXIST and os.path.isdir(dirname):
858+
if exp.errno == errno.EEXIST and os.path.isdir(dir_name):
816859
# Directory still exists...
817860
pass
818861
else:
819862
self.pre_log.append(("ERROR",
820863
"Daemon directory '%s' did not exist, "
821864
"and I could not create. Exception: %s"
822-
% (dirname, exp)))
865+
% (dir_name, exp)))
823866
self.exit_on_error("Daemon directory '%s' did not exist, "
824867
"and I could not create.'. Exception: %s"
825-
% (dirname, exp), exit_code=3)
868+
% (dir_name, exp), exit_code=3)
826869

827870
def do_stop(self):
828871
"""Execute the stop of this daemon:
@@ -1238,9 +1281,9 @@ def change_to_workdir(self):
12381281
12391282
:return: None
12401283
"""
1241-
logger.info("Changing working directory to: %s", self.workdir)
1284+
self.pre_log.append(("INFO", "Changing working directory to: %s" % self.workdir))
12421285

1243-
self.check_dir(self.workdir)
1286+
# self.check_dir(self.workdir)
12441287
try:
12451288
os.chdir(self.workdir)
12461289
except OSError as exp:
@@ -1748,7 +1791,9 @@ def get_header(self, configuration=False):
17481791
u"-----",
17491792
u"Python: %s.%s" % (sys.version_info.major, sys.version_info.minor),
17501793
u"-----",
1751-
u"My pid: %s" % self.pid]
1794+
u"My pid: %s" % self.pid,
1795+
u"My host name: %s" % self.host_name,
1796+
u"-----"]
17521797

17531798
if configuration:
17541799
header = ["My configuration: "]
@@ -2161,7 +2206,7 @@ def setup_alignak_logger(self):
21612206
# Configure the daemon logger
21622207
try:
21632208
# Make sure that the log directory is existing
2164-
self.check_dir(self.logdir)
2209+
# self.check_dir(self.logdir)
21652210

21662211
if self.logger_configuration and not os.path.exists(self.logger_configuration):
21672212
print("The logger configuration file does not exist: %s"

etc/alignak.ini

+7
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ workdir=%(_dist_RUN)s
6363
logdir=%(_dist_LOG)s
6464
etcdir=%(_dist_ETC)s
6565
bindir=%(_dist_BIN)s
66+
vardir=%(_dist_VAR)s
67+
68+
; Set to try creating the directory if it does not exist
69+
; It will also try to set ownership and permissions for the user account the daemon runs with
70+
; Default is unset
71+
;create_directories=0
72+
create_directories=1
6673
; --------------------------------------------------------------------
6774

6875

tests/alignak_test.py

+6
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ def _prepare_configuration(self, copy=True, cfg_folder='/tmp/alignak', daemons_l
286286
# Nagios legacy files
287287
cfg.set('alignak-configuration', 'cfg', '%s/etc/alignak.cfg' % cfg_folder)
288288

289+
# Directory for running daemons
290+
cfg.set('alignak-configuration', 'daemons_script_location', '/usr/local/bin')
291+
289292
# Daemons launching and check
290293
cfg.set('alignak-configuration', 'polling_interval', '1')
291294
cfg.set('alignak-configuration', 'daemons_check_period', '1')
@@ -505,6 +508,9 @@ def _run_alignak_daemons(self, cfg_folder='/tmp/alignak', runtime=30,
505508
# Nagios legacy files
506509
cfg.set('alignak-configuration', 'cfg', '%s/etc/alignak.cfg' % cfg_folder)
507510

511+
# Directory for running daemons
512+
cfg.set('alignak-configuration', 'daemons_script_location', '/usr/local/bin')
513+
508514
# Daemons launching and check
509515
cfg.set('alignak-configuration', 'polling_interval', '1')
510516
cfg.set('alignak-configuration', 'daemons_check_period', '1')

tests_integ/alignak_test.py

+10
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ def _prepare_configuration(self, copy=True, cfg_folder='/tmp/alignak', daemons_l
286286
# Nagios legacy files
287287
cfg.set('alignak-configuration', 'cfg', '%s/etc/alignak.cfg' % cfg_folder)
288288

289+
# Directory for running daemons
290+
cfg.set('alignak-configuration', 'daemons_script_location', '/usr/local/bin')
291+
289292
# Daemons launching and check
290293
cfg.set('alignak-configuration', 'polling_interval', '1')
291294
cfg.set('alignak-configuration', 'daemons_check_period', '1')
@@ -505,6 +508,9 @@ def _run_alignak_daemons(self, cfg_folder='/tmp/alignak', runtime=30,
505508
# Nagios legacy files
506509
cfg.set('alignak-configuration', 'cfg', '%s/etc/alignak.cfg' % cfg_folder)
507510

511+
# Directory for running daemons
512+
cfg.set('alignak-configuration', 'daemons_script_location', '/usr/local/bin')
513+
508514
# Daemons launching and check
509515
cfg.set('alignak-configuration', 'polling_interval', '1')
510516
cfg.set('alignak-configuration', 'daemons_check_period', '1')
@@ -513,6 +519,10 @@ def _run_alignak_daemons(self, cfg_folder='/tmp/alignak', runtime=30,
513519
cfg.set('alignak-configuration', 'daemons_new_conf_timeout', '1')
514520
cfg.set('alignak-configuration', 'daemons_dispatch_timeout', '1')
515521

522+
# Poller/reactionner workers count limited to 1
523+
cfg.set('alignak-configuration', 'min_workers', '1')
524+
cfg.set('alignak-configuration', 'max_workers', '1')
525+
516526
with open('%s/etc/alignak.ini' % cfg_folder, "w") as modified:
517527
cfg.write(modified)
518528
except Exception as exp:

tests_integ/test_daemons_api.py

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ def _prepare_my_configuration(self, daemons_list=None, remove_daemons=None,
107107
cfg = configparser.ConfigParser()
108108
cfg.read(files)
109109

110+
# Directory for running daemons
111+
cfg.set('alignak-configuration', 'daemons_script_location', '/usr/local/bin')
112+
110113
cfg.set('alignak-configuration', 'launch_missing_daemons', '1')
111114

112115
cfg.set('alignak-configuration', 'daemons_check_period', '5')

0 commit comments

Comments
 (0)