Skip to content

Commit cee9351

Browse files
committed
package removal
1 parent 8f92080 commit cee9351

File tree

3 files changed

+81
-7
lines changed

3 files changed

+81
-7
lines changed

ioc/pkg.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,34 @@
2323
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2424
# POSSIBILITY OF SUCH DAMAGE.
2525
"""Jail package management subcommand for the CLI."""
26+
import typing
2627
import click
2728

2829
import iocage.Jail
2930
import iocage.Pkg
3031
import iocage.Logger
3132
import iocage.errors
3233

33-
__rootcmd__ = True
34+
from .shared.click import IocageClickContext
3435

3536

3637
@click.command(name="pkg", help="Manage packages in a jail.")
3738
@click.pass_context
39+
@click.option(
40+
"--remove", "-r",
41+
"remove",
42+
is_flag=True,
43+
default=False,
44+
help="Remove the packages instead of installing/updating them."
45+
)
3846
@click.argument("jail")
3947
@click.argument("packages", nargs=-1)
40-
def cli(ctx, jail, packages):
48+
def cli(
49+
ctx: IocageClickContext,
50+
remove: bool,
51+
jail: str,
52+
packages: typing.Tuple[str, ...]
53+
) -> None:
4154
"""Manage packages within jails using an offline mirror."""
4255
logger = ctx.parent.logger
4356

@@ -57,10 +70,16 @@ def cli(ctx, jail, packages):
5770
zfs=ctx.parent.zfs,
5871
host=ctx.parent.host
5972
)
60-
events = pkg.fetch_and_install(
61-
jail=ioc_jail,
62-
packages=list(packages)
63-
)
73+
if remove is False:
74+
events = pkg.fetch_and_install(
75+
jail=ioc_jail,
76+
packages=list(packages)
77+
)
78+
else:
79+
events = pkg.remove(
80+
jail=ioc_jail,
81+
packages=list(packages)
82+
)
6483
ctx.parent.print_events(events)
6584
except iocage.errors.IocageException:
6685
exit(1)

iocage/Pkg.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def install(
218218
"install",
219219
"--yes",
220220
"--repository", "libiocage",
221-
" ".join(packages)
221+
" ".join(_packages)
222222
])
223223
] + postinstall)
224224
temporary_jail = self._get_temporary_jail(jail)
@@ -242,6 +242,46 @@ def install(
242242
yield packageInstallEvent.fail(e)
243243
raise e
244244

245+
def remove(
246+
self,
247+
packages: typing.Union[str, typing.List[str]],
248+
jail: 'iocage.Jail.JailGenerator',
249+
event_scope: typing.Optional['iocage.events.Scope']=None
250+
) -> typing.Generator[iocage.events.IocageEvent, None, None]:
251+
"""Remove installed packages from a jail."""
252+
_packages = self._normalize_packages(packages)
253+
254+
packageRemoveEvent = iocage.events.PackageRemove(
255+
packages=_packages,
256+
jail=jail,
257+
scope=event_scope
258+
)
259+
command = [
260+
"/usr/sbin/pkg",
261+
"remove",
262+
"--yes",
263+
" ".join(_packages)
264+
]
265+
yield packageRemoveEvent.begin()
266+
try:
267+
if jail.running is False:
268+
temporary_jail = self._get_temporary_jail(jail)
269+
_command = "\n".join([
270+
"export ASSUME_ALWAYS_YES=yes",
271+
" ".join(command)
272+
])
273+
yield from temporary_jail.fork_exec(
274+
_command,
275+
passthru=False,
276+
event_scope=packageRemoveEvent.scope
277+
)
278+
else:
279+
jail.exec(command)
280+
except Exception as err:
281+
yield packageRemoveEvent.fail(err)
282+
raise err
283+
yield packageRemoveEvent.end()
284+
245285
def _get_latest_pkg_archive(self, package_source_directory: str) -> str:
246286
for package_archive in os.listdir(f"{package_source_directory}/cache"):
247287
if package_archive.endswith(".txz") is False:

iocage/events.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,21 @@ def __init__(
885885
JailEvent.__init__(self, jail=jail, message=message, scope=scope)
886886

887887

888+
class PackageRemove(JailEvent):
889+
"""Remove packages from a jail."""
890+
891+
def __init__(
892+
self,
893+
packages: typing.List[str],
894+
jail: 'iocage.Jail.JailGenerator',
895+
message: typing.Optional[str]=None,
896+
scope: typing.Optional[Scope]=None
897+
) -> None:
898+
899+
self.packages = packages
900+
JailEvent.__init__(self, jail=jail, message=message, scope=scope)
901+
902+
888903
class PackageConfiguration(JailEvent):
889904
"""Install packages in a jail."""
890905

0 commit comments

Comments
 (0)