Skip to content

Commit

Permalink
package removal
Browse files Browse the repository at this point in the history
  • Loading branch information
gronke committed Oct 1, 2018
1 parent 8f92080 commit cee9351
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 7 deletions.
31 changes: 25 additions & 6 deletions ioc/pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,34 @@
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""Jail package management subcommand for the CLI."""
import typing
import click

import iocage.Jail
import iocage.Pkg
import iocage.Logger
import iocage.errors

__rootcmd__ = True
from .shared.click import IocageClickContext


@click.command(name="pkg", help="Manage packages in a jail.")
@click.pass_context
@click.option(
"--remove", "-r",
"remove",
is_flag=True,
default=False,
help="Remove the packages instead of installing/updating them."
)
@click.argument("jail")
@click.argument("packages", nargs=-1)
def cli(ctx, jail, packages):
def cli(
ctx: IocageClickContext,
remove: bool,
jail: str,
packages: typing.Tuple[str, ...]
) -> None:
"""Manage packages within jails using an offline mirror."""
logger = ctx.parent.logger

Expand All @@ -57,10 +70,16 @@ def cli(ctx, jail, packages):
zfs=ctx.parent.zfs,
host=ctx.parent.host
)
events = pkg.fetch_and_install(
jail=ioc_jail,
packages=list(packages)
)
if remove is False:
events = pkg.fetch_and_install(
jail=ioc_jail,
packages=list(packages)
)
else:
events = pkg.remove(
jail=ioc_jail,
packages=list(packages)
)
ctx.parent.print_events(events)
except iocage.errors.IocageException:
exit(1)
Expand Down
42 changes: 41 additions & 1 deletion iocage/Pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def install(
"install",
"--yes",
"--repository", "libiocage",
" ".join(packages)
" ".join(_packages)
])
] + postinstall)
temporary_jail = self._get_temporary_jail(jail)
Expand All @@ -242,6 +242,46 @@ def install(
yield packageInstallEvent.fail(e)
raise e

def remove(
self,
packages: typing.Union[str, typing.List[str]],
jail: 'iocage.Jail.JailGenerator',
event_scope: typing.Optional['iocage.events.Scope']=None
) -> typing.Generator[iocage.events.IocageEvent, None, None]:
"""Remove installed packages from a jail."""
_packages = self._normalize_packages(packages)

packageRemoveEvent = iocage.events.PackageRemove(
packages=_packages,
jail=jail,
scope=event_scope
)
command = [
"/usr/sbin/pkg",
"remove",
"--yes",
" ".join(_packages)
]
yield packageRemoveEvent.begin()
try:
if jail.running is False:
temporary_jail = self._get_temporary_jail(jail)
_command = "\n".join([
"export ASSUME_ALWAYS_YES=yes",
" ".join(command)
])
yield from temporary_jail.fork_exec(
_command,
passthru=False,
event_scope=packageRemoveEvent.scope
)
else:
jail.exec(command)
except Exception as err:
yield packageRemoveEvent.fail(err)
raise err
yield packageRemoveEvent.end()

def _get_latest_pkg_archive(self, package_source_directory: str) -> str:
for package_archive in os.listdir(f"{package_source_directory}/cache"):
if package_archive.endswith(".txz") is False:
Expand Down
15 changes: 15 additions & 0 deletions iocage/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,21 @@ def __init__(
JailEvent.__init__(self, jail=jail, message=message, scope=scope)


class PackageRemove(JailEvent):
"""Remove packages from a jail."""

def __init__(
self,
packages: typing.List[str],
jail: 'iocage.Jail.JailGenerator',
message: typing.Optional[str]=None,
scope: typing.Optional[Scope]=None
) -> None:

self.packages = packages
JailEvent.__init__(self, jail=jail, message=message, scope=scope)


class PackageConfiguration(JailEvent):
"""Install packages in a jail."""

Expand Down

0 comments on commit cee9351

Please sign in to comment.