Liferay DevOps

March 22, 2017

DevOps became necessary tool/approach in developer and sysadmin life. Most of companies introduced or will introduce own mechanisms to automate internal software processes. There are many programs that simplify and automate software development, delivery and integration. Combination of DevOps tools (for example Docker, Git, Jenkins, Bamboo, Bash scripts, Nagios etc.) can minimize human effort.

Running technical operations is one part. There is also business part that can be automated. After new application or release is delivered, there are tasks that need to be done on running web application. For example specific content need to be published, user password need to be reset, email need to be sent etc. This requirement appears very often during my work with Liferay. How to automate post delivery processes? Liferay allow you to run scripts via browser (Groovy, Javascript, Python, Beanshell, Ruby scripts in control panel)

Unfortunately this approach is not very automation friendly. This is why i have built simple portlet (currently for Liferay 6.2 but version for DXP is very similar) to automate groovy script execution.

Our portlet (called lfrdevops) allow to execute groovy scripts from file stored in a given location. Script can be invoked many ways

via curl (portlet need to be placed on public page if we want to skip authentication):

curl "http://localhost:8080/web/guest/home?p\_p\_id=lfrdevops\_WAR\_lfrdevops&p\_p\_lifecycle=1&p\_p\_state=normal&p\_p\_mode=view&p\_p\_col\_id=column-1&p\_p\_col\_pos=2&p\_p\_col_count=3" -H "Host: localhost:8080"

via browser interface:

We can place our portlet on the page authomatically using many options, for example in init method or via LAR

There are 2 important properties to set in

# path to file with groovy script (file should have read write permissions)


# we need to add portlet lfrdevops_WAR_lfrdevops name if we want skip auth token (therfore portlet action url will not change)


Url to invoke script does not change because portlet has no instance. Url will change only when portlet is moved to another page. We can ensure that portlet is on the page via LAR import/export or custom code on init portlet.

For security reason, we rename groovy file after each execution. This will ensure that script was invoked only once. We simply append execution time to file name.

File name before execution:


File name after execution:


This is very simple version of the portlet to demonstrate how to automate groovy scripts. We can secure execution many ways: authentication, proxy etc.

 import com.liferay.portal.kernel.exception.PortalException;
 import com.liferay.portal.kernel.log.Log;
 import com.liferay.portal.kernel.log.LogFactoryUtil;
 import com.liferay.portal.kernel.scripting.ScriptingException;
 import com.liferay.portal.kernel.scripting.ScriptingHelperUtil;
 import com.liferay.portal.kernel.scripting.ScriptingUtil;
 import com.liferay.portal.kernel.util.PropsUtil;
 import com.liferay.portal.kernel.util.StringPool;
 import com.liferay.portal.kernel.util.UnsyncPrintWriterPool;
 import com.liferay.util.bridges.mvc.MVCPortlet;
 import javax.portlet.ActionRequest;
 import javax.portlet.ActionResponse;
 import javax.portlet.PortletException;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.Map;
 public class LfrDevOpsPortlet extends MVCPortlet {
     private static Log _log = LogFactoryUtil.getLog(LfrDevOpsPortlet .class);
     private static final String FILENAME = PropsUtil.get(LfrDevOpsConstants.GROOVY_FILE_PATH);
     private static final String datePattern = "yyyy_MM_dd_hh_mm_SSS";
     public void processAction(ActionRequest actionRequest, ActionResponse actionResponse)
             throws IOException, PortletException {"Groovy File path in "+LfrDevOpsConstants.GROOVY_FILE_PATH+"="+FILENAME); fileOld = new;
             _log.error("File does not exist: "+FILENAME);
         try {
                 String language = LfrDevOpsConstants.GROOVY;
                 String script = loadScriptFromFile();
                 Map<String, Object> portletObjects = ScriptingHelperUtil.getPortletObjects(getPortletConfig(), getPortletContext(),
                         actionRequest, actionResponse);
                 UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();
                 UnsyncPrintWriter unsyncPrintWriter = UnsyncPrintWriterPool.borrow(unsyncByteArrayOutputStream);
                 portletObjects.put("out", unsyncPrintWriter);
       "Executing groovy script");
                 ScriptingUtil.exec(null, portletObjects, language, script, StringPool.EMPTY_ARRAY);
                 String output = unsyncByteArrayOutputStream.toString();
                 actionResponse.setRenderParameter(LfrDevOpsConstants.SCRIPT_OUTPUT, output);
             } catch (ScriptingException e) {
             } catch (PortalException e) {
         String newFileName = FILENAME+"_executed_"+getExecutionTime(); fileNew = new;
         boolean success = fileOld.renameTo(fileNew);
   "Renamed file to: "+newFileName);
         }"LfrDevOps finished");
     private String getExecutionTime() {
         Calendar cal = Calendar.getInstance();
         Date currentTime = cal.getTime();
         SimpleDateFormat format = new SimpleDateFormat(datePattern);
         String executionTime = format.format(currentTime);
         return executionTime;
     private String loadScriptFromFile() {
         String script = StringPool.BLANK;"file: "+FILENAME);
         try {
             BufferedReader br = new BufferedReader(new FileReader(FILENAME));
             try {
                 StringBuilder sb = new StringBuilder();
                 String line = br.readLine();
                 while (line != null) {
                     script += line+System.getProperty("line.separator");
                     line = br.readLine();
                 String everything = sb.toString();
             } catch (IOException ioe) {
             } finally {
         } catch (FileNotFoundException fnfe) {
         } catch (IOException ioe) {
         }"reading file finished");
         return script;

Code above comply with java 6, 7 und 8. Of course it can be optimized for specific java.

Sample groovy script that reindexes web content portlet:

import com.liferay.portal.service.PortletLocalServiceUtil;
import com.liferay.portal.util.*;
import com.liferay.portal.kernel.dao.shard.*;

companyId = PortalUtil.getDefaultCompanyId();
companyIdString = ""+companyId;
String[] companyIds = [companyIdString]
portlet=PortletLocalServiceUtil.getPortletById(companyId, portletId)
indexes = portlet.getIndexerInstances();
for(index in indexes){
         searchEngineId = index.getSearchEngineId()
         SearchEngineUtil.deletePortletDocuments(searchEngineId, companyId, portletId, true);
for(index in indexes){
         try {
         } catch (Exception e) {
         } finally {
println("Indexing finished for portlet: "+portletId)

You can download whole portlet lfrdevops.

About the author: Piotr Swiniarski