fix vh_le escaped spaces in commands. add option --not-only-sub-domains. replace www of the conf by test

This commit is contained in:
dadel 2017-03-03 10:18:02 +01:00
parent a6e1020c60
commit 810424a13b
2 changed files with 60 additions and 15 deletions

View File

@ -1,12 +1,12 @@
# the main domain
domain_main = "www.hadoly.fr"
domain_main = "test.hadoly.fr"
# list of TLDs used to determine the secondary domains
tld = ['fr', 'org']
# domain's base name used to determine the secondary domains
name = "hadoly"
# list of subdomains used to determine the secondary domains
sub = ['www']
sub = ['test']
# the IPv6
ip6_back = "2001:0912:3064:XXXX"

View File

@ -30,6 +30,8 @@ parser.add_argument('-f', '--from-step', dest='from_step',
help='run from a given step to the end (e.g. 3)')
parser.add_argument('-v', '--verbose', help='If set will print more informations',
action="store_true")
parser.add_argument('-o', '--not-only-sub-domains', help='If set will considere main domain and sub-domains (default considere only sub-domains)',
action="store_true")
parser.add_argument('-c', '--config-file', dest='config_file',
help='the configuration file of the script (default ./vh_le.conf)')
@ -139,6 +141,46 @@ def get_config_from_file(filename):
return config
def _escape_quoted_spaces(msg):
'''Replace all spaces comprised between single or double quotes by
the string "<ESCAPED_SPACE>"
'''
res = ''
curr_escape = False
curr_sep = None
for c in msg:
if curr_escape == True:
if c == ' ':
c = '<ESCAPED_SPACE>'
if c == curr_sep:
curr_escape = False
curr_sep = None
c = ''
else:
if c in ['"', "'"]:
curr_sep = c
curr_escape = True
c = ''
res += c
return res
def _replace_quoted_spaces(msg):
'''Replace all occurrences of the string "<ESCAPED_SPACE>" by spaces
'''
return msg.replace('<ESCAPED_SPACE>', ' ')
def _split_keeping_escaped(msg):
'''Split a string base on spaces but keep strings with spaces comprised
between single or double quotes.
'''
res = _escape_quoted_spaces(msg).split(' ')
return [_replace_quoted_spaces(e) for e in res]
def shell_command(cmd, get_stderr=False, **kwargs):
"""Run a shell command
@ -149,9 +191,9 @@ def shell_command(cmd, get_stderr=False, **kwargs):
ret = None
try:
if get_stderr:
ret = subprocess.check_output(cmd.split(' '), **kwargs)
ret = subprocess.check_output(_split_keeping_escaped(cmd), **kwargs)
else:
ret = subprocess.check_output(cmd.split(' '), stderr=subprocess.PIPE, **kwargs)
ret = subprocess.check_output(_split_keeping_escaped(cmd), stderr=subprocess.PIPE, **kwargs)
print_debug("run command '{}'".format(cmd))
except Exception as e:
print_debug("failed to run command: {}\n got exception: <{}>".format(cmd, e))
@ -242,8 +284,6 @@ if args.ls_steps:
print (steps)
sys.exit(0)
############### Prepare config
@ -291,9 +331,14 @@ if args.sites_available:
# build secondary domains
only_sub_domains = True
if args.not_only_sub_domains:
only_sub_domains = False
domains = []
for tld in c['tld']:
domains.append(c['name'] + '.' + tld)
if not only_sub_domains:
domains.append(c['name'] + '.' + tld)
for sub in c['sub']:
domains.append(sub + '.' + c['name'] + '.' + tld)
@ -342,7 +387,7 @@ if step[1]:
if step[2]:
print_info("\n++++++ STEP 2 : Create Nginx available configuration ++++++")
shell_command("sudo mkdir -p {} 2>/dev/null".format(c['available_path']))
shell_command("sudo mkdir -p '{}' 2>/dev/null".format(c['available_path']))
if generate_file(c['template_le_nginx_vhost'],
c['available_path'] + "/" + c['domain_main'],
{'second_domains': ' '.join(domains)}) == False:
@ -355,7 +400,7 @@ if step[2]:
if step[3]:
print_info("\n++++++ STEP 3 : Create sites-enabled Nginx link ++++++")
shell_command("ln -s {} {}".format(os.path.abspath(c['available_path'] + "/" + c['domain_main']),
shell_command("ln -s '{}' '{}'".format(os.path.abspath(c['available_path'] + "/" + c['domain_main']),
c['domain_main']),
cwd=c['enabled_path'])
@ -374,7 +419,7 @@ if step[4]:
if step[5]:
print_info("\n++++++ STEP 5 : create Nginx folder that will receive the Let's Encrypt certificate ++++++")
shell_command("sudo mkdir -p {}/{}".format(c['le_certificate_folder'],
shell_command("sudo mkdir -p '{}/{}'".format(c['le_certificate_folder'],
c['domain_main']))
##### create the acme configuration file
@ -382,7 +427,7 @@ if step[5]:
if step[6]:
print_info("\n++++++ STEP 6 : create the acme configuration file ++++++")
shell_command("sudo mkdir -p {} 2>/dev/null".format(c['acme_folder']))
shell_command("sudo mkdir -p '{}' 2>/dev/null".format(c['acme_folder']))
config_file = "{}/{}.conf".format(c['acme_folder'], c['domain_main'])
if generate_file(c['template_acme_conf'], config_file,
{"main_domain": c['domain_main'],
@ -397,8 +442,8 @@ if step[6]:
if step[7]:
print_info("\n++++++ STEP 7 : acme create ++++++")
shell_command("sudo mkdir -p {} 2>/dev/null".format(c['acme_folder']))
cmd = "sudo /usr/local/bin/acme_create --config {}/{}.conf"
shell_command("sudo mkdir -p '{}' 2>/dev/null".format(c['acme_folder']))
cmd = "sudo /usr/local/bin/acme_create --config '{}/{}.conf'"
shell_command(cmd.format(c['acme_folder'], c['domain_main']), get_stderr=True)
@ -407,8 +452,8 @@ if step[7]:
if step[8]:
print_info("\n++++++ STEP 8 : acme renew ++++++")
shell_command("sudo mkdir -p {} 2>/dev/null".format(c['acme_folder']))
cmd = "sudo -u acme /usr/local/bin/acme_renew --config {}/{}.conf"
shell_command("sudo mkdir -p '{}' 2>/dev/null".format(c['acme_folder']))
cmd = "sudo -u acme /usr/local/bin/acme_renew --config '{}/{}.conf'"
shell_command(cmd.format(c['acme_folder'], c['domain_main']), get_stderr=True)