This page was generated
March  13,  2012
4:49  AM
XQuery & XSLT Built-In & Modules Function Reference

Module: Configuration Packaging

The Configuration Packaging API module contains functions for scripting the packaging of MarkLogic Server resource configurations, such as database and App Server settings. Use the API to create, compare, and install configuration packages.

The Configuration Packaging API is installed as the following file:


install_dir/Modules/MarkLogic/package/package.xqy
  

To use the Package module as part of your own XQuery module, include the following line in your XQuery prolog:


import module namespace package = "http://marklogic.com/package/package"
        at "/MarkLogic/package/package.xqy";
  

The library namespace prefix package is not predefined in the server.

Function Summary
package:add-appserver Add the configuration settings for an App Server to a package.
package:add-database Add the configuration settings for a database to a package.
package:compare Compare two configuration packages previously created using the packaging API, and return the differences as a pkg:package-diff.
package:create Create an empty configuration for use with other packaging functions.
package:errors Check a package for errors and, if there are errors, return a description of the errors as pkg:configuration-error elements.
package:install Apply the configuration settings in a package to a MarkLogic Server instance.
package:revert Revert configuration settings to those prior to a particular package installation.
package:valid Test a package for consistency.
Function Detail
package:add-appserver(
$pkg as element(pkg:package),
$groupname as xs:string,
$appname as xs:string
)  as   element(pkg:package)
Summary:

Add the configuration settings for an App Server to a package.

Parameters:
$pkg : The package to which to add App Server configuration settings.
$groupname : The name of the Group containing the target App Server.
$appname : The name of the App Server whose settings should be added to the package. The App Server must be in the Group identified by $groupname.

Required Privilege:

http://marklogic.com/xdmp/privileges/manage

Usage Notes:

If the named App Server is already in the package, this function has no effect.

If the App Server described by the $groupname and $appname does not exist, an exception is raised.

The security role manage-user grants http://marklogic.com/xdmp/privileges/manage.


Example:
xquery version "1.0-ml";
import module namespace package = "http://marklogic.com/package/package" 
		  at "/MarkLogic/package/package.xqy";

let $my-package := package:create()
return package:add-appserver($my-package, "Default", "sample-server")
  
  => A package containing the configuration settings for
     the App Server "sample-server" in the "Default" group.
  

package:add-database(
$pkg as element(pkg:package),
$dbname as xs:string
)  as   element(pkg:package)
Summary:

Add the configuration settings for a database to a package.

Parameters:
$pkg : The package to which to add database configuration settings.
$dbname :

Required Privilege:

http://marklogic.com/xdmp/privileges/manage

Usage Notes:

If the database is already in the package, this function has no effect.

If the database does not exist, an exception is raised.

The pre-defined security role manage-user grants http://marklogic.com/xdmp/privileges/manage.


Example:
xquery version "1.0-ml";
import module namespace package = "http://marklogic.com/package/package" 
		  at "/MarkLogic/package/package.xqy";

let $my-package := package:create()
return package:add-database($my-package, "sample-db")
  
  => A package containing the configuration settings for
     the database "sample-db".
  

package:compare(
$source-pkg as node(),
$target-pkg as node()
)  as   element(pkg:package-diff)
Summary:

Compare two configuration packages previously created using the packaging API, and return the differences as a pkg:package-diff.

Parameters:
$source-pkg : The "updated" package to diff against the baseline. The node must be element(pkg:package) or a document node rooted at pkg:package.
$target-pkg : The "original" package to diff against. Differences are reported as changes from this baseline. The node must be a element(pkg:package) or a document node rooted at pkg:package.

Usage Notes:

The returned pkg:package-diff contains the entire configuration, with differences called out in pkg:delta elements.

The differences between the two input packages are reported as changes that transform from-pkg into to-pkg.


Example:
xquery version "1.0-ml";

(: package the original database configuration and save as a document :)
import module namespace package = "http://marklogic.com/package/package" 
    at "/MarkLogic/package/package.xqy";
let $from-package := package:create()
let $from-doc :=
  xdmp:document-insert("/package/from", 
    package:add-database($from-package, "samples"))
return "saved from-package";

(: modify the database configuration by flipping word positions setting :)
import module namespace admin = "http://marklogic.com/xdmp/admin" 
    at "/MarkLogic/admin.xqy";
let $db-id := xdmp:database("samples")
let $config := admin:get-configuration()
return admin:save-configuration( 
  admin:database-set-word-positions(
    $config, $db-id,
    fn:not(admin:database-get-word-positions($config,$db-id))
  ));

(: package the modified database configuration and save as a document :)
import module namespace package = "http://marklogic.com/package/package" 
    at "/MarkLogic/package/package.xqy";
let $to-package := package:create()
let $to-doc :=
  xdmp:document-insert("/package/to", 
    package:add-database($to-package, "samples"))
return "saved to-package";

(: compare the original and modified configurations :)
import module namespace package = "http://marklogic.com/package/package" 
    at "/MarkLogic/package/package.xqy";
declare namespace pkg = "http://marklogic.com/package";
let $deltas :=
  package:compare(fn:doc("/package/to"), fn:doc("/package/from"))//pkg:delta
for $delta in ($deltas)
return $delta/..


  => The package differences, showing word positions changed
     from false to true:

saved from-package
saved to-package
<db:timestamp xmlns:pkg="http://marklogic.com/package" 
              xmlns:db="http://marklogic.com/package/databases">
  <pkg:delta>
    <pkg:del>2011-09-26T12:19:55.237658-07:00</pkg:del>
    <pkg:add>2011-09-26T12:19:55.732692-07:00</pkg:add>
  </pkg:delta>
</db:timestamp>
<word-positions xmlns:pkg="http://marklogic.com/package" 
                xmlns:db="http://marklogic.com/package/databases" 
                xmlns="http://marklogic.com/package/databases">
  <pkg:delta>
    <pkg:del>false</pkg:del>
    <pkg:add>true</pkg:add>
  </pkg:delta>
</word-positions>
  

package:create(  ) as  element(pkg:package)
Summary:

Create an empty configuration for use with other packaging functions.

Example:
xquery version "1.0-ml";
import module namespace package = "http://marklogic.com/package/package" 
		  at "/MarkLogic/package/package.xqy";

package:create()
  
  => An empty package. Use package:add-appserver
     and package:add-database to add settings.
  

package:errors(
$pkg as element()
)  as   element(pkg:configuration-errors)*
Summary:

Check a package for errors and, if there are errors, return a description of the errors as pkg:configuration-error elements.

Parameters:
$pkg : The package for which to report errors. This should be an element(pkg:package).

Example:
xquery version "1.0-ml";
import module namespace package = "http://marklogic.com/package/package" 
    at "/MarkLogic/package/package.xqy";

let $incomplete-package := 
  <pkg:package xmlns:pkg="http://marklogic.com/package">
   <package-database xmlns="http://marklogic.com/package/databases">
    <name>samples</name>
   </package-database>
  </pkg:package>
return package:errors($incomplete-package)
  
  => A schema violation error because the package is incomplete:

  <pkg:configuration-errors xmlns:pkg="http://marklogic.com/package">
    <pkg:resource-type>package</pkg:resource-type>
    <pkg:configuration-error>
      The package document is not schema-valid.
    </pkg:configuration-error>
  </pkg:configuration-errors>
  

package:install(
$pkg as node()
)  as   element(pkg:result)
Summary:

Apply the configuration settings in a package to a MarkLogic Server instance.

Parameters:
$pkg : The packaged configuration to install. The node must be either an element(pkg:package), or a document node rooted at element(pkg:package).

Usage Notes:

Only users in the admin role may install packages.

Installing a package creates a ticket. The ticket number is provided in the returned pkg:result. Use info:ticket to check the status of the ticket.

Modifying some settings requires a server restart. If package installation requires a server restart to complete, the returned result directs you to restart.

The returned pkg:result reports success or failure in the pkg:result-status child element. If the installation fails, the live configuration is unchanged; errors are reported in the pkg:configuration-errors child element.

Attempting to install two or more packages concurrently may leave your configuration in an indeterminate state.


Example:
(: install a package previously saved as a document :)
import module namespace package = "http://marklogic.com/package/package" 
    at "/MarkLogic/package/package.xqy";
package:install(fn:doc("/package/to"))

  => The settings in /package/to are applied and a pkg:result is
     returned, indicating success or failure. For example:

  <pkg:result xmlns:pkg="http://marklogic.com/package">
    <pkg:ticket-id>/tickets/ticket/2438123874498566113</pkg:ticket-id>
    <pkg:result-status>COMPLETED</pkg:result-status>
    <pkg:result-event>PACKAGE_WRITTEN</pkg:result-event>
  </pkg:result>
  

package:revert(
$ticket-id as xs:string
)  as   element(pkg:result)
Summary:

Revert configuration settings to those prior to a particular package installation.

Parameters:
$ticket-id : The id of a ticket created by a previous successful call to package:install.

Usage Notes:

Reverting a package installation restores all settings to their state at the time ticket was created by calling package:install. If other configuration changes are applied between the successful install and the attempt to revert, those changes are lost.

Successful calls to package:install create a ticket associated with the configuration changes. The ticket id provided in the pkg:result returned by package:install. Use this ticket id to revert the changes.


Example:
xquery version "1.0-ml";
import module namespace package = "http://marklogic.com/package/package" 
    at "/MarkLogic/package/package.xqy";

(: install a package :)
package:install(fn:doc("/package/to"));

(: Produces the following for some previously saved package:

  <pkg:result xmlns:pkg="http://marklogic.com/package">
    <pkg:ticket-id>/tickets/ticket/14006129137320060991</pkg:ticket-id>
    <pkg:result-status>COMPLETED</pkg:result-status>
    <pkg:result-event>NO_CONFIG_CHANGE</pkg:result-event>
  </pkg:result>
:)

(: revert the above install :)
import module namespace package = "http://marklogic.com/package/package" 
    at "/MarkLogic/package/package.xqy";

package:revert("/tickets/ticket/14006129137320060991")

  
  => The changes applied by the package are reverted:

  <pkg:result xmlns:pkg="http://marklogic.com/package">
    <pkg:ticket-id>/tickets/ticket/2438123874498566113</pkg:ticket-id>
    <pkg:result-status>COMPLETED</pkg:result-status>
    <pkg:result-event>PACKAGE_WRITTEN</pkg:result-event>
  </pkg:result>

  

package:valid(
$pkg as element()
)  as   xs:boolean
Summary:

Test a package for consistency.

Parameters:
$pkg : The package to check. This should be an element(pkg:package).

Usage Notes:

This function returns true if the package is valid and false otherwise. Use package:errors to view a list of errors for an invalid package.

A package may be invalid for many reasons, including a violation of the package schema (Config/package.xsd), fields with erroneous values, or duplicate settings.


Example:
xquery version "1.0-ml";
import module namespace package = "http://marklogic.com/package/package" 
    at "/MarkLogic/package/package.xqy";

let $incomplete-package := 
  <pkg:package xmlns:pkg="http://marklogic.com/package">
   <package-database xmlns="http://marklogic.com/package/databases">
    <name>samples</name>
   </package-database>
  </pkg:package>
return package:valid($incomplete-package)
  
  => false because the package is incomplete