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.

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