Skip to content

Instantly share code, notes, and snippets.

@holmesw
Last active July 27, 2024 09:11
Show Gist options
  • Save holmesw/069fb25609601afc3f3b2ca0d2c5b09f to your computer and use it in GitHub Desktop.
Save holmesw/069fb25609601afc3f3b2ca0d2c5b09f to your computer and use it in GitHub Desktop.
Clone a MarkLogic App Server

Clone An ML App Server in XQuery 3.1

This XQuery script is designed to clone an existing server in MarkLogic. If the new server already exists, it returns the name of the new server. Otherwise, it creates a new server and returns the configuration.

Version

  • Version: 1.0
  • Since: 2024-03-26
  • XQuery Version: 3.1

Namespaces

Module Import

Variables

  • $config: The current configuration.
  • $group-id: The current group id.
  • $server-name: The name of the server to clone.
  • $port: The port of the server to clone.
  • $new-server-name: The name for the new server.
  • $new-port: The port for the new server.
  • $servers: All the servers.
  • $existing-server: The server to be cloned.
  • $new-server: The new server to be created.

Main Logic

If the new server does not exist, the script clones the existing server. Otherwise, it returns the name of the new server. The cloning process is done by copying the configuration of the existing server to the new server using the admin:appserver-copy function. The new configuration is then saved without restarting the server using the admin:save-configuration-without-restart function. If the new server already exists, the script simply returns the name of the new server.

Usage

Replace "existing-app-server-name-here" with the name of the server you want to clone, and adjust the $port, $new-server-name, and $new-port variables as needed. Run the script in a context where the xdmp:group() function returns the ID of the group containing the server to clone.

(:~
: @version 1.0
: @since 2024-03-26
: @module.version 3.1
: @return The name of the new server if it already exists,
: otherwise creates a new server and returns the configuration.
~:)
xquery version "3.1";
(:~
: @namespace xdmp MarkLogic Built-In Functions.
: @see https://docs.marklogic.com/xdmp
~:)
declare namespace xdmp = "http://marklogic.com/xdmp";
(:~
: @namespace fn XPath and XQuery Functions.
: @see http://www.w3.org/2005/xpath-functions
~:)
declare namespace fn = "http://www.w3.org/2005/xpath-functions";
(:~
: @module.namespace admin "http://marklogic.com/xdmp/admin"
: @module.uri "/MarkLogic/admin.xqy"
: @module.import admin at "/MarkLogic/admin.xqy"
: @see https://docs.marklogic.com/admin
~:)
import module namespace admin =
"http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";
(:~
: @variable $config The current configuration.
~:)
declare variable $config as element(configuration) :=
admin:get-configuration();
(:~
: @variable $group-id The current group id.
~:)
declare variable $group-id as xs:unsignedLong := xdmp:group();
(:~
: @variable $server-name The name of the server to clone.
~:)
declare variable $server-name as xs:string :=
"existing-app-server-name-here";
(:~
: @variable $port The port of the server to clone.
~:)
declare variable $port as xs:unsignedInt := 9052;
(:~
: @variable $new-server-name The name for the new server.
~:)
declare variable $new-server-name as xs:string :=
$server-name || "-clone";
(:~
: @variable $new-port The port for the new server.
~:)
declare variable $new-port as xs:unsignedInt := 9053;
(:~
: @variable $servers All the servers.
~:)
declare variable $servers as xs:unsignedLong* :=
xdmp:servers();
(:~
: @variable $existing-server The server to be cloned.
~:)
declare variable $existing-server as xs:unsignedLong* :=
$servers[xdmp:server-name(.) eq $server-name and
xdmp:server-port(.) eq $port and
xdmp:server-group(.) eq $group-id];
(:~
: @variable $new-server The new server to be created.
~:)
declare variable $new-server as xs:unsignedLong* :=
$servers[xdmp:server-port(.) eq $new-port and
xdmp:server-group(.) eq $group-id];
(:~
: If the new server does not exist, clone the existing server.
: Otherwise, return the name of the new server.
~:)
if (fn:empty($new-server)) then
for $server as xs:unsignedLong in $existing-server
return
(
admin:save-configuration-without-restart(
admin:appserver-copy(
$config, $server, $group-id, $new-server-name, $new-port
)
),
"created " || $new-server-name
)
else
fn:string-join(
(
"server ",
$new-server ! xdmp:server-name(.),
" already exists"
),
""
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment