mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 05:05:42 +00:00 
			
		
		
		
	Merge pull request #2748 from matmair/matmair/issue2524
Run plugin install on startup
This commit is contained in:
		| @@ -900,3 +900,4 @@ if DEBUG or TESTING: | |||||||
| PLUGIN_TESTING = get_setting('PLUGIN_TESTING', TESTING)  # are plugins beeing tested? | 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_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_RETRY = get_setting('PLUGIN_RETRY', 5)  # how often should plugin loading be tried? | ||||||
|  | PLUGIN_FILE_CHECKED = False                    # Was the plugin file checked? | ||||||
|   | |||||||
| @@ -1007,6 +1007,13 @@ class InvenTreeSetting(BaseInvenTreeSetting): | |||||||
|             'validator': bool, |             '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 |         # Settings for plugin mixin features | ||||||
|         'ENABLE_PLUGINS_URL': { |         'ENABLE_PLUGINS_URL': { | ||||||
|             'name': _('Enable URL integration'), |             'name': _('Enable URL integration'), | ||||||
|   | |||||||
| @@ -30,6 +30,15 @@ class PluginAppConfig(AppConfig): | |||||||
|  |  | ||||||
|                 if not registry.is_loading: |                 if not registry.is_loading: | ||||||
|                     # this is the first startup |                     # 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.collect_plugins() | ||||||
|                     registry.load_plugins() |                     registry.load_plugins() | ||||||
|  |  | ||||||
|   | |||||||
| @@ -8,6 +8,8 @@ Registry for loading and managing multiple plugins at run-time | |||||||
| import importlib | import importlib | ||||||
| import pathlib | import pathlib | ||||||
| import logging | import logging | ||||||
|  | import os | ||||||
|  | import subprocess | ||||||
| from typing import OrderedDict | from typing import OrderedDict | ||||||
| from importlib import reload | from importlib import reload | ||||||
|  |  | ||||||
| @@ -211,6 +213,27 @@ class PluginsRegistry: | |||||||
|         # Log collected plugins |         # Log collected plugins | ||||||
|         logger.info(f'Collected {len(self.plugin_modules)} plugins!') |         logger.info(f'Collected {len(self.plugin_modules)} plugins!') | ||||||
|         logger.info(", ".join([a.__module__ for a in self.plugin_modules])) |         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 |     # endregion | ||||||
|  |  | ||||||
|     # region registry functions |     # region registry functions | ||||||
|   | |||||||
| @@ -16,7 +16,6 @@ from django.utils import timezone | |||||||
| from rest_framework import serializers | from rest_framework import serializers | ||||||
|  |  | ||||||
| from plugin.models import PluginConfig, PluginSetting | from plugin.models import PluginConfig, PluginSetting | ||||||
| from InvenTree.config import get_plugin_file |  | ||||||
| from common.serializers import SettingsSerializer | from common.serializers import SettingsSerializer | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -123,7 +122,7 @@ class PluginConfigInstallSerializer(serializers.Serializer): | |||||||
|  |  | ||||||
|         # save plugin to plugin_file if installed successfull |         # save plugin to plugin_file if installed successfull | ||||||
|         if success: |         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') |                 plugin_file.write(f'{" ".join(install_name)}  # Installed {timezone.now()} by {str(self.context["request"].user)}\n') | ||||||
|  |  | ||||||
|         return ret |         return ret | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user