From aa0e94041bd2fab025d7901e0da7282e49d3b349 Mon Sep 17 00:00:00 2001 From: Geert Josten Date: Mon, 6 Jul 2015 20:21:46 +0200 Subject: [PATCH] Fixed #298 and #452: made capturing modules more robust --- deploy/lib/server_config.rb | 92 ++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 41 deletions(-) diff --git a/deploy/lib/server_config.rb b/deploy/lib/server_config.rb index 68eac185..b6a174cd 100644 --- a/deploy/lib/server_config.rb +++ b/deploy/lib/server_config.rb @@ -1237,26 +1237,27 @@ def capture # send the target db, and the destination directory save_files_to_fs(target_db, "#{tmp_dir}/src") - # check if this is a REST project to capture REST configuration - if (port != nil) - - # make sure that REST options directory exists - if Dir.exists? @properties['ml.rest-options.dir'] - - # set up the options - FileUtils.cp_r( - "#{tmp_dir}/src/#{@properties['ml.group']}/" + target_db.sub("-modules", "") + "/rest-api/.", - @properties['ml.rest-options.dir'] - ) - FileUtils.rm_rf("#{tmp_dir}/src/#{@properties['ml.group']}/") - - # Make sure REST properties are in accurate format, so you can directly deploy them again.. - r = go("http://#{@hostname}:#{port}/v1/config/properties", "get") - r.body = parse_json(r.body) - File.open("#{@properties['ml.rest-options.dir']}/properties.xml", 'wb') { |file| file.write(r.body) } - else - raise HelpException.new("capture", "attempting --app-builder REST capture into non-REST project, you may try capture with --modules-db to only capture modules without the REST configuration") - end + # check if this is a REST project to capture REST configuration + if (port != nil) + + # make sure that REST options directory exists + if Dir.exists? @properties['ml.rest-options.dir'] + + # set up the options + FileUtils.cp_r( + "#{tmp_dir}/src/#{@properties['ml.group']}/" + target_db.sub("-modules", "") + "/rest-api/.", + @properties['ml.rest-options.dir'] + ) + FileUtils.rm_rf("#{tmp_dir}/src/#{@properties['ml.group']}/") + + # Make sure REST properties are in accurate format, so you can directly deploy them again.. + r = go("http://#{@hostname}:#{port}/v1/config/properties", "get") + r.body = parse_json(r.body) + File.open("#{@properties['ml.rest-options.dir']}/properties.xml", 'wb') { |file| file.write(r.body) } + + else + raise HelpException.new("capture", "attempting --app-builder REST capture into non-REST project, you may try capture with --modules-db to only capture modules without the REST configuration") + end end # If we have an application/custom directory, we've probably done a capture @@ -1298,9 +1299,18 @@ def save_files_to_fs(target_db, target_dir) dirs = execute_query %Q{ xquery version "1.0-ml"; - for $uri in cts:uris() - order by $uri - return $uri + try { + for $uri in cts:uris() + order by $uri + return $uri + + } catch ($ignore) { + (: In case URI lexicon has not been enabled :) + for $doc in collection() + let $uri := xdmp:node-uri($doc) + order by $uri + return $uri + } }, { :db_name => target_db } @@ -1312,16 +1322,16 @@ def save_files_to_fs(target_db, target_dir) if ['5', '6'].include? @properties['ml.server-version'] # In ML5 and ML6, the response was a bunch of text. Split on the newlines. dirs.body.split(/\r?\n/).each do |uri| - if (uri.end_with?("/")) - # create the directory so that it will exist when we try to save files - Dir.mkdir("#{target_dir}" + uri) - else - r = execute_query %Q{ - fn:doc("#{uri}") - }, - { :db_name => target_db } - - File.open("#{target_dir}#{uri}", 'wb') { |file| file.write(r.body) } + r = execute_query %Q{ + fn:doc("#{uri}") + }, + { :db_name => target_db } + + path = "#{target_dir}#{uri}" + parentdir = File.dirname path + FileUtils.mkdir_p(parentdir) unless File.exists?(parentdir) + if ! uri.end_with?("/") + File.open("#{path}", 'wb') { |file| file.write(r.body) } end end else @@ -1334,14 +1344,14 @@ def save_files_to_fs(target_db, target_dir) JSON.parse(dirs.body).each do |item| uri = item['result'] - if (uri.end_with?("/")) - # create the directory so that it will exist when we try to save files - Dir.mkdir("#{target_dir}" + uri) - else - r = go("#{@protocol}://#{@hostname}:#{@bootstrap_port}/qconsole/endpoints/view.xqy?dbid=#{db_id}&uri=#{uri}", "get") - file_content = r.body - File.open("#{target_dir}#{uri}", 'wb') { |file| file.write(file_content) } - end + r = go("#{@protocol}://#{@hostname}:#{@bootstrap_port}/qconsole/endpoints/view.xqy?dbid=#{db_id}&uri=#{URI.escape(uri).gsub(/\$/, '%24')}", "get") + + path = "#{target_dir}#{uri}" + parentdir = File.dirname path + FileUtils.mkdir_p(parentdir) unless File.exists?(parentdir) + if ! uri.end_with?("/") + File.open("#{path}", 'wb') { |file| file.write(r.body) } + end end end end