Eli Mesika has uploaded a new change for review.

Change subject: [WIP] core: adding unlock_disks.sh script
......................................................................

[WIP] core: adding unlock_disks.sh script

Adding a new script that enables to unlock all VM/Template disks or a
specific disk if it is in the LOCKED state.
Only plugged disks are considered as candidates of this command.

The new script can be invoked basically by

unlock_disks.sh -t TYPE -i ID
where
     TYPE is either "vm" or "template" or "disk"
     ID is the entity name in case of VM/Tempalte, UUID is case of
     a disk.

     The script supports all regular DB parameters as USER DATABASE
     etc. including the option to run the script remotely.

Change-Id: I3f1712fb945933d92ee9a6b97bc71d4ba3a01597
Signed-off-by: Eli Mesika <emes...@redhat.com>
---
M backend/manager/dbscripts/common_sp.sql
M backend/manager/dbscripts/dbfunctions.sh
A backend/manager/dbscripts/unlock_disks.sh
3 files changed, 111 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/88/8688/1

diff --git a/backend/manager/dbscripts/common_sp.sql 
b/backend/manager/dbscripts/common_sp.sql
index 454d887..48e1c86 100644
--- a/backend/manager/dbscripts/common_sp.sql
+++ b/backend/manager/dbscripts/common_sp.sql
@@ -400,3 +400,36 @@
     end if;
 END; $procedure$
 LANGUAGE plpgsql;
+
+-- Unlocks a specific disk
+create or replace FUNCTION fn_db_unlock_disk(v_id UUID)
+returns void
+AS $procedure$
+declare 
+    OK integer;
+    LOCKED integer;
+begin
+    OK:=1;
+    LOCKED:=2;
+    update images set imagestatus = OK where imagesstatus = LOCKED and
+    image_group_id in (select device_id from vm_device where device_id = v_id 
and is_plugged);
+END; $procedure$
+LANGUAGE plpgsql;
+
+-- Unlocks all VM/Template disks
+create or replace FUNCTION fn_db_unlock_disks(v_object_type varchar(10), 
v_name varchar(255))
+returns void
+AS $procedure$
+declare 
+    OK integer;
+    LOCKED integer;
+    v_id UUID;
+begin
+    OK:=1;
+    LOCKED:=2;
+    select v_id = vm_name from vm_static where vm_name = v_name and 
entity_type ilike v_object_type;
+    update images set imagestatus = OK where imagesstatus = LOCKED and
+    image_group_id in (select device_id from vm_device where vm_id = v_id and 
is_plugged);
+END; $procedure$
+LANGUAGE plpgsql;
+
diff --git a/backend/manager/dbscripts/dbfunctions.sh 
b/backend/manager/dbscripts/dbfunctions.sh
index 572199a..d8a35e0 100755
--- a/backend/manager/dbscripts/dbfunctions.sh
+++ b/backend/manager/dbscripts/dbfunctions.sh
@@ -379,3 +379,28 @@
    # current implementation of execute_command use --echo-all flag of psql 
that outputs the query in 1st line
    echo $(execute_command "${cmd}" ${DATABASE} ${SERVERNAME} ${PORT} | sed 
's/^ *//g' | head -2 | tail -1 | tr -d ' ')
 }
+
+#unlocks the given VM/Template disks or a given disk
+#in case of VM/Template the id is the name, in case of a disk, the id is teh 
disk UUID
+unlock_disks() {_
+   local object_type=${1}
+   local id=${2}
+
+   CMD=""
+   if [ "${object_type}"="vm" -o "${object_type}"="template" ]; then
+      CMD="perform fn_db_unlock_disks('${object_type}', '${id}');"
+   elif [ "${object_type}" = "disk" ]; then
+      CMD="perform fn_db_unlock_disk('${id}');"
+   else
+      printf "Error : $* "
+   fi
+
+   if [ "${CMD}" != "" ]; then
+       execute_command "$CMD" "${DATABASE}" "${SERVERNAME}" "${PORT}"
+       if [ $? -eq 0 ]; then
+           printf "unlock_disks completes successfully."
+       else
+           printf "unlock_disks completes with errors.."
+       fi
+   fi
+}
diff --git a/backend/manager/dbscripts/unlock_disks.sh 
b/backend/manager/dbscripts/unlock_disks.sh
new file mode 100755
index 0000000..882f36c
--- /dev/null
+++ b/backend/manager/dbscripts/unlock_disks.sh
@@ -0,0 +1,53 @@
+#!/bin/bash
+#include db general functions
+source ./dbfunctions.sh
+source ./dbcustomfunctions.sh
+
+#setting defaults
+set_defaults
+
+usage() {
+    printf "Usage: ${ME} -t TYPE -i ID [-h] [-s SERVERNAME [-p PORT]] [-d 
DATABASE] [-u USERNAME] [-l LOGFILE] [-v]\n"
+    printf "\n"
+    printf "\t-t TYPE       - The object type {vm | template | disk} \n"
+    printf "\t-i ID         - The object name in case of vm/template , UUID in 
case of a disk \n"
+    printf "\t-s SERVERNAME - The database servername for the database  (def. 
${SERVERNAME})\n"
+    printf "\t-p PORT       - The database port for the database        (def. 
${PORT})\n"
+    printf "\t-d DATABASE   - The database name                         (def. 
${DATABASE})\n"
+    printf "\t-u USERNAME   - The admin username for the database.\n"
+    printf "\t-l LOGFILE    - The logfile for capturing output          (def. 
${LOGFILE})\n"
+    printf "\t-v            - Turn on verbosity                         
(WARNING: lots of output)\n"
+    printf "\t-h            - This help text.\n"
+    printf "\n"
+
+    exit $ret
+}
+
+DEBUG () {
+    if $VERBOSE; then
+        printf "DEBUG: $*"
+    fi
+}
+
+while getopts :ht:i:s:d:u:p:l:f:v option; do
+    case $option in
+        t) TYPE=$OPTARG;;
+        i) ID=$OPTARG;;
+        s) SERVERNAME=$OPTARG;;
+        p) PORT=$OPTARG;;
+        d) DATABASE=$OPTARG;;
+        u) USERNAME=$OPTARG;;
+       l) LOGFILE=$OPTARG;;
+        v) VERBOSE=true;;
+        h) ret=0 && usage;;
+       \?) ret=1 && usage;;
+    esac
+done
+
+if [[ -n "${TYPE}" -a -n "${ID}" ]]; then
+    unlock_disks ${TYPE} ${ID}
+else
+    usage();
+fi
+
+exit $?


--
To view, visit http://gerrit.ovirt.org/8688
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3f1712fb945933d92ee9a6b97bc71d4ba3a01597
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Eli Mesika <emes...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to