mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-28 08:03:36 +00:00
- Use action helper so that Plinth can run unprivilaged and action script can run as root. - Use Status-Fd feature of apt-get to report progress. Don't report much detail. - Capture all stderr of the apt-get process and present it only in case of failure. - Remove package installation using PackageKit. Remove dependency on PackageKit. - Merge --setup package installation with regular package installation. This should fix the following problems: - PackageKit throws errors when APT encounters an error and later corrects them and proceeds well. This is reported upstream but not fixed. - PackageKit does not install recommends by default and there is no easy way to tell it to do so. - In some rare cases, PackageKit could get stuck for interactive input even though interactive flag is set to false. - PackageKit does not work without network manager connections. (Could have been mitigated by altering packagekit configuration). - PackageKit glib library leaks file descriptors after each operation. This leads to running out of fds during long running refresh operations such as OpenVPN setup. (This should have subsided by not checking package install with the new setup mechanism.)] Known issues: - In development mode, inside action scripts the python modules are always loaded from system path and not development directory. - With PackageKit it is possible to run multiple operations simultaneously. Others would wait while the first is being installed. With new implementation, the others error out unable to obtain lock.
107 lines
3.2 KiB
Python
Executable File
107 lines
3.2 KiB
Python
Executable File
#!/usr/bin/python3
|
|
#
|
|
# This file is part of Plinth.
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
# License, or (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
|
|
"""
|
|
Wrapper to handle package installation with apt-get.
|
|
"""
|
|
|
|
import argparse
|
|
from importlib import import_module
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
from plinth import cfg
|
|
|
|
|
|
def parse_arguments():
|
|
"""Return parsed command line arguments as dictionary."""
|
|
parser = argparse.ArgumentParser()
|
|
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
|
|
|
|
subparsers.add_parser('update', help='update the package lists')
|
|
|
|
subparser = subparsers.add_parser('install', help='install packages')
|
|
subparser.add_argument(
|
|
'module', help='name of module for which package is being installed')
|
|
subparser.add_argument(
|
|
'packages', nargs='+', help='list of packages to install')
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
def _run_apt_command(arguments):
|
|
"""Run apt-get with provided arguments."""
|
|
# Ask apt-get to output its progress to file descriptor 3.
|
|
command = ['apt-get', '--assume-yes', '--quiet=2', '--option',
|
|
'APT::Status-Fd=3'] + arguments
|
|
|
|
# Duplicate stdout to file descriptor 3 for this process.
|
|
os.dup2(1, 3)
|
|
|
|
# Pass on file descriptor 3 instead of closing it. Close stdout
|
|
# so that regular output is ignored.
|
|
env = os.environ.copy()
|
|
env['DEBIAN_FRONTEND'] = 'noninteractive'
|
|
process = subprocess.run(
|
|
command, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
|
|
close_fds=False, env=env)
|
|
sys.exit(process.returncode)
|
|
|
|
|
|
def subcommand_update(arguments):
|
|
"""Update apt package lists."""
|
|
_run_apt_command(['update'])
|
|
|
|
|
|
def subcommand_install(arguments):
|
|
"""Install packages using apt-get."""
|
|
try:
|
|
_assert_managed_packages(arguments.module, arguments.packages)
|
|
except Exception as exception:
|
|
print('Access check failed:', exception, file=sys.stderr)
|
|
sys.exit(99)
|
|
|
|
_run_apt_command(['install'] + arguments.packages)
|
|
|
|
|
|
def _assert_managed_packages(module, packages):
|
|
"""Check that list of packages are in fact managed by module."""
|
|
cfg.read()
|
|
module_file = os.path.join(cfg.config_dir, 'modules-enabled', module)
|
|
|
|
with open(module_file, 'r') as file_handle:
|
|
module_path = file_handle.read().strip()
|
|
|
|
module = import_module(module_path)
|
|
for package in packages:
|
|
assert package in module.managed_packages
|
|
|
|
|
|
def main():
|
|
"""Parse arguments and perform all duties."""
|
|
arguments = parse_arguments()
|
|
|
|
subcommand = arguments.subcommand.replace('-', '_')
|
|
subcommand_method = globals()['subcommand_' + subcommand]
|
|
subcommand_method(arguments)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|