144 lines
4.9 KiB
Python
Executable File
144 lines
4.9 KiB
Python
Executable File
#!/usr/bin/python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import argparse
|
|
import time
|
|
import sys
|
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
from lxcmgr import lxcmgr
|
|
from lxcmgr.pkgmgr import App, Stage, PkgMgr
|
|
|
|
parser = argparse.ArgumentParser(description='LXC container and package manager')
|
|
subparsers = parser.add_subparsers()
|
|
|
|
parser_list = subparsers.add_parser('list')
|
|
subparsers_list = parser_list.add_subparsers()
|
|
parser_list_installed = subparsers_list.add_parser('installed')
|
|
parser_list_installed.set_defaults(action='list-installed')
|
|
parser_list_online = subparsers_list.add_parser('online')
|
|
parser_list_online.set_defaults(action='list-online')
|
|
parser_list_updates = subparsers_list.add_parser('updates')
|
|
parser_list_updates.set_defaults(action='list-updates')
|
|
|
|
parser_install = subparsers.add_parser('install')
|
|
parser_install.set_defaults(action='install')
|
|
parser_install.add_argument('app', help='Application to install')
|
|
|
|
parser_update = subparsers.add_parser('update')
|
|
parser_update.set_defaults(action='update')
|
|
parser_update.add_argument('app', help='Application to update')
|
|
|
|
parser_uninstall = subparsers.add_parser('uninstall')
|
|
parser_uninstall.set_defaults(action='uninstall')
|
|
parser_uninstall.add_argument('app', help='Application to uninstall')
|
|
|
|
parser_container = subparsers.add_parser('container')
|
|
subparsers_container = parser_container.add_subparsers()
|
|
|
|
parser_container_prepare = subparsers_container.add_parser('prepare')
|
|
parser_container_prepare.set_defaults(action='container-prepare')
|
|
parser_container_prepare.add_argument('layers', help='OverlayFS LXC rootfs layers')
|
|
parser_container_prepare.add_argument('container', help='Container name')
|
|
parser_container_prepare.add_argument('lxc', nargs=argparse.REMAINDER)
|
|
|
|
parser_container_cleanup = subparsers_container.add_parser('cleanup')
|
|
parser_container_cleanup.set_defaults(action='container-cleanup')
|
|
parser_container_cleanup.add_argument('container', help='Container name')
|
|
parser_container_cleanup.add_argument('lxc', nargs=argparse.REMAINDER)
|
|
|
|
def print_apps(packages):
|
|
for app, meta in packages.items():
|
|
print('{} {}'.format(app, meta['version']))
|
|
for key, value in meta['meta'].items():
|
|
print(' {}: {}'.format(key, value))
|
|
|
|
def list_online():
|
|
pm = PkgMgr()
|
|
pm.fetch_online_packages()
|
|
apps = pm.online_packages['apps']
|
|
if apps:
|
|
print_apps(apps)
|
|
else:
|
|
print('Repository lists no applications packages.')
|
|
|
|
def list_installed():
|
|
pm = PkgMgr()
|
|
apps = pm.installed_packages['apps']
|
|
if apps:
|
|
print_apps(apps)
|
|
else:
|
|
print('No applications packages installed.')
|
|
|
|
def list_updates():
|
|
pm = PkgMgr()
|
|
apps = pm.installed_packages['apps']
|
|
if apps:
|
|
updateable_apps = [app for app in apps if pm.has_update(app)]
|
|
if updateable_apps:
|
|
updates = {name: meta for (name, meta) in pm.online_packages['apps'].items() if name in updateable_apps}
|
|
print_apps(updates)
|
|
else:
|
|
print('All installed application packages are up-to-date.')
|
|
else:
|
|
print('No applications packages installed.')
|
|
|
|
def install_app(app):
|
|
pm = PkgMgr()
|
|
app = App(app)
|
|
with ThreadPoolExecutor() as executor:
|
|
future = executor.submit(pm.install_app, app)
|
|
while not future.done():
|
|
time.sleep(1)
|
|
print_install_status(app)
|
|
# Get the result of the future and let it raise exception, if there was any
|
|
data = future.result()
|
|
print_install_status(app)
|
|
|
|
def print_install_status(app):
|
|
if app.stage == Stage.QUEUED:
|
|
print('\x1b[KQueued...', end='\r')
|
|
elif app.stage == Stage.DOWNLOAD:
|
|
print('\x1b[KDownloading... {} % ({} / {} bytes)'.format(app.percent_processed, app.bytes_processed, app.bytes_total), end='\r')
|
|
elif app.stage == Stage.UNPACK:
|
|
print('\x1b[KUnpacking...', end='\r')
|
|
elif app.stage == Stage.INSTALL:
|
|
print('\x1b[KInstalling...', end='\r')
|
|
elif app.stage == Stage.UNINSTALL:
|
|
print('\x1b[KUninstalling...', end='\r')
|
|
elif app.stage == Stage.DONE:
|
|
print('\x1b[KDone.')
|
|
|
|
def update_app(app):
|
|
pm = PkgMgr()
|
|
pm.update_app(app)
|
|
|
|
def uninstall_app(app):
|
|
pm = PkgMgr()
|
|
pm.uninstall_app(app)
|
|
|
|
args = parser.parse_args()
|
|
if not hasattr(args, 'action'):
|
|
parser.print_usage()
|
|
sys.exit(1)
|
|
|
|
if args.action == 'list-installed':
|
|
list_installed()
|
|
elif args.action == 'list-online':
|
|
list_online()
|
|
elif args.action == 'list-updates':
|
|
list_updates()
|
|
elif args.action == 'install':
|
|
install_app(args.app)
|
|
elif args.action == 'update':
|
|
update_app(args.app)
|
|
elif args.action == 'uninstall':
|
|
uninstall_app(args.app)
|
|
elif args.action == 'container-prepare':
|
|
# Used with LXC hooks on container startup
|
|
lxcmgr.prepare_container(args.container, args.layers)
|
|
elif args.action == 'container-cleanup':
|
|
# Used with LXC hooks on container stop
|
|
lxcmgr.cleanup_container(args.container)
|