/engine/imscp-rqst-mngr
Perl | 640 lines | 502 code | 76 blank | 62 comment | 42 complexity | 2e970820c20dfea131ca715e73d5c58f MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-2.0
- #!/usr/bin/perl
- # i-MSCP - internet Multi Server Control Panel
- # Copyright (C) 2010-2015 by internet Multi Server Control Panel
- #
- # This program is free software; you can redistribute it and/or
- # modify it under the terms of the GNU General Public License
- # as published by the Free Software Foundation; either version 2
- # of the License, or (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- #
- # @category i-MSCP
- # @copyright 2010-2015 by i-MSCP | http://i-mscp.net
- # @author Daniel Andreca <sci2tech@gmail.com>
- # @author Laurent Declercq <l.declercq@nuxwin.com>
- # @link http://i-mscp.net i-MSCP Home Site
- # @license http://www.gnu.org/licenses/gpl-2.0.html GPL v2
- use strict;
- use warnings;
- no if $] >= 5.017011, warnings => 'experimental::smartmatch';
- use FindBin;
- use lib "$FindBin::Bin", "$FindBin::Bin/PerlLib", "$FindBin::Bin/PerlVendor";
- use iMSCP::Debug;
- use iMSCP::Bootstrapper;
- use iMSCP::EventManager;
- use iMSCP::Execute;
- use MIME::Base64;
- use iMSCP::Getopt;
- use File::Basename;
- use JSON;
- # Set umask ( default is 0 when this script is run through the i-MSCP daemon, which leads to some problems )
- umask 0022;
- # Turn off localisation features to force any command output to be in english
- $ENV{'LANG'} = 'C';
- # Do not clear screen at end of script
- $ENV{'IMSCP_CLEAR_SCREEN'} = 0;
- newDebug('imscp-rqst-mngr.log');
- # Initialize command line options
- $main::execmode = '';
- # Parse command line options
- iMSCP::Getopt->parseNoDefault(sprintf("Usage: perl %s [OPTION]...", basename($0)) . qq {
- Script which process i-MSCP backend requests.
- OPTIONS:
- -s, --setup Setup mode.
- -v, --verbose Enable verbose mode.},
- 'setup|s' => sub { $main::execmode = 'setup'; },
- 'verbose|v' => sub { setVerbose(@_); }
- );
- iMSCP::Bootstrapper->getInstance()->boot({ 'norequirements' => 'yes' });
- my $dbh = iMSCP::Database->factory()->getRawDb();
- sub _process
- {
- my ($moduleName, $sql, $ipModule) = @_;
- my $rs = 0;
- debug("Processing $moduleName module items");
- if ($main::execmode ne 'setup') {
- debug("Processing $moduleName items.");
- }
- my $rows = $dbh->selectall_arrayref($sql, { Slice => { } });
- if(!defined $rows || $dbh->err) {
- error($dbh->errstr);
- return 1;
- }
- if(@{$rows}) {
- my $i = 1;
- my $totalItems = scalar @{$rows};
- for my $row(@{$rows}) {
- my ($id, $name, $status) = ($row->{'id'}, $row->{'name'}, $row->{'status'});
- $$ipModule++ if $moduleName ~~ ['Domain', 'Subdomain', 'Alias', 'SubAlias'];
- debug("Processing $id, $name, $status");
- print STDOUT ("$moduleName\t$status\t$name\t$id\t$totalItems\t$i\n") if $main::execmode eq 'setup';
- $i++;
- newDebug("${moduleName}_module_$name.log");
- my $module = "Modules::$moduleName";
- eval "require $module";
- unless($@) {
- $rs = $module->new()->process($id);
- } else {
- error($@);
- $rs = 1;
- }
- endDebug();
- if($rs) {
- error("Error while processing $id, $name, $status.");
- error("See $main::imscpConfig{'LOG_DIR'}/${moduleName}_module_$name.log for more details.");
- }
- return $rs if $rs;
- }
- } else {
- debug("No item to process for the $moduleName module.");
- }
- 0;
- }
- sub run
- {
- # Process plugins
- # Must always be processed first to allow plugins to register their listeners on the event manager
- my $rs = _process(
- 'Plugin',
- "
- SELECT
- plugin_id AS id, plugin_name AS name, plugin_status AS status
- FROM
- plugin
- WHERE
- plugin_status IN ('enabled', 'toinstall', 'toenable', 'toupdate', 'tochange', 'todisable', 'touninstall')
- AND
- plugin_error IS NULL
- AND
- plugin_backend = 'yes'
- ORDER BY
- plugin_priority DESC
- "
- );
- $rs ||= iMSCP::EventManager->getInstance()->trigger('beforeDispatchRequest');
- # Process toadd|tochange SSL certificates tasks
- $rs ||= _process(
- 'SSLcertificate',
- "
- SELECT
- cert_id AS id, domain_type AS name, status AS status
- FROM
- ssl_certs
- WHERE
- status IN ('toadd', 'tochange', 'todelete')
- ORDER BY
- cert_id ASC
- "
- );
- # Process toadd|tochange users tasks
- $rs ||= _process(
- 'User',
- "
- SELECT
- admin_id AS id, admin_name AS name, admin_status AS status
- FROM
- admin
- WHERE
- admin_type = 'user'
- AND
- admin_status IN ('toadd', 'tochange')
- ORDER BY
- admin_id ASC
- "
- );
- my $ipsModule = 0;
- # Process toadd|tochange|torestore|toenable|todisable domain tasks
- # (for each entitty, process only if the parent entity is in a consistent state)
- $rs ||= _process(
- 'Domain',
- "
- SELECT
- domain_id AS id, domain_name AS name, domain_status AS status
- FROM
- domain
- INNER JOIN
- admin ON(admin_id = domain_admin_id)
- WHERE
- domain_status IN ('toadd', 'tochange', 'torestore', 'toenable', 'todisable')
- AND
- admin_status IN('ok', 'disabled')
- ORDER BY
- domain_id ASC
- ",
- \$ipsModule
- );
- # Process toadd|tochange|torestore|toenable|todisable subdomains tasks
- # (for each entitty, process only if the parent entity is in a consistent state)
- $rs ||= _process(
- 'Subdomain',
- "
- SELECT
- subdomain_id AS id, subdomain_name AS name, subdomain_status AS status
- FROM
- subdomain
- INNER JOIN
- domain USING(domain_id)
- WHERE
- subdomain_status IN ('toadd', 'tochange', 'torestore', 'toenable', 'todisable')
- AND
- domain_status IN('ok', 'disabled')
- ORDER BY
- subdomain_id ASC
- ",
- \$ipsModule
- );
- # Process toadd|tochange|torestore|toenable|todisable domain aliases tasks
- # (for each entitty, process only if the parent entity is in a consistent state)
- $rs ||= _process(
- 'Alias',
- "
- SELECT
- alias_id AS id, alias_name AS name, alias_status AS status
- FROM
- domain_aliasses
- INNER JOIN
- domain USING(domain_id)
- WHERE
- alias_status IN ('toadd', 'tochange', 'torestore', 'toenable', 'todisable')
- AND
- domain_status IN('ok', 'disabled')
- ORDER BY
- alias_id ASC
- ",
- \$ipsModule
- );
- # Process toadd|tochange|torestore|toenable|todisable subdomains of domain aliases tasks
- # (for each entitty, process only if the parent entity is in a consistent state)
- $rs ||= _process(
- 'SubAlias',
- "
- SELECT
- subdomain_alias_id AS id, subdomain_alias_name AS name, subdomain_alias_status AS status
- FROM
- subdomain_alias
- INNER JOIN
- domain_aliasses USING(alias_id)
- WHERE
- subdomain_alias_status IN ('toadd', 'tochange', 'torestore', 'toenable', 'todisable')
- AND
- alias_status IN('ok', 'disabled')
- ORDER BY
- subdomain_alias_id ASC
- ",
- \$ipsModule
- );
- # Process toadd|tochange|toenable|todisable|todelete mail tasks
- # (for each entitty, process only if the parent entity is in a consistent state)
- $rs ||= _process(
- 'Mail',
- "
- SELECT
- mail_id AS id, mail_addr AS name, status AS status
- FROM
- mail_users
- INNER JOIN
- domain USING(domain_id)
- WHERE
- status IN ('toadd', 'tochange', 'toenable', 'todelete', 'todisable')
- AND
- domain_status IN('ok', 'todelete', 'disabled')
- ORDER BY
- mail_id ASC
- "
- );
- # Process toadd|tochange|todelete Htusers tasks
- # (for each entitty, process only if the parent entity is in a consistent state)
- $rs ||= _process(
- 'Htusers',
- "
- SELECT
- id, CONCAT(uname, ':', id) AS name, status
- FROM
- htaccess_users
- INNER JOIN
- domain ON(domain_id = dmn_id)
- WHERE
- status IN ('toadd', 'tochange', 'todelete')
- AND
- domain_status = 'ok'
- ORDER BY
- id ASC
- "
- );
- # Process toadd|tochange|todelete Htgroups tasks
- # (for each entitty, process only if the parent entity is in a consistent state)
- $rs ||= _process(
- 'Htgroup',
- "
- SELECT
- id, CONCAT(ugroup, ':', id) AS name, status
- FROM
- htaccess_groups
- INNER JOIN
- domain ON(domain_id = dmn_id)
- WHERE
- status IN ('toadd', 'tochange', 'todelete')
- AND
- domain_status = 'ok'
- ORDER BY
- id ASC
- "
- );
- # Process toadd|tochange|todelete Htaccess tasks
- # (for each entitty, process only if the parent entity is in a consistent state)
- $rs ||= _process(
- 'Htaccess',
- "
- SELECT
- id, CONCAT(auth_name, ':', id) AS name, status
- FROM
- htaccess
- INNER JOIN
- domain ON(domain_id = dmn_id)
- WHERE
- status IN ('toadd', 'tochange', 'todelete')
- AND
- domain_status = 'ok'
- ORDER BY
- id ASC
- "
- );
- # Process todelete subdomain aliases tasks
- $rs ||= _process(
- 'SubAlias',
- "
- SELECT
- subdomain_alias_id AS id, subdomain_alias_name AS name, subdomain_alias_status AS status
- FROM
- subdomain_alias
- WHERE
- subdomain_alias_status = 'todelete'
- ORDER BY
- subdomain_alias_id ASC
- ",
- \$ipsModule
- );
- # Process todelete domain aliases tasks
- # (For each entity, process only if the entity do not have any direct children)
- $rs ||= _process(
- 'Alias',
- "
- SELECT DISTINCT
- alias_id AS id, alias_name AS name, alias_status AS status
- FROM
- domain_aliasses
- LEFT JOIN
- subdomain_alias USING(alias_id)
- WHERE
- alias_status = 'todelete'
- AND
- subdomain_alias_id IS NULL
- ORDER BY
- alias_id ASC
- ",
- \$ipsModule
- );
- # Process todelete subdomains tasks
- $rs ||= _process(
- 'Subdomain',
- "
- SELECT
- subdomain_id AS id, subdomain_name AS name, subdomain_status AS status
- FROM
- subdomain
- WHERE
- subdomain_status = 'todelete'
- ORDER BY
- subdomain_id ASC
- ",
- \$ipsModule
- );
- # Process todelete domains tasks
- # (For each entity, process only if the entity do not have any direct children)
- $rs ||= _process(
- 'Domain',
- "
- SELECT DISTINCT
- domain_id AS id, domain_name AS name, domain_status AS status
- FROM
- domain
- LEFT JOIN
- domain_aliasses USING(domain_id)
- LEFT JOIN
- mail_users USING(domain_id)
- WHERE
- domain_status = 'todelete'
- AND
- alias_id IS NULL
- AND
- mail_id IS NULL
- ORDER BY
- domain_id ASC
- ",
- \$ipsModule
- );
- # Process todelete users tasks
- # (For each entity, process only if the entity do not have any direct children)
- $rs ||= _process(
- 'User',
- "
- SELECT
- admin_id AS id, admin_name AS name, admin_status AS status
- FROM
- admin
- LEFT JOIN
- domain ON(domain_id = admin_id)
- WHERE
- admin_type = 'user'
- AND
- admin_status = 'todelete'
- AND
- domain_id IS NULL
- ORDER BY
- admin_id ASC
- "
- );
- # Process network cards tasks
- $rs ||= _process(
- 'NetCard',
- "
- SELECT
- ip_id AS id, ip_status AS name, ip_status AS status
- FROM
- server_ips
- WHERE
- ip_status <> 'ok'
- LIMIT
- 1
-
- "
- );
- # Process IP addresses tasks
- unless($rs || (! $ipsModule && $main::execmode ne 'setup')) {
- newDebug("Ips_module.log");
- eval { require Modules::Ips };
- unless($@) {
- $rs = Modules::Ips->new()->process();
- } else {
- error($@);
- $rs = 1;
- }
- endDebug();
- }
- # software installation BEGIN
- unless($rs) {
- my $db = iMSCP::Database->factory();
- my $rdata = $db->doQuery(
- 'software_id',
- "
- SELECT
- domain_id, alias_id, subdomain_id, subdomain_alias_id, software_id, path, software_prefix,
- db, database_user, database_tmp_pwd, install_username, install_password, install_email,
- software_status, software_depot, software_master_id
- FROM
- web_software_inst
- WHERE
- software_status IN ('toadd', 'todelete')
- ORDER BY
- domain_id ASC
- "
- );
- unless(ref $rdata eq 'HASH'){
- error($rdata);
- $rs = 1;
- } else {
- my $count = scalar keys %{$rdata};
- newDebug('imscp_sw_mngr_engine') if $count;
- for (keys %$rdata) {
- my (
- $sw_domain_id, $sw_aliasdomain_id, $sw_subdomain_id, $sw_aliassubdomain_id, $sw_software_id,
- $sw_path, $sw_software_prefix, $sw_database, $sw_database_user, $sw_database_tmp_pwd,
- $sw_install_username, $sw_install_password, $sw_install_email, $sw_software_status,
- $sw_software_depot, $sw_software_master_id
- ) = (
- $rdata->{$_}->{'domain_id'}, $rdata->{$_}->{'alias_id'}, $rdata->{$_}->{'subdomain_id'},
- $rdata->{$_}->{'subdomain_alias_id'}, $rdata->{$_}->{'software_id'}, $rdata->{$_}->{'path'},
- $rdata->{$_}->{'software_prefix'}, $rdata->{$_}->{'db'}, $rdata->{$_}->{'database_user'},
- $rdata->{$_}->{'database_tmp_pwd'}, $rdata->{$_}->{'install_username'},
- $rdata->{$_}->{'install_password'}, $rdata->{$_}->{'install_email'},
- $rdata->{$_}->{'software_status'}, $rdata->{$_}->{'software_depot'},
- $rdata->{$_}->{'software_master_id'}
- );
- # Encoding data to push to another script
- my $imscp_sw_mngr_pushstring = encode_base64(
- encode_json(
- [
- $sw_domain_id, $sw_software_id, $sw_path, $sw_software_prefix, $sw_database,
- $sw_database_user, $sw_database_tmp_pwd, $sw_install_username, $sw_install_password,
- $sw_install_email, $sw_software_status, $sw_software_depot, $sw_software_master_id,
- $sw_aliasdomain_id, $sw_subdomain_id, $sw_aliassubdomain_id
- ]
- )
- );
- my ($stdout, $stderr);
- $rs = execute(
- "$main::imscpConfig{'CMD_PERL'} $main::imscpConfig{'ENGINE_ROOT_DIR'}/imscp-sw-mngr " .
- "\"$imscp_sw_mngr_pushstring\"",
- \$stdout,
- \$stderr
- );
- debug($stdout) if $stdout;
- error($stderr) if $stderr && $rs;
- last if $rs;
- $rs = execute(
- "$main::imscpConfig{'CMD_RM'} -fR /tmp/sw-$sw_domain_id-$sw_software_id", \$stdout, \$stderr
- );
- debug($stdout) if $stdout;
- error($stderr) if $stderr && $rs;
- last if $rs;
- }
- endDebug() if $count;
- unless($rs) {
- $rdata = $db->doQuery(
- 'software_id',
- "
- SELECT
- software_id, reseller_id, software_archive, software_status, software_depot
- FROM
- web_software
- WHERE
- software_status = 'toadd'
- ORDER BY
- reseller_id ASC
- "
- );
- unless(ref $rdata eq 'HASH'){
- error($rdata);
- $rs = 1;
- } else {
- $count = scalar keys %$rdata;
- newDebug('imscp_pkt_mngr_engine.log') if $count;
- for (keys %$rdata) {
- my (
- $sw_software_id, $sw_reseller_id, $sw_software_archive, $sw_software_status,
- $sw_software_depot
- ) = (
- $rdata->{$_}->{'software_id'}, $rdata->{$_}->{'reseller_id'},
- $rdata->{$_}->{'software_archive'}, $rdata->{$_}->{'software_status'},
- $rdata->{$_}->{'software_depot'}
- );
- # Encoding data to push to another script
- my $pushstring = encode_base64(
- encode_json(
- [
- $sw_software_id, $sw_reseller_id, $sw_software_archive, $sw_software_status,
- $sw_software_depot
- ]
- )
- );
- my ($stdout, $stderr);
- $rs = execute(
- "$main::imscpConfig{'CMD_PERL'} $main::imscpConfig{'ENGINE_ROOT_DIR'}/imscp-pkt-mngr " .
- "\"$pushstring\"",
- \$stdout,
- \$stderr
- );
- debug($stdout) if $stdout;
- error($stderr) if $stderr && $rs;
- last if $rs;
- $rs = execute(
- "$main::imscpConfig{'CMD_RM'} -fR /tmp/sw-$sw_software_archive-$sw_software_id",
- \$stdout,
- \$stderr
- );
- debug($stdout) if $stdout;
- error($stderr) if $stderr && $rs;
- last if $rs;
- }
- }
- }
- endDebug() if $count;
- }
- }
- iMSCP::EventManager->getInstance()->trigger('afterDispatchRequest', $rs);
- $rs;
- }
- exit run();