Skip to content

Commit

Permalink
Merge pull request certbot#3356 from certbot/osrelease_like
Browse files Browse the repository at this point in the history
Add os-release ID_LIKE parsing if original distribution mapping not found in constants
  • Loading branch information
pde authored Aug 5, 2016
2 parents eff181c + c346cdf commit 274702c
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 4 deletions.
65 changes: 62 additions & 3 deletions certbot-apache/certbot_apache/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,23 @@
import pkg_resources
from certbot import util


CLI_DEFAULTS_DEFAULT = dict(
server_root="/etc/apache2",
vhost_root="/etc/apache2/sites-available",
vhost_files="*",
version_cmd=['apache2ctl', '-v'],
define_cmd=['apache2ctl', '-t', '-D', 'DUMP_RUN_CFG'],
restart_cmd=['apache2ctl', 'graceful'],
conftest_cmd=['apache2ctl', 'configtest'],
enmod=None,
dismod=None,
le_vhost_ext="-le-ssl.conf",
handle_mods=False,
handle_sites=False,
challenge_location="/etc/apache2",
MOD_SSL_CONF_SRC=pkg_resources.resource_filename(
"certbot_apache", "options-ssl-apache.conf")
)
CLI_DEFAULTS_DEBIAN = dict(
server_root="/etc/apache2",
vhost_root="/etc/apache2/sites-available",
Expand Down Expand Up @@ -71,7 +87,25 @@
MOD_SSL_CONF_SRC=pkg_resources.resource_filename(
"certbot_apache", "options-ssl-apache.conf")
)
CLI_DEFAULTS_SUSE = dict(
server_root="/etc/apache2",
vhost_root="/etc/apache2/vhosts.d",
vhost_files="*.conf",
version_cmd=['apache2ctl', '-v'],
define_cmd=['apache2ctl', '-t', '-D', 'DUMP_RUN_CFG'],
restart_cmd=['apache2ctl', 'graceful'],
conftest_cmd=['apache2ctl', 'configtest'],
enmod="a2enmod",
dismod="a2dismod",
le_vhost_ext="-le-ssl.conf",
handle_mods=False,
handle_sites=False,
challenge_location="/etc/apache2/vhosts.d",
MOD_SSL_CONF_SRC=pkg_resources.resource_filename(
"certbot_apache", "options-ssl-apache.conf")
)
CLI_DEFAULTS = {
"default": CLI_DEFAULTS_DEFAULT,
"debian": CLI_DEFAULTS_DEBIAN,
"ubuntu": CLI_DEFAULTS_DEBIAN,
"centos": CLI_DEFAULTS_CENTOS,
Expand All @@ -83,6 +117,8 @@
"gentoo": CLI_DEFAULTS_GENTOO,
"gentoo base system": CLI_DEFAULTS_GENTOO,
"darwin": CLI_DEFAULTS_DARWIN,
"opensuse": CLI_DEFAULTS_SUSE,
"suse": CLI_DEFAULTS_SUSE,
}
"""CLI defaults."""

Expand Down Expand Up @@ -115,13 +151,36 @@


def os_constant(key):
"""Get a constant value for operating system
"""
Get a constant value for operating system
:param key: name of cli constant
:return: value of constant for active os
"""

os_info = util.get_os_info()
try:
constants = CLI_DEFAULTS[os_info[0].lower()]
except KeyError:
constants = CLI_DEFAULTS["debian"]
constants = os_like_constants()
if not constants:
constants = CLI_DEFAULTS["default"]
return constants[key]


def os_like_constants():
"""
Try to get constants for distribution with
similar layout and configuration, indicated by
/etc/os-release variable "LIKE"
:returns: Constants dictionary
:rtype: `dict`
"""

os_like = util.get_systemd_os_like()
if os_like:
for os_name in os_like:
if os_name in CLI_DEFAULTS.keys():
return CLI_DEFAULTS[os_name]
return {}
17 changes: 17 additions & 0 deletions certbot-apache/certbot_apache/tests/constants_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,20 @@ def test_get_default_value(self, os_info):
os_info.return_value = ('Nonexistent Linux', '', '')
self.assertEqual(constants.os_constant("vhost_root"),
"/etc/apache2/sites-available")

@mock.patch("certbot.util.get_os_info")
def test_get_default_constants(self, os_info):
os_info.return_value = ('Nonexistent Linux', '', '')
with mock.patch("certbot.util.get_systemd_os_like") as os_like:
# Get defaults
os_like.return_value = False
c_hm = constants.os_constant("handle_mods")
c_sr = constants.os_constant("server_root")
self.assertFalse(c_hm)
self.assertEqual(c_sr, "/etc/apache2")
# Use darwin as like test target
os_like.return_value = ["something", "nonexistent", "darwin"]
d_vr = constants.os_constant("vhost_root")
d_em = constants.os_constant("enmod")
self.assertFalse(d_em)
self.assertEqual(d_vr, "/etc/apache2/other")
2 changes: 1 addition & 1 deletion certbot/tests/testdata/os-release
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
NAME="SystemdOS"
VERSION="42.42.42 LTS, Unreal"
ID=systemdos
ID_LIKE=debian
ID_LIKE="something nonexistent debian"
VERSION_ID="42"
HOME_URL="http://www.example.com/"
SUPPORT_URL="http://help.example.com/"
9 changes: 9 additions & 0 deletions certbot/tests/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,15 @@ def test_systemd_os_release(self):
with mock.patch('os.path.isfile', return_value=False):
self.assertEqual(get_systemd_os_info(), ("", ""))

def test_systemd_os_release_like(self):
from certbot.util import get_systemd_os_like

with mock.patch('os.path.isfile', return_value=True):
id_likes = get_systemd_os_like(test_util.vector_path(
"os-release"))
self.assertEqual(len(id_likes), 3)
self.assertTrue("debian" in id_likes)

@mock.patch("certbot.util.subprocess.Popen")
def test_non_systemd_os_info(self, popen_mock):
from certbot.util import (get_os_info, get_python_os_info,
Expand Down
13 changes: 13 additions & 0 deletions certbot/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,19 @@ def get_systemd_os_info(filepath="/etc/os-release"):
return (os_name, os_version)


def get_systemd_os_like(filepath="/etc/os-release"):
"""
Get a list of strings that indicate the distribution likeness to
other distributions.
:param str filepath: File path of os-release file
:returns: List of distribution acronyms
:rtype: `list` of `str`
"""

return _get_systemd_os_release_var("ID_LIKE", filepath).split(" ")


def _get_systemd_os_release_var(varname, filepath="/etc/os-release"):
"""
Get single value from systemd /etc/os-release
Expand Down

0 comments on commit 274702c

Please sign in to comment.