osal.sh 5.8 KB

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