Skip to content
This repository was archived by the owner on Nov 9, 2022. It is now read-only.

Commit aa586fc

Browse files
committed
#808: added initial stab at export_config to split existing ml-config.xml into parts
1 parent 6478b7c commit aa586fc

File tree

6 files changed

+90
-7
lines changed

6 files changed

+90
-7
lines changed

deploy/lib/server_config.rb

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,50 @@ def deploy
12041204
return true
12051205
end
12061206

1207+
def export_config
1208+
setup = File.read ServerConfig.expand_path("#{@@path}/lib/xquery/setup.xqy")
1209+
query = %Q{
1210+
#{setup}
1211+
try {
1212+
for $part in setup:split-config(
1213+
setup:rewrite-config(#{get_config(true)}, (), fn:true()),
1214+
"#{@properties["ml.app-name"]}"
1215+
)
1216+
return
1217+
xdmp:quote(
1218+
$part,
1219+
<options xmlns="xdmp:quote">
1220+
<indent>yes</indent>
1221+
<indent-untyped>yes</indent-untyped>
1222+
</options>
1223+
)
1224+
} catch($ex) {
1225+
xdmp:log($ex),
1226+
fn:concat($ex/err:format-string/text(), '&#10;See MarkLogic Server error log for more details.')
1227+
}
1228+
}
1229+
logger.debug query
1230+
r = execute_query query
1231+
logger.debug "code: #{r.code.to_i}"
1232+
1233+
r.body = parse_body(r.body)
1234+
logger.info r.body
1235+
return true
1236+
end
1237+
1238+
def export
1239+
what = ARGV.shift
1240+
raise HelpException.new("export", "Missing WHAT") unless what
1241+
1242+
case what
1243+
when 'config'
1244+
export_config
1245+
else
1246+
raise HelpException.new("export", "Invalid WHAT")
1247+
end
1248+
return true
1249+
end
1250+
12071251
def load
12081252
dir = ARGV.shift
12091253
db = find_arg(['--db']) || @properties['ml.content-db']
@@ -2355,7 +2399,7 @@ def mlRest
23552399
end
23562400
end
23572401

2358-
def get_config
2402+
def get_config(preserve_props = false)
23592403
if @server_version > 7 && @properties["ml.app-type"] == 'rest' && @properties["ml.url-rewriter"] == "/MarkLogic/rest-api/rewriter.xqy"
23602404
@logger.info "WARN: XQuery REST rewriter has been deprecated since MarkLogic 8"
23612405
@properties["ml.url-rewriter"] = "/MarkLogic/rest-api/rewriter.xml"
@@ -2371,7 +2415,7 @@ def get_config
23712415
@logger.info " See https://github.com/marklogic/roxy/issues/416 for details."
23722416
end
23732417

2374-
@config ||= build_config(@options[:config_file])
2418+
@config ||= build_config(@options[:config_file], preserve_props)
23752419
end
23762420

23772421
def execute_query_4(query, properties)
@@ -2890,7 +2934,7 @@ def ssl_certificate_xml
28902934
}
28912935
end
28922936

2893-
def build_config(config_paths)
2937+
def build_config(config_paths, preserve_props = false)
28942938
config_files = []
28952939
config_paths.split(",").each do |config_path|
28962940
if File.directory?(config_path)
@@ -3048,7 +3092,9 @@ def build_config(config_paths)
30483092
config.gsub!("@ml.ssl-certificate-xml", "")
30493093
end
30503094

3051-
replace_properties(config, File.basename(config_file))
3095+
if ! preserve_props
3096+
replace_properties(config, File.basename(config_file))
3097+
end
30523098

30533099
# escape unresolved braces, they have special meaning in XQuery
30543100
config.gsub!("{", "{{")

deploy/lib/xquery/setup.xqy

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ declare function setup:rewrite-config($import-configs as node()+, $properties as
654654

655655
let $default-group := ($import-configs/self::*:configuration/@default-group, "Default")[1]
656656
for $group in fn:distinct-values((
657-
$import-configs/descendant-or-self::gr:group/gr:group-name,
657+
$import-configs/descendant-or-self::gr:group/gr:group-name/fn:string(),
658658
$import-configs/descendant-or-self::*/(
659659
self::gr:http-server, self::gr:xdbc-server,
660660
self::gr:odbc-server, self::gr:task-server, self::db:database
@@ -677,7 +677,7 @@ declare function setup:rewrite-config($import-configs as node()+, $properties as
677677
let $databases := $import-configs/descendant-or-self::db:database[
678678
@group = $group or ( $group = $default-group and fn:empty(@group) )
679679
]
680-
let $group-config := $import-configs/descendant-or-self::gr:group[gr:group-name = $group]
680+
let $group-config := $import-configs/descendant-or-self::gr:group[gr:group-name/fn:string() = $group]
681681
where fn:exists($servers | $databases | $group-config)
682682
return
683683
<group>
@@ -726,7 +726,7 @@ declare function setup:rewrite-config($import-configs as node()+, $properties as
726726
let $_ :=
727727
if ($silent) then ()
728728
else
729-
for $group in $config/gr:groups/gr:group/gr:group-name
729+
for $group in $config/gr:groups/gr:group/gr:group-name/fn:string()
730730
let $hosts := ($config/ho:hosts/ho:host[ho:group/@name = $group], try { xdmp:group-hosts(xdmp:group($group)) } catch ($ignore) {})
731731
where fn:empty($hosts)
732732
return
@@ -741,6 +741,43 @@ declare function setup:rewrite-config($import-configs as node()+, $properties as
741741
)
742742
};
743743

744+
declare function setup:split-config($config as element(configuration), $app-name as xs:string) as node()* {
745+
for $part in (
746+
$config/*/*,
747+
$config/gr:groups/gr:group/(
748+
(gr:http-servers, gr:xdbc-servers, gr:odbc-servers)/*,
749+
gr:task-server
750+
)
751+
)
752+
let $type := fn:local-name($part)
753+
754+
let $name :=
755+
if ($part instance of element(gr:task-server)) then
756+
"TaskServer"
757+
else
758+
$part/*[local-name() = ("name", "forest-name", "local-name", concat($type, "-name"))][1]
759+
/fn:replace(fn:replace(fn:string(), "^(.*/)?([^/]+)", "$2"), "^\$\{group\}$", "default-group")
760+
761+
let $path := fn:replace(fn:replace($type, "(http|xdbc|odbc|task)-", "") || "s", "ys$", "ies")
762+
let $path :=
763+
if (fn:contains(fn:namespace-uri($part), "security")) then
764+
"security/" || $path
765+
else
766+
fn:replace($path, "assignments/", "forests/")
767+
768+
let $file := fn:replace($name, "(@ml.|[${}])", "") || ".xml"
769+
770+
return (
771+
comment { "SAVE-PART-AS:" || $path || "/" || $file },
772+
typeswitch ($part)
773+
case element(gr:group)
774+
return element gr:group {
775+
$part/@*,
776+
$part/(node() except (gr:http-servers, gr:xdbc-servers, gr:odbc-servers, gr:task-server))
777+
}
778+
default return $part
779+
)
780+
};
744781

745782
(:
746783
base-name : Original forest base name - this should be the name from the config.

0 commit comments

Comments
 (0)