mirror of
https://github.com/certbot/certbot.git
synced 2026-06-04 14:26:10 -04:00
Add method and test for dumping nginx configs
This commit is contained in:
parent
4f3bf3d720
commit
d8ac31acae
4 changed files with 53 additions and 15 deletions
|
|
@ -105,6 +105,4 @@ def dumps(blocks, indentation=4):
|
|||
|
||||
|
||||
def dump(blocks, _file, indentation=4):
|
||||
_file.write(dumps(blocks, indentation))
|
||||
_file.close()
|
||||
return _file
|
||||
return _file.write(dumps(blocks, indentation))
|
||||
|
|
|
|||
|
|
@ -129,13 +129,15 @@ class NginxParser(object):
|
|||
"""
|
||||
enabled = True # We only look at enabled vhosts for now
|
||||
vhosts = []
|
||||
for filename, tree in self.parsed:
|
||||
for filename in self.parsed:
|
||||
tree = self.parsed[filename]
|
||||
vhost = obj.VirtulHost(filename,
|
||||
self._get_addrs(tree),
|
||||
self._get_ssl(tree),
|
||||
enabled,
|
||||
self._get_names(tree))
|
||||
vhosts.append(vhost)
|
||||
return vhosts
|
||||
|
||||
def add_dir_to_ifmodssl(self, aug_conf_path, directive, val):
|
||||
"""Adds directive and value to IfMod ssl block.
|
||||
|
|
@ -369,9 +371,10 @@ class NginxParser(object):
|
|||
if f in self.parsed:
|
||||
continue
|
||||
try:
|
||||
parsed = load(open(f))
|
||||
self.parsed[f] = parsed
|
||||
trees.append(parsed)
|
||||
with open(f) as fo:
|
||||
parsed = load(fo)
|
||||
self.parsed[f] = parsed
|
||||
trees.append(parsed)
|
||||
except IOError:
|
||||
logging.warn("Could not open file: %s" % f)
|
||||
except pyparsing.ParseException:
|
||||
|
|
@ -431,6 +434,23 @@ class NginxParser(object):
|
|||
raise errors.LetsEncryptNoInstallationError(
|
||||
"Could not find configuration root")
|
||||
|
||||
def filedump(self, ext='tmp'):
|
||||
"""Dumps parsed configurations into files.
|
||||
|
||||
:param str ext: The file extension to use for the dumped files. If
|
||||
empty, this overrides the existing conf files.
|
||||
|
||||
"""
|
||||
for filename in self.parsed:
|
||||
tree = self.parsed[filename]
|
||||
if ext:
|
||||
filename = filename + os.path.extsep + ext
|
||||
try:
|
||||
with open(filename, 'w') as f:
|
||||
dump(tree, f)
|
||||
except IOError:
|
||||
logging.error("Could not open file for writing: %s" % filename)
|
||||
|
||||
|
||||
def case_i(string):
|
||||
"""Returns case insensitive regex.
|
||||
|
|
|
|||
|
|
@ -95,6 +95,7 @@ class TestRawNginxParser(unittest.TestCase):
|
|||
['index', 'index.html index.htm']]]]])
|
||||
f = open(util.get_data_filename('nginx.new.conf'), 'w')
|
||||
dump(parsed, f)
|
||||
f.close()
|
||||
parsed_new = load(open(util.get_data_filename('nginx.new.conf')))
|
||||
self.assertEquals(parsed, parsed_new)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
"""Tests for letsencrypt.client.plugins.nginx.parser."""
|
||||
import glob
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
|
|
@ -29,8 +30,8 @@ class NginxParserTest(util.NginxTest):
|
|||
shutil.rmtree(self.work_dir)
|
||||
|
||||
def test_root_normalized(self):
|
||||
path = os.path.join(self.temp_dir, "debian_nginx_2_4/////"
|
||||
"two_vhost_80/../../testdata")
|
||||
path = os.path.join(self.temp_dir, "foo/////"
|
||||
"bar/../../testdata")
|
||||
parser = NginxParser(path, None)
|
||||
self.assertEqual(parser.root, self.config_path)
|
||||
|
||||
|
|
@ -46,20 +47,38 @@ class NginxParserTest(util.NginxTest):
|
|||
"""Test recursive conf file parsing.
|
||||
|
||||
"""
|
||||
self.parser = NginxParser(self.config_path, self.ssl_options)
|
||||
self.assertEqual(set(map(self.parser.abs_path,
|
||||
parser = NginxParser(self.config_path, self.ssl_options)
|
||||
self.assertEqual(set(map(parser.abs_path,
|
||||
['foo.conf', 'nginx.conf', 'server.conf',
|
||||
'sites-enabled/default',
|
||||
'sites-enabled/example.com'])),
|
||||
set(self.parser.parsed.keys()))
|
||||
set(parser.parsed.keys()))
|
||||
self.assertEqual([['server_name', 'somename alias another.alias']],
|
||||
self.parser.parsed[self.parser.abs_path(
|
||||
'server.conf')])
|
||||
parser.parsed[parser.abs_path('server.conf')])
|
||||
self.assertEqual([[['server'], [['listen', '9000'],
|
||||
['server_name', 'example.com']]]],
|
||||
self.parser.parsed[self.parser.abs_path(
|
||||
parser.parsed[parser.abs_path(
|
||||
'sites-enabled/example.com')])
|
||||
|
||||
def test_abs_path(self):
|
||||
parser = NginxParser(self.config_path, self.ssl_options)
|
||||
self.assertEqual('/etc/nginx/*', parser.abs_path('/etc/nginx/*'))
|
||||
self.assertEqual(os.path.join(self.config_path, 'foo/bar/'),
|
||||
parser.abs_path('foo/bar/'))
|
||||
|
||||
def test_filedump(self):
|
||||
parser = NginxParser(self.config_path, self.ssl_options)
|
||||
parser.filedump('test')
|
||||
# pylint: disable=protected-access
|
||||
parsed = parser._parse_files(parser.abs_path(
|
||||
'sites-enabled/example.com.test'))
|
||||
self.assertEqual(3, len(glob.glob(parser.abs_path('*.test'))))
|
||||
self.assertEqual(2, len(
|
||||
glob.glob(parser.abs_path('sites-enabled/*.test'))))
|
||||
self.assertEqual([[['server'], [['listen', '9000'],
|
||||
['server_name', 'example.com']]]],
|
||||
parsed[0])
|
||||
|
||||
# def test_find_dir(self):
|
||||
# from letsencrypt.client.plugins.nginx.parser import case_i
|
||||
# test = self.parser.find_dir(case_i("Listen"), "443")
|
||||
|
|
|
|||
Loading…
Reference in a new issue