Merge pull request #2748 from matmair/matmair/issue2524

Run plugin install on startup
This commit is contained in:
Oliver 2022-03-21 07:15:37 +11:00 committed by GitHub
commit c082cec3ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 2 deletions

View File

@ -900,3 +900,4 @@ if DEBUG or TESTING:
PLUGIN_TESTING = get_setting('PLUGIN_TESTING', TESTING) # are plugins beeing tested?
PLUGIN_TESTING_SETUP = get_setting('PLUGIN_TESTING_SETUP', False) # load plugins from setup hooks in testing?
PLUGIN_RETRY = get_setting('PLUGIN_RETRY', 5) # how often should plugin loading be tried?
PLUGIN_FILE_CHECKED = False # Was the plugin file checked?

View File

@ -1007,6 +1007,13 @@ class InvenTreeSetting(BaseInvenTreeSetting):
'validator': bool,
},
'PLUGIN_ON_STARTUP': {
'name': _('Check plugins on startup'),
'description': _('Check that all plugins are installed on startup - enable in container enviroments'),
'default': False,
'validator': bool,
'requires_restart': True,
},
# Settings for plugin mixin features
'ENABLE_PLUGINS_URL': {
'name': _('Enable URL integration'),

View File

@ -30,6 +30,15 @@ class PluginAppConfig(AppConfig):
if not registry.is_loading:
# this is the first startup
try:
from common.models import InvenTreeSetting
if InvenTreeSetting.get_setting('PLUGIN_ON_STARTUP', create=False):
# make sure all plugins are installed
registry.install_plugin_file()
except:
pass
# get plugins and init them
registry.collect_plugins()
registry.load_plugins()

View File

@ -8,6 +8,8 @@ Registry for loading and managing multiple plugins at run-time
import importlib
import pathlib
import logging
import os
import subprocess
from typing import OrderedDict
from importlib import reload
@ -211,6 +213,27 @@ class PluginsRegistry:
# Log collected plugins
logger.info(f'Collected {len(self.plugin_modules)} plugins!')
logger.info(", ".join([a.__module__ for a in self.plugin_modules]))
def install_plugin_file(self):
"""
Make sure all plugins are installed in the current enviroment
"""
if settings.PLUGIN_FILE_CHECKED:
logger.info('Plugin file was already checked')
return
try:
output = str(subprocess.check_output(['pip', 'install', '-U', '-r', settings.PLUGIN_FILE], cwd=os.path.dirname(settings.BASE_DIR)), 'utf-8')
except subprocess.CalledProcessError as error: # pragma: no cover
logger.error(f'Ran into error while trying to install plugins!\n{str(error)}')
return False
logger.info(f'plugin requirements were run\n{output}')
# do not run again
settings.PLUGIN_FILE_CHECKED = True
# endregion
# region registry functions

View File

@ -16,7 +16,6 @@ from django.utils import timezone
from rest_framework import serializers
from plugin.models import PluginConfig, PluginSetting
from InvenTree.config import get_plugin_file
from common.serializers import SettingsSerializer
@ -123,7 +122,7 @@ class PluginConfigInstallSerializer(serializers.Serializer):
# save plugin to plugin_file if installed successfull
if success:
with open(get_plugin_file(), "a") as plugin_file:
with open(settings.PLUGIN_FILE, "a") as plugin_file:
plugin_file.write(f'{" ".join(install_name)} # Installed {timezone.now()} by {str(self.context["request"].user)}\n')
return ret