50
50
import sys
51
51
52
52
from mig .shared .defaults import default_http_port , default_https_port , \
53
- mig_user , mig_group , default_source , default_destination , \
53
+ MIG_BASE , mig_user , mig_group , default_source , default_destination , \
54
54
auth_openid_mig_db , auth_openid_ext_db , STRONG_TLS_CIPHERS , \
55
55
STRONG_TLS_CURVES , STRONG_SSH_KEXALGOS , STRONG_SSH_LEGACY_KEXALGOS , \
56
56
STRONG_SSH_CIPHERS , STRONG_SSH_LEGACY_CIPHERS , STRONG_SSH_MACS , \
@@ -74,6 +74,15 @@ def _override_apache_initd(template_name, user_dict):
74
74
return "%s-%s" % (file_name , user_dict ['__MIG_USER__' ])
75
75
76
76
77
+ def abspath (path , start ):
78
+ """Return an absolute path as per os.path.abspath() - from an explicit
79
+ starting cwd if necessary.
80
+ """
81
+ if os .path .isabs (path ):
82
+ return path
83
+ return os .path .normpath (os .path .join (start , path ))
84
+
85
+
77
86
def determine_timezone (_environ = os .environ , _path_exists = os .path .exists , _print = print ):
78
87
"""Attempt to detect the timezone in various known portable ways."""
79
88
@@ -255,7 +264,11 @@ def template_remove(template_file, remove_pattern):
255
264
'group' ,
256
265
'user' ,
257
266
'timezone' ,
267
+ '_getcwd' ,
258
268
'_getpwnam' ,
269
+ '_prepare' ,
270
+ '_writefiles' ,
271
+ '_instructions' ,
259
272
]
260
273
261
274
@@ -485,36 +498,55 @@ def generate_confs(
485
498
ca_fqdn = '' ,
486
499
ca_user = 'mig-ca' ,
487
500
ca_smtp = 'localhost' ,
501
+ _getcwd = os .getcwd ,
488
502
_getpwnam = pwd .getpwnam ,
503
+ _prepare = None ,
504
+ _writefiles = None ,
505
+ _instructions = None ,
489
506
):
490
507
"""Generate Apache and MiG server confs with specified variables"""
491
508
509
+ # TODO: override in signature as a non-functional follow-up change
510
+ if _prepare is None :
511
+ _prepare = _generate_confs_prepare
512
+ if _writefiles is None :
513
+ _writefiles = _generate_confs_writefiles
514
+ if _instructions is None :
515
+ _instructions = _generate_confs_instructions
516
+
492
517
# Read out dictionary of args with defaults and overrides
493
518
494
519
thelocals = locals ()
495
520
expanded = {k : v for k , v in thelocals .items () if k not in
496
521
_GENERATE_CONFS_NOFORWARD_KEYS }
497
522
498
523
# expand any directory path specific as "auto" relative to CWD
524
+ thecwd = _getcwd ()
525
+
499
526
if source == keyword_auto :
500
- template_path = os .path .dirname (sys .argv [0 ])
527
+ # use the templates from this copy of the code tree
528
+ template_dir = os .path .join (MIG_BASE , "mig/install" )
501
529
else :
502
- template_path = source
530
+ # construct a path using the supplied value made absolute
531
+ template_dir = abspath (source , start = thecwd )
503
532
504
533
if destination == keyword_auto :
505
- destination_link = os .path .dirname (sys .argv [0 ])
534
+ # write output into a confs folder within the CWD
535
+ destination = os .path .join (thecwd , 'confs' )
506
536
else :
507
- destination_link = destination
537
+ # construct a path from the supplied value made absolute
538
+ destination = abspath (destination , start = thecwd )
539
+
540
+ # finalize destination paths up-front
541
+ destination_link = destination
542
+ destination_dir = "%s%s" % (destination , destination_suffix )
508
543
509
544
# expand any user information marked as "auto" based on the environment
510
545
if user == keyword_auto :
511
546
user = pwd .getpwuid (os .getuid ())[0 ]
512
547
if group == keyword_auto :
513
548
group = grp .getgrgid (os .getgid ())[0 ]
514
549
515
- # finalize a destination path up-front
516
- destination_dir = "%s%s" % (destination , destination_suffix )
517
-
518
550
# Backwards compatibility with old name
519
551
if public_port and not public_http_port :
520
552
public_http_port = public_port
@@ -528,16 +560,16 @@ def generate_confs(
528
560
'command_line' : generateconfs_command ,
529
561
'destination_dir' : destination_dir ,
530
562
'destination_link' : destination_link ,
531
- 'template_dir' : template_path ,
563
+ 'template_dir' : template_dir ,
532
564
'timezone' : timezone ,
533
565
'user_gid' : user_pw_info .pw_gid ,
534
566
'user_group' : group ,
535
567
'user_uid' : user_pw_info .pw_uid ,
536
568
'user_uname' : user ,
537
569
}
538
- user_dict = _generate_confs_prepare (options , ** expanded )
539
- _generate_confs_write (options , user_dict )
540
- _generate_confs_instructions (options , user_dict )
570
+ user_dict = _prepare (options , ** expanded )
571
+ _writefiles (options , user_dict )
572
+ _instructions (options , user_dict )
541
573
return options
542
574
543
575
@@ -2131,7 +2163,7 @@ def _generate_confs_prepare(
2131
2163
return user_dict
2132
2164
2133
2165
2134
- def _generate_confs_write (options , user_dict , insert_list = [], cleanup_list = []):
2166
+ def _generate_confs_writefiles (options , user_dict , insert_list = [], cleanup_list = []):
2135
2167
"""Actually write generated confs"""
2136
2168
assert os .path .isabs (options ['destination_dir' ])
2137
2169
assert os .path .isabs (options ['template_dir' ])
0 commit comments