|
| 1 | +#!/usr/local/hestia/php/bin/php |
| 2 | +<?php |
| 3 | +//# info: Install Quick Install Web App via CLI |
| 4 | +//# options: action [user] [domain] [app] [options ...] |
| 5 | +//# |
| 6 | +//# example: v-quick-install-app install admin domain.com wordpress email="info@hestiacp" password="123456" username="admin" site_name="HestiaCP Demo" install_directory="/" language="nl_NL" php_version="8.2" database_create="true" |
| 7 | +//# example: v-quick-install-app apps |
| 8 | +//# example: v-quick-install-app options admin domain.com wordpress |
| 9 | +//# Install Quick Install Web App via CLI run v-quick-install-app apps for supported apps. |
| 10 | +//# v-quick-install-app options for the app options and v-quick-install-app install to install the app |
| 11 | +//# Please note app names are case sensitive |
| 12 | + |
| 13 | +use Symfony\Component\Console\Application; |
| 14 | +use Symfony\Component\Console\Input\InputArgument; |
| 15 | +use Symfony\Component\Console\Command\Command; |
| 16 | +use Symfony\Component\Console\Output\ConsoleOutput; |
| 17 | + |
| 18 | + |
| 19 | +use Hestiacp\quoteshellarg\quoteshellarg; |
| 20 | + |
| 21 | +session_start(); |
| 22 | +require_once( __DIR__ . '/../web/inc/vendor/autoload.php'); |
| 23 | +require_once( __DIR__ . '/../web/src/init.php'); |
| 24 | + |
| 25 | + |
| 26 | +define("HESTIA_DIR_BIN", "/usr/local/hestia/bin/"); |
| 27 | +define("HESTIA_CMD", "/usr/bin/sudo /usr/local/hestia/bin/"); |
| 28 | +define("DEFAULT_PHP_VERSION", "php-" . exec('php -r "echo substr(phpversion(),0,3);"')); |
| 29 | + |
| 30 | +exec(HESTIA_CMD . "v-list-sys-config json", $output, $return_var); |
| 31 | +$data = json_decode(implode("", $output), true); |
| 32 | +$sys_arr = $data["config"]; |
| 33 | +foreach ($sys_arr as $key => $value) { |
| 34 | +$_SESSION[$key] = $value; |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +$_SESSION['userContext'] = 'user'; |
| 39 | +$application = new Application(); |
| 40 | +$application -> register('install') |
| 41 | + ->setDescription('Install app via the CLI') |
| 42 | + -> addArgument('user', InputArgument::REQUIRED, 'Hestia User') |
| 43 | + -> addArgument('domain', InputArgument::REQUIRED, 'Domain') |
| 44 | + -> addArgument('app', InputArgument::REQUIRED, 'App Name') |
| 45 | + -> addArgument('options', InputArgument::IS_ARRAY, 'Options') |
| 46 | + -> setCode(function($input, $output){ |
| 47 | + $user = $input -> getArgument('user'); |
| 48 | + $_SESSION['user'] = $user; |
| 49 | + $v_domain = $input -> getArgument('domain'); |
| 50 | + $app = $input -> getArgument('app'); |
| 51 | + $options = $input -> getArgument('options'); |
| 52 | + $data = []; |
| 53 | + foreach($options as $option){ |
| 54 | + $o = explode('=', $option); |
| 55 | + $data['webapp_'.$o[0]] = $o[1]; |
| 56 | + } |
| 57 | + $hestia = new \Hestia\System\HestiaApp(); |
| 58 | + if(class_exists("\Hestia\WebApp\Installers\\" . $app . "\\" . $app . "Setup") === false){ |
| 59 | + $output -> writeln('App not found'); |
| 60 | + return Command::FAILURE; |
| 61 | + } |
| 62 | + $app_installer_class = "\Hestia\WebApp\Installers\\" . $app . "\\" . $app . "Setup"; |
| 63 | + $app_installer = new $app_installer_class($v_domain, $hestia); |
| 64 | + |
| 65 | + // check for default fields |
| 66 | + $WebappInstaller = new \Hestia\WebApp\AppWizard($app_installer, $v_domain, $hestia); |
| 67 | + $fields = $WebappInstaller -> getOptions(); |
| 68 | + $array = []; |
| 69 | + foreach($fields as $key => $field){ |
| 70 | + if(is_array($field)){ |
| 71 | + if(!empty($field['value'])){ |
| 72 | + $array['webapp_'.$key] = $field['value']; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + $data = array_merge($array, $data); |
| 77 | + //loop trough data and check all fields are set |
| 78 | + |
| 79 | + $error = false; |
| 80 | + foreach($fields as $key => $field){ |
| 81 | + if(empty($data['webapp_'.$key])){ |
| 82 | + if(strpos($key, 'database_') !== false){ |
| 83 | + if($data['webapp_database_create'] != true){ |
| 84 | + $output -> writeln('Missing required field: ' . $key); |
| 85 | + $error = true; |
| 86 | + } |
| 87 | + }else{ |
| 88 | + //all ways the case |
| 89 | + $output -> writeln('Missing required field: ' . $key); |
| 90 | + $error = true; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if($error !== false){ |
| 96 | + return Command::FAILURE; |
| 97 | + } |
| 98 | + |
| 99 | + $installer = new \Hestia\WebApp\AppWizard($app_installer, $v_domain, $hestia); |
| 100 | + $installer -> execute($data); |
| 101 | + return Command::SUCCESS; |
| 102 | + }); |
| 103 | + |
| 104 | +$application -> register('apps') |
| 105 | + ->setDescription('List availble apps') |
| 106 | + -> setCode(function($input, $output){ |
| 107 | + $appInstallers = glob(__DIR__ . "/../web/src/app/WebApp/Installers/*/*.php"); |
| 108 | + $output -> writeln('Available Apps'); |
| 109 | + $output -> writeln('---------------------------------'); |
| 110 | + foreach($appInstallers as $appInstaller){ |
| 111 | + $app = basename(dirname($appInstaller)); |
| 112 | + $hestia = new \Hestia\System\HestiaApp(); |
| 113 | + $domain = 'demo.hestiacp.com'; |
| 114 | + if( !file_exists(__DIR__ . "/../web/src/app/WebApp/Installers/" . $app . "/". $app . "Setup.php") ){ |
| 115 | + continue; |
| 116 | + } |
| 117 | + $app_installer_class = "\Hestia\WebApp\Installers\\" . $app . "\\" . $app . "Setup"; |
| 118 | + $app_installer = new $app_installer_class($domain, $hestia); |
| 119 | + $info = $app_installer -> info(); |
| 120 | + $output -> writeln($info['name'] . ' - ' . $info['version']); |
| 121 | + } |
| 122 | + $output -> writeln('---------------------------------'); |
| 123 | + $output -> writeln('Please note app names are case sensitive'); |
| 124 | + |
| 125 | + return Command::SUCCESS; |
| 126 | +}); |
| 127 | + |
| 128 | +$application -> register('options') |
| 129 | +->setDescription('List options requied / optional for the app') |
| 130 | + -> addArgument('user', InputArgument::REQUIRED, 'Hestia User') |
| 131 | + -> addArgument('domain', InputArgument::REQUIRED, 'Domain') |
| 132 | + -> addArgument('app', InputArgument::REQUIRED, 'App Name') |
| 133 | + -> setCode(function($input, $output){ |
| 134 | + $user = $input -> getArgument('user'); |
| 135 | + $_SESSION['user'] = $user; |
| 136 | + $v_domain = $input -> getArgument('domain'); |
| 137 | + $app = $input -> getArgument('app'); |
| 138 | + $hestia = new \Hestia\System\HestiaApp(); |
| 139 | + |
| 140 | + if(class_exists("\Hestia\WebApp\Installers\\" . $app . "\\" . $app . "Setup") === false){ |
| 141 | + $output -> writeln('App not found'); |
| 142 | + return Command::FAILURE; |
| 143 | + } |
| 144 | + $app_installer_class = "\Hestia\WebApp\Installers\\" . $app . "\\" . $app . "Setup"; |
| 145 | + $app_installer = new $app_installer_class($v_domain, $hestia); |
| 146 | + $WebappInstaller = new \Hestia\WebApp\AppWizard($app_installer, $v_domain, $hestia); |
| 147 | + $output -> writeln('To install '.$app.' use the following command:'); |
| 148 | + $output -> writeln('v-quick-install-app install ' . $user . ' ' . $v_domain . ' ' . $app . ' email="info@hestiacp" password="12346"'); |
| 149 | + $output -> writeln('---------------------------------'); |
| 150 | + $output -> writeln('Options for ' . $app); |
| 151 | + $output -> writeln('---------------------------------'); |
| 152 | + $options = $WebappInstaller -> getOptions(); |
| 153 | + foreach($options as $key => $option){ |
| 154 | + if(!is_array($option)){ |
| 155 | + $output -> writeln('Key: ' . $key . ' Type: ' . $option .' (Required)'); |
| 156 | + }else{ |
| 157 | + $required = ''; |
| 158 | + if(empty($option['value'])){ |
| 159 | + $option['value'] = 'none'; |
| 160 | + if(strpos($key, 'database_') === false){ |
| 161 | + $required = '(' . 'Required' . ')'; |
| 162 | + } |
| 163 | + } |
| 164 | + if(!empty($option['type'])){ |
| 165 | + if($option['type'] == 'boolean'){ |
| 166 | + $option['value'] = $option['value'] ? 'true' : 'false'; |
| 167 | + } |
| 168 | + $output -> writeln('Key: ' .$key . ' Default Value: ' . $option['value'] .' Type: ' . $option['type'] . ' ' . $required); |
| 169 | + }else{ |
| 170 | + $output -> writeln('Key :' .$key . ' Default Value: ' . $option['value'] . ' ' . $required); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + return Command::SUCCESS; |
| 175 | + }); |
| 176 | + |
| 177 | + |
| 178 | +$application -> run(); |
0 commit comments