@@ -201,8 +201,8 @@ def get_all_groups():
201
201
from alignak .http .daemon import HTTPDaemon , PortNotFree
202
202
from alignak .stats import statsmgr
203
203
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 )
206
206
from alignak .misc .common import setproctitle , SIGNALS_TO_NAMES_DICT
207
207
from alignak .version import VERSION
208
208
@@ -244,7 +244,8 @@ class Daemon(object): # pylint: disable=too-many-instance-attributes
244
244
'env_filename' :
245
245
StringProp (default = u'' ),
246
246
247
- 'log_loop' : # Set True to log the daemon loop activity
247
+ # Set True to log the daemon loop activity
248
+ 'log_loop' :
248
249
BoolProp (default = False ),
249
250
250
251
'pid_filename' :
@@ -262,6 +263,10 @@ class Daemon(object): # pylint: disable=too-many-instance-attributes
262
263
'bindir' : # Default is empty
263
264
PathProp (default = '' ),
264
265
266
+ # Create missing directories
267
+ 'create_directories' :
268
+ BoolProp (default = False ),
269
+
265
270
# Interface the daemon will listen to
266
271
'host' :
267
272
StringProp (default = u'127.0.0.1' ),
@@ -387,6 +392,8 @@ def __init__(self, name, **kwargs):
387
392
388
393
:param kwargs: list of key/value pairs from the daemon command line and configuration
389
394
"""
395
+ self .use_ssl = False
396
+
390
397
self .alignak_env = None
391
398
self .legacy_cfg_files = []
392
399
self .modules_manager = None
@@ -406,6 +413,9 @@ def __init__(self, name, **kwargs):
406
413
self .name = name
407
414
self .host_name = socket .getfqdn ()
408
415
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 )))
409
419
410
420
# Check if /dev/shm exists and usable...
411
421
self .check_shm ()
@@ -516,7 +526,7 @@ def __init__(self, name, **kwargs):
516
526
print ("Daemon '%s' did not correctly read Alignak environment file: %s"
517
527
% (self .name , args ['<cfg_file>' ]))
518
528
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 ))
520
530
521
531
# Stop me if it I am disabled in the configuration
522
532
if not self .active :
@@ -585,6 +595,13 @@ def __init__(self, name, **kwargs):
585
595
self .exit_on_error ("Configured user/group (%s/%s) are not valid."
586
596
% (self .user , self .group ), exit_code = 3 )
587
597
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
+
588
605
# Alignak logger configuration file
589
606
if os .getenv ('ALIGNAK_LOGGER_CONFIGURATION' , None ):
590
607
self .logger_configuration = os .getenv ('ALIGNAK_LOGGER_CONFIGURATION' , None )
@@ -783,46 +800,72 @@ def scheme(self):
783
800
_scheme = 'https'
784
801
return _scheme
785
802
786
- def check_dir (self , dirname ):
803
+ def check_dir (self , dir_name , create ):
787
804
"""Check and create directory
788
805
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
791
809
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
793
817
"""
794
818
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 )
802
842
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 )))
804
849
805
850
self .pre_log .append (("DEBUG" ,
806
851
"Daemon '%s' directory %s checking... "
807
852
"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
809
856
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 )))
814
857
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 ):
816
859
# Directory still exists...
817
860
pass
818
861
else :
819
862
self .pre_log .append (("ERROR" ,
820
863
"Daemon directory '%s' did not exist, "
821
864
"and I could not create. Exception: %s"
822
- % (dirname , exp )))
865
+ % (dir_name , exp )))
823
866
self .exit_on_error ("Daemon directory '%s' did not exist, "
824
867
"and I could not create.'. Exception: %s"
825
- % (dirname , exp ), exit_code = 3 )
868
+ % (dir_name , exp ), exit_code = 3 )
826
869
827
870
def do_stop (self ):
828
871
"""Execute the stop of this daemon:
@@ -1238,9 +1281,9 @@ def change_to_workdir(self):
1238
1281
1239
1282
:return: None
1240
1283
"""
1241
- logger . info ( " Changing working directory to: %s", self .workdir )
1284
+ self . pre_log . append (( "INFO" , " Changing working directory to: %s" % self .workdir ) )
1242
1285
1243
- self .check_dir (self .workdir )
1286
+ # self.check_dir(self.workdir)
1244
1287
try :
1245
1288
os .chdir (self .workdir )
1246
1289
except OSError as exp :
@@ -1748,7 +1791,9 @@ def get_header(self, configuration=False):
1748
1791
u"-----" ,
1749
1792
u"Python: %s.%s" % (sys .version_info .major , sys .version_info .minor ),
1750
1793
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"-----" ]
1752
1797
1753
1798
if configuration :
1754
1799
header = ["My configuration: " ]
@@ -2161,7 +2206,7 @@ def setup_alignak_logger(self):
2161
2206
# Configure the daemon logger
2162
2207
try :
2163
2208
# Make sure that the log directory is existing
2164
- self .check_dir (self .logdir )
2209
+ # self.check_dir(self.logdir)
2165
2210
2166
2211
if self .logger_configuration and not os .path .exists (self .logger_configuration ):
2167
2212
print ("The logger configuration file does not exist: %s"
0 commit comments