Skip to content

Instantly share code, notes, and snippets.

@nateyolles
Last active October 2, 2024 12:24
Show Gist options
  • Save nateyolles/dd4ebe0a6b83c369029b to your computer and use it in GitHub Desktop.
Save nateyolles/dd4ebe0a6b83c369029b to your computer and use it in GitHub Desktop.
AEM/CQ cURL: Adding include/exclude rules to package filters
# Adding include/exclude rules to CQ/AEM package filters through cURL.
# Through a simple search, you will find numerous lists of CQ/AEM cURL commands.
# However, I haven't seen an example of adding rules to package filters. The
# JSON "rules" key takes an array value. You can leave the array empty if you
# don't need to include any rules. The array is of JSON objects with a
# "modifier" key and value of "include" or "exclude", and a "pattern" key with
# your path or regular expression as the value.
# create package
curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/testpackage?cmd=create \
-d packageName=testpackage \
-d groupName=my_packages
# add filters
curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/update.jsp \
-F path=/etc/packages/my_packages/testpackage.zip -F packageName=testpackage \
-F groupName=my_packages \
-F filter="[{\"root\" : \"/content/my-site\", \"rules\": [{\"modifier\" : \"exclude\", \"pattern\" : \"/content/my-site/(.*)/folder-to-exclude(/.*)?\"}]}]" \
-F '_charset_=UTF-8'
# build package
curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/testpackage.zip?cmd=build
@schapman-mohegan
Copy link

Thank you so much for posting this - I found a few blog posts on creating the packages with filters, but everything I tried failed.
None of the posts I saw mentioned I had to create the package in one step, and then add the filters in another.

Once I did that, it worked.
Also, I used Chrome Debugger to check what URL Package Manager was using to create it, and it used (for AEM 6.1 at least):
http://localhost:4502/crx/packmgr/service/exec.json?cmd=create
rather than:
http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/testpackage?cmd=create

One other thing...
I see many posts with people using the -u admin:admin
You can set up an _netrc file in your home directory, and just use -n without the admin:admin
The format of the _netrc would be:
machine localhost login admin password admin

Much safer if you're using a administrative account that's not admin:admin since your commands can be logged, and in Windows, your dos window title actually changes to your command when you run it.

The _netrc filename or location may be different if you're using unix.

@ashdevil
Copy link

ashdevil commented Nov 9, 2017

Hey @vkkohls1
For me it gives package updated successfully but there is no filter added in the package when I check crx packmgr or crxde

@sudheersundalam
Copy link

sudheersundalam commented Jan 23, 2018

@ashdevil: I faced the same problem. I used below curl commands and it worked always.

curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/testpackage?cmd=create -d packageName=testpackage -d groupName=my_packages

curl -F jcr:primaryType=nt:unstructured -u admin:admin http://localhost:4502/etc/packages/my_packages/testpackage.zip/jcr:content/vlt:definition/filter/f2

curl -u admin:admin -F root="/content/dam/onlocation" http://localhost:4502/etc/packages/my_packages/testpackage.zip/jcr:content/vlt:definition/filter/f2.rw.html

curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/service/.json/etc/packages/my_packages/testpackage.zip?cmd=build

@atulkumar7
Copy link

@sudheersundalam : I am trying to add rules to the filters but facing issues.

I am using below command:
curl -u admin:admin POST -Froot="/conf" -Frules="[exclude:/conf/global]" http://localhost:4502/etc/packages/my_packages/testpackage.zip/jcr:content/vlt:definition/filter/f2.rw.html

This command is simply adding rules property in String variable. But if we add rules from UI, the values gets stored as String[].

Can define type of property while adding it to a node using curl?

Thanks,
Atul

@atulkumar7
Copy link

Found a way to do it. :)

curl -u admin:admin POST -Froot="/conf" -F"rules=exclude:/conf/global" -F"rules=exclude:/conf/we-retail" http://localhost:4502/etc/packages/my_packages/testpackage.zip/jcr:content/vlt:definition/filter/f2.rw.html

Repetition of -Frules will set the property as String[].

Thanks,
Atul

@rohit-kapoor4
Copy link

I need to add multiple roots to it but it seems to only pick up the first one when I am adding more than one.
For example: I'd like it to have the content of We-retail and WKND both. I'm using the same command as @nateyolles has described and I'm adding another root/ to it. Here's the command:

curl -u admin:admin -X POST http://localhost:4502/crx/packmgr/update.jsp \
-F path=/etc/packages/my_packages/testpackage.zip -F packageName=testpackage \
-F groupName=my_packages \
-F {filter="[{\"root\" : \"/content/WKND\", \"rules\": [{\"modifier\" : \"exclude\", \"pattern\" : \"/content/xx/yy(/.*)?\"}]}]}" \
-F {filter="[{\"root\" : \"/content/we-retail\", \"rules\": [{\"modifier\" : \"exclude\", \"pattern\" : \"/content/xx/(.*)/dam(/.*)?\"}]}]}" -F '_charset_=UTF-8'

Only the first one gets picked, In this case /content/WKND

@atulkumar7
Copy link

Hi Rohit,

You can create a config file where you can list all your filters and then write a function to iterate through the list of filters:

create_filters() {
	for (( c=0; c<=$filtersNum-1; c++ ))
	do
		# Creae filter node 
		curl --data jcr:primaryType=nt:unstructured --user $username:$password http://$host:$port/etc/packages/$grpName/$packageName1.zip/jcr:content/vlt:definition/filter/f$c >> log.txt
		echo "add root path in filter number $c"
		#echo "Status code for f$c : $addstatuscode"
		# add root path in filter
		curl -u $username:$password POST -F"root=${FILTERS[$c]}" $rules http://$host:$port/etc/packages/$grpName/$packageName.zip/jcr:content/vlt:definition/filter/f$c.rw.html >> log.txt
		#echo "Status code for f$c : $addrootstatuscode"
	done
}

Config:
host=localhost
port=4502
username=admin
password=admin
packageName=test
grpName=my_packages
filtersNum=2
FILTERS=(/content/WKND,/content/we-retail)

Hope this helps!!

@kbrahmbh
Copy link

If you need to add AC Handing to the package along with filters here is how you can do it :
curl -u admin:password -X POST http://HOSTNAME:PORT/crx/packmgr/update.jsp -F path=/etc/packages/my_packages/testpackage3.zip -F packageName=testpackage3 -F groupName=my_packages -F filter="[{"root" : "/content/xx/yy/CA", "rules": []}]" -F 'acHandling=merge_preserve' -F 'charset=UTF-8'

@munim
Copy link

munim commented Nov 2, 2023

Made it to work on AEM 6.5

curl --insecure -u admin:password -X POST https://IP-Address/crx/packmgr/update.jsp \
-F path=/etc/packages/com.example/example-content-en-20231102.zip \
-F packageName=example-content-en-20231102 \
-F groupName=com.example \
-F 'filter=[{"root": "/content/example/en", "rules":[{"modifier": "exclude", "pattern": "/content/example/en/bad-content"}]}]'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment