Add --setup-no-install command

Acts the same as --setup, but does not install any packages. Will
raise an error if any required package is not already installed.
This commit is contained in:
James Valleroy 2016-08-31 19:09:02 -04:00 committed by Sunil Mohan Adapa
parent a1a8e17497
commit 548e0ebd60
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2
6 changed files with 30 additions and 7 deletions

View File

@ -20,6 +20,7 @@
ppp \ ppp \
pppoe \ pppoe \
python3 \ python3 \
python3-apt \
python3-augeas \ python3-augeas \
python3-bootstrapform \ python3-bootstrapform \
python3-cherrypy3 \ python3-cherrypy3 \

View File

@ -21,7 +21,6 @@ import django.conf
from django.contrib.messages import constants as message_constants from django.contrib.messages import constants as message_constants
import django.core.management import django.core.management
import django.core.wsgi import django.core.wsgi
from django.utils import translation
import importlib import importlib
import logging import logging
import os import os
@ -55,6 +54,9 @@ def parse_arguments():
parser.add_argument( parser.add_argument(
'--setup', default=False, nargs='*', '--setup', default=False, nargs='*',
help='run setup tasks on all essential modules and exit') help='run setup tasks on all essential modules and exit')
parser.add_argument(
'--setup-no-install', default=False, nargs='*',
help='run setup tasks without installing packages and exit')
parser.add_argument( parser.add_argument(
'--diagnose', action='store_true', default=False, '--diagnose', action='store_true', default=False,
help='run diagnostic tests and exit') help='run diagnostic tests and exit')
@ -249,14 +251,14 @@ def configure_django():
os.chmod(cfg.store_file, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP) os.chmod(cfg.store_file, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP)
def run_setup_and_exit(module_list): def run_setup_and_exit(module_list, allow_install=True):
"""Run setup on all essential modules and exit.""" """Run setup on all essential modules and exit."""
error_code = 0 error_code = 0
try: try:
if not module_list: if not module_list:
setup.setup_modules(essential=True) setup.setup_modules(essential=True, allow_install=allow_install)
else: else:
setup.setup_modules(module_list) setup.setup_modules(module_list, allow_install=allow_install)
except Exception as exception: except Exception as exception:
logger.error('Error running setup - %s', exception) logger.error('Error running setup - %s', exception)
error_code = 1 error_code = 1
@ -302,6 +304,8 @@ def main():
module_loader.load_modules() module_loader.load_modules()
if arguments.setup is not False: if arguments.setup is not False:
run_setup_and_exit(arguments.setup) run_setup_and_exit(arguments.setup)
if arguments.setup_no_install is not False:
run_setup_and_exit(arguments.setup, allow_install=False)
if arguments.diagnose: if arguments.diagnose:
run_diagnostics_and_exit() run_diagnostics_and_exit()

View File

@ -33,3 +33,8 @@ class ActionError(PlinthError):
class DomainRegistrationError(PlinthError): class DomainRegistrationError(PlinthError):
"""Domain registration failed""" """Domain registration failed"""
pass pass
class PackageNotInstalledError(PlinthError):
"""Could not complete module setup due to missing package."""
pass

View File

@ -19,10 +19,12 @@
Plinth module with utilites for performing application setup operations. Plinth module with utilites for performing application setup operations.
""" """
import apt
import logging import logging
import threading import threading
from . import package from . import package
from .errors import PackageNotInstalledError
import plinth import plinth
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -58,7 +60,7 @@ class Helper(object):
self.is_finished = None self.is_finished = None
return exception return exception
def run(self): def run(self, allow_install=True):
"""Execute the setup process.""" """Execute the setup process."""
# Setup for the module is already running # Setup for the module is already running
if self.current_operation: if self.current_operation:
@ -68,6 +70,7 @@ class Helper(object):
if current_version >= self.module.version: if current_version >= self.module.version:
return return
self.allow_install = allow_install
self.exception = None self.exception = None
self.current_operation = None self.current_operation = None
self.is_finished = False self.is_finished = False
@ -89,6 +92,14 @@ class Helper(object):
def install(self, package_names): def install(self, package_names):
"""Install a set of packages marking progress.""" """Install a set of packages marking progress."""
if self.allow_install is False:
# Raise error if packages are not already installed.
cache = apt.Cache()
for package_name in package_names:
if not cache[package_name].is_installed:
raise PackageNotInstalledError(package_name)
return
logger.info('Running install for module - %s, packages - %s', logger.info('Running install for module - %s, packages - %s',
self.module_name, package_names) self.module_name, package_names)
@ -150,7 +161,7 @@ def init(module_name, module):
module.setup_helper = Helper(module_name, module) module.setup_helper = Helper(module_name, module)
def setup_modules(module_list=None, essential=False): def setup_modules(module_list=None, essential=False, allow_install=True):
"""Run setup on selected or essential modules.""" """Run setup on selected or essential modules."""
logger.info('Running setup for modules, essential - %s, ' logger.info('Running setup for modules, essential - %s, '
'selected modules - %s', essential, module_list) 'selected modules - %s', essential, module_list)
@ -161,4 +172,4 @@ def setup_modules(module_list=None, essential=False):
if module_list and module_name not in module_list: if module_list and module_name not in module_list:
continue continue
module.setup_helper.run() module.setup_helper.run(allow_install=allow_install)

View File

@ -3,5 +3,6 @@ coverage >= 3.7
django >= 1.10.0 django >= 1.10.0
django-stronghold django-stronghold
psutil psutil
python-apt
python-augeas python-augeas
ruamel.yaml ruamel.yaml

View File

@ -184,6 +184,7 @@ setuptools.setup(
'django-bootstrap-form', 'django-bootstrap-form',
'django-stronghold', 'django-stronghold',
'psutil', 'psutil',
'python-apt',
'python-augeas', 'python-augeas',
'requests', 'requests',
'ruamel.yaml', 'ruamel.yaml',