Bug 743379

Summary: CLI fails on running *.js files with error
Product: [Other] RHQ Project Reporter: Nabeel Saad <nsaad>
Component: CLIAssignee: Lukas Krejci <lkrejci>
Status: CLOSED CURRENTRELEASE QA Contact: Mike Foley <mfoley>
Severity: urgent Docs Contact:
Priority: high    
Version: 3.0.0 Beta1CC: gcooper, hrupp, lkrejci
Target Milestone: ---   
Target Release: JON 3.0.0   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
: 786976 (view as bug list) Environment:
Last Closed: 2012-02-07 19:25:20 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 717392, 734807, 786976    
Attachments:
Description Flags
autoimport JS file to test the CLI - currently works in JON 2.4.1 with all the latest patches none

Description Nabeel Saad 2011-10-04 17:59:20 UTC
Created attachment 526287 [details]
autoimport JS file to test the CLI - currently works in JON 2.4.1 with all the latest patches

Description of problem:
When I try to run a CLI command inputting a file name like so:

  /opt/jon-demo-3.0/jon-tools/rhq-remoting-cli-4.1.1-BETA1/bin/rhq-cli.sh -f autoImport.js

I get the following error:
  Exception in thread "main" java.lang.NullPointerException
	at org.rhq.bindings.StandardBindings.<init>(StandardBindings.java:92)
	at org.rhq.enterprise.client.commands.ScriptCommand.initBindings(ScriptCommand.java:171)
	at org.rhq.enterprise.client.commands.ScriptCommand.execute(ScriptCommand.java:78)
	at org.rhq.enterprise.client.ClientMain.processArguments(ClientMain.java:505)
	at org.rhq.enterprise.client.ClientMain.main(ClientMain.java:106)
rhq-cli.sh: /opt/jon-demo-3.0/jon-tools/rhq-remoting-cli-4.1.1-BETA1/bin/rhq-cli.sh done.

I seem to remember seeing a similar NullPointerException with the CLI in JON 2.4.1 that required one of the patches available in CSP at the moment (BZ704371), not sure if this has some how reverted.

Version-Release number of selected component (if applicable):
JON 3.0.0-Beta1 --> rhq-remoting-cli-4.1.1-BETA1

How reproducible:
Everytime

Steps to Reproduce:
1. Start JON server
2. Setup CLI locally, go to bin folder
3. Save the uploaded autoimport.js file to the bin folder
4. Run the following command:
      ./rhq-cli.sh -f autoImport.js
  
Actual results:
The error displayed above

Expected results:
It should actually run the JS file and either import resources into the JON server or say there are no resources to import.

Additional info:
This breaks a lot of the scripting abilities and is quite important to resolve.
FYI, the CLI still works if you log in manually and then type in the commands from the autoimport.js file, but passing it seems to fail.

Comment 2 Charles Crouch 2011-10-05 13:47:58 UTC
regression

Comment 3 John Sanda 2011-10-10 13:27:47 UTC
I just reproduced this bug with a simple script.

// inventory.js
rhq.login('rhqadmin', 'rhqadmin');
var resources = ResourceManager.findResourcesByCriteria(ResourceCriteria());
println('There are ' + resources.size() + ' resources in inventory');

// end script

I get the same NPE when I try running with rhq-cli.sh -f inventory.js. This is
a major regression. It looks like it prevents users from running script files
in batch or non-interactive mode. Fortunately there is somewhat of a work
around. You can execute script files from the interactive shell using the exec
command. If I log into the interactive shell, I can run the script file from
there. Here is an example to illustrate:

bash-4.1$ ./rhq-cli.sh 
RHQ - RHQ Enterprise Remote CLI 4.1.0-SNAPSHOT
unconnected$ login rhqadmin rhqadmin
Remote server version is: 4.1.0-SNAPSHOT (3177dd2)
Login successful

rhqadmin@localhost:7080$ exec -f inventory.js
Remote server version is: 4.1.0-SNAPSHOT (3177dd2)
There are 200 resources in inventory

Comment 4 Nabeel Saad 2011-10-10 14:19:41 UTC
Hello John,

Glad you were able to reproduce this so easily.  The workaround is good to know; however, it still doesn't allow for running the script via an "automated" process, can you?

i.e you can't have an external script call a CLI JS script...  

I'm trying to find a way around that because in the next couple of weeks I will NEED this functionality for an important demo...

Out of the things that you did, I would have expected that you wouldn't have to do:
   login rhqadmin rhqadmin

Given that your script had that line in the JS file.  I think that is the main problem.  If you try to do the line:
   exec -f inventory.js

before the login, it fails with the same NPE stack.

I guess in the meantime, I could do the following to get things working the way I want it to:

   (echo "login rhqadmin rhqadmin"; echo "exec -f /opt/CLI/autoImport.js"; echo "quit") | ./rhq-cli.sh 

Cheers.
Nabeel

Comment 5 Heiko W. Rupp 2011-10-11 07:49:16 UTC
In org.rhq.enterprise.client.ClientMain#executePromptCommand at
     boolean result = commands.get("exec").execute(this, args);
this is supposed to be a ClientMain that has a remoteClient already set.
This is not true, which later makes the cli go to org.rhq.enterprise.client.commands.ScriptCommand#initBindings

    public void initBindings(ClientMain client) {
        if (jsEngine == null) {
            bindings = new StandardBindings(client.getPrintWriter(), client.getRemoteClient());

and then NPE in

org.rhq.bindings.StandardBindings#StandardBindings when trying to get the managers

    public StandardBindings(PrintWriter output, RhqFacade rhqFacade) {
        PageControl pc = new PageControl();
        pc.setPageNumber(-1);

        managers = rhqFacade.getManagers();

Comment 6 Lukas Krejci 2011-10-11 15:43:35 UTC
commit 909e4f54d6d932c1c9168bce97f6a9cad42b33a2
Author: Lukas Krejci <lkrejci>
Date:   Tue Oct 11 17:33:19 2011 +0200

    BZ 743379 - Make sure to initialize the script engine with as much bindings
    as possible before the user logs in (and add the rest when logged in).

Comment 7 Mike Foley 2011-10-13 14:27:25 UTC
verified RHQ 10/13 build.

Comment 8 Mike Foley 2012-02-07 19:25:20 UTC
changing status of VERIFIED BZs for JON 2.4.2 and JON 3.0 to CLOSED/CURRENTRELEASE

Comment 9 Charles Crouch 2012-02-15 00:20:32 UTC
Setting Target Release correctly