osal.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/bin/sh
  2. # Identifies OS type and variant
  3. # Setups variables and provides OS-agnostic wrapper functions
  4. OS_TYPE=$(grep "^ID=" /etc/os-release | cut -f 2 -d '=' | sed -e 's/^"//' -e 's/"$//' | sed -e 's/\(.*\)/\L\1/')
  5. case "$OS_TYPE" in
  6. debian)
  7. OS_BASE='debian'
  8. OS_VERSION=$(cat /etc/debian_version|grep -o "[0-9]\{1,2\}"|head -n1)
  9. OS_CODENAME="$(cat /etc/os-release |grep VERSION= |cut -f 2 -d \(|cut -f 1 -d \))"
  10. ;;
  11. ubuntu)
  12. OS_BASE='debian'
  13. OS_VERSION="$(lsb_release -s -r)".
  14. OS_CODENAME="$(lsb_release -s -c)"
  15. ;;
  16. centos|rhel|fedora|redhat)
  17. OS_BASE='rhel'
  18. OS_VERSION=$(cat /etc/os-release | grep VERSION_ID | sed -e "s/VERSION_ID=//" | sed -e 's/^"//' -e 's/"$//')
  19. OS_CODENAME=''
  20. ;;
  21. *)
  22. OS_BASE='unknown'
  23. ;;
  24. esac
  25. OSAL_PATH="$(cd "$(dirname "$BASH_SOURCE")" >/dev/null 2>&1 ; pwd -P)"
  26. for OSAL_FILE in "osal_${OS_BASE}_based" \
  27. "osal_${OS_TYPE}" \
  28. "osal_${OS_TYPE}_${OS_VERSION}"
  29. do
  30. # Search for OS specific OSAL file and source it
  31. if [ -f "$OSAL_PATH/${OSAL_FILE}.sh" ]; then
  32. source "$OSAL_PATH/${OSAL_FILE}.sh"
  33. fi
  34. done
  35. # service_start 'service-name'
  36. osal_service_start() {
  37. [ "$HESTIA_DEBUG" ] && >&2 echo Start service $1
  38. /usr/bin/systemctl start ${1}.service
  39. }
  40. # service_stop 'service-name'
  41. osal_service_stop() {
  42. [ "$HESTIA_DEBUG" ] && >&2 echo Stop service $1
  43. /usr/bin/systemctl stop ${1}.service
  44. }
  45. # service_restart 'service-name'
  46. osal_service_restart() {
  47. [ "$HESTIA_DEBUG" ] && >&2 echo Restart service $1
  48. /usr/bin/systemctl restart ${1}.service
  49. }
  50. # service_enable 'service-name'
  51. osal_service_enable() {
  52. [ "$HESTIA_DEBUG" ] && >&2 echo Enable service $1
  53. /usr/bin/systemctl enable ${1}.service
  54. }
  55. # service_disable 'service-name'
  56. osal_service_disable() {
  57. [ "$HESTIA_DEBUG" ] && >&2 echo Disable service $1
  58. /usr/bin/systemctl disable ${1}.service
  59. }
  60. # VAR=$(ini_get 'file' 'section' 'param' 'value')
  61. osal_ini_get() {
  62. #echo /usr/bin/crudini --get $@
  63. local retval=$(/usr/bin/crudini --get $@ 2>1)
  64. if [ $? -eq 0 ]; then
  65. echo $retval
  66. fi
  67. }
  68. # ini_set 'file' 'section' 'param' 'newvalue'
  69. osal_ini_set() {
  70. if [ "$OSAL_DEBUG" ]; then
  71. echo /usr/bin/crudini --set $@
  72. fi
  73. /usr/bin/crudini --set $@
  74. }
  75. # For use in osal_kv_*
  76. sed_escape() {
  77. sed -e 's/[]\/$*.^[]/\\&/g'
  78. }
  79. # Writes a value to a key-value file.
  80. # osal_kv_write 'path' 'key' 'value'
  81. osal_kv_write() {
  82. osal_kv_delete "$1" "$2"
  83. echo "$2=$3" >> "$1"
  84. }
  85. # Reads a value from a key-value file. # Exits successfully if it does.
  86. # value=$(osal_kv_read path key defaultvalue)
  87. osal_kv_read() {
  88. local kv_keyname=$(echo "$2" | sed_escape)
  89. if [ -f "$1" ]; then
  90. local retval=$(grep "^$kv_keyname\s*=" "$1" | sed "s/^$kv_keyname\s*=\s*//" | tail -1 | sed "s/^\([\"']\)\(.*\)\1\$/\2/g")
  91. if [ "$retval" ]; then
  92. echo $retval
  93. else
  94. echo $3
  95. fi
  96. else
  97. echo $3
  98. fi
  99. }
  100. # Deletes a value in a key-value file.
  101. # osal_kv_delete 'filename' 'key'
  102. osal_kv_delete() {
  103. local kv_keyname=$(echo "$2" | sed_escape)
  104. test -f "$1" && sed -i "/^${kv_keyname}\s*=.*$/d" "$1"
  105. }
  106. # Tests if a value exists in a key-value file.
  107. # Exits successfully if it does.
  108. # osal_kv_haskey 'filename' 'key'
  109. osal_kv_haskey() {
  110. local kv_keyname=$(echo "$2" | sed_escape)
  111. test -f "$1" && grep "^${kv_keyname}\s*=" "$1" > /dev/null
  112. if [ $? -eq 0 ]; then
  113. return 0
  114. else
  115. return 1
  116. fi
  117. }
  118. # Tests if a boolean value is true in a key-value file.
  119. # Exits successfully if it does.
  120. # osal_kv_read_bool 'filename' 'keyname'
  121. osal_kv_read_bool() {
  122. local retval=$(osal_kv_read $@)
  123. if [ "${retval,,}" == "yes" ] \
  124. || [ "${retval,,}" == "true" ] \
  125. || [ "${retval,,}" == "on" ] \
  126. || [ "$retval" == "1" ]; then
  127. return 0
  128. else
  129. return 1
  130. fi
  131. }
  132. # Converts a boolean value to 'yes'/'no' (or two terms provided as second and third argument)
  133. # answer=$(osal_bool_tostring boolean_value yes_value no_value)
  134. osal_bool_tostring() {
  135. if [ "${1,,}" == "yes" ] \
  136. || [ "${1,,}" == "true" ] \
  137. || [ "${1,,}" == "on" ] \
  138. || [ "$1" == "1" ]; then
  139. if [ -n "$2" ]; then echo "$2"; else echo 'yes'; fi
  140. else
  141. if [ -n "$3" ]; then echo "$3"; else echo 'no'; fi
  142. fi
  143. }
  144. # Executes a process silently in the background while showing a spinner
  145. osal_execute_with_spinner() {
  146. if [ "$OSAL_DEBUG" ]; then
  147. echo "$@"
  148. $@
  149. else
  150. $@ > /dev/null 2>&1 &
  151. local BACK_PID=$!
  152. local spinner="/-\|"
  153. local spin_i=1
  154. while kill -0 $BACK_PID > /dev/null 2>&1 ; do
  155. printf "\b${spinner:spin_i++%${#spinner}:1}"
  156. sleep 0.5
  157. done
  158. # Do a blank echo to get the \n back
  159. echo
  160. fi
  161. }
  162. # Generates a random password
  163. osal_gen_pass() {
  164. local MATRIX='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  165. local LENGTH=16
  166. while [ ${n:=1} -le $LENGTH ]; do
  167. PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}"
  168. let n+=1
  169. done
  170. echo "$PASS"
  171. }