Browse Source

Info, list, fixes

Ernesto Nicolás Carrea 5 years ago
parent
commit
4a22e5ec56
8 changed files with 128 additions and 45 deletions
  1. 19 14
      bin/hestia
  2. 9 6
      bin/module/func.inc
  3. 4 3
      bin/module/info.inc
  4. 0 11
      bin/module/list
  5. 23 0
      bin/module/list.inc
  6. 7 6
      bin/module/vsftpd/install.inc
  7. 6 5
      bin/module/vsftpd/remove.inc
  8. 60 0
      func/osal.sh

+ 19 - 14
bin/hestia

@@ -58,11 +58,12 @@ do
         # It's a --parameter
         if [ "$param_name" ] && [ ! "${!param_name}" ]; then
             # Previous --arg is empty, so set it to true before continuing
-            declare $param_name=true
+            declare $param_name=1
         fi
         param_name="param_${param:2}"     # trim --
         # Add param_name to the list of used params (unless it's there already)
         [[ $params =~ (^|[[:space:]])$param_name($|[[:space:]]) ]] || params="$params $param_name"
+        last_was='name'
     else
         # Not a --parameter, so it's a value
         if [ "$param_name" ]; then
@@ -74,26 +75,30 @@ do
                 declare $param_name="$param"
             fi
         fi
+        last_was='value'
     fi
 done
+# Process trailing --boolean_param
+if [ "$last_was" == 'name' ]; then
+    declare $param_name=1
+fi
 
 # Trim leading space
 [ "$params" ] && params="${params:1}"
 
 if [ $cmd_file ]; then
-    echo "Command file          : $cmd_file"
-    echo "Command type          : $cmd_type"
-    echo "Command function name : hestia_${cmd_name}()"
-    echo "Remaining arguments   : $@"
-    echo "Parameters used       : $params"
-    echo "Parameter values      : "
-    for param in $params
-    do
-        echo "    --$param ${!param}"
-    done
-    echo ""
-    echo "*** Now the command itself:"
-    echo ""
+    #echo "Command file          : $cmd_file"
+    #echo "Command type          : $cmd_type"
+    #echo "Command function name : hestia_${cmd_name}()"
+    #echo "Remaining arguments   : $@"
+    #echo "Parameters used       : $params"
+    #echo "Parameter values      : "
+    #for param in $params; do
+    #    echo "    --$param ${!param}"
+    #done
+    #echo ""
+    #echo "*** Now the command itself:"
+    #echo ""
 
     if [ "$cmd_type" = 'include' ]; then
         source $cmd_file

+ 9 - 6
bin/module/func.inc

@@ -1,25 +1,28 @@
 #!/bin/sh
 
+HESTIA_CONF_MODULES=$HESTIA/conf/modules
+mkdir -p $HESTIA_CONF_MODULES
+
 # Returns 1 if module is installed
 hestia_module_isinstalled() {
-    osal_ini_get $HESTIA/conf/modules.conf $1 'installed' '1'
+    osal_kv_read_bool $HESTIA_CONF_MODULES/${1}.conf 'installed'
 }
 
 # Returns 1 if module is installed and enabled
 hestia_module_isenabled() {
-    is_installed=$(osal_ini_get $HESTIA/conf/modules.conf $1 'installed' '1')
-    is_enabled=$(osal_ini_get $HESTIA/conf/modules.conf $1 'enabled' '1')
-    if [[ "$is_installed" && "$is_enabled" ]]; then
+    is_installed=$(osal_kv_read_bool $HESTIA_CONF_MODULES/${1}.conf 'installed')
+    is_enabled=$(osal_kv_read_bool $HESTIA_CONF_MODULES/${1}.conf 'enabled')
+    if [ "$is_installed" ] && [ "$is_enabled" ]; then
         echo 1
     fi
 }
 
 hestia_module_getversion() {
-    osal_ini_get $HESTIA/conf/modules.conf $1 'version'
+    osal_kv_read $HESTIA_CONF_MODULES/${1}.conf 'version'
 }
 
 hestia_module_getvariant() {
-    osal_ini_get $HESTIA/conf/modules.conf $1 'variant'
+    osal_kv_read $HESTIA_CONF_MODULES/${1}.conf 'variant'
 }
 
 # Backup config files (usually prior to module install)

+ 4 - 3
bin/module/info.inc

@@ -12,12 +12,13 @@ hestia_module_info() {
         module_variant=$(hestia_module_getvariant $modulename)
         module_version=$(hestia_module_getversion $modulename)
 
-        echo "Installed       : Yes"
-        echo "Enabled         : $module_enabled"
+        echo "Installed       : yes"
+        echo "Description     : $(osal_kv_read $HESTIA_CONF_MODULES/${1}.conf 'description')"
+        echo "Enabled         : $(osal_bool_yes $module_enabled)"
         echo "Variant         : $module_variant"
         echo "Version         : $module_version"
     else
-        echo "Installed       : No"
+        echo "Installed       : no"
     fi
 }
 

+ 0 - 11
bin/module/list

@@ -1,11 +0,0 @@
-#!/bin/sh
-
-hestia_module_list() {
-    echo "Hestia web domain list $@"
-    echo "Demo of 3-level commands"
-    echo "This works as both Hestia CLI command and regular executable"
-}
-
-# If this files is _not_ being sourced, act immediately
-# (otherise, wait for hestia cli to call the main function)
-[[ $_ == $0 ]] && hestia_web_domain_list $@

+ 23 - 0
bin/module/list.inc

@@ -0,0 +1,23 @@
+#!/bin/sh
+
+hestia_module_list() {
+    source $HESTIA/bin/module/func.inc
+
+    for conf_file in $HESTIA_CONF_MODULES/*.conf; do
+        mod_name=$(basename $conf_file .conf)
+        mod_descr=$(osal_kv_read ${conf_file} 'description')
+
+        module_installed=$(hestia_module_isinstalled $mod_name)
+        module_enabled=$(hestia_module_isenabled $mod_name)
+
+        if [ "$module_installed" ]; then
+            if [ "$module_enabled" ] || [ "$param_all" ]; then
+                echo "$mod_name: $mod_descr"
+            fi
+        fi
+    done
+}
+
+# If this files is _not_ being sourced, act immediately
+# (otherise, wait for hestia cli to call the main function)
+[[ $_ == $0 ]] && hestia_module_list $@

+ 7 - 6
bin/module/vsftpd/install.inc

@@ -29,12 +29,13 @@ hestia_module_vsftpd_install() {
 
     check_result $? "vsftpd start failed"
 
-    osal_ini_set $HESTIA/conf/hestia.conf '' 'FTP_SYSTEM' 'vsftpd'
-    osal_ini_set $HESTIA/conf/modules.conf 'ftp' 'installed' '1'
-    osal_ini_set $HESTIA/conf/modules.conf 'ftp' 'enabled' '1'
-    osal_ini_set $HESTIA/conf/modules.conf 'ftp' 'variant' 'vsftpd'
-    osal_ini_set $HESTIA/conf/modules.conf 'ftp' 'version' '1'
-    osal_ini_set $HESTIA/conf/modules.conf 'ftp' 'service_name' 'vsftpd'
+    osal_kv_write $HESTIA/conf/hestia.conf 'FTP_SYSTEM' 'vsftpd'
+    osal_kv_write $HESTIA_CONF_MODULES/ftp.conf 'installed' '1'
+    osal_kv_write $HESTIA_CONF_MODULES/ftp.conf 'description' 'Hestia FTP (Vsftpd) module'
+    osal_kv_write $HESTIA_CONF_MODULES/ftp.conf 'enabled' '1'
+    osal_kv_write $HESTIA_CONF_MODULES/ftp.conf 'variant' 'vsftpd'
+    osal_kv_write $HESTIA_CONF_MODULES/ftp.conf 'version' '1'
+    osal_kv_write $HESTIA_CONF_MODULES/ftp.conf 'service_name' 'vsftpd'
 
     exit 0
 }

+ 6 - 5
bin/module/vsftpd/remove.inc

@@ -23,11 +23,12 @@ hestia_module_vsftpd_remove() {
 
     rm -f $OSAL_PATH_VSFTPD_CONF/vsftpd.conf
 
-    osal_ini_set $HESTIA/conf/hestia.conf '' 'FTP_SYSTEM' 'no'
-    osal_ini_set $HESTIA/conf/modules.conf 'ftp' 'installed' '0'
-    osal_ini_set $HESTIA/conf/modules.conf 'ftp' 'enabled' '0'
-    osal_ini_set $HESTIA/conf/modules.conf 'ftp' 'variant' ''
-    osal_ini_set $HESTIA/conf/modules.conf 'ftp' 'version' '0'
+    osal_kv_write $HESTIA/conf/hestia.conf 'FTP_SYSTEM' 'no'
+    osal_kv_write $HESTIA_CONF_MODULES/ftp.conf 'installed' '0'
+    osal_kv_write $HESTIA_CONF_MODULES/ftp.conf 'description' ''
+    osal_kv_write $HESTIA_CONF_MODULES/ftp.conf 'enabled' '0'
+    osal_kv_write $HESTIA_CONF_MODULES/ftp.conf 'variant' ''
+    osal_kv_write $HESTIA_CONF_MODULES/ftp.conf 'version' '0'
 
     exit 0
 }

+ 60 - 0
func/osal.sh

@@ -51,6 +51,66 @@ osal_ini_set() {
     /usr/bin/crudini --set $@
 }
 
+# For use in osal_kv_*
+sed_escape() {
+    sed -e 's/[]\/$*.^[]/\\&/g'
+}
+
+# osal_kv_write path key value
+osal_kv_write() { 
+    osal_kv_delete "$1" "$2"
+    echo "$2=$3" >> "$1"
+}
+
+# value=$(osal_kv_read path key defaultvalue)
+osal_kv_read() {
+    kv_keyname=$(echo "$2" | sed_escape)
+    if [ -f "$1" ]; then
+        retval=$(grep "^$kv_keyname\s*=" "$1" | sed "s/^$kv_keyname\s*=\s*//" | tail -1)
+        if [ "$retval" ]; then
+            echo $retval
+        else
+            echo $3
+        fi
+    else
+        echo $3
+    fi
+}
+
+osal_kv_delete() { # path, key
+    kv_keyname=$(echo "$2" | sed_escape)
+    test -f "$1" && sed -i "/^${kv_keyname}\s*=.*$/d" "$1"
+}
+
+osal_kv_haskey() { # path, key
+    kv_keyname=$(echo "$2" | sed_escape)
+    test -f "$1" && grep "^${kv_keyname}\s*=" "$1" > /dev/null
+    if [ $? -eq 0 ]; then
+        echo 1
+    fi
+}
+
+osal_kv_read_bool() {
+    retval=$(osal_kv_read $@)
+    if [ "${retval,,}" == "yes" ] \
+        || [ "${retval,,}" == "true" ] \
+        || [ "${retval,,}" == "on" ] \
+        || [ "$retval" == "1" ]; then
+        echo 1
+    fi
+}
+
+osal_bool_yes() {
+    if [ "${1,,}" == "yes" ] \
+        || [ "${1,,}" == "true" ] \
+        || [ "${1,,}" == "on" ] \
+        || [ "$1" == "1" ]; then
+        echo 'yes'
+    else
+        echo 'no'
+    fi
+}
+
 osal_execute_with_spinner() {
     if [ "$OSAL_DEBUG" ]; then
         echo "$@"