Skip to content

Commit 2a53eee

Browse files
authored
Merge pull request #103 from TomasTomecek/fix-101
workaround ansible bug with selinux on and...
2 parents 00b1e3a + 3a29a2e commit 2a53eee

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

ansible_bender/core.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"""
3131
import copy
3232
import datetime
33+
import importlib
3334
import json
3435
import logging
3536
import os
@@ -38,6 +39,7 @@
3839
import subprocess
3940
import sys
4041
import tempfile
42+
from pathlib import Path
4143

4244
import yaml
4345

@@ -238,12 +240,38 @@ def __init__(self, playbook_path):
238240
self.metadata = ImageMetadata()
239241
self.build.metadata = self.metadata
240242

243+
def _check_selinux_iz_gud(self):
244+
"""
245+
This is a workaround for a weird behavior of ansible: if selinux is
246+
in the enforcing mode and python3-libselinux is not installed, ansible freezes
247+
248+
https://bugzilla.redhat.com/show_bug.cgi?id=1696706
249+
:return:
250+
"""
251+
try:
252+
enforcing_status = Path("/sys/fs/selinux/enforce").read_text()
253+
except FileNotFoundError:
254+
logger.debug("this system is not using selinux, /sys/fs/selinux/enforce is not present")
255+
return
256+
logger.debug(f"selinux enforce status = {enforcing_status}")
257+
# it can be enforcing or not, selinux python module needs to be present
258+
try:
259+
importlib.import_module("selinux")
260+
except ModuleNotFoundError:
261+
raise RuntimeError(
262+
"\nThis system is using selinux(8) and selinux python module is not installed. "
263+
"There is a known issue in ansible that it freezes in this setup:\n"
264+
" https://bugzilla.redhat.com/show_bug.cgi?id=1696706\n"
265+
"Please install libselinux python bindings (on Fedora the package name is python3-libselinux)."
266+
)
267+
241268
def expand_pb_vars(self):
242269
"""
243270
populate vars from a playbook, defined in vars section
244271
245272
:return: dict with the content of ansible_bender var
246273
"""
274+
self._check_selinux_iz_gud()
247275
with open(self.playbook_path) as fd:
248276
plays = yaml.safe_load(fd)
249277

tests/unit/test_ansibla.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import importlib
2+
from functools import partial
3+
from pathlib import Path
4+
5+
import pytest
6+
from flexmock import flexmock
7+
8+
from ansible_bender.core import PbVarsParser
9+
10+
11+
def mock_read_text(return_val=None, raise_exc=False):
12+
if raise_exc:
13+
def _f():
14+
raise FileNotFoundError()
15+
flexmock(Path, read_text=_f)
16+
else:
17+
flexmock(Path, read_text=lambda: return_val)
18+
19+
20+
def mock_import_module(raise_exc=False):
21+
if raise_exc:
22+
def _f(name, package=None):
23+
raise ModuleNotFoundError()
24+
flexmock(importlib, import_module=_f)
25+
else:
26+
flexmock(importlib, import_module=lambda name: None)
27+
28+
29+
@pytest.mark.parametrize("mock_r_t,mock_i_m,should_raise", (
30+
(
31+
partial(mock_read_text, "1"),
32+
partial(mock_import_module, False),
33+
False
34+
),
35+
(
36+
partial(mock_read_text, "1"),
37+
partial(mock_import_module, True),
38+
True
39+
),
40+
(
41+
partial(mock_read_text, "0"),
42+
partial(mock_import_module, False),
43+
False
44+
),
45+
(
46+
partial(mock_read_text, "0"),
47+
partial(mock_import_module, True),
48+
True
49+
),
50+
(
51+
partial(mock_read_text, None, True),
52+
partial(mock_import_module, False),
53+
False
54+
),
55+
))
56+
def test_ansible_selinux_workaround(mock_r_t, mock_i_m, should_raise):
57+
mock_r_t()
58+
mock_i_m()
59+
p = PbVarsParser("")
60+
if should_raise:
61+
with pytest.raises(RuntimeError) as ex:
62+
p._check_selinux_iz_gud()
63+
assert "libselinux" in str(ex.value)
64+
else:
65+
p._check_selinux_iz_gud()

0 commit comments

Comments
 (0)