At one of our ANDigital client sites I've had the pleasure of working with Adobe Experience Manager, and more specifically, automating some of the workflows involved in managing environments running AEM. One of the specific issues we've had is changing Apache Sling mappings to reflect a unique hostname that's associated with the environment.

In our specific use case, we are using a generic snapshot of an environment with all the required components in it. Being a snapshot, it has entries with a generic hostname set in the Sling mappings - something we need to change whenever a new environment spins up.

The approach we're currently using involves creating a package in AEM CRX with the correct filters (/etc/map.publish in our case), building it, downloading it, changing the hostname, re-packaging it, and finally uploading and installing it back to the AEM instance.

This is a script to do exactly that, with a locally running AEM CQ5.6 Publish instance:

#!/bin/bash
# A script to amend Sling mappings in a locally-running AEM publisher installation

# Get the new hostname from the command line
NEW_ENV_HOSTNAME=$1

# First, create the package
curl -u admin:admin -X POST http://localhost:4503/crx/packmgr/service/.json/etc/packages/sling-mappings.zip?cmd=create -d packageName=sling-mappings -d groupName=my_packages

# Add a filter in the package to include /etc/map.publish
curl -u admin:admin -X POST -F 'path=/etc/packages/my_packages/sling-mappings.zip' -F 'packageName=sling-mappings' -F 'groupName=my_packages' -F 'version=1' -F 'filter=[{"root":"/etc/map.publish", "rules":[]}]' -F '_charset_=UTF-8' http://localhost:4503/crx/packmgr/update.jsp

# Ask AEM to build the package; this should block until it's done
curl -u admin:admin -X POST http://localhost:4503/crx/packmgr/service/.json/etc/packages/my_packages/sling-mappings-1.zip?cmd=build

# Download the package
curl -u admin:admin http://localhost:4503/etc/packages/my_packages/sling-mappings-1.zip > /tmp/sling-mappings-1.zip

# Unzip the package
unzip /tmp/sling-mappings-1.zip -d /tmp/sling-mappings/

# Replace the hostname in every mapping
find /tmp/sling-mappings/ -type f -exec sed -ri "s/old-generic-hostname.com/${NEW_ENV_HOSTNAME}/g" {} +

# Re-package the zip
cd /tmp/sling-mappings && zip -r /tmp/sling-mappings-2.zip *

# Install the changed package
curl -u admin:admin -F file=@/tmp/sling-mappings-2.zip -F name="sling-mappings" -F force=true -F install=true http://localhost:4503/crx/packmgr/service.jsp

The approach is definitely less than optimal (some might even say hacky!) - but with my very limited experience of AEM still, it's something that could get us past this blocker. Done is better than perfect.

1 Comment