Browse Source

Implemented "rotate" mode

The aim of this operational mode is the rotation of the shifts file.
(e.g. when new 150s are employed)
The old shifts file is backed up and compressed. A new shifts file is
initialised with correct ownership and mode.
Only root can operate in this mode.
Matteo Savatteri 5 years ago
parent
commit
9c434a8437
5 changed files with 93 additions and 15 deletions
  1. 3 0
      functions
  2. 7 6
      helper_functions
  3. 7 4
      record_functions
  4. 58 0
      rotate_functions
  5. 18 5
      shiftctl

+ 3 - 0
functions

@@ -8,3 +8,6 @@
 
 # Includi funzioni modalità query
 . "$SHIFTCTL_DIR/query_functions"
+
+# Includi funzioni modalità rotate
+. "$SHIFTCTL_DIR/rotate_functions"

+ 7 - 6
helper_functions

@@ -29,7 +29,8 @@ print_usage(){
   echo "options:"
   echo "  -h  Print this help message"
   echo "  -v  Print current version"
-  echo "  -t  Print the grand total of shift durations"
+  echo "  -t  Print the grand total of shifts durations"
+  echo "  -r  Rotate shifts file"
 
   return 0
 }
@@ -37,11 +38,11 @@ print_usage(){
 # Inizializza il file dei turni
 initialize_dbfile(){
   echo "date;operator;host;comment" > $SHIFT_FILE 2>/dev/null ||\
-  echoerr "Failed to create shift file $SHIFT_FILE."
+  echoerr "Failed to create shifts file $SHIFT_FILE."
   chgrp "$OPGROUP" "$SHIFT_FILE" &>/dev/null ||\
-  echoerr "Failed to change group ownership of shift file $SHIFT_FILE to $OPGROUP."
-  chmod g+w $SHIFT_FILE &>/dev/null ||\
-  echoerr "Failed to change mode of shift file $SHIFT_FILE."
+  echoerr "Failed to change group ownership of shifts file $SHIFT_FILE to $OPGROUP."
+  chmod g+w "$SHIFT_FILE" &>/dev/null ||\
+  echoerr "Failed to change mode of shifts file $SHIFT_FILE."
 
   return 0
 }
@@ -95,7 +96,7 @@ check_dbfile(){
 smartinit_dbfile(){
   check_dbfile exist || initialize_dbfile
   check_dbfile mode ||\
-  echoerr "Shift file "$SHIFT_FILE" is not readable and/or writable by you."
+  echoerr "Shifts file "$SHIFT_FILE" is not readable and/or writable by you."
 
   return 0
 }

+ 7 - 4
record_functions

@@ -27,8 +27,9 @@ SHIFT_DURATION=
 set_shift_duration(){
 while true
   do
-    echo "Enter the duration of your shift in hours [Use an integer. Default: 2]:"
+    echo -n "Enter the duration of your shift in hours [Use an integer. Default: 2]: "
     read SHIFT_DURATION
+    echo
 
     # Controlla che esista tra 1 e 4, se non esiste setta il default e interrompi
     case "$SHIFT_DURATION" in
@@ -77,15 +78,17 @@ confirm_entry(){
 
   while true
   do
-    echo "Confirm entry? [y/n]"
-    read BOOL
+    echo -n "Confirm entry? [y/n]: "
+    read -n 1 BOOL
+    echo
+
     case "$BOOL" in
       y|Y)
         ## Registra l'entrata
         echo "$SHIFT_DATE;$RUNNER;$SHIFT_DURATION;$ENC_SHIFT_COMMENT" >> $SHIFT_FILE ||\
 	echoerr "Failed to record entry."
 
-        echo -e "\n\e[0;32mEntry recorded\e[0m. Congratulation, more 150 points for you. [O_o]"
+        echo -e "\n\e[0;32mEntry recorded.\e[0m Congratulation, more 150 points for you. *_*"
         break
         ;;
       n|N)

+ 58 - 0
rotate_functions

@@ -0,0 +1,58 @@
+# Modalità "rotate"
+# Variabili e funzioni di supporto
+# a modalità rotate
+
+# Fai un backup compresso del db file
+# Assegna il nome del file di backup
+# alla variabile attesa come primo
+# argomento.
+bkcomp_dbfile(){
+  local COMPBK_NAME="${SHIFT_FILE}.bk$(date +%F_%T).gz"
+
+  gzip -c9 "$SHIFT_FILE" > "$COMPBK_NAME" 2>/dev/null ||\
+  echoerr "Failed to backup shifts file ${SHIFT_FILE}."
+
+  chgrp "$OPGROUP" "$COMPBK_NAME" &>/dev/null ||\
+  echoerr "Failed to change group ownership of backed up shifts file $COMPBK_NAME to $OPGROUP."
+  chmod g+w "$COMPBK_NAME" &>/dev/null ||\
+  echoerr "Failed to change mode of backed up shifts file $COMPBK_NAME."
+
+  eval $1="$COMPBK_NAME"
+
+  return 0
+}
+
+# Chiede conferma della rotazione.
+ask_rotate(){
+  echo -e "\n\e[0;33mWarning\e[0m: rotation of shifts file clears current shifts records."
+  echo "This operation can only be reverted manually."
+  echo "Proceed at your own risk."
+  echo -n 'Enter "YES" to proceed, anything else to abort: '
+  
+  local ANSWER 
+  read ANSWER
+  
+  [ "$ANSWER" == "YES" ] ||\
+  { echo -e "\nRotation aborted. You don't make me spin. :("; exit 0; }
+}
+
+# Ruota il file dei turni
+rotate_dbfile(){
+  # Check esistenza
+  check_dbfile exist ||\
+  echoerr "There's nothing to rotate: $SHIFT_FILE does not exist."
+  
+  # Conferma
+  ask_rotate  
+
+  # Backup
+  local SHIFT_FILE_COMPBK
+  bkcomp_dbfile SHIFT_FILE_COMPBK
+
+  # Init nuovo file turni
+  initialize_dbfile
+
+  # Notifica
+  echo -e "\n\e[0;32mRotation successful!\e[0m I feel dizzy. O.o"
+  echo "Old shifts file backed up as ${SHIFT_FILE_COMPBK}."
+}

+ 18 - 5
shiftctl

@@ -17,7 +17,7 @@ SHIFTCTL_DIR="/usr/local/src/lcm-unimi/shiftctl"
 . "$SHIFTCTL_DIR/conf"
 
 ## Set variabili globali
-VERSION="1.3"
+VERSION="2.0"
 RUNNER="$(whoami)"
 MODE="record"
 
@@ -30,7 +30,7 @@ MODE="record"
 ## Init
 
 # Parsa la linea di comando
-while getopts ":hvt" flag
+while getopts ":hvtr" flag
 do
   case "$flag" in
     h)
@@ -44,6 +44,11 @@ do
       ;;
     t)
       MODE=grand_total
+      break
+      ;;
+    r)
+      MODE=rotate
+      break
       ;;
     ?)
       print_banner
@@ -56,9 +61,13 @@ done
 print_banner
 
 # Esegui i check
-check_user
-smartinit_dbfile
-
+if [ "$MODE" == "rotate" ]
+then
+  check_root
+else
+  check_user
+  smartinit_dbfile
+fi
 
 ## Main
 # Seleziona la modalità corretta
@@ -71,6 +80,10 @@ case "$MODE" in
   grand_total)
     # Produce il gran totale delle ore
     print_gtotal
+    ;;
+  rotate)
+    # routa il file dei turni
+    rotate_dbfile
 esac
 
 # Esci con grazia