Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Access data passed from cPanel in a custom hook?

Discussion in 'cPanel Developers' started by domeneas, Apr 12, 2018.

Tags:
  1. domeneas

    domeneas Active Member

    Joined:
    Sep 20, 2013
    Messages:
    34
    Likes Received:
    2
    Trophy Points:
    8
    cPanel Access Level:
    Root Administrator
    Hi,

    I am starting to write some hooks instead of running shell scripts to further automation, but am having some trouble starting out.

    I can make and register my hook, but I cannot run it using variables, I have to hard code everything which defeats the purpose.

    Specifically I am starting with one hook that should rename a file on account upgrade/downgrade.

    What is the syntax to access the data that you read into $data using

    Code:
    my ( $context, $data ) = @_;
    ?

    I can print the information I want to /usr/local/cpanel/logs/error_log using :

    Code:
    sub _more_logging {
        my ( $context, $data ) = @_;
        my $pretty_json = JSON->new->pretty;
        $logger->info( $pretty_json->encode($context) );
        $logger->info( $pretty_json->encode($data) );
    
    
    and
    Code:
    _more_logging($context, $data);
    That gives:
    Code:
      info [whostmgr2] {
       "category" : "Whostmgr",
       "blocking" : 1,
       "event" : "Accounts::change_package",
       "point" : "main",
       "stage" : "pre"
    }
    
       info [whostmgr2] {
       "cur_pkg" : "Level1pkg",
       "new_pkg" : "Level2pkg",
       "user" : "cpanel_username"
    
    The username, current package and new package all show up, but I cannot seem to access the information in any way.

    I need to be able to use the 3 variables cur_pkg, new_pkg and user for some comparisons and to identify the file to be edited.

    I've tried entering the "user" field in $data into my own variable $username using various methods found on forums and in guides and documentation, but have been unsuccessful. I've looked at ARGV and OPTS, but am coming up short as I don't quite grasp it. All the cpanel guides I've seen do not actually use any of the passed data to the hook from what i can tell...oddly enough.

    As mentioned, if I skip the variable, the hook runs fine and does it's thing.


    Thank you in advance for any pointers.
     
  2. domeneas

    domeneas Active Member

    Joined:
    Sep 20, 2013
    Messages:
    34
    Likes Received:
    2
    Trophy Points:
    8
    cPanel Access Level:
    Root Administrator
    Hi,

    those are the guides I have been looking at for a couple days, but I cannot see ANY example in them on how to address a field read into the $data variable.

    Tutorial page: In the example the only things they manipulate is a hard coded backup file. They never use anything passed from cpanel to the hook through $data. From what I gather, there isn't even a point to reading:
    Code:
    my ( $context, $data ) = @_;
    in the example code at all.

    WHOSTMGR functions page: Lists the information and field names put into $data. As stated, I can output those fine into the log file, see my post above, I know they are there, but I cannot access them in the perl module I am writing.

    Developer Documentation: Really only links to the two guides above. It does however say
    So that what I'm doing.

    Specifically, I need to write a statement similar to:
    Code:
    if $data(cur_pkg) = "Level2pkg" and if $data(new_pkg) = "Level1pkg" {
    move ($old_name, $new_name) or die "Rename failed: $!";
    } else {
    die "No close needed: $!";
    }
    The $old_name and $new_name is based on $data's "user" field.

    How do I address the fields "cur_pkg", "new_pkg" and "user" passed into $data after running
    Code:
    my ( $context, $data ) = @_;
    ?

    Thanks again.
     
    #3 domeneas, Apr 13, 2018
    Last edited: Apr 13, 2018
  3. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    42,782
    Likes Received:
    1,712
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
Loading...

Share This Page