The Nuxeo Shell

Table of Contents

50.1. Overview
50.2. User Manual
50.2.1. Command Options
50.2.2. Commands
50.3. Troubleshooting
50.3.1. Check listened IP
50.3.2. Check connected server
50.3.3. Multi-machine case
50.4. Extending the shell
50.4.1. Registering New Custom Commands
50.4.2. Java Code for the new commands
50.4.3. Building the shell plugin
50.4.4. Deploying the shell plugin

50.1. Overview

The Nuxeo Shell is a command line tool for everyone who needs simple and quick remote access to a Nuxeo EP server. You can see it as the Swiss army knife for the Nuxeo EP world. It can be used by regular users to browse or modify content, by advanced users to administrate Nuxeo servers, or by programmers to test and debug.

The Nuxeo Shell is designed as an intuitive and extensible command line application. It can both serve as a user, administrator or programmer tool, or as a framework to develop new Nuxeo command line clients.

The application is based on the Nuxeo Runtime framework and thus offers the same extension point mechanism you can find on the server application.

The main features of the command line tool are:

  • Two operating modes: an interactive and a batch mode.

  • Advanced command line editing like:

    • auto-completion

    • command history

    • command line colors

    • command line shortcuts like: CTRL+K, CTR+A, etc.

  • JSR223 scripting integration. You can thus connect and execute commands on a Nuxeo server in pure scripting mode.

  • Extensibility - using Nuxeo Runtime extension points

The advanced command line handling is done using the JLine library.

The only requirement is Java 5+. The tool works on any Unix-like OS, on Windows and on Mac OS X.

50.2. User Manual

The Nuxeo Shell application is packaged as a zip archive. To install it, you need to unzip and copy the content in a folder.

The application folder structure is as follow:

+ nxshell
  + app
    + bundles
    + config
    + data
    + lib
  + lib
  - Launcher.class
  - log4j.properties
  - launcher.properties
  - nxshell.sh
  • The lib folder contains JARs needed by the application launcher. The Launcher.class is the application launcher and launcher.properties contains configuration for the launcher.

  • log4j.properties is as you expect the configuration for log4j.

  • nxshell.sh is a shell script that launches the application.

  • The app folder contains the application code and data.

    • app/bundles contains the application OSGi bundles. These bundles will be deployed using a minimal implementation of OSGi (the same that is used on the server side). We may replace the default implementation by equinox later.

    • app/lib contains third party libraries needed by the application bundles.

    • app/config contains the application configuration files.

    • app/data contains temporary data.

The only file you may need to change is the nxshell.sh script. Here is the content of this file:

#!/bin/bash

#JAVA_OPTS="$JAVA_OPTS -Xdebug -Xrunjdwp:transport=dt_socket,address=127.0.0.1:8788,server=y,suspend=y"
JAVA_OPTS="$JAVA_OPTS -Dorg.nuxeo.runtime.1.3.3.streaming.port=3233"
java $JAVA_OPTS Launcher launcher.properties $@

If you uncomment the first line, you will be able to run Nuxeo Shell in debug mode.

The second line [must] be commented out if on the server you are running on a nuxeo runtime 1.4.SNAPSHOT. When running against Nuxeo EP 5.1 you need to let this uncommented.

You can run the application in two modes:

  1. In batch mode - in this mode you specify the command that will be executed. After the command execution the process will exit. The command has to be passed as the first argument.

    Here is an example of a command executed in batch mode:

    $ ./nxclient.sh export /path/to/remote/doc /path/to/local/folder

    Here is an example of a command executed in batch mode while also passing parameters:

    $ ./nxclient.sh ls --host 192.168.2.54
  2. In interactive mode - in this mode you can share a single session to run multiple commands against the remote Nuxeo EP. To start the application in that mode you should run the interactive command in one of two ways:

    $ ./nxclient.sh interactive
    $ ./nxshell.sh

    After entering in interactive mode a command prompt will be displayed. At this prompt you can enter commands in the same way you specify them on the command line in batch mode.

    When not connected to a server, the prompt will be displayed as:

    |> 

    After connecting to a server named, let's say "nuxeo-platform", the prompt will be displayed as:

    |nuxeo-platform> 

So, as we've seen in the above examples, executing a command is done by using the following syntax:

command [options] [parameters]

where options and parameters are optional and are specific to the command you run.

Example:

import -u /path/to/doc /path/to/local_file

In this case "import" is the command, "-u" is a flag option and "path/to/doc" and "/path/to/local_file" are both parameters

Parameters are stand alone arguments (they are not bound to a specific option) and can be retrieved programmatically using their index. In the example above, the first parameter will have the index 0 while the second the index 1.

50.2.1. Command Options

Command options are defined by a name and optionally a shortcut (a short name). When referring to an option using its name you should prefix it by two hyphens '--'. When using short names you should only use one hyphen as a prefix. For example if you have an option named "host" and using a short name of "h" the following commands are equivalent:

$ ./nxshell.sh interactive -h localhost

$ ./nxshell.sh interactive --host localhost

Options may take values or may be used as flags turning on / off a specific option by their simple presence.

When using long names you should specify the option values immediately after the option. However when using short names you can group options together. Let say for example we have a command that support 4 options: a, v, i, o. a and v are flag options and both i and o takes an argument as value. In this case the you can group options like the following:

command -avi in_file -o out_file

or

command -avio in_file out_file

or anyhow you want. You should keep in mind that when grouping options that take arguments, these arguments will be assigned to each of this options in the order they were specified on the command line.

50.2.1.1. Global Options

Besides the specific options that commands may define, there are several global options that apply to all commands. These are:

  • host (--host or -h) the Nuxeo EP host where to connect - defaults to localhost

  • port (--port or -p) the Nuxeo EP port where to connect - defaults to 62474

  • username (--username or -u) the username to use - defaults to the "system" user

  • password (--password or -P) the password to use

  • debug (--debug or -d) to start with debug mode (logs at DEBUG level)

50.2.2. Commands

There is the list of all built-in commands of nuxeo shell.

Notes:

  1. At the time of this writing some of these built-in commands are not yet implemented. Those are be marked with an asterisk (*).

  2. The commands can be grouped in two categories:

    • offline commands - command that doesn't need a connection to work. Example: help, log etc.

    • online commands - commands that requires a connection to work. These commands are automatically connecting if no connection is currently established.

  3. Some commands make no sense and are not available in both modes (batch and interactive). This will be specified by each command if it is the case.

50.2.2.1. interactive

Runs in the interactive mode. This command is not available when already in interactive mode.

Has no specific options.

$ ./nxshell.sh interactive

50.2.2.2. help

Displays the help page.

Takes an optional parameter which is the name of a command.

By default, displays the global help page. If a command is given as an argument, displays the help page for that command.

$ ./nxshell.sh help ls

50.2.2.3. log

Manage logs from console without having to edit log4j.xml. Allow to switch on/off a debug mode, add/remove loggers, ...

log filename [log level [package or class]] creates a file (created in $PWD/log/ if filename do not contain path separator, else given path is used). It will record logs at level greater or equal to level (if specified, else default level is INFO; available values are TRACE, DEBUG, INFO, WARN, ERROR, FATAL). If a package or a class (canonical name) is specified, logs will be filtered from this package/class.

log off [package or class] stops logging. Applies on all custom appenders (console and filenames) if no package or class is specified. Don't worry about the warning log off command may cause (the logger is re-created):

log4j:WARN No appenders could be found for logger (org.nuxeo.ecm.shell.commands.InteractiveCommand).
log4j:WARN Please initialize the log4j system properly.

log debug switches debug mode on/off. Same result as starting the shell with -d (--debug) option. This decrease the log level to DEBUG on console and filenames.

See log4j documentation for more information.

Available only in interactive mode

50.2.2.4. connect *

Connects to a given server. If a connection is already established, close it first.

Available only in interactive mode

50.2.2.5. disconnect *

Disconnects from the current connected server. If no connection exists, does nothing.

Available only in interactive mode

50.2.2.6. ls

Lists the children of the current folder in the repository.

By default, Folderish types are displayed in blue.

Note that the ls directory-name command is accepted but won't list the content of directory-name, it will only list the content of the current directory. This might be improved in the future to provide a behavior alike the one of the Unix ls command.

Available only in interactive mode

50.2.2.7. tree

Displays the complete tree structure of the documents as it would be returned by the Unix tree command.

Available only in interactive mode

50.2.2.8. cd

Changes the current directory in the repository (to a Folderish document)

50.2.2.9. pwd

Displays the current path in the repository.

50.2.2.10. view

Views info about document. The information displayed can be controlled using these command options:

--all (-a) - view all data

--system (-s) - view only the system data

--acp - view the ACLs on that document

50.2.2.11. rm

Removes a document or a tree of documents.

50.2.2.12. mkdir

Creates a Folder document.

50.2.2.13. put

Uploads a blob to a file document. If the target document doesn't exists, creates it.

50.2.2.14. putx

Creates a document other than a file. Metadata (and blobs) are specified in the Nuxeo export format.

50.2.2.15. get

Downloads the blob from a File document.

50.2.2.16. getx

Gets a document as an XML file (as export, but only for a document).

50.2.2.17. export

Exports documents from the repository.

50.2.2.18. import

Imports documents into the repository.

50.2.2.19. script

Makes it possible to run external scripts. Here are examples of such scripts.

From interactive mode use:

script --file <path_to_your_script> <args>

50.2.2.20. chperm *

Changes a privilege on the given document.

50.2.2.21. useradd

Creates new user(s).

To create user joe:

useradd joe

To create the users define in the users.csv CSV file:

useradd --file users.csv

50.2.2.22. groupadd *

Creates new group(s).

50.2.2.23. groupmod

Modifies a group

To add user joe to the company-service1 group:

groupmod --user joe company-service1

To set the users of the company-service1 group to be only joe

groupmod --set --user madarche company-service1

To add the users in the users_for_group.csv CSV file to the company-service1 group:

groupmod --file users_for_group.csv company-service1

To set the users of the company-service1 group to be the users define in the users_for_group.csv CSV file:

groupmod --file users_for_group.csv company-service1

50.2.2.24. select

Search the repository using the NXQL query language. For example:

select * from Document where ...

This command can be used as a starting point to write another script to perform search service queries if needed.

This command is only available in the Nuxeo 5.2 branch.

50.2.2.25. lstypes *

50.2.2.26. viewtype *

50.2.2.27. lsusers *

50.2.2.28. lsgroups *

50.2.2.29. viewuser *

50.2.2.30. viewgroup *

50.3. Troubleshooting

50.3.1. Check listened IP

Be sure on what precise IP address and what precise port the server is listening to. To find out, use the netstat command on the server host. The example below shows that, on the same host, there are 2 servers listening on the 62474 port but on different IP addresses. You should always use as arguments to the nuxeo shell the exact IP address and port you find out.

$ sudo netstat -ntlap | grep 62474
tcp        0      0 ::ffff:192.0.0.11:62474     :::*                        LISTEN      3623/java
tcp        0      0 ::ffff:192.0.0.10:62474     :::*                        LISTEN      1346/java      

Alternatively you can use the lsof command.

$ sudo lsof -i :62474
COMMAND   PID       USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
java     3623      jboss  266u  IPv6 0x5d69a70      0t0  TCP [::192.0.0.11]:62474 (LISTEN)
java     1346      jboss  266u  IPv6 0x5d7af30      0t0  TCP [::192.0.0.10]:62474 (LISTEN)      

50.3.2. Check connected server

Be sure to be connected on the right server. To do so, issue a view command. This will display information on the current context:

192.0.0.10> view
--------------------------------------------------------------------
UID: 2a13db70-f133-473c-9a90-6838d01610aa
Path: /
Type: Root
--------------------------------------------------------------------
Title: 2a13db70-f133-473c-9a90-6838d01610aa
Author: null
Created: null
Last Modified: null
--------------------------------------------------------------------
Description: 2a13db70-f133-473c-9a90-6838d01610aa
--------------------------------------------------------------------      

50.3.3. Multi-machine case

When working with multi-machine installation, connect the Nuxeo Shell on the server running Nuxeo Core (i.e. the stateful server on a bi-machine configuration).

50.4. Extending the shell

If you a need a new command (a new functionality) not provided by the Nuxeo Shell, you can add it very simply by writing a Nuxeo plugin in exactly the same manner you would for a Nuxeo server instance. This is done by writing a Java class for each new shell command and declaring (that is registering) each command in a single XML file as a contribution to an extension point.

Here is an example of how to register two imaginary new custom commands addapplicants and purgeobsoletedocs.

50.4.1. Registering New Custom Commands

This is how to register the new commands:

<?xml version="1.0"?>
<component name="com.company.nuxeo.shellcommands" version="1.0">

  <documentation>
    Extra Nuxeo Shell commands shown as examples for the Nuxeo Book.
  </documentation>

  <extension target="org.nuxeo.ecm.shell.CommandLineService" point="commands">

    <command name="purgeobsoletedocs" class="com.company.nuxeo.shellcommands.ObsoleteDocsPurgeCommand">
      <description>
        Purge obsolete documents considering based on predefined hard-coded logic.
      </description>
      <help>Purge obsolete documents</help>
    </command>

    <command name="addapplicants" class="com.company.nuxeo.shellcommands.ApplicantsAdderCommand">
      <description>
        Adds applicants by creating the right folders with the right permissions.
      </description>
      <params>
        <param index="0" type="path" />
      </params>
      <help>Adds applicants</help>
    </command>

  </extension>

</component>
      

50.4.2. Java Code for the new commands

This is how to write a Java class for a new command:


public class ApplicantsAdderCommand extends AbstractCommand {

    public static final String COMMAND_NAME = "addApplicants";

    public void run(CommandLine cmdLine) throws Exception {
        String[] elements = cmdLine.getParameters();
        // parse command line
        if (elements.length != 1) {
            log.error("Usage: " + COMMAND_NAME + " file");
            return;
        }
        File file = new File(elements[0]);

        addApplicants([...]);
    }

    void addApplicants([...]) {
    }

}
      

50.4.3. Building the shell plugin

Finally the pom.xml file for the plugin to the Nuxeo shell needs to at least contain the following dependency to be able to build extend the AbstractCommand class. Building the plugin will generate a XXX.jar file. Note that you should of course replace the version given as an example with the version suited for your need.

<dependencies>

  [...]

  <dependency>
    <groupId>org.nuxeo.ecm.platform</groupId>
    <artifactId>nuxeo-shell-commands-base</artifactId>
    <version>5.1.7-SNAPSHOT</version>
  </dependency>

  [...]

</dependencies>
      

50.4.4. Deploying the shell plugin

  1. Install or decide on using a nuxeo shell already installed program.

  2. Copy the generated XXX.jar file into the bundles directory of the nuxeo shell installed program.

  3. Delete the data directory, if there is any, of the nuxeo installed program. This is to purge any caching of registered JARs.

The next time the nuxeo shell is restarted your new commands will be available.