Ernesto Nicolás Carrea 5 жил өмнө
parent
commit
3acf199cdf

+ 79 - 3
bin/module/install.inc

@@ -1,12 +1,88 @@
 #!/bin/bash
 
 hestia_module_install() {
-    if [ "$1" ] && [ "$1" != 'install' ] && [ "$1" != 'remove' ]; then
-        module_name=$1
+    source $HESTIA/bin/module/func.inc
+    
+    if [ "$1" ]; then
+        local mod_name=$1
         shift
-        $BIN/hestia module $module_name install "$@"
+            
+        local mod_provider=$(hestia module what-provides $mod_name)
+        if [ "$mod_provider" != "$mod_name" ]; then
+            echo "Module '${mod_provider}' selected as provider of '${mod_name}'"
+        fi
+
+        local hmd=$HESTIA/data/modules/${mod_provider}.hmd
+        [ "$HESTIA_DEBUG" ] && echo "Module definition: $hmd"
+
+        local mod_conflicts=$(osal_kv_read $hmd 'conflicts')
+        [ "$HESTIA_DEBUG" ] && echo "Conflicts: $mod_conflicts"
+        if [ "$mod_conflicts" ] && ! hestia_module_install_check_conflicts "$mod_provider" "$mod_conflicts"; then
+            return 1
+        fi
+
+        local mod_requires=$(osal_kv_read $hmd 'requires')
+        [ "$HESTIA_DEBUG" ] && echo "Requires: $mod_requires"
+        if [ "$mod_requires" ] && ! hestia_module_install_check_requires "$mod_provider" "$mod_requires"; then
+            return 1
+        fi
+
+        [ "$HESTIA_DEBUG" ] && echo "Installing provider module $mod_provider"
+        hestia module $mod_provider install "$@"
+
+        # Write installed module info
+        osal_kv_write $HESTIA_CONF_MODULES/$mod_provider.conf 'installed' '1'
+        osal_kv_write $HESTIA_CONF_MODULES/$mod_provider.conf 'hmd' "$hmd"
+        osal_kv_write $HESTIA_CONF_MODULES/$mod_provider.conf 'enabled' '1'
+
+        local mod_provides=$(osal_kv_read $hmd 'provides')
+        if [ "$mod_provides" ]; then
+            # Write what this module provides
+            for mod in $mod_provides; do
+                local current_variant=$(hestia_module_variant_installed $mod)
+                current_variant="$mod_name $current_variant"
+
+                osal_kv_write $HESTIA_CONF_MODULES/$mod.conf $mod_provider 'yes'
+                osal_kv_write $HESTIA_CONF_MODULES/$mod.conf 'variant' $current_variant
+            done
+        fi
     else
         echo "Usage: module install module_name"
         return 0
     fi
 }
+
+# Check whether conflicting modules are installed,
+# returns 1 if a conflict is found.
+hestia_module_install_check_conflicts() {
+    local mod_name=$1
+    shift
+
+    for mod in "$@"; do
+        [ "$HESTIA_DEBUG" ] && echo "Check conflict: $mod"
+        if hestia_module_isinstalled $mod; then
+            echo "Module '$mod' conflicts with required module '$mod_name'"
+            return 1
+        fi
+    done
+    return 0
+}
+
+# Check whether required modules are installed and installs
+# them if necessary, returns 1 if a requirements can't be installed.
+hestia_module_install_check_requires() {
+    local mod_name=$1
+    shift
+
+    for mod in $@; do
+        [ "$HESTIA_DEBUG" ] && echo "Check dependency: $mod"
+        if ! hestia_module_isinstalled $mod; then
+            if $BIN/hestia module install $mod; then
+                echo "Module '${mod}' is required for module '${mod_name}' but can not be installed"
+                return 1
+            fi
+        fi
+    done
+
+    return 0
+}

+ 1 - 2
bin/module/php/remove.inc

@@ -4,8 +4,7 @@ hestia_module_php_remove() {
     source $HESTIA/bin/module/func.inc
     source $HESTIA/bin/module/php/func.inc
 
-    module_installed=$(hestia_module_isinstalled php)
-    if [ ! "$module_installed" ] && [ ! "$param_force" ]; then
+    if ! hestia_module_isinstalled 'php' && [ ! "$param_force" ]; then
         echo "PHP module is not installed. See 'hestia module info php'."
         return 1
     fi

+ 5 - 4
bin/module/remove.inc

@@ -1,12 +1,13 @@
 #!/bin/sh
 
 hestia_module_remove() {
-    if [ "$1" ] && [ "$1" != 'install' ] && [ "$1" != 'remove' ]; then
-        module_name=$1
+    if [ "$1" ]; then
+        local mod_name=$1
         shift
-        $BIN/hestia module $module_name remove "$@"
+
+        $BIN/hestia module $mod_name remove "$@"
     else
-        echo "Usage: module install module_name"
+        echo "Usage: module remove module_name"
         return 0
     fi
 }

+ 40 - 0
bin/module/what-provides.inc

@@ -0,0 +1,40 @@
+#!/bin/bash
+
+# Rerurns the preferred module that provides $1, or nothing
+# if nothing provides $1
+# i.e. `hestia_module_what-provides 'web'` = 'apache'
+# i.e. `hestia_module_what-provides 'exim'` = 'exim'
+hestia_module_what-provides() {
+    if [ -e $HESTIA/data/modules/$1.hmd ]; then
+        # There's a module with that name
+        echo $1
+        return 0
+    fi
+
+    # Search for a module that provides $1
+    current_pref=0
+    for hmd in $HESTIA/data/modules/*.hmd; do
+        local mod_provides=$(osal_kv_read $hmd 'provides')
+        if [ "$mod_provides" = "$1" ]; then
+            if [ "$param_all" ]; then
+                # Return all
+                mod_name=$(basename -s '.hmd' "$hmd")
+                echo $mod_name
+            else
+                # Return only the highest preference
+                local mod_preference=$(osal_kv_read $hmd 'preference')
+                if [ ! "$mod_preference" ]; then mod_preference=0; fi
+                if [ $current_pref -eq 0 ] || [ $mod_preference -ge $current_pref ]; then
+                    current_pref=$mod_preference
+                    mod_name=$(basename -s '.hmd' "$hmd")
+                fi
+            fi
+        fi
+    done
+    if [ "$mod_name" ]; then
+        [ "$param_all" ] || echo $mod_name      # if --all, it was already echoed
+        return 0
+    else
+        return 1
+    fi
+}

+ 58 - 0
docs-cli.md

@@ -0,0 +1,58 @@
+# Hestia command line interface
+
+The hestia command
+
+## Modules
+
+### List available modules
+
+Lists modules, indicating what each module provides and installation status.
+
+`hestia module list`
+
+```
+Module           Provides     Inst Description
+apache           web          No   Apache web server
+clamav           antivirus    Yes  ClamAV antivirus
+dovecot          imap         Yes  Dovecot IMAP/POP3 server
+exim             mta          No   Exim mail transport agent
+nginx-web        web          No   Nginx web server
+nginx-proxy      rproxy       No   Nginx reverse proxy
+nginx-web        web          No   Nginx web server
+php-fpm          php          Yes  PHP language FPM
+phpmyadmin       phpmyadmin   Yes  phpMyAdmin MariaDB/MySQL web admin
+php-ruid2        php          No   PHP language ruid2
+spamassassin     antispam     Yes  SpamAssassin antispam
+vsftpd           ftp          No   Vsftpd FTP server
+```
+
+### Get module information
+
+`hestia module info web`
+
+```
+Module name     : apache
+Installed       : yes
+Description     : Hestia Apache module
+Variant         : apache
+Version         : 1
+```
+
+### Query module provides
+
+`hestia module what-provides web`
+
+```
+apache
+```
+
+`hestia module what-provides exim`
+
+```
+exim
+```
+
+`hestia module what-provides nonexistent`
+
+(exit code 1)
+

+ 2 - 1
install/modules/apache.hmd

@@ -1,5 +1,6 @@
 name=apache
 provides=web
 requires=
-conflicts=
+conflicts=web
+preference=100
 description=Apache web server

+ 1 - 1
install/modules/dovecot.hmd

@@ -1,5 +1,5 @@
 name=dovecot
 provides=imap
 requires=
-conflicts=
+conflicts=imap
 description=Dovecot IMAP/POP3 server

+ 1 - 1
install/modules/exim.hmd

@@ -1,5 +1,5 @@
 name=exim
 provides=mta
 requires=
-conflicts=
+conflicts=mta
 description=Exim mail transport agent

+ 6 - 0
install/modules/mariadb.hmd

@@ -0,0 +1,6 @@
+name=mariadb
+provides=mysqldb
+requires=
+conflicts=mysql
+preference=100
+description=MariaDB database

+ 1 - 1
install/modules/nginx-proxy.hmd

@@ -1,5 +1,5 @@
 name=nginx-proxy
 provides=rproxy
 requires=apache
-conflicts=nginx-web
+conflicts=nginx
 description=Nginx reverse proxy

+ 1 - 1
install/modules/nginx-web.hmd → install/modules/nginx.hmd

@@ -1,4 +1,4 @@
-name=nginx-web
+name=nginx
 provides=web
 requires=
 conflicts=web

+ 2 - 1
install/modules/php-fpm.hmd

@@ -1,5 +1,6 @@
 name=php-fpm
 provides=php
-requires=
+requires=web
 conflicts=
+preference=100
 description=PHP language FPM

+ 1 - 1
install/modules/php-ruid2.hmd

@@ -1,5 +1,5 @@
 name=php-ruid2
 provides=php
-requires=
+requires=apache
 conflicts=
 description=PHP language ruid2

+ 1 - 0
install/modules/vsftpd.hmd

@@ -2,4 +2,5 @@ name=vsftpd
 provides=ftp
 requires=
 conflicts=ftp
+preference=100
 description=Vsftpd FTP server