forked from Edirom/MerMEId
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-install.xql
102 lines (85 loc) · 4.43 KB
/
post-install.xql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
xquery version "1.0";
import module namespace xdb="http://exist-db.org/xquery/xmldb";
import module namespace util="http://exist-db.org/xquery/util";
import module namespace dbutil="http://exist-db.org/xquery/dbutil";
import module namespace sm="http://exist-db.org/xquery/securitymanager";
import module namespace file="http://exist-db.org/xquery/file";
import module namespace config="https://github.com/edirom/mermeid/config" at "modules/config.xqm";
declare namespace dcm="http://www.kb.dk/dcm";
(: The following external variables are set by the repo:deploy function :)
(: file path pointing to the exist installation directory :)
declare variable $home external;
(: path to the directory containing the unpacked .xar package :)
declare variable $dir external;
(: the target collection into which the app is deployed :)
declare variable $target external := "/db/apps/mermeid";
declare function local:set-options() as xs:string* {
for $opt in available-environment-variables()[starts-with(., 'MERMEID_')][not(. = ('MERMEID_admin_password', 'MERMEID_admin_password_file',
'MERMEID_mermeid_password', 'MERMEID_mermeid_password_file'))]
return
config:set-property(substring($opt, 9), normalize-space(environment-variable($opt)))
};
declare function local:set-admin-password() as empty-sequence() {
let $opt :=
(: process only one possible option to set the admin password with a preference for a secret file :)
(
available-environment-variables()[. = 'MERMEID_admin_password_file'],
available-environment-variables()[. = 'MERMEID_admin_password']
)[1]
return
if($opt = 'MERMEID_admin_password_file') then
if(file:exists(string(environment-variable($opt)))) then sm:passwd('admin', normalize-space(file:read(normalize-space(environment-variable($opt)))))
else util:log-system-out(concat('unable to read from file "', normalize-space(environment-variable($opt)), '"'))
else if($opt = 'MERMEID_admin_password') then sm:passwd('admin', string(environment-variable($opt)))
else ()
};
declare function local:create-group() as empty-sequence() {
sm:create-group('mermedit')
};
declare function local:change-group() as empty-sequence() {
sm:chgrp(xs:anyURI(concat($target, '/data')), 'mermedit'),
sm:chmod(xs:anyURI(concat($target, '/data')), 'rwxrwxr-x'),
dbutil:scan(xs:anyURI(concat($target, '/data')), function($collection, $resource) {
if ($resource) then (
sm:chgrp($resource, "mermedit"),
sm:chmod($resource, 'rwxrwxr-x')
) else
()
})
};
declare function local:create-user() as empty-sequence() {
let $opt :=
(: process only one possible option to set the admin password with a preference for a secret file :)
(
available-environment-variables()[. = 'MERMEID_mermeid_password_file'],
available-environment-variables()[. = 'MERMEID_mermeid_password']
)[1]
let $password := if($opt = 'MERMEID_mermeid_password_file') then
if(file:exists(string(environment-variable($opt)))) then
normalize-space(file:read(normalize-space(environment-variable($opt))))
else util:log-system-out(concat('unable to read from file "', normalize-space(environment-variable($opt)), '"'))
else if($opt = 'MERMEID_mermeid_password') then
string(environment-variable($opt))
else "mermeid"
return if ($password) then
sm:create-account('mermeid', $password, 'mermeid', ('mermedit'))
else ()
};
declare function local:force-xml-mime-type-xbl() as xs:string* {
let $forms-includes := concat($target, '/forms/includes'),
$log := util:log-system-out(concat('Storing .xbl as XML documents in ', $forms-includes))
return for $r in xdb:get-child-resources($forms-includes)
where ends-with($r, '.xbl')
let $doc := util:binary-doc(concat($forms-includes,'/',$r))
(:return $r||' '||xdb:get-mime-type(xs:anyURI(concat($forms-includes,'/',$r))):)
return if (exists($doc)) then xdb:store($forms-includes, $r, $doc, 'application/xml') else ()
};
(: set options provided as environment variables :)
local:set-options(),
local:force-xml-mime-type-xbl(),
(: set admin password if provided.
NB, this has to be the last command otherwise the other commands will not be executed properly :)
local:create-group(),
local:change-group(),
local:create-user(),
local:set-admin-password()