i-MSCP - Customization - 1.1.0-rc1.4 and newer version

  • Hello ;


    Since several days, I can read some threads that talk about the ways to customize default services configuration as provided by i-MSCP, and how prevent the installer to remove these customizations. Today again, I've received a mail from an user that asked me a way to achieve such thing.


    In all of the threads, I can read things like this:


    - If you want customize i-MSCP, you must edit the templates under /etc/imscp...
    - If you want keep your changes after update, you must first backup your custom templates, update i-MSCP, restore your custom templates and then, run the imscp-setup script...
    ...


    All these methods are not really the way to go with i-MSCP version >= 1.1.0-rc1.4.


    I'll so give you a little example that explain how customize services default configuration without having to modify any template. This example also show the power of i-MSCP.


    For this example, let's imagine we want modify the Postfix configuration to allow it to relay mail of any machine that is part of our VPN (Virtual Private Network). We can do such thing using hooks.


    To do so, We must create a little perl script in which we will define hook functions that will act on some events during install process:


    File /root/imscp_custom.pl


    PHP
    1. #!/usr/bin/perl# This is a hook script for i-MSCP installeruse strict;use warnings;use iMSCP::Debug;use iMSCP::HooksManager;use iMSCP::Execute;# Getting hook manager instancemy $hooksManager = iMSCP::HooksManager->getInstance();# Relay mail of any machine that is part of the VPN# Here we are using postconf to edit the main.cf filesub custom_mtaPostfixMynetworkSyle{ my ($rs, $stdout, $stderr) = (0, undef, undef); # We change value of the 'mynetwork_style' parameter ('subnet' instead of 'host') $rs = execute('postconf -e mynetworks_style=subnet', \$stdout, \$stderr); debug($stdout) if $stdout; error($stderr) if $stderr && $rs; $rs;}# Let register this hook function on the hook manager$hooksManager->register('afterMtaBuildConf', \&custom_mtaPostfixMynetworkSyle);# Add some custom parameters without using postconfsub custom_mtaPostfixCustomParameters{ # Here, $fileContent is a reference to a string representing the main.cf file content as generated by i-MSCP my $fileContent = shift; # We add the 'mynetworks' parameter. This parameter take precedence over the 'mynetwork_style' parameter $$fileContent .= <<EOF;# Custom parameters beginmynetworks = 192.168.5.0/24, 127.0.0.0/8# Custom parameters endingEOF 0;}# Let register this hook function on the hook manager$hooksManager->register('afterMtaBuildMainCfFile', \&custom_mtaPostfixCustomParameters);1;[/php]So what we are doing here? We are defining two hook functions, the [b]custom_mtaPostfixMynetworkSyle[/b] and the [b]custom_mtaPostfixCustomParameters[/b] functions that will act when the hook for which they are registered are triggered. To let's the hook manager know about these hook functions, we register them on it. Here, the involved hooks are:- [b]afterMtaBuildConf[/b] (triggered after that the whole MTA configuration has been done)- [b]afterMtaBuildMainCfFile[/b] (triggered after that the main.cf file has been generated)To resume, when these hooks are triggered, our hooks functions are automatically executed. Each hook function can receive or not some parameters, depending of the hook for which you register them.The last thing we must do is to inform the installer about our hook script. To achieve this, we must run the installer with a specific command line option:[code]# perl imscp-autoinstall -dh /root/imscp_custom.pl


    instead of

    Code
    1. # perl imscp-autoinstall -d


    or, if you want only run the imscp-setup script:


    Code
    1. # cd /var/www/imscp/engine/setup# perl imscp-setup -dh /root/imscp_custom.pl


    instead of

    Code
    1. # cd /var/www/imscp/engine/setup
    2. # perl imscp-setup -dh /root/imscp_custom.pl


    That all for now. ;)


    Note1: In near future, i'll add a new directory such as /etc/imscp/custom.d in which you will put your hook scripts. Then, the engine (including installer) will be able to run any script present in that directory.


    Note2: List of all available hooks will be documented ASAP.


    Note3: Take the example above only as an example. It is probably incomplete.


    Thank you for using i-MSCP

    badge.php?id=1239063037&bid=2518&key=1747635596&format=png&z=547451206

    Edited once, last by Nuxwin ().

  • I would like to donate to the team of R-MSCP five dollars, but I have no paypal, I can defrente deposit your credit card,,