OpenSource CLI-App to install and handle stuff related to Web-Server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
7.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Commands;
  3. use Illuminate\Console\Scheduling\Schedule;
  4. use LaravelZero\Framework\Commands\Command;
  5. use Illuminate\Support\Facades\File;
  6. use App\Facades\Install;
  7. use App\Facades\NginxVhost;
  8. use App\Facades\NginxVhostFactory;
  9. use App\Facades\TerminalHelper;
  10. use App\Facades\Menus\StylesFactory;
  11. use App\Helpers\NginxTemplateHelper;
  12. use App\BladeFile;
  13. use PhpSchool\CliMenu\Builder\CliMenuBuilder;
  14. use PhpSchool\CliMenu\CliMenu;
  15. use PhpSchool\CliMenu\MenuItem\CheckboxItem;
  16. use PhpSchool\CliMenu\Style\SelectableStyle;
  17. use PhpSchool\CliMenu\MenuItem\MenuMenuItem;
  18. use Respect\Validation\Validator as v;
  19. use Respect\Validation\Exceptions\NestedValidationException;
  20. use Log;
  21. use Closure;
  22. /**
  23. * Add and Edit Configurations of Vhosts from Nginx
  24. *
  25. * Using php-school/cli-menu
  26. *
  27. *
  28. *
  29. */
  30. class NginxVhostsCommand extends Command
  31. {
  32. // path templates
  33. const TEMPLATES_DIR = '/resources/nginx/templates';
  34. // index for vhosts
  35. const VHOST_INDEX = 0;
  36. /**
  37. * The signature of the command.
  38. *
  39. * @var string
  40. */
  41. protected $signature = 'nginx:vhosts';
  42. /**
  43. * The description of the command.
  44. *
  45. * @var string
  46. */
  47. protected $description = 'Create and Manage Nginx Vhosts';
  48. /**
  49. *
  50. *
  51. *
  52. */
  53. private function selectTemplate()
  54. {
  55. $menu = function(CliMenuBuilder $builder)
  56. {
  57. // create blade
  58. $bladeFile = new BladeFile(self::TEMPLATES_DIR);
  59. $builder
  60. ->setTitle('Nginx > Add')
  61. ->setGoBackButtonText('Back');
  62. $nginxTemplateHelper = new NginxTemplateHelper();
  63. foreach($nginxTemplateHelper->find() as $template) {
  64. $submenuCallable = $this->createConfiguration($template, $bladeFile);
  65. $builder
  66. ->addSubMenu($template['name'], $submenuCallable);
  67. }
  68. $builder->addLineBreak('-');
  69. };
  70. return $menu;
  71. }
  72. /**
  73. * create Configuration, add for each template a submenu
  74. *
  75. *
  76. * @param array $template
  77. * @param Blade $blade
  78. * @return CliMenuBuilder
  79. */
  80. private function createConfiguration($template, $bladeFile)
  81. {
  82. $menu = function(CliMenuBuilder $builder) use ($template, $bladeFile)
  83. {
  84. $configuration = [
  85. 'domain' => '',
  86. 'root' => '',
  87. 'index' => 'index.php',
  88. 'ssl' => true,
  89. 'redirect_www' => true
  90. ];
  91. exec('find /lib/systemd/system/ -name "php[0-9\.]*-fpm.service"', $files);
  92. // create checkbox for ssl
  93. $checkboxSSL = new CheckboxItem('ssl', function(CliMenu $menu) use (&$configuration) {
  94. $configuration['ssl'] = $menu->getSelectedItem()->getChecked();
  95. });
  96. $checkboxSSL->setChecked($configuration['ssl']);
  97. // create checkbox for redirect from www
  98. $checkboxRedirect = new CheckboxItem('redirect www', function(CliMenu $menu) use (&$configuration) {
  99. $configuration['redirect_www'] = $menu->getSelectedItem()->getChecked();
  100. });
  101. $checkboxRedirect->setChecked($configuration['redirect_www']);
  102. $builder
  103. ->setTitle('Nginx > Add > '.$template['name'])
  104. ->setGoBackButtonText('Cancel')
  105. // input domain
  106. ->addItem('domain: -', function(CliMenu $menu) use (&$configuration) {
  107. $result = $menu->askText()->ask();
  108. $configuration['domain'] = $result->fetch();
  109. $menu->getSelectedItem()->setText('domain: '.$result->fetch());
  110. $menu->redraw();
  111. })
  112. // input root
  113. ->addItem('root: -', function(CliMenu $menu) use (&$configuration) {
  114. $result = $menu->askText()->ask();
  115. $configuration['root'] = $result->fetch();
  116. $menu->getSelectedItem()->setText('root: '.$result->fetch());
  117. $menu->redraw();
  118. })
  119. // input index
  120. ->addItem('index: '.$configuration['index'], function(CliMenu $menu) use (&$configuration) {
  121. $result = $menu->askText()->ask();
  122. $configuration['index'] = $result->fetch();
  123. $menu->getSelectedItem()->setText('index: '.$result->fetch());
  124. $menu->redraw();
  125. })
  126. ->addLineBreak('-');
  127. foreach($files as $index => $file) {
  128. $file = str_replace('/lib/systemd/system/', '', $file);
  129. $file = str_replace('.service', '', $file);
  130. $builder->addRadioItem($file, function(CliMenu $menu) use (&$configuration) {
  131. $configuration['php_fpm'] = $menu->getSelectedItem()->getText();
  132. });
  133. }
  134. $builder
  135. ->addLineBreak('-')
  136. // options
  137. ->addMenuItem($checkboxSSL)
  138. ->addMenuItem($checkboxRedirect)
  139. ->addLineBreak('-')
  140. // create
  141. ->addItem('Create', function(CliMenu $menu) use (&$configuration, $template, $bladeFile) {
  142. $data = $configuration;
  143. // add directory for validator to check if file exits
  144. $data['index'] = $data['root'].'/'.$data['index'];
  145. $validator = v::key('domain', v::domain(false))
  146. ->key('root', v::directory())
  147. ->key('index', v::file())
  148. ->key('php_fpm', v::notEmpty());
  149. try {
  150. $validator->assert($data);
  151. } catch(NestedValidationException $exception) {
  152. $errors = $exception->getMessages();
  153. }
  154. if (isset($errors)) {
  155. TerminalHelper::confirmArray($menu, $errors);
  156. } else {
  157. // create filename
  158. $filename = $configuration['domain'].'.conf';
  159. // write configuration to file
  160. $bladeFile->put($template['name'], '/etc/nginx/sites-available/'.$filename, $configuration);
  161. $menu->confirm('Success!')->display('Ok!');
  162. // prepare for close current submenu and open mainmenu
  163. $parent = $menu->getParent();
  164. $menu->closeThis();
  165. $mainmenu = $parent->getParent();
  166. $mainmenu = NginxVhostFactory::addVhosts($mainmenu, NginxVhost::find(), self::VHOST_INDEX);
  167. $mainmenu->open();
  168. }
  169. })
  170. ->addLineBreak('-');
  171. };
  172. return $menu;
  173. }
  174. /**
  175. * let it rain
  176. *
  177. *
  178. */
  179. public function handle()
  180. {
  181. $submenuSelectTemplate = $this->selectTemplate();
  182. // create menu
  183. $builder = $this->menu('Nginx')
  184. // add Submenu for select templates
  185. ->addLineBreak('-')
  186. ->addSubMenu('Add', $submenuSelectTemplate);
  187. $builder->addLineBreak('-');
  188. // apperance
  189. $builder = StylesFactory::setMenuStyles($builder);
  190. $mainmenu = $builder->build();
  191. // remove first item
  192. $items = $mainmenu->getItems();
  193. unset($items[0]);
  194. $mainmenu->setItems($items);
  195. // adding current vhosts
  196. $mainmenu = NginxVhostFactory::addVhosts($mainmenu, NginxVhost::find(), self::VHOST_INDEX);
  197. $mainmenu->open();
  198. }
  199. }