diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..48b8bf9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..4f23987 --- /dev/null +++ b/composer.json @@ -0,0 +1,25 @@ +{ + "name": "thomas/make-webapp", + "description": "Auto configuration of webserver for new web app", + // "repositories":[ + // { + // "type":"vcs", + // "url":"git@vitis.vinatis.com:tech/infra/deployment-script.git" + // } + // ], + "require": { + "league/climate": "^3.4" + }, + "license": "MIT", + "authors": [ + { + "name": "Thomas Chartron", + "email": "thomas.chartron@gmail.com" + } + ], + "autoload": { + "psr-4": { + "Helpers\\": "src/Helpers/" + } + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..8d6ca1d --- /dev/null +++ b/composer.lock @@ -0,0 +1,174 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "127348feacc4a5b6a7707fffd08bbd53", + "packages": [ + { + "name": "league/climate", + "version": "3.4.1", + "source": { + "type": "git", + "url": "https://github.com/thephpleague/climate.git", + "reference": "d657a19837c1f79a891381fb128b755aa3386381" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/thephpleague/climate/zipball/d657a19837c1f79a891381fb128b755aa3386381", + "reference": "d657a19837c1f79a891381fb128b755aa3386381", + "shasum": "" + }, + "require": { + "php": "^5.6|^7.0", + "psr/log": "^1.0", + "seld/cli-prompt": "^1.0" + }, + "require-dev": { + "mikey179/vfsstream": "^1.4", + "mockery/mockery": "^1.0", + "phpunit/phpunit": "^5.7.16" + }, + "suggest": { + "ext-mbstring": "If ext-mbstring is not available you MUST install symfony/polyfill-mbstring" + }, + "type": "library", + "autoload": { + "psr-4": { + "League\\CLImate\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Craig Duncan", + "email": "git@duncanc.co.uk", + "homepage": "https://github.com/duncan3dc", + "role": "Developer" + }, + { + "name": "Joe Tannenbaum", + "email": "hey@joe.codes", + "homepage": "http://joe.codes/", + "role": "Developer" + } + ], + "description": "PHP's best friend for the terminal. CLImate allows you to easily output colored text, special formats, and more.", + "keywords": [ + "cli", + "colors", + "command", + "php", + "terminal" + ], + "time": "2018-04-29T16:43:54+00:00" + }, + { + "name": "psr/log", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/log.git", + "reference": "6c001f1daafa3a3ac1d8ff69ee4db8e799a654dd" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/log/zipball/6c001f1daafa3a3ac1d8ff69ee4db8e799a654dd", + "reference": "6c001f1daafa3a3ac1d8ff69ee4db8e799a654dd", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Log\\": "Psr/Log/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for logging libraries", + "homepage": "https://github.com/php-fig/log", + "keywords": [ + "log", + "psr", + "psr-3" + ], + "time": "2018-11-20T15:27:04+00:00" + }, + { + "name": "seld/cli-prompt", + "version": "1.0.3", + "source": { + "type": "git", + "url": "https://github.com/Seldaek/cli-prompt.git", + "reference": "a19a7376a4689d4d94cab66ab4f3c816019ba8dd" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Seldaek/cli-prompt/zipball/a19a7376a4689d4d94cab66ab4f3c816019ba8dd", + "reference": "a19a7376a4689d4d94cab66ab4f3c816019ba8dd", + "shasum": "" + }, + "require": { + "php": ">=5.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Seld\\CliPrompt\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jordi Boggiano", + "email": "j.boggiano@seld.be" + } + ], + "description": "Allows you to prompt for user input on the command line, and optionally hide the characters they type", + "keywords": [ + "cli", + "console", + "hidden", + "input", + "prompt" + ], + "time": "2017-03-18T11:32:45+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/scripts/make-webapp b/scripts/make-webapp new file mode 100755 index 0000000..6d16606 --- /dev/null +++ b/scripts/make-webapp @@ -0,0 +1,182 @@ +#!/usr/bin/env php +arguments->add([ + 'web-server' => [ + 'prefix' => 'ws', + 'longPrefix' => 'web-server', + 'description' => 'Webserver name (apache2 nginx)', + // 'required' => true + ], + 'name' => [ + 'prefix' => 'n', + 'longPrefix' => 'name', + 'description' => 'Website name', + // 'required' => true + ], + 'vhost' => [ + 'prefix' => 'h', + 'longPrefix' => 'vhost', + 'description' => 'Vhost name', + // 'required' => true + ], + 'local-address' => [ + 'prefix' => 'a', + 'longPrefix' => 'address', + 'description' => 'Local address to create (eg site-name.work)', + // 'required' => true + ], + 'interactive' => [ + 'prefix' => 'i', + 'longPrefix' => 'interactive', + 'description' => 'Interactive creation', + // 'required' => true + ], + 'verbose' => [ + 'prefix' => 'v', + 'longPrefix' => 'verbose', + 'description' => 'Verbose output', + 'noValue' => true, + ], + 'help' => [ + 'longPrefix' => 'help', + 'description' => 'Prints a usage statement', + 'noValue' => true, + ], + 'other' => [ + 'description' => 'The other args passed', + ], +]); +$climate->arguments->parse(); + +if($climate->arguments->defined('help')) { + $climate->usage(); +} + +$climate->backgroundLightGreen()->bold()->black()->out('Welcome to web app configuration'); + +//Here we should be able to choose if apache2 or nginx is used + +//////////////// +//CORE SCRIPT // +//////////////// + +$shellCurrentUser = execOrFail("whoami"); +$shellCurrentUser = $shellCurrentUser[0]; //dirty change +// die($shellCurrentUser[0]); +//Interactive mode +if($climate->arguments->defined('interactive')) { + $args = interactive(); + $climate->backgroundLightGreen()->black()->out('WebApp configuration :'); + $climate->backgroundLightGreen()->black()->br()->out('Web server : '.$args['web-server']); + $climate->backgroundLightGreen()->black()->br()->out('Site Folder : /home/'.$shellCurrentUser.'/www/'.$args['site-name']); + $climate->backgroundLightGreen()->black()->br()->out('Vhost file name : '.$args['vhost-name']); + $climate->backgroundLightGreen()->black()->br()->out('Local address : '.$args['local-address']); + $climate->backgroundLightGreen()->black()->br()->out('DocumentRoot : '.$args['document-root']); + $input = $climate->input('Should we create this ? [Y/n]'); + $input->defaultTo('Y'); + $input->accept(['Y', 'n']); + $response = $input->prompt(); + + if($response === "Y") { + makeApp($args); + + } + + // die(var_dump($args)); +} + + +// $args = $climate->arguments->get('other'); +// die(var_dump($args)); + + + +$climate->backgroundLightGreen()->bold()->black()->out('Congratz !'); + +/** + * Allow for interactive needed argument filling + * @return [type] [description] + */ +function interactive() : array +{ + global $climate; + $args = []; + //Site folder + $input = $climate->input('Web server ? default : apache2'); + $input->defaultTo('apache2'); + $args['web-server'] = $input->prompt(); + + $input = $climate->input('Site name folder ? ex : site-name'); + $input->accept(function($response) { + return ($response !== ''); + }); + $args['site-name'] = $input->prompt(); + + $input = $climate->input('Vhost file name ? ex : 001-site-name.conf'); + $input->accept(function($response) { + return ($response !== ''); + }); + $args['vhost-name'] = $input->prompt(); + + $input = $climate->input('Local address ? default : site-name.work'); + $input->defaultTo($args['site-name'].".work"); + $args['local-address'] = $input->prompt(); + + $input = $climate->input('Document Root ? default : /var/www/site-name/public'); + $input->defaultTo("/var/www/".$args['site-name']."/public"); + $args['document-root'] = $input->prompt(); + + return $args; +} + +function makeApp($args) : bool +{ + global $shellCurrentUser, $climate; + foreach ($args as $key => $value) { + switch($key) { + // case 'web-server': + + // break; + case 'site-name': + if($args['web-server'] === "apache2") { + execOrFail('mkdir -p /home/'.$shellCurrentUser.'/www/'.$args['site-name']); + if($climate->arguments->defined('verbose')) { + $climate->backgroundLightBlue()->black()->br()->out('Creating /home/'.$shellCurrentUser.'/www/'.$args['site-name'].' folder'); + } + } + break; + case 'vhost-name': + if($args['web-server'] === "apache2") { + execOrFail('mkdir -p /home/'.$shellCurrentUser.'/www/'.$args['site-name']); + if($climate->arguments->defined('verbose')) { + $climate->backgroundLightBlue()->black()->br()->out('Creating /home/'.$shellCurrentUser.'/www/'.$args['site-name'].' folder'); + } + } + break; + case 'local-address': + if($args['web-server'] === "apache2") { + + } + break; + + } + } + return false; +} + +function execOrFail($command) { + try { + exec($command, $output); + } catch (Exception $e) { + return $e->getMessage(); + } + // return (!empty($output)) ? $output : true; + return $output; +} diff --git a/src/Helpers/CLImateHelper.php b/src/Helpers/CLImateHelper.php new file mode 100644 index 0000000..3e6fc75 --- /dev/null +++ b/src/Helpers/CLImateHelper.php @@ -0,0 +1,32 @@ +backgroundLightGreen()->bold()->black()->out('This prints to the terminal.'); + + return $climateInstance.$call; + } +}