Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace deployment on wildfly via HTTP API

We would like to replace an existing deployment of our app.war with curl. The post below gives a nice way to deploy a war file. This works well as long as there is no war file deployed with the same name. It fails, however, if there is already a deployment present. Is there any way we can replace the existing deployment via curl?

http://blog.arungupta.me/2014/01/deploy-to-wildfly-using-curl-tech-tip-10/

Is there a way to get the complete interface documentation of the HTTP API of wildfly

like image 655
bertolami Avatar asked Apr 10 '14 07:04

bertolami


People also ask

How to deploy an application on a WildFly domain?

This kind of deployment is available on standalone mode only. Therefore, if you are about to deploy applications on a WildFly domain, you have to use the standard management instruments (CLI or Admin Console). File system deployment just requires that you copy an archive zip into the deployments folder, in order to deploy your application. Example:

Can I change the model in WildFly http management API?

The WildFly HTTP Management API adheres to the REST principles so the GET operations must be idempotent. This means that using a request with method GET can be used to read the model but you won't be able to change it. You must use POST to change the model or read it.

How to deploy Spring Boot project to Wildfly?

Deploying Spring Boot project to WildFly will be divided into three parts: 1. Setting up WildFly Firstly download WildFly application server, after download is complete unzip archive, save it and open terminal in WildFly directory.

How do I deploy a war archive in WildFly?

With Wildfly, you can deploy file types such as EJB-JAR, WAR, EAR, or any kind of standard archive (such as RAR). We’ll be creating a WAR archive for our Java deployment. In that directory create two more directories with the commands: <%! Save and close the file. Where NAME is your name. Save and close that file. Save and close the file.


2 Answers

We wrote a little Shell-Script to achieve this:

#!/bin/bash
echo "Undeploy old war"
curl -S -H "content-Type: application/json" -d '{"operation":"undeploy", "address":[{"deployment":"old.war"}]}' --digest http://user:password@hostname:9990/management
echo ""

echo "Remove old war"
curl -S -H "content-Type: application/json" -d '{"operation":"remove", "address":[{"deployment":"old.war"}]}' --digest http://user:password@hostname:9990/management
echo ""

echo "Upload new war"
bytes_value=`curl -F "file=@/path/to/new.war" --digest http://user:password@$hostname:9990/management/add-content | perl -pe 's/^.*"BYTES_VALUE"\s*:\s*"(.*)".*$/$1/'`
echo $bytes_value

json_string_start='{"content":[{"hash": {"BYTES_VALUE" : "'
json_string_end='"}}], "address": [{"deployment":"new.war"}], "operation":"add", "enabled":"true"}'
json_string="$json_string_start$bytes_value$json_string_end"

echo "Deploy new war"
result=`curl -S -H "Content-Type: application/json" -d "$json_string" --digest http://user:password@hostname:9990/management | perl -pe 's/^.*"outcome"\s*:\s*"(.*)".*$/$1/'`
echo $result

if [ "$result" != "success" ]; then
  exit -1
fi

First of all the old WAR-File is going to be removed. After that the new archive is beeing uploaded and deployed. For us this works even if no content has been deployed yet. In this case the first two calls will fail but the new content is going to be deployed anyway.

We were able to reduce the deployment time from about 20 to 4 minutes by switching from Wildfly Maven-Plugin to this script!

Hope that helps. Cheers

like image 184
nioe Avatar answered Feb 02 '23 00:02

nioe


Many thanks to @nioe for the script! Here's a configurable version with silenced curl better suited for CI scripting:

#!/bin/bash

# Deploys given WAR to WildFly server, pass full path to WAR as argument

set -e
set -u

[[ -f "$1" ]] || { >&2 echo "Usage: $0 WAR-filename ('$1' is not a file)"; exit 1; }

WILDFLY_MANAGEMENT_URL=http://username:password@hostname:9990
WAR_NAME=`basename $1`
WAR_PATH=`dirname $1`

echo "Deploying '$WAR_NAME' from '$WAR_PATH' to '$WILDFLY_MANAGEMENT_URL'"
echo '-------------------'

echo "-> Undeploy old war"
curl -sS -H "content-Type: application/json" -d '{"operation":"undeploy", "address":[{"deployment":"'"${WAR_NAME}"'"}]}' --digest ${WILDFLY_MANAGEMENT_URL}/management
echo ""

echo "-> Remove old war"
curl -sS -H "content-Type: application/json" -d '{"operation":"remove", "address":[{"deployment":"'"${WAR_NAME}"'"}]}' --digest ${WILDFLY_MANAGEMENT_URL}/management
echo ""

echo "-> Upload new war"
bytes_value=`curl -sF "file=@${WAR_PATH}/${WAR_NAME}" --digest ${WILDFLY_MANAGEMENT_URL}/management/add-content | perl -pe 's/^.*"BYTES_VALUE"\s*:\s*"(.*)".*$/$1/'`
echo $bytes_value

json_string_start='{"content":[{"hash": {"BYTES_VALUE" : "'
json_string_end='"}}], "address": [{"deployment":"'"${WAR_NAME}"'"}], "operation":"add", "enabled":"true"}'
json_string="$json_string_start$bytes_value$json_string_end"

echo "-> Deploy new war"
result=`curl -sS -H "Content-Type: application/json" -d "$json_string" --digest ${WILDFLY_MANAGEMENT_URL}/management | perl -pe 's/^.*"outcome"\s*:\s*"(.*)".*$/$1/'`
echo $result

if [ "$result" != "success" ]; then
  exit -1
fi
like image 20
mrts Avatar answered Feb 02 '23 01:02

mrts