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

Commit f13fbc0

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

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
@@ -1198,6 +1198,50 @@ def deploy
11981198
return true
11991199
end
12001200

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

2352-
def get_config
2396+
def get_config(preserve_props = false)
23532397
if @server_version > 7 && @properties["ml.app-type"] == 'rest' && @properties["ml.url-rewriter"] == "/MarkLogic/rest-api/rewriter.xqy"
23542398
@logger.info "WARN: XQuery REST rewriter has been deprecated since MarkLogic 8"
23552399
@properties["ml.url-rewriter"] = "/MarkLogic/rest-api/rewriter.xml"
@@ -2365,7 +2409,7 @@ def get_config
23652409
@logger.info " See https://github.com/marklogic/roxy/issues/416 for details."
23662410
end
23672411

2368-
@config ||= build_config(@options[:config_file])
2412+
@config ||= build_config(@options[:config_file], preserve_props)
23692413
end
23702414

23712415
def execute_query_4(query, properties)
@@ -2884,7 +2928,7 @@ def ssl_certificate_xml
28842928
}
28852929
end
28862930

2887-
def build_config(config_paths)
2931+
def build_config(config_paths, preserve_props = false)
28882932
config_files = []
28892933
config_paths.split(",").each do |config_path|
28902934
if File.directory?(config_path)
@@ -3042,7 +3086,9 @@ def build_config(config_paths)
30423086
config.gsub!("@ml.ssl-certificate-xml", "")
30433087
end
30443088

3045-
replace_properties(config, File.basename(config_file))
3089+
if ! preserve_props
3090+
replace_properties(config, File.basename(config_file))
3091+
end
30463092

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

deploy/lib/xquery/setup.xqy

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ declare function setup:rewrite-config($import-configs as node()+, $silent as xs:
536536
<groups xmlns="http://marklogic.com/xdmp/group">{
537537
let $default-group := ($import-configs/self::*:configuration/@default-group, "Default")[1]
538538
for $group in fn:distinct-values((
539-
$import-configs/descendant-or-self::gr:group/gr:group-name,
539+
$import-configs/descendant-or-self::gr:group/gr:group-name/fn:string(),
540540
$import-configs/descendant-or-self::*/(
541541
self::gr:http-server, self::gr:xdbc-server,
542542
self::gr:odbc-server, self::gr:task-server, self::db:database
@@ -559,7 +559,7 @@ declare function setup:rewrite-config($import-configs as node()+, $silent as xs:
559559
let $databases := $import-configs/descendant-or-self::db:database[
560560
@group = $group or ( $group = $default-group and fn:empty(@group) )
561561
]
562-
let $group-config := $import-configs/descendant-or-self::gr:group[gr:group-name = $group]
562+
let $group-config := $import-configs/descendant-or-self::gr:group[gr:group-name/fn:string() = $group]
563563
where fn:exists($servers | $databases | $group-config)
564564
return
565565
<group>
@@ -608,7 +608,7 @@ declare function setup:rewrite-config($import-configs as node()+, $silent as xs:
608608
let $_ :=
609609
if ($silent) then ()
610610
else
611-
for $group in $config/gr:groups/gr:group/gr:group-name
611+
for $group in $config/gr:groups/gr:group/gr:group-name/fn:string()
612612
let $hosts := ($config/ho:hosts/ho:host[ho:group/@name = $group], try { xdmp:group-hosts(xdmp:group($group)) } catch ($ignore) {})
613613
where fn:empty($hosts)
614614
return
@@ -621,6 +621,43 @@ declare function setup:rewrite-config($import-configs as node()+, $silent as xs:
621621
return if ($keep-comments) then $config else setup:suppress-comments($config)
622622
};
623623

624+
declare function setup:split-config($config as element(configuration), $app-name as xs:string) as node()* {
625+
for $part in (
626+
$config/*/*,
627+
$config/gr:groups/gr:group/(
628+
(gr:http-servers, gr:xdbc-servers, gr:odbc-servers)/*,
629+
gr:task-server
630+
)
631+
)
632+
let $type := fn:local-name($part)
633+
634+
let $name :=
635+
if ($part instance of element(gr:task-server)) then
636+
"TaskServer"
637+
else
638+
$part/*[local-name() = ("name", "forest-name", "local-name", concat($type, "-name"))][1]
639+
/fn:replace(fn:replace(fn:string(), "^(.*/)?([^/]+)", "$2"), "^\$\{group\}$", "default-group")
640+
641+
let $path := fn:replace(fn:replace($type, "(http|xdbc|odbc|task)-", "") || "s", "ys$", "ies")
642+
let $path :=
643+
if (fn:contains(fn:namespace-uri($part), "security")) then
644+
"security/" || $path
645+
else
646+
fn:replace($path, "assignments/", "forests/")
647+
648+
let $file := fn:replace($name, "(@ml.|[${}])", "") || ".xml"
649+
650+
return (
651+
comment { "SAVE-PART-AS:" || $path || "/" || $file },
652+
typeswitch ($part)
653+
case element(gr:group)
654+
return element gr:group {
655+
$part/@*,
656+
$part/(node() except (gr:http-servers, gr:xdbc-servers, gr:odbc-servers, gr:task-server))
657+
}
658+
default return $part
659+
)
660+
};
624661

625662
(:
626663
base-name : Original forest base name - this should be the name from the config.

0 commit comments

Comments
 (0)