Liferay DXP - manage your portal via shell and groovy scripts without browser

May 18, 2018

Every administrator of web portal need to do many tasks in a web browser. Content needs to be added, planned, edited, expired, removed. Users need to be created, edited, deactivated etc. System administrators need to replicate cluster, reindex web content or documents, setup cron jobs and so on.

What if those operations can be executed without web browser? Imagine that every action can be stored as a script in GIT repository and can be executed automatically by System admins or dev ops. Each execution can have trace in log and script version.

Shell Scripts, Apache GoGo shell, Groovy, OSGI and Liferay DXP are perfect combination to achieve that.

Felix Gogo Shell interacts with Liferay modules. We can connect to GoGo Shell of Liferay with telnet client (usually port 11311). However I want just execute script and exit. I use blade tool as helper, thus I use following command:

blade sh

Let's create file gogo_groovy_executor.txt and add OSGI/Groovy logic into it:

GroovyExecutor = $.context getService ($.context getServiceReference com.liferay.portal.kernel.scripting.ScriptingExecutor)
script="import com.liferay.portal.kernel.service.UserLocalServiceUtil; println('Users count: '+UserLocalServiceUtil.getUsersCount())"
$GroovyExecutor eval null null null $script

We simply invoke method eval of com.liferay.portal.kernel.scripting.ScriptingExecutor service.

We have now the script and we we know that our connection to OSGI layer is working. I want simply execute newly created file. After executing below command, we should see number of registered users:

blade sh gogo:sh /Users/piotr/development/projects/liferay/scripts/gogo_groovy_executor.txt
Users count: 5

In the example above, groovy script is embedded in the Shell file. Let's separate groovy script from OSGI logic. We can split logic into 2 files: gogo_file_executor.txt and groovy_script.groovy Content of gogo_file_executor.txt:

GroovyExecutor = $.context getService ($.context getServiceReference com.liferay.portal.kernel.scripting.ScriptingExecutor)
Files = $.context getService ($.context getServiceReference org.apache.felix.gogo.command.Files)
path = $Files ls scripts/groovy_script.groovy
file = $path 0
$GroovyExecutor eval null null null $file

Content of groovy_script.groovy:

import com.liferay.portal.kernel.service.UserLocalServiceUtil
println('Users count from file: '+UserLocalServiceUtil.getUsersCount())

We can now execute it with following command:

blade sh gogo:sh /Users/piotr/development/projects/liferay/scripts/gogo_file_executor.txt

Expected output: Users count from file: 5

The default file paths start in LIFERAY_HOME directory (my groovy file path is LIFERAY_HOME/scripts/groovy_script.groovy).

This 2 simple examples show how we can make our life easier. We can reduce manual steps for content createros, portal administrators, sys admins and developers. It is not solution for everything, but should automate many web browser activities.

About the author: Piotr Swiniarski
Comments
Join us