Skip to content

Commit

Permalink
jail property resolver=None skips touching /etc/resolv.conf
Browse files Browse the repository at this point in the history
  • Loading branch information
gronke committed Nov 20, 2017
1 parent 1f2f619 commit 3deb720
Showing 1 changed file with 49 additions and 26 deletions.
75 changes: 49 additions & 26 deletions iocage/lib/Config/Jail/Properties/Resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import typing
import collections
import shutil

import iocage.lib.helpers
Expand All @@ -31,19 +32,20 @@
import iocage.lib.Logger


class ResolverProp(list):
class ResolverProp(collections.MutableSequence):

config: 'iocage.lib.Config.Jail.JailConfig.JailConfig'
property_name: str = "resolver"
none_matches: typing.List[str] = ["/dev/null", "-", ""]
_entries: typing.List[str] = []

def __init__(
self,
config,
config: 'iocage.lib.Config.Jail.JailConfig.JailConfig',
logger: typing.Optional[iocage.lib.Logger.Logger]=None,
**kwargs
) -> None:

list.__init__(self, [])
self.logger = iocage.lib.helpers.init_logger(self, logger)
self.config = config
self.config.attach_special_property(
Expand All @@ -60,56 +62,61 @@ def method(self) -> str:
return self._get_method(self.value)

def _get_method(self, value: typing.Any) -> str:
if value == "/etc/resolv.conf":
return "copy"

elif value == "/dev/null":
if value is None:
return "skip"

elif value == "/etc/resolv.conf":
return "copy"
else:
return "manual"

@property
def value(self) -> str:
return str(self.config.data["resolver"])

def apply(self, jail):
def value(self) -> typing.Optional[str]:
value = self.config.data["resolver"]
try:
iocage.lib.helpers.parse_none(value, self.none_matches)
return None
except TypeError:
return str(value)

def apply(self, jail: 'iocage.lib.Jail.JailGenerator') -> None:
self.logger.verbose(
f"Configuring nameserver for Jail '{jail.humanreadable_name}'"
)

remote_path = f"{jail.root_path}/{self.conf_file_path}"

if self.method == "copy":
if self.method == "skip":
self.logger.verbose("resolv.conf untouched")

elif self.method == "copy":
shutil.copy(self.conf_file_path, remote_path)
self.logger.verbose("resolv.conf copied from host")

elif self.method == "manual":
lines = map(lambda address: f"nameserver {address}", self._entries)
with open(remote_path, "w") as f:
f.write("\n".join(self))
f.write("\n".join(lines))
f.close()
self.logger.verbose("resolv.conf written manually")

else:
self.logger.verbose("resolv.conf not touched")

def set(
self,
value: typing.Optional[typing.Union[str, list]]=None,
notify: bool=True
) -> None:

self.clear()
self._entries.clear()
method = self._get_method(value)
if method == "manual":
if isinstance(value, str):
self += str(value).split(";") # noqa: T484
self._entries += str(value).split(";") # noqa: T484
elif isinstance(value, list):
self += list(value) # noqa: T484
elif value is None:
return
self._entries += list(value) # noqa: T484
else:
raise TypeError("value can be list or string")
elif method == "skip":
# directly set config property
self.config.data["resolver"] = None
else:
if isinstance(value, str):
self.append(str(value), notify=False)
Expand All @@ -120,10 +127,27 @@ def set(

self.__notify(notify)

def append(self, value: str, notify: bool=True):
list.append(self, value)
def append(self, value: str, notify: bool=True) -> None:
self._entries.append(value)
self.__notify(notify)

def insert(
self,
index: int,
value: str
) -> None:
self._entries.insert(index, value)
self.__notify()

def __delitem__(self, key) -> None:
del self._entries[key]

def __getitem__(self, key) -> typing.Any:
return self._entries[key]

def __len__(self):
return self._entries.__len__()

def __setitem__( # noqa: T484
self,
key: str,
Expand All @@ -134,8 +158,7 @@ def __setitem__( # noqa: T484
self.__notify()

def __str__(self):
out = ";".join(list(self))
return out
return iocage.lib.helpers.to_string(self._entries, delimiter=";")

def __notify(self, notify: bool=True):
if not notify:
Expand Down

0 comments on commit 3deb720

Please sign in to comment.