osal.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 > /dev/null 2>&1
  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 > /dev/null 2>&1
  59. }
  60. osal_value_in_list() {
  61. local needle=$1
  62. shift
  63. if [[ -z "$@" ]]; then
  64. # Empty list. Return not found.
  65. return 1;
  66. fi
  67. [[ $@ =~ (^|[[:space:]])$needle($|[[:space:]]) ]] && return 0
  68. return 1
  69. }
  70. # VAR=$(ini_get 'file' 'section' 'param' 'value')
  71. osal_ini_get() {
  72. #echo /usr/bin/crudini --get $@
  73. local retval=$(/usr/bin/crudini --get $@ 2>1)
  74. if [ $? -eq 0 ]; then
  75. echo $retval
  76. fi
  77. }
  78. # ini_set 'file' 'section' 'param' 'newvalue'
  79. osal_ini_set() {
  80. if [ "$OSAL_DEBUG" ]; then
  81. echo /usr/bin/crudini --set $@
  82. fi
  83. /usr/bin/crudini --set $@
  84. }
  85. # For use in osal_kv_*
  86. sed_escape() {
  87. sed -e 's/[]\/$*.^[]/\\&/g'
  88. }
  89. # Writes a value to a key-value file.
  90. # osal_kv_write 'path' 'key' 'value'
  91. osal_kv_write() {
  92. osal_kv_delete "$1" "$2"
  93. echo "$2='$3'" >> "$1"
  94. if [ "$1" == "$HESTIA/conf/hestia.conf" ]; then
  95. # Writing config value. Take effect immediately.
  96. declare -x $2="$3"
  97. fi
  98. }
  99. # Reads a value from a key-value file. # Exits successfully if it does.
  100. # value=$(osal_kv_read path key defaultvalue)
  101. osal_kv_read() {
  102. local kv_keyname=$(echo "$2" | sed_escape)
  103. if [ -f "$1" ]; then
  104. local retval=$(grep "^$kv_keyname\s*=" "$1" | sed "s/^$kv_keyname\s*=\s*//" | tail -1 | sed "s/^\([\"']\)\(.*\)\1\$/\2/g")
  105. if [ "$retval" ]; then
  106. echo $retval
  107. else
  108. echo $3
  109. fi
  110. else
  111. echo $3
  112. fi
  113. }
  114. # Deletes a value in a key-value file.
  115. # osal_kv_delete 'filename' 'key'
  116. osal_kv_delete() {
  117. local kv_keyname=$(echo "$2" | sed_escape)
  118. test -f "$1" && sed -i "/^${kv_keyname}\s*=.*$/d" "$1"
  119. if [ "$1" == "$HESTIA/conf/hestia.conf" ]; then
  120. # Deleting config value. Take effect immediately.
  121. declare $2=''
  122. fi
  123. }
  124. # Tests if a value exists in a key-value file.
  125. # Exits successfully if it does.
  126. # osal_kv_haskey 'filename' 'key'
  127. osal_kv_haskey() {
  128. local kv_keyname=$(echo "$2" | sed_escape)
  129. test -f "$1" && grep "^${kv_keyname}\s*=" "$1" > /dev/null
  130. if [ $? -eq 0 ]; then
  131. return 0
  132. else
  133. return 1
  134. fi
  135. }
  136. # Tests if a boolean value is true in a key-value file.
  137. # Exits successfully if it does.
  138. # osal_kv_read_bool 'filename' 'keyname'
  139. osal_kv_read_bool() {
  140. local retval=$(osal_kv_read $@)
  141. if [ "${retval,,}" == "yes" ] \
  142. || [ "${retval,,}" == "true" ] \
  143. || [ "${retval,,}" == "on" ] \
  144. || [ "$retval" == "1" ]; then
  145. return 0
  146. else
  147. return 1
  148. fi
  149. }
  150. # Converts a boolean value to 'yes'/'no' (or two terms provided as second and third argument)
  151. # answer=$(osal_bool_tostring boolean_value yes_value no_value)
  152. osal_bool_tostring() {
  153. if [ "${1,,}" == "yes" ] \
  154. || [ "${1,,}" == "true" ] \
  155. || [ "${1,,}" == "on" ] \
  156. || [ "$1" == "1" ]; then
  157. if [ -n "$2" ]; then echo "$2"; else echo 'yes'; fi
  158. else
  159. if [ -n "$3" ]; then echo "$3"; else echo 'no'; fi
  160. fi
  161. }
  162. # Executes a process silently in the background while showing a spinner
  163. osal_execute_with_spinner() {
  164. if [ "$OSAL_DEBUG" ]; then
  165. echo "$@"
  166. $@
  167. else
  168. $@ > /dev/null 2>&1 &
  169. local BACK_PID=$!
  170. local spinner="/-\|"
  171. local spin_i=1
  172. while kill -0 $BACK_PID > /dev/null 2>&1 ; do
  173. printf "\b${spinner:spin_i++%${#spinner}:1}"
  174. sleep 0.5
  175. done
  176. # Do a blank echo to get the \n back
  177. echo
  178. fi
  179. }
  180. # Generates a random password
  181. osal_gen_pass() {
  182. local MATRIX='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  183. local LENGTH=16
  184. while [ ${n:=1} -le $LENGTH ]; do
  185. PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}"
  186. let n+=1
  187. done
  188. echo "$PASS"
  189. }