Skip to content

libioc should support iocage's template jails #578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion iocage/Config/Jail/JailConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def update_special_property(self, name: str) -> None:
"""Triggered when a special property was updated."""
BaseConfig.update_special_property(self, name)

if (name == "ip6_addr") and (self.jail is not None):
if (name == "ip6_addr") and (self.jail.exists is True):
rc_conf = self.jail.rc_conf
rc_conf["rtsold_enable"] = "accept_rtadv" in str(self["ip6_addr"])

Expand Down
3 changes: 3 additions & 0 deletions iocage/Config/Jail/Properties/Defaultrouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def set(
if isinstance(data, str) is True:
data = iocage.helpers.parse_user_input(data)

if isinstance(data, (ipaddress.IPv4Address, ipaddress.IPv6Address)):
data = data._ip

if data is None:
gateway = None
self._ip = None
Expand Down
2 changes: 2 additions & 0 deletions iocage/Config/Jail/Properties/Resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ def set(
skip_on_error: bool=False
) -> None:
"""Clear and set all nameservers."""
if isinstance(value, ResolverProp):
value = value._entries.copy()
error_log_level = "warn" if (skip_on_error is True) else "error"
self._entries.clear()
method = self._get_method(value)
Expand Down
5 changes: 5 additions & 0 deletions iocage/Datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ def pkg(self) -> libzfs.ZFSDataset:
"""Get or create the pkg cache."""
return self._get_or_create_dataset("pkg")

@property
def templates(self) -> libzfs.ZFSDataset:
"""Get or create the legacy iocage templates dataset."""
return self._get_or_create_dataset("templates")

def _get_or_create_dataset(
self,
asset_name: str
Expand Down
19 changes: 12 additions & 7 deletions iocage/Jail.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""iocage Jail module."""
import itertools
import typing
import os
import random
Expand Down Expand Up @@ -326,7 +327,7 @@ def __init__(
}

if "id" in data.keys():
data["id"] = self._resolve_name(data["id"])
dataset, data["id"] = self._resolve_name(data["id"])

JailResource.__init__(
self,
Expand Down Expand Up @@ -2014,8 +2015,9 @@ def _get_absolute_path_from_jail_asset(

return iocage.Types.AbsolutePath(f"{self.root_path}{value}")

def _resolve_name(self, text: str) -> str:

def _resolve_name(self, text: str) -> typing.Tuple[
libzfs.ZFSDataset, str
]:
if (text is None) or (len(text) == 0):
raise iocage.errors.JailNotSupplied(logger=self.logger)

Expand All @@ -2027,16 +2029,19 @@ def _resolve_name(self, text: str) -> str:
root_datasets = resource_selector.filter_datasets(self.host.datasets)

for datasets_key, datasets in root_datasets.items():
for dataset in list(datasets.jails.children):
for dataset in itertools.chain(
datasets.jails.children,
datasets.templates.children
):
dataset_name = str(
dataset.name[(len(datasets.jails.name) + 1):]
)
dataset.name
).split('/')[-1]
humanreadable_name = iocage.helpers.to_humanreadable_name(
dataset_name
)
possible_names = [dataset_name, humanreadable_name]
if resource_selector.name in possible_names:
return dataset_name
return dataset, dataset_name

raise iocage.errors.JailNotFound(text, logger=self.logger)

Expand Down
2 changes: 1 addition & 1 deletion iocage/Jails.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(
iocage.ListableResource.ListableResource.__init__(
self,
sources=self.host.datasets,
namespace="jails",
namespace=["jails", "templates"],
filters=filters,
zfs=zfs,
logger=logger
Expand Down
15 changes: 11 additions & 4 deletions iocage/ListableResource.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"""iocage Resource module."""
import typing
import libzfs
import itertools
import abc

import iocage.Filter
Expand All @@ -42,7 +43,7 @@ class ListableResource(list):
def __init__(
self,
sources: 'iocage.Datasets.Datasets',
namespace: typing.Optional[str]=None,
namespace: typing.Optional[typing.Union[list, str]]=None,
filters: typing.Optional['iocage.Filter.Terms']=None,
logger: typing.Optional['iocage.Logger.Logger']=None,
zfs: typing.Optional['iocage.ZFS.ZFS']=None,
Expand All @@ -53,6 +54,8 @@ def __init__(
self.logger = iocage.helpers_object.init_logger(self, logger)
self.zfs = iocage.helpers_object.init_zfs(self, zfs)

if isinstance(namespace, str):
namespace = [namespace]
self.namespace = namespace
self.sources = sources
self.filters = filters
Expand Down Expand Up @@ -81,7 +84,7 @@ def __iter__(
self
) -> typing.Generator['iocage.Resource.Resource', None, None]:
"""Return an iterator over the child datasets."""
if self.namespace is None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we make this an OR with the below addition?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if I followed you correctly. Are you suggesting self.namespace is None or not self.namespace ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep! Stefan probably has a specific use for None.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I was not aware of that. not self.namespace should normally cover the general negation scenario but if we have a specific use case, then that needs to be followed through. Let's confirm this ? @gronke could you confirm this please ?

if not self.namespace:
raise iocage.errors.ListableResourceNamespaceUndefined(
logger=self.logger
)
Expand All @@ -94,8 +97,12 @@ def __iter__(
if (filters.match_source(root_name) is False):
# skip when the resources defined source does not match
continue
children = root_datasets.__getattribute__(self.namespace).children
for child_dataset in children:

children = [
root_datasets.__getattribute__(n).children
for n in self.namespace
]
for child_dataset in itertools.chain(*children):
name = self._get_asset_name_from_dataset(child_dataset)
if has_filters and (filters.match_key("name", name) is False):
# Skip all jails that do not even match the name
Expand Down