7. How to create custom plugins¶
In order to use MFSERV and create your custom plugin, one of the first thing, you have to load the “MFSERV environment” in your shell session. There are several ways to do that: check the related documentation.
Plugins can be custom made quickly and easily by using existing templates. Making plugins with mfserv is as simple as editing a configuration file. You just have to create a plugin using an existing template, then edit the configuration file so that the plugin fulfills your need, and finally release the plugin.
Predefined templates are available in order to create your plugin (see Plugin templates section).
7.1. Create and customize the plugin¶
To create a plugin, simply run the command.
bootstrap_plugin.py create --template={TEMPLATE} {PLUGIN_NAME}
where {TEMPLATE}
is the tmplate you want to use and {PLUGIN_NAME}
the name of ypur plugin.
Notice if you omit the --template
argument, the default template will be used.
Once you have entered this command, you will be asked to fill in some fields to configure and customize your plugin.
You can also configure your plugin anytime by editing the mfserv/{PLUGIN_NAME}/config.ini
config file. For more details about each field, check the documentation in the mfserv/{PLUGIN_NAME}/config.ini
file.
Quick Start and Tutorials may help you to create your plugin.
7.2. Custom plugin configuration¶
You may need to customize the :index:configuration of your plugin (application). In order to do this, set your parameter(s) in the MFSERV module configuration file config/config.ini
. This configuration file can contain a section per plugin. The section must be named [plugin_{plugin_name}]
.
Each parameter will be will transform into an environment variable whose pattern is {MODULE}_{SECTION_NAME}_{PARAMETER_NAME}
.
Note
Environment variables are always in uppercase.
To get the new value, you have to close/reopen your terminal to force a new profile loading.
To change daemons and services behaviour (like nginx listening port in your example), you have to restart services from a newly restarted terminal or from a root user through service metwork restart command.
For more details, see How to configure mfserv metwork module ?.
7.3. Plugin templates¶
Predefined templates are available in order to create your plugin.
The following command allows to display the available templates:
bootstrap_plugin.py list
Output:
List of available plugin templates:
* default
* empty
* node
* django
* static
* flask
7.3.1. The default
template¶
This template allows you to create either a Python WSGI web server (synchronous/standard web server) or a Python asynchronous/asyncio web server.
When you create a plugin with this template, we will asked what kind (type) of plugin to create:
python3_sync: for synchronous/standard web Python3 applications (implemented with the “new way”)
python2_sync: for synchronous/standard web Python2 applications (implemented with the “new way”) .. _mfserv_create_plugins_aiohttp_sync
aiohttp: for asynchronous Python3/asyncio web applications (implemented with the “new way” with the aiohttp framework)
gunicorn3_sync: for synchronous/standard web Python3 applications (implemented with the “old way”)
gunicorn2_sync: for synchronous/standard web Python2 applications (implemented with the “old way”)
gunicorn3_asyncio: for asynchronous Python3/asyncio applications (implemented with the “old way”)
Note
Tip
Note
7.3.2. The django
template¶
The django
plugin template enables you to initialize a plugin containing a single rough Django project and a first Hello World!
application in this project.
One your plugin is initialized, you will have a python virtual environment containing a collection of modules, including Django.
The Django version provided by MetWork can be check in the python3_virtualenv_sources/requirements-to-freeze.txt
file in the plugin directory. You can change it if needed.
Important
The Django secret key of your first project is randomly generated during the process. Your project will own a secret key similar to the one that is set when you
create a new Django project with the Django command django-admin startproject
(see Django secret key documentation).
Once you bootstrapped a plugin with the Django template, a postinstall script is create in the plugin directory. It is executed when you launch the make develop
or plugins.install
command.
The postinstall script do the following:
Create the Django project
django-admin startproject {PLUGIN_NAME}
(see django-admin startproject documentation)Create the “Hello World!” application
django-admin startapp hello
(see django-admin startapp documentation)Configure the project
Run
python manage.py migrate
(see Django migrate documentation)Run
python manage.py collectstatic
(see Django collectstatic documentation)
You may modify postinstall script, for example if you want to do the same for other Django projects inside your plugin.
Note
Danger
Important
See also
7.3.3. The flask
template¶
The flask
plugin template allows you to create a Python WSGI web server through the Flask framework.
One your plugin is initialized, you will have a python virtual environment containing a collection of modules, including Flask and Jinja2.
Important
This template works with Python3 only
The Flask version provided by MetWork can be check in the python3_virtualenv_sources/requirements-to-freeze.txt
file in the plugin directory. You can change it if needed.
See also
7.3.4. The node
template¶
This template allows you to create a Node.js server plugin through the Express web application framework.
See also
7.3.5. The static
template¶
This template allows you to create a static website plugin.
The template generate an main
directory with just contains a index.html
. Then, you have to create your static files.
7.3.6. The empty
template¶
This template is a special template that must be used as a container e.g. a crontab container (by selecting crontab_support=yes
) or one or more daemons (it doesn’t create any web application)
If you need to only run daemon(s), you have to configure an [extra_daemon_xxx] per daemon in the
config.ini` file in the plugin directory.
See also