Просмотр исходного кода

rename couchdb before restore if exists

https://codeberg.org/flohmarkt/flohmarkt_ynh/issues/62
Chris Vogel 1 год назад
Родитель
Сommit
38f8e48f93
2 измененных файлов с 46 добавлено и 2 удалено
  1. 36 2
      scripts/_common.sh
  2. 10 0
      scripts/restore

+ 36 - 2
scripts/_common.sh

@@ -168,7 +168,7 @@ flohmarkt_ynh_couchdb_user_permissions() {
   ynh_local_curl -n -m PUT -u admin -p "$password_couchdb_admin" \
     -H "Accept: application/json" -H "Content-Type: application/json" \
     -d "{\"members\":{\"names\": [\"${app}\"],\"roles\": [\"editor\"]}}" \
-    --line_match='ok' \
+    --line_match='"ok":true' \
     "http://127.0.0.1:5984/${app}/_security"
 }
 
@@ -183,8 +183,42 @@ flohmarkt_ynh_exists_couchdb_db() {
 }
 
 flohmarkt_ynh_delete_couchdb_db() {
+  local legacy_args='n'
+  local -A args_array=( [n]=name= )
+  ynh_handle_getopts_args "$@"
+  local name=${name:-${app}}
+
   ynh_local_curl -n -m DELETE -u admin -p "$password_couchdb_admin" \
-    --line_match='"ok":true' "http://127.0.0.1:5984/${app}"
+    --line_match='"ok":true' "http://127.0.0.1:5984/${name}"
+}
+
+# replicate couchdb to a new database
+flohmarkt_ynh_copy_couchdb() {
+  local legacy_args='on'
+  local -A args_array=( [o]=old_name= [n]=new_name= )
+  local new_name
+  local old_name
+  ynh_handle_getopts_args "$@"
+  old_name=${old_name:-${app}}
+
+  ynh_local_curl -n -m POST -u admin -p "$password_couchdb_admin" \
+    -H "Accept: application/json" -H "Content-Type: application/json" \
+    -d '{ "create_target":true,"source" : "' -d"${old_name}" -d'",' \
+    -d '"target":"' -d "${new_name}" -d'"}' --seperator=none \
+    --line_match='"ok":true' "http://127.0.0.1:5984/_replicate"
+}
+
+# copy couchdb to new name and delete source
+flohmarkt_ynh_rename_couchdb() {
+  local legacy_args='on'
+  local -A args_array=( [o]=old_name= [n]=new_name= )
+  local new_name
+  local old_name
+  ynh_handle_getopts_args "$@"
+  old_name=${old_name:-${app}}
+
+  flohmarkt_ynh_copy_couchdb -o "$old_name" -n "$new_name"
+  flohmarkt_ynh_delete_couchdb_db "$old_name"
 }
 
 # check whether old couchdb user or database exist before creating the new ones

+ 10 - 0
scripts/restore

@@ -23,6 +23,16 @@ else
   ynh_script_progression --message="CouchDB configuration file already exists." --weight=1
 fi
 
+# check if couchdb already exists and if so rename it to make room for the restore
+if flohmarkt_ynh_exists_couchdb_db; then
+  flohmarkt_couchdb_rename_to="${app}_$(date '+%Y-%m-%d_%H-%M-%S_%N')"
+  if flohmarkt_ynh_rename_couchdb "${app}" "${flohmarkt_couchdb_rename_to}"; then
+    ynh_print_warn --message="renamed existing database ${app} to ${flohmarkt_couchdb_rename_to}"
+  else
+    ynh_die --message="could not rename existing couchdb database and cannot overwrite it"
+  fi
+fi
+
 # restore couchdb from json
 ynh_script_progression --message="Importing couchdb from json backup..." --weight=8
 flohmarkt_ynh_restore_couchdb