snewton

Active Member
Aug 4, 2015
41
11
58
Redford, MI
cPanel Access Level
Root Administrator
I'm trying to create a hook that will enable CloudLinux Reseller Limits and set the limits for the reseller account when it is created and uses a specific reseller plan/package.

This is what I have but it is not working:



Code:
#!/usr/local/cpanel/3rdparty/bin/perl

package NewAccountHook;

use strict;
use warnings;

sub describe {
    my $hook = {
        'category' => 'Whostmgr',
        'event' => 'Accounts::Create',
        'stage' => 'post',
        'hook' => 'NewAccountHook::actions',
        'exectype' => 'module',
    };
    return $hook;
}

sub actions {

    my ( $context, $data ) = @_;
    my $user = $data->{'user'};
    my $plan = $data->{'plan'};

if ( $plan eq 'Reseller Plan 1' ) {
    system`/sbin/cloudlinux-limits enable-reseller-limits --reseller-name $user --json`;
    system`/sbin/cloudlinux-limits set --reseller-name $user --speed=100% --pmem=3G --nproc=200 --maxEntryProcs=30 --iops=5024 --io=100MB/s --json`;
} elsif ( $plan eq 'Reseller Plan 2' ) {
    system`/sbin/cloudlinux-limits enable-reseller-limits --reseller-name $user --json`;
    system`/sbin/cloudlinux-limits set --reseller-name $user --speed=150% --pmem=5G --nproc=200 --maxEntryProcs=50 --iops=7024 --io=250MB/s --json`;
} else {
   # Executes when the none of the above condition is true
}


}

1;
I've created other cPanel hooks, so I have the basics and it is created correctly in hooks, etc.

Perl is definitely not my expertise, so I'm struggling to get this to work.

Any help would be awesome!

Thanks!
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
14,237
2,217
363
cPanel Access Level
Root Administrator
Hey there! That feature request you found is correct as the hooks do not currently work for resellers. I did reach out to our Accounts team and they confirmed this isn't something that is doable at the moment, although they are aware of the feature request.
 

snewton

Active Member
Aug 4, 2015
41
11
58
Redford, MI
cPanel Access Level
Root Administrator

snewton

Active Member
Aug 4, 2015
41
11
58
Redford, MI
cPanel Access Level
Root Administrator
That does seem out of date as it's pointed to our older documentation site as well, where the new data is now at cPanel & WHM Developer Portal · cPanel & WHM Developer Portal for API calls and cPanel & WHM Documentation | cPanel & WHM Documentation for the standard documentation.
Why cPanel hooks are ran prior to reseller permissions being added during account creation makes no sense.

I must say that I really find it annoying that I got this to work with a hook on our DirectAdmin servers quite easily, but can't get it to work on cPanel (the control panel that costs me 10x more).
 

snewton

Active Member
Aug 4, 2015
41
11
58
Redford, MI
cPanel Access Level
Root Administrator
I had an idea for a work around for this, but during testing the "$plan" doesn't seem to be working.

I came across the event "Whostmgr::Resellers::setup".

Any idea if that would achieve what I need?

Basically, if a reseller account is created, I want the ability to automatically assign specific CloudLinux Reseller Limits to that reseller account.

There simply has to be a way to achieve this with cPanel.
 

snewton

Active Member
Aug 4, 2015
41
11
58
Redford, MI
cPanel Access Level
Root Administrator
Just to update, I've been working with CloudLinux support regarding this and the correct command to use for enabling Reseller Limits for an account, so I am using the following code:

Code:
#!/usr/local/cpanel/3rdparty/bin/perl

package ResellerLimits;

use strict;
use warnings;

sub describe {
    my $hook = {
        'category' => 'Whostmgr',
        'event' => 'Accounts::Create',
        'stage' => 'post',
        'hook' => 'ResellerLimits::actions',
        'exectype' => 'module',
    };
    return $hook;
}

sub actions {

    my ( $context, $data ) = @_;
    my $user = $data->{'user'};
    my $plan = $data->{'plan'};

if ( $plan == 'Reseller Plan 1' ) {
    system`lvectl set-reseller $user --speed='100%' --pmem=3G --nproc=200 --maxEntryProcs=30 --iops=5024 --io=102400`;
} elsif ( $plan == 'Reseller Plan 2' ) {
    system`lvectl set-reseller $user --speed='150%' --pmem=5G --nproc=200 --maxEntryProcs=50 --iops=7024 --io=256000`;
} elsif ( $plan == 'Reseller Plan 3' ) {
    system`lvectl set-reseller $user --speed='200%' --pmem=6G --nproc=200 --maxEntryProcs=60 --iops=10024 --io=358400`;
} elsif ( $plan == 'Reseller Plan 4 ' ) {
    system`lvectl set-reseller $user --speed='250%' --pmem=4G --nproc=200 --maxEntryProcs=40 --iops=10024 --io=204800`;
} else {
   # Do nothing
}


}

1;
However, it still does not work.
 

cPanelTJ

Product Owner
Staff member
Jan 29, 2019
97
50
93
Houston, TX
cPanel Access Level
Root Administrator
Twitter
Just to update, I've been working with CloudLinux support regarding this and the correct command to use for enabling Reseller Limits for an account, so I am using the following code:

...

However, it still does not work.
@snewton

My apologies for the delay in response. It has been quite a week playing catch-up as we're working on a lot of new, cool things right now.

I wasn't able to fully finish reproducing your issue as I'm not exceptionally proficient with CloudLinux and lvectl, but one problem that jumps out to me immediately is that your perl operator for comparing $plan to the account's package is incorrect. In perl, `==` is meant for numerical comparisons. For string comparisons, `eq` should be used.

Here is a resource with some good examples: Perl | Comparing Scalars - GeeksforGeeks

If this doesn't work, you can turn on Standardized Hooks - Debug Mode (level 4) in WHM -> Tweak Settings, then you can watch the error_log in a split/separate terminal window (tail -F /usr/local/cpanel/logs/error_log) to confirm that your hook triggers when you create a new account.
 
  • Like
Reactions: cPRex

snewton

Active Member
Aug 4, 2015
41
11
58
Redford, MI
cPanel Access Level
Root Administrator
@snewton

My apologies for the delay in response. It has been quite a week playing catch-up as we're working on a lot of new, cool things right now.

I wasn't able to fully finish reproducing your issue as I'm not exceptionally proficient with CloudLinux and lvectl, but one problem that jumps out to me immediately is that your perl operator for comparing $plan to the account's package is incorrect. In perl, `==` is meant for numerical comparisons. For string comparisons, `eq` should be used.

Here is a resource with some good examples: Perl | Comparing Scalars - GeeksforGeeks

If this doesn't work, you can turn on Standardized Hooks - Debug Mode (level 4) in WHM -> Tweak Settings, then you can watch the error_log in a split/separate terminal window (tail -F /usr/local/cpanel/logs/error_log) to confirm that your hook triggers when you create a new account.
I originally used "eq" and that didn't work either.

I just tried using the following:

Code:
#!/usr/local/cpanel/3rdparty/bin/perl

package ResellerLimits;

use strict;
use warnings;

sub describe {
    my $hook = {
        'category' => 'Whostmgr',
        'event' => 'Accounts::Create',
        'stage' => 'post',
        'hook' => 'ResellerLimits::actions',
        'exectype' => 'module',
    };
    return $hook;
}

sub actions {

    my ( $context, $data ) = @_;
    my $user = $data->{'user'};
    my $plan = $data->{'plan'};

if ( $plan eq 'Reseller Plan 1' ) {
    system`echo "Test succeeded" >> /usr/local/cpanel/test.txt`;
} else {
    system`echo "Test failed" >> /usr/local/cpanel/test.txt`;
}


}

1;
The above should write to the file test.txt with either succeeded or failed after account creation and instead the file test.txt is empty after account creation, so something is not right.
 

snewton

Active Member
Aug 4, 2015
41
11
58
Redford, MI
cPanel Access Level
Root Administrator
Are you getting any error spew from the hook module when you call whmapi1 createacct over CLI?

What directory is your hook in?

Is it registering properly through the manage_hooks script without error?
No error is shown when creating an account via CLI.

The hook .pm file has the correct permissions and is in: /usr/local/cpanel/

The hook is properly registered with no errors. (I've tried deleting and re-adding it several times trying to debug the issue)
 

cPanelTJ

Product Owner
Staff member
Jan 29, 2019
97
50
93
Houston, TX
cPanel Access Level
Root Administrator
Twitter
Hi @snewton ,

The script is working for me in /usr/local/cpanel and in /user/local/cpanel/hooks.

Here is my code from ULC:

Perl:
#!/usr/local/cpanel/3rdparty/bin/perl

package ResellerTest;

use strict;
use warnings;

sub describe {
    my $hook = {
        'category' => 'Whostmgr',
        'event' => 'Accounts::Create',
        'stage' => 'post',
        'hook' => 'ResellerTest::actions',
        'exectype' => 'module',
    };
    return $hook;
}

sub actions {

    my ( $context, $data ) = @_;
    my $user = $data->{'user'};
    my $plan = $data->{'plan'};

if ( $plan eq 'Package with Spaces' ) {
    system`echo "FROM ULC Test succeeded for User: $user and Plan: $plan" >> /usr/local/cpanel/test.txt`;
 } else {
    system`echo "Test failed FROM ULC" >> /usr/local/cpanel/test.txt`;
 }

}

1;
Here is my output to the test.txt file:
Code:
FROM ULC Test succeeded for User: delete13me and Plan: Package with Spaces