|
| 1 | +xquery version "3.1"; |
| 2 | + |
| 3 | +(:~ |
| 4 | + : Simple script to uninstall a package, and install a new |
| 5 | + : version of the package from a xar file uploaded to a |
| 6 | + : location in the db. |
| 7 | + :) |
| 8 | +declare namespace repo = "http://exist-db.org/xquery/repo"; |
| 9 | +declare namespace util = "http://exist-db.org/xquery/util"; |
| 10 | + |
| 11 | +declare variable $existdb-public-repo-url := "http://exist-db.org/exist/apps/public-repo"; |
| 12 | + |
| 13 | +(:~ |
| 14 | + : Name of the package to uninstall and install a new version of. |
| 15 | + :) |
| 16 | +declare variable $pkg-name := "${package-name}"; |
| 17 | + |
| 18 | +(:~ |
| 19 | + : Location where the new version of the xar file for the package |
| 20 | + : can be found in the database. |
| 21 | + :) |
| 22 | +declare variable $pkg-db-tmp-path := "${db-tmp-collection}/${package-final-name}.xar"; |
| 23 | + |
| 24 | +declare variable $undeploy-error := xs:QName("undeploy-error"); |
| 25 | +declare variable $remove-error := xs:QName("remove-error"); |
| 26 | +declare variable $no-pkg-error := xs:QName("no-pkg-error"); |
| 27 | +declare variable $install-and-deploy-error := xs:QName("no-pkg-error"); |
| 28 | + |
| 29 | + |
| 30 | +declare function local:pkg-installed($pkg-name as xs:string) as xs:boolean { |
| 31 | + exists(repo:list()[. eq $pkg-name]) |
| 32 | +}; |
| 33 | + |
| 34 | +declare function local:undeploy-and-remove-pkg($pkg-name as xs:string) as empty-sequence() { |
| 35 | + let $undeploy-result := repo:undeploy($pkg-name) |
| 36 | + return |
| 37 | + if ($undeploy-result/@result ne "ok") |
| 38 | + then |
| 39 | + fn:error($undeploy-error, "Unable to undeploy existing package: " || $pkg-name) |
| 40 | + else |
| 41 | + let $remove-result := repo:remove($pkg-name) |
| 42 | + return |
| 43 | + if ($remove-result eq fn:false()) |
| 44 | + then |
| 45 | + fn:error($remove-error, "Unable to remove existing package: " || $pkg-name) |
| 46 | + else |
| 47 | + () |
| 48 | +}; |
| 49 | + |
| 50 | + |
| 51 | +if (local:pkg-installed($pkg-name)) |
| 52 | +then |
| 53 | + local:undeploy-and-remove-pkg($pkg-name) |
| 54 | +else () |
| 55 | + |
| 56 | +, |
| 57 | + |
| 58 | +if (util:binary-doc-available($pkg-db-tmp-path) eq fn:false()) |
| 59 | +then |
| 60 | + fn:error($no-pkg-error, "Package: " || $pkg-name || ", cannot be found at: " || $pkg-db-tmp-path) |
| 61 | +else |
| 62 | + let $install-and-deploy-result := repo:install-and-deploy-from-db($pkg-db-tmp-path) |
| 63 | + return |
| 64 | + if ($install-and-deploy-result/@result ne "ok") |
| 65 | + then |
| 66 | + fn:error($install-and-deploy-error, "Unable to install and deploy package: " || $pkg-name) |
| 67 | + else |
| 68 | + <success timestamp="{util:system-dateTime()}"/> |
0 commit comments