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

Commit d6c64d0

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

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
@@ -1220,6 +1220,50 @@ def deploy
12201220
return true
12211221
end
12221222

1223+
def export_config
1224+
setup = File.read ServerConfig.expand_path("#{@@path}/lib/xquery/setup.xqy")
1225+
query = %Q{
1226+
#{setup}
1227+
try {
1228+
for $part in setup:split-config(
1229+
setup:rewrite-config(#{get_config(true)}, (), fn:true()),
1230+
"#{@properties["ml.app-name"]}"
1231+
)
1232+
return
1233+
xdmp:quote(
1234+
$part,
1235+
<options xmlns="xdmp:quote">
1236+
<indent>yes</indent>
1237+
<indent-untyped>yes</indent-untyped>
1238+
</options>
1239+
)
1240+
} catch($ex) {
1241+
xdmp:log($ex),
1242+
fn:concat($ex/err:format-string/text(), '&#10;See MarkLogic Server error log for more details.')
1243+
}
1244+
}
1245+
logger.debug query
1246+
r = execute_query query
1247+
logger.debug "code: #{r.code.to_i}"
1248+
1249+
r.body = parse_body(r.body)
1250+
logger.info r.body
1251+
return true
1252+
end
1253+
1254+
def export
1255+
what = ARGV.shift
1256+
raise HelpException.new("export", "Missing WHAT") unless what
1257+
1258+
case what
1259+
when 'config'
1260+
export_config
1261+
else
1262+
raise HelpException.new("export", "Invalid WHAT")
1263+
end
1264+
return true
1265+
end
1266+
12231267
def load
12241268
dir = ARGV.shift
12251269
db = find_arg(['--db']) || @properties['ml.content-db']
@@ -2457,7 +2501,7 @@ def mlRest
24572501
end
24582502
end
24592503

2460-
def get_config
2504+
def get_config(preserve_props = false)
24612505
if @server_version > 7 && @properties["ml.app-type"] == 'rest' && @properties["ml.url-rewriter"] == "/MarkLogic/rest-api/rewriter.xqy"
24622506
@logger.info "WARN: XQuery REST rewriter has been deprecated since MarkLogic 8"
24632507
@properties["ml.url-rewriter"] = "/MarkLogic/rest-api/rewriter.xml"
@@ -2473,7 +2517,7 @@ def get_config
24732517
@logger.info " See https://github.com/marklogic/roxy/issues/416 for details."
24742518
end
24752519

2476-
@config ||= build_config(@options[:config_file])
2520+
@config ||= build_config(@options[:config_file], preserve_props)
24772521
end
24782522

24792523
def execute_query_4(query, properties)
@@ -2977,7 +3021,7 @@ def ssl_certificate_xml
29773021
}
29783022
end
29793023

2980-
def build_config(config_paths)
3024+
def build_config(config_paths, preserve_props = false)
29813025
config_files = []
29823026
config_paths.split(",").each do |config_path|
29833027
if File.directory?(config_path)
@@ -3135,7 +3179,9 @@ def build_config(config_paths)
31353179
config.gsub!("@ml.ssl-certificate-xml", "")
31363180
end
31373181

3138-
replace_properties(config, File.basename(config_file), true)
3182+
if ! preserve_props
3183+
replace_properties(config, File.basename(config_file), true)
3184+
end
31393185

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

deploy/lib/xquery/setup.xqy

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

661661
let $default-group := ($import-configs/self::*:configuration/@default-group, "Default")[1]
662662
for $group in fn:distinct-values((
663-
$import-configs/descendant-or-self::gr:group/gr:group-name,
663+
$import-configs/descendant-or-self::gr:group/gr:group-name/fn:string(),
664664
$import-configs/descendant-or-self::*/(
665665
self::gr:http-server, self::gr:xdbc-server,
666666
self::gr:odbc-server, self::gr:task-server, self::db:database
@@ -683,7 +683,7 @@ declare function setup:rewrite-config($import-configs as node()+, $properties as
683683
let $databases := $import-configs/descendant-or-self::db:database[
684684
@group = $group or ( $group = $default-group and fn:empty(@group) )
685685
]
686-
let $group-config := $import-configs/descendant-or-self::gr:group[gr:group-name = $group]
686+
let $group-config := $import-configs/descendant-or-self::gr:group[gr:group-name/fn:string() = $group]
687687
where fn:exists($servers | $databases | $group-config)
688688
return
689689
<group>
@@ -732,7 +732,7 @@ declare function setup:rewrite-config($import-configs as node()+, $properties as
732732
let $_ :=
733733
if ($silent) then ()
734734
else
735-
for $group in $config/gr:groups/gr:group/gr:group-name
735+
for $group in $config/gr:groups/gr:group/gr:group-name/fn:string()
736736
let $hosts := ($config/ho:hosts/ho:host[ho:group/@name = $group], try { xdmp:group-hosts(xdmp:group($group)) } catch ($ignore) {})
737737
where fn:empty($hosts)
738738
return
@@ -745,6 +745,43 @@ declare function setup:rewrite-config($import-configs as node()+, $properties as
745745
return if ($keep-comments) then $config else setup:suppress-comments($config)
746746
};
747747

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

749786
(:
750787
base-name : Original forest base name - this should be the name from the config.

0 commit comments

Comments
 (0)