From 62146c4027e48cfbdb4f518de137de8430392e24 Mon Sep 17 00:00:00 2001 From: Fbenas Date: Sun, 18 Feb 2018 22:29:36 +0000 Subject: Split client and manager --- src/Client/Config/Loader.php | 150 +++++++++++++ src/Client/Filesystem/CreateDirectory.php | 49 ++++ src/Client/Filesystem/CreateFile.php | 65 ++++++ src/Client/Script/Command/Site/Create.php | 348 +++++++++++++++++++++++++++++ src/Client/Script/Console.php | 24 ++ src/Client/Task/TaskInterface.php | 11 + src/Config/Loader.php | 150 ------------- src/Filesystem/CreateDirectory.php | 49 ---- src/Filesystem/CreateFile.php | 65 ------ src/Manager/Config/Loader.php | 150 +++++++++++++ src/Manager/Filesystem/CreateDirectory.php | 49 ++++ src/Manager/Filesystem/CreateFile.php | 65 ++++++ src/Manager/Script/Command/Site/Create.php | 348 +++++++++++++++++++++++++++++ src/Manager/Script/Console.php | 24 ++ src/Manager/Task/TaskInterface.php | 11 + src/Script/Command/Site/Create.php | 348 ----------------------------- src/Script/Console.php | 24 -- src/Task/TaskInterface.php | 11 - 18 files changed, 1294 insertions(+), 647 deletions(-) create mode 100644 src/Client/Config/Loader.php create mode 100644 src/Client/Filesystem/CreateDirectory.php create mode 100644 src/Client/Filesystem/CreateFile.php create mode 100644 src/Client/Script/Command/Site/Create.php create mode 100644 src/Client/Script/Console.php create mode 100644 src/Client/Task/TaskInterface.php delete mode 100644 src/Config/Loader.php delete mode 100644 src/Filesystem/CreateDirectory.php delete mode 100644 src/Filesystem/CreateFile.php create mode 100644 src/Manager/Config/Loader.php create mode 100644 src/Manager/Filesystem/CreateDirectory.php create mode 100644 src/Manager/Filesystem/CreateFile.php create mode 100644 src/Manager/Script/Command/Site/Create.php create mode 100644 src/Manager/Script/Console.php create mode 100644 src/Manager/Task/TaskInterface.php delete mode 100644 src/Script/Command/Site/Create.php delete mode 100644 src/Script/Console.php delete mode 100644 src/Task/TaskInterface.php diff --git a/src/Client/Config/Loader.php b/src/Client/Config/Loader.php new file mode 100644 index 0000000..97df62e --- /dev/null +++ b/src/Client/Config/Loader.php @@ -0,0 +1,150 @@ + + */ +class Loader +{ + /** + * A loaded config array + * + * @var mixed[] + */ + protected $config; + + /** + * Try and load a config + * + * @author Phil Burton + */ + public function __construct() + { + foreach ($this->getConfigPaths() as $path) { + if ($config = $this->loadConfig($path)) { + $this->validate(); + $this->clean(); + return; + } + } + + // No config found + throw new \Exception('Could not find config file.'); + } + + /** + * Try and load a config file + * If we can't return false + * + * @author Phil Burton + * @return mixed[]|false + */ + public function loadConfig(string $path) + { + $filename = $path . '/' . $this->getConfigFilename(); + + if (!$this->config) { + if (!file_exists($filename)) { + return false; + } + + if (!is_readable($filename)) { + return false; + } + + if (!$config = include($filename)) { + return false; + } + + $this->config = $config; + } + + return $this->config; + } + + /** + * Return the nsam of the config file to load + * + * @author Phil Burton + * @return string; + */ + public function getConfigFilename() + { + return 'dipper.php'; + } + + /** + * Make sure the config is up to scratch + * + * @author Phil Burton + */ + public function validate() + { + $config = $this->getConfig(); + + foreach ($this->getRequired() as $required) { + if (!isset($config[$required])) { + throw new \Exception("Could not find " . $required . " in config file."); + } + } + } + + /** + * Clean up the config ready for use + * + * @author Phil Burton + */ + public function clean() + { + $config = $this->getConfig(); + foreach ($config as &$item) { + // Remove trailing slashes + $item = rtrim($item, "/"); + } + + $this->config = $config; + } + + /** + * Return the array of config paths + * + * @author Phil Burton + * @return string[] + */ + public function getConfigPaths() + { + return [ + realpath(dirname(__FILE__) . "/../../"), // Root of dipper + realpath(dirname(__FILE__) . "/../../config") // Config directory in dipper + ]; + } + + /** + * Return the loaded config + * + * @author Phil Burton + * @return mixed[] + */ + public function getConfig() + { + return $this->config; + } + + /** + * Return the required config keys + * + * @author Phil Burton + * @return string[] + */ + public function getRequired() + { + return [ + "SITES_ROOT", + "CONFIG_ROOT" + ]; + } +} diff --git a/src/Client/Filesystem/CreateDirectory.php b/src/Client/Filesystem/CreateDirectory.php new file mode 100644 index 0000000..2621169 --- /dev/null +++ b/src/Client/Filesystem/CreateDirectory.php @@ -0,0 +1,49 @@ + + */ +class CreateDirectory implements TaskInterface +{ + /** + * Directory to create + * + * @var string + */ + protected $directory; + + /** + * Check if the directory already exists + * + * @param string $filename + * @author Phil Burton + */ + public function __construct(string $directory) + { + $this->directory = $directory; + + if (!is_writable(dirname($directory))) { + throw new \Exception('Cannot create directory at: ' . $directory); + } + + if (file_exists($directory)) { + throw new \Exception('Directory already exists at: ' . $directory); + } + } + + /** + * Create the new directory + * + * @author Phil Burton + */ + public function execute() + { + mkdir($this->directory); + } +} diff --git a/src/Client/Filesystem/CreateFile.php b/src/Client/Filesystem/CreateFile.php new file mode 100644 index 0000000..ce9537f --- /dev/null +++ b/src/Client/Filesystem/CreateFile.php @@ -0,0 +1,65 @@ + + */ +class CreateFile implements TaskInterface +{ + /** + * Name of file to create + * + * @var string + */ + protected $filename; + + /** + * File contents to write + * + * @var string + */ + protected $contents; + + /** + * Check if the file already exists + * + * @param string $filename + * @author Phil Burton + */ + public function __construct(string $filename, $contents = false) + { + $this->filename = $filename; + + if ($contents) { + $this->contents = $contents; + } + + if (!is_writable(dirname($filename))) { + throw new \Exception('Cannot create file at: ' . $filename); + } + + if (file_exists($filename)) { + throw new \Exception('File already exists at: ' . $filename); + } + } + + /** + * Create the new file + * + * @author Phil Burton + */ + public function execute() + { + touch($this->filename); + + $contents = $this->contents; + if ($contents) { + file_put_contents($this->filename, $contents); + } + } +} diff --git a/src/Client/Script/Command/Site/Create.php b/src/Client/Script/Command/Site/Create.php new file mode 100644 index 0000000..df9fc18 --- /dev/null +++ b/src/Client/Script/Command/Site/Create.php @@ -0,0 +1,348 @@ + + */ +class Create extends SyCommand +{ + /** + * Symfony Progress Bar for command + * + * @var Symfony\Component\Console\Helper\ProgressBar + */ + protected $progressBar; + + /** + * Array of task sets containing tasks + * + * @var mixed + */ + protected $tasks; + + /** + * Name of the deployment + * + * @var string + */ + protected $name; + + /** + * Domain name of the deployment + * + * @var string + */ + protected $domain; + + /** + * Configure the command + * + * @author Phil Burton + */ + protected function configure() + { + $this->addArgument('name', InputArgument::REQUIRED, 'The name of the new site.'); + $this->addArgument('domain', InputArgument::REQUIRED, 'The domain name of the new site.'); + + $this + ->setName('site:create') + ->setDescription('Create a new site.') + ->setHelp('This command allows you to create a new site.'); + + $this->addOption( + 'test', + 't', + InputOption::VALUE_NONE, + 'Run in test mode without any file or directory created?' + ); + + $this->addOption( + 'ignore-config', + 'c', + InputOption::VALUE_NONE, + 'Ignore writing the config?' + ); + + $this->addOption( + 'ignore-filesystem', + 'f', + InputOption::VALUE_NONE, + 'Ignore writing the deployment files and directories?' + ); + } + + /** + * Run the command + * + * @param InputInterface $input + * @param OutputInterface $output + * @author Phil Burton + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $taskCount = 9; + if ($input->getOption('ignore-filesystem')) { + $taskCount -= 8; + } + + if ($input->getOption('ignore-config')) { + $taskCount -= 1; + } + + if ($taskCount <= 0) { + echo "Nothing to do.\n"; + exit; + } + + $this->name = $input->getArgument('name'); + $this->domain = $input->getArgument('domain'); + + $output->writeln( + 'Creating new site with name: ' + . $input->getArgument('name') + . ' and domain: ' + . $input->getArgument('domain') + ); + + // Set-up progress bar + $this->progressBar = new ProgressBar($output, $taskCount); + $this->progressBar->setFormatDefinition('custom', ' %current%/%max% -- %message%'); + $this->progressBar->setFormat('custom'); + $this->progressBar->setMessage('Starting...'); + $this->progressBar->start(); + $this->progressBar->setMessage('Running pre-flight checks'); + + // Construct the file commands so we run checks first + $this->progressBar->advance(); + + try { + $this->runPreflightChecks($input); + } catch (\Exception $e) { + $this->complete($output, "Error with pre-flight checks:\n" . $e->getMessage()); + } + try { + if (!$input->getOption('test')) { + if (!$input->getOption('ignore-config')) { + $this->createConfig($input); + } + + if (!$input->getOption('ignore-filesystem')) { + $this->createFilesystem($input); + } + } + } catch (\Exception $e) { + $this->complete($output, "Unexpected Error creating files and directories:\n " . $e->getMessage()); + } + // Finish up + $this->complete($output, 'Complete', true); + } + + /** + * Show a message and end the program + * Optionally complete the progress bar + * + * @author Phil Burton + * @param OutputInterface $output + * @param string $message + * @param boolean $completeProgressBar + */ + public function complete(OutputInterface $output, string $message, $completeProgressBar = false) + { + if ($completeProgressBar) { + $this->progressBar->advance(); + $this->progressBar->finish(); + } + + $output->writeln(''); + $output->writeln($message); + exit; + } + + /** + * Run the required tasks to check we can complete the full deployment + * + * @author Phil Burton + * @param InputInterface $input + */ + public function runPreflightChecks(InputInterface $input) + { + $ignoreFilesystem = $input->getOption('ignore-filesystem'); + $ignoreConfig = $input->getOption('ignore-config'); + + if (!$ignoreFilesystem) { + $this->addTask( + 'check-filesystem', + 'Creating site directory', + new CreateDirectory( + $this->getFullSitesPath($this->name) + ) + ); + } + + if (!$ignoreConfig) { + $this->addTask( + 'check-config', + 'Creating nginx config file', + new CreateFile( + CONFIG_ROOT . '/' . $this->name . '.conf', + include_once(DIPPER_ROOT . '/nginx/stub.php') + ) + ); + } + } + + /** + * Run the creation of the config tasks + * + * @author Phil Burton + */ + public function createConfig() + { + $this->runTaskSet('check-config'); + } + + /** + * Run the creatuion of the filesystem tasks + * + * @author Phil Burton + * @param InputInterface $input + */ + public function createFilesystem(InputInterface $input) + { + $this->runTaskSet('check-filesystem'); + + $this->runTask( + 'Creating logs directory', + new CreateDirectory( + $this->getFullSitesPath($this->name) . '/logs' + ), + true + ); + + $this->runTask( + 'Creating access log file', + new CreateFile( + $this->getFullSitesPath($this->name) . '/logs/' . $this->name . '.access_log' + ), + true + ); + + $this->runTask( + 'Creating error log file', + new CreateFile( + $this->getFullSitesPath($this->name) . '/logs/' . $this->name . '.errors_log' + ), + true + ); + + $this->runTask( + 'Creating web folder', + new CreateDirectory( + $this->getFullSitesPath($this->name) . '/web' + ), + true + ); + + $this->runTask( + 'Creating directory index.php', + new CreateFile( + $this->getFullSitesPath($this->name) . '/web/index.php' + ), + true + ); + } + + /** + * Add a task to a task set + * + * @author Phil Burton + * @param string $taskSetName + * @param string $taskName + * @param TaskInterface $task + */ + public function addTask(string $taskSetName, string $taskName, TaskInterface $task) + { + $this->taskPriority[$taskSetName][] = $taskName; + $this->tasks[$taskSetName][$taskName] = $task; + } + + /** + * Get the array of tasks from a taskset + * + * @author Phil Burton + * @param string $taskSetName + * @return TaskInterface[] + */ + public function getTaskSet(string $taskSetName) + { + $tasks = $this->tasks; + if (array_key_exists($taskSetName, $tasks)) { + return $tasks[$taskSetName]; + } + + return false; + } + + /** + * Run all the tasks in a taskset + * + * @author Phil Burton + * @param string $taskSetName] + * @param boolean $advanceProgressBar + */ + public function runTaskSet(string $taskSetName, $advanceProgressBar = false) + { + $tasks = $this->getTaskSet($taskSetName); + + if (!$tasks) { + throw new \Exception('Could not find task set `' . $taskSetName . '`'); + } + foreach ($this->taskPriority[$taskSetName] as $message) { + $task = $tasks[$message]; + // Create file in web root for execution + $this->runTask($message, $task, $advanceProgressBar); + } + } + + /** + * Return the full path of a given file + * + * @author Phil Burton + * @param string $path + * @return string + */ + protected function getFullSitesPath($path) + { + return SITES_ROOT . "/" . $path; + } + + /** + * Run a task + * + * @author Phil Burton + * @param mixed $task + * @param string $message + * @return string + */ + protected function runTask(string $message, TaskInterface $task, $advanceProgressBar = false) + { + if ($advanceProgressBar) { + $this->progressBar->advance(); + $this->progressBar->setMessage($message); + } + $task->execute(); + } +} diff --git a/src/Client/Script/Console.php b/src/Client/Script/Console.php new file mode 100644 index 0000000..5f5d90d --- /dev/null +++ b/src/Client/Script/Console.php @@ -0,0 +1,24 @@ + + */ + public function __construct() + { + $application = new Application(); + $application->add(new Create()); + $application->run(); + } +} diff --git a/src/Client/Task/TaskInterface.php b/src/Client/Task/TaskInterface.php new file mode 100644 index 0000000..407e6c8 --- /dev/null +++ b/src/Client/Task/TaskInterface.php @@ -0,0 +1,11 @@ + - */ -class Loader -{ - /** - * A loaded config array - * - * @var mixed[] - */ - protected $config; - - /** - * Try and load a config - * - * @author Phil Burton - */ - public function __construct() - { - foreach ($this->getConfigPaths() as $path) { - if ($config = $this->loadConfig($path)) { - $this->validate(); - $this->clean(); - return; - } - } - - // No config found - throw new \Exception('Could not find config file.'); - } - - /** - * Try and load a config file - * If we can't return false - * - * @author Phil Burton - * @return mixed[]|false - */ - public function loadConfig(string $path) - { - $filename = $path . '/' . $this->getConfigFilename(); - - if (!$this->config) { - if (!file_exists($filename)) { - return false; - } - - if (!is_readable($filename)) { - return false; - } - - if (!$config = include($filename)) { - return false; - } - - $this->config = $config; - } - - return $this->config; - } - - /** - * Return the nsam of the config file to load - * - * @author Phil Burton - * @return string; - */ - public function getConfigFilename() - { - return 'dipper.php'; - } - - /** - * Make sure the config is up to scratch - * - * @author Phil Burton - */ - public function validate() - { - $config = $this->getConfig(); - - foreach ($this->getRequired() as $required) { - if (!isset($config[$required])) { - throw new \Exception("Could not find " . $required . " in config file."); - } - } - } - - /** - * Clean up the config ready for use - * - * @author Phil Burton - */ - public function clean() - { - $config = $this->getConfig(); - foreach ($config as &$item) { - // Remove trailing slashes - $item = rtrim($item, "/"); - } - - $this->config = $config; - } - - /** - * Return the array of config paths - * - * @author Phil Burton - * @return string[] - */ - public function getConfigPaths() - { - return [ - realpath(dirname(__FILE__) . "/../../"), // Root of dipper - realpath(dirname(__FILE__) . "/../../config") // Config directory in dipper - ]; - } - - /** - * Return the loaded config - * - * @author Phil Burton - * @return mixed[] - */ - public function getConfig() - { - return $this->config; - } - - /** - * Return the required config keys - * - * @author Phil Burton - * @return string[] - */ - public function getRequired() - { - return [ - "SITES_ROOT", - "CONFIG_ROOT" - ]; - } -} diff --git a/src/Filesystem/CreateDirectory.php b/src/Filesystem/CreateDirectory.php deleted file mode 100644 index 2621169..0000000 --- a/src/Filesystem/CreateDirectory.php +++ /dev/null @@ -1,49 +0,0 @@ - - */ -class CreateDirectory implements TaskInterface -{ - /** - * Directory to create - * - * @var string - */ - protected $directory; - - /** - * Check if the directory already exists - * - * @param string $filename - * @author Phil Burton - */ - public function __construct(string $directory) - { - $this->directory = $directory; - - if (!is_writable(dirname($directory))) { - throw new \Exception('Cannot create directory at: ' . $directory); - } - - if (file_exists($directory)) { - throw new \Exception('Directory already exists at: ' . $directory); - } - } - - /** - * Create the new directory - * - * @author Phil Burton - */ - public function execute() - { - mkdir($this->directory); - } -} diff --git a/src/Filesystem/CreateFile.php b/src/Filesystem/CreateFile.php deleted file mode 100644 index ce9537f..0000000 --- a/src/Filesystem/CreateFile.php +++ /dev/null @@ -1,65 +0,0 @@ - - */ -class CreateFile implements TaskInterface -{ - /** - * Name of file to create - * - * @var string - */ - protected $filename; - - /** - * File contents to write - * - * @var string - */ - protected $contents; - - /** - * Check if the file already exists - * - * @param string $filename - * @author Phil Burton - */ - public function __construct(string $filename, $contents = false) - { - $this->filename = $filename; - - if ($contents) { - $this->contents = $contents; - } - - if (!is_writable(dirname($filename))) { - throw new \Exception('Cannot create file at: ' . $filename); - } - - if (file_exists($filename)) { - throw new \Exception('File already exists at: ' . $filename); - } - } - - /** - * Create the new file - * - * @author Phil Burton - */ - public function execute() - { - touch($this->filename); - - $contents = $this->contents; - if ($contents) { - file_put_contents($this->filename, $contents); - } - } -} diff --git a/src/Manager/Config/Loader.php b/src/Manager/Config/Loader.php new file mode 100644 index 0000000..97df62e --- /dev/null +++ b/src/Manager/Config/Loader.php @@ -0,0 +1,150 @@ + + */ +class Loader +{ + /** + * A loaded config array + * + * @var mixed[] + */ + protected $config; + + /** + * Try and load a config + * + * @author Phil Burton + */ + public function __construct() + { + foreach ($this->getConfigPaths() as $path) { + if ($config = $this->loadConfig($path)) { + $this->validate(); + $this->clean(); + return; + } + } + + // No config found + throw new \Exception('Could not find config file.'); + } + + /** + * Try and load a config file + * If we can't return false + * + * @author Phil Burton + * @return mixed[]|false + */ + public function loadConfig(string $path) + { + $filename = $path . '/' . $this->getConfigFilename(); + + if (!$this->config) { + if (!file_exists($filename)) { + return false; + } + + if (!is_readable($filename)) { + return false; + } + + if (!$config = include($filename)) { + return false; + } + + $this->config = $config; + } + + return $this->config; + } + + /** + * Return the nsam of the config file to load + * + * @author Phil Burton + * @return string; + */ + public function getConfigFilename() + { + return 'dipper.php'; + } + + /** + * Make sure the config is up to scratch + * + * @author Phil Burton + */ + public function validate() + { + $config = $this->getConfig(); + + foreach ($this->getRequired() as $required) { + if (!isset($config[$required])) { + throw new \Exception("Could not find " . $required . " in config file."); + } + } + } + + /** + * Clean up the config ready for use + * + * @author Phil Burton + */ + public function clean() + { + $config = $this->getConfig(); + foreach ($config as &$item) { + // Remove trailing slashes + $item = rtrim($item, "/"); + } + + $this->config = $config; + } + + /** + * Return the array of config paths + * + * @author Phil Burton + * @return string[] + */ + public function getConfigPaths() + { + return [ + realpath(dirname(__FILE__) . "/../../"), // Root of dipper + realpath(dirname(__FILE__) . "/../../config") // Config directory in dipper + ]; + } + + /** + * Return the loaded config + * + * @author Phil Burton + * @return mixed[] + */ + public function getConfig() + { + return $this->config; + } + + /** + * Return the required config keys + * + * @author Phil Burton + * @return string[] + */ + public function getRequired() + { + return [ + "SITES_ROOT", + "CONFIG_ROOT" + ]; + } +} diff --git a/src/Manager/Filesystem/CreateDirectory.php b/src/Manager/Filesystem/CreateDirectory.php new file mode 100644 index 0000000..2621169 --- /dev/null +++ b/src/Manager/Filesystem/CreateDirectory.php @@ -0,0 +1,49 @@ + + */ +class CreateDirectory implements TaskInterface +{ + /** + * Directory to create + * + * @var string + */ + protected $directory; + + /** + * Check if the directory already exists + * + * @param string $filename + * @author Phil Burton + */ + public function __construct(string $directory) + { + $this->directory = $directory; + + if (!is_writable(dirname($directory))) { + throw new \Exception('Cannot create directory at: ' . $directory); + } + + if (file_exists($directory)) { + throw new \Exception('Directory already exists at: ' . $directory); + } + } + + /** + * Create the new directory + * + * @author Phil Burton + */ + public function execute() + { + mkdir($this->directory); + } +} diff --git a/src/Manager/Filesystem/CreateFile.php b/src/Manager/Filesystem/CreateFile.php new file mode 100644 index 0000000..ce9537f --- /dev/null +++ b/src/Manager/Filesystem/CreateFile.php @@ -0,0 +1,65 @@ + + */ +class CreateFile implements TaskInterface +{ + /** + * Name of file to create + * + * @var string + */ + protected $filename; + + /** + * File contents to write + * + * @var string + */ + protected $contents; + + /** + * Check if the file already exists + * + * @param string $filename + * @author Phil Burton + */ + public function __construct(string $filename, $contents = false) + { + $this->filename = $filename; + + if ($contents) { + $this->contents = $contents; + } + + if (!is_writable(dirname($filename))) { + throw new \Exception('Cannot create file at: ' . $filename); + } + + if (file_exists($filename)) { + throw new \Exception('File already exists at: ' . $filename); + } + } + + /** + * Create the new file + * + * @author Phil Burton + */ + public function execute() + { + touch($this->filename); + + $contents = $this->contents; + if ($contents) { + file_put_contents($this->filename, $contents); + } + } +} diff --git a/src/Manager/Script/Command/Site/Create.php b/src/Manager/Script/Command/Site/Create.php new file mode 100644 index 0000000..df9fc18 --- /dev/null +++ b/src/Manager/Script/Command/Site/Create.php @@ -0,0 +1,348 @@ + + */ +class Create extends SyCommand +{ + /** + * Symfony Progress Bar for command + * + * @var Symfony\Component\Console\Helper\ProgressBar + */ + protected $progressBar; + + /** + * Array of task sets containing tasks + * + * @var mixed + */ + protected $tasks; + + /** + * Name of the deployment + * + * @var string + */ + protected $name; + + /** + * Domain name of the deployment + * + * @var string + */ + protected $domain; + + /** + * Configure the command + * + * @author Phil Burton + */ + protected function configure() + { + $this->addArgument('name', InputArgument::REQUIRED, 'The name of the new site.'); + $this->addArgument('domain', InputArgument::REQUIRED, 'The domain name of the new site.'); + + $this + ->setName('site:create') + ->setDescription('Create a new site.') + ->setHelp('This command allows you to create a new site.'); + + $this->addOption( + 'test', + 't', + InputOption::VALUE_NONE, + 'Run in test mode without any file or directory created?' + ); + + $this->addOption( + 'ignore-config', + 'c', + InputOption::VALUE_NONE, + 'Ignore writing the config?' + ); + + $this->addOption( + 'ignore-filesystem', + 'f', + InputOption::VALUE_NONE, + 'Ignore writing the deployment files and directories?' + ); + } + + /** + * Run the command + * + * @param InputInterface $input + * @param OutputInterface $output + * @author Phil Burton + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $taskCount = 9; + if ($input->getOption('ignore-filesystem')) { + $taskCount -= 8; + } + + if ($input->getOption('ignore-config')) { + $taskCount -= 1; + } + + if ($taskCount <= 0) { + echo "Nothing to do.\n"; + exit; + } + + $this->name = $input->getArgument('name'); + $this->domain = $input->getArgument('domain'); + + $output->writeln( + 'Creating new site with name: ' + . $input->getArgument('name') + . ' and domain: ' + . $input->getArgument('domain') + ); + + // Set-up progress bar + $this->progressBar = new ProgressBar($output, $taskCount); + $this->progressBar->setFormatDefinition('custom', ' %current%/%max% -- %message%'); + $this->progressBar->setFormat('custom'); + $this->progressBar->setMessage('Starting...'); + $this->progressBar->start(); + $this->progressBar->setMessage('Running pre-flight checks'); + + // Construct the file commands so we run checks first + $this->progressBar->advance(); + + try { + $this->runPreflightChecks($input); + } catch (\Exception $e) { + $this->complete($output, "Error with pre-flight checks:\n" . $e->getMessage()); + } + try { + if (!$input->getOption('test')) { + if (!$input->getOption('ignore-config')) { + $this->createConfig($input); + } + + if (!$input->getOption('ignore-filesystem')) { + $this->createFilesystem($input); + } + } + } catch (\Exception $e) { + $this->complete($output, "Unexpected Error creating files and directories:\n " . $e->getMessage()); + } + // Finish up + $this->complete($output, 'Complete', true); + } + + /** + * Show a message and end the program + * Optionally complete the progress bar + * + * @author Phil Burton + * @param OutputInterface $output + * @param string $message + * @param boolean $completeProgressBar + */ + public function complete(OutputInterface $output, string $message, $completeProgressBar = false) + { + if ($completeProgressBar) { + $this->progressBar->advance(); + $this->progressBar->finish(); + } + + $output->writeln(''); + $output->writeln($message); + exit; + } + + /** + * Run the required tasks to check we can complete the full deployment + * + * @author Phil Burton + * @param InputInterface $input + */ + public function runPreflightChecks(InputInterface $input) + { + $ignoreFilesystem = $input->getOption('ignore-filesystem'); + $ignoreConfig = $input->getOption('ignore-config'); + + if (!$ignoreFilesystem) { + $this->addTask( + 'check-filesystem', + 'Creating site directory', + new CreateDirectory( + $this->getFullSitesPath($this->name) + ) + ); + } + + if (!$ignoreConfig) { + $this->addTask( + 'check-config', + 'Creating nginx config file', + new CreateFile( + CONFIG_ROOT . '/' . $this->name . '.conf', + include_once(DIPPER_ROOT . '/nginx/stub.php') + ) + ); + } + } + + /** + * Run the creation of the config tasks + * + * @author Phil Burton + */ + public function createConfig() + { + $this->runTaskSet('check-config'); + } + + /** + * Run the creatuion of the filesystem tasks + * + * @author Phil Burton + * @param InputInterface $input + */ + public function createFilesystem(InputInterface $input) + { + $this->runTaskSet('check-filesystem'); + + $this->runTask( + 'Creating logs directory', + new CreateDirectory( + $this->getFullSitesPath($this->name) . '/logs' + ), + true + ); + + $this->runTask( + 'Creating access log file', + new CreateFile( + $this->getFullSitesPath($this->name) . '/logs/' . $this->name . '.access_log' + ), + true + ); + + $this->runTask( + 'Creating error log file', + new CreateFile( + $this->getFullSitesPath($this->name) . '/logs/' . $this->name . '.errors_log' + ), + true + ); + + $this->runTask( + 'Creating web folder', + new CreateDirectory( + $this->getFullSitesPath($this->name) . '/web' + ), + true + ); + + $this->runTask( + 'Creating directory index.php', + new CreateFile( + $this->getFullSitesPath($this->name) . '/web/index.php' + ), + true + ); + } + + /** + * Add a task to a task set + * + * @author Phil Burton + * @param string $taskSetName + * @param string $taskName + * @param TaskInterface $task + */ + public function addTask(string $taskSetName, string $taskName, TaskInterface $task) + { + $this->taskPriority[$taskSetName][] = $taskName; + $this->tasks[$taskSetName][$taskName] = $task; + } + + /** + * Get the array of tasks from a taskset + * + * @author Phil Burton + * @param string $taskSetName + * @return TaskInterface[] + */ + public function getTaskSet(string $taskSetName) + { + $tasks = $this->tasks; + if (array_key_exists($taskSetName, $tasks)) { + return $tasks[$taskSetName]; + } + + return false; + } + + /** + * Run all the tasks in a taskset + * + * @author Phil Burton + * @param string $taskSetName] + * @param boolean $advanceProgressBar + */ + public function runTaskSet(string $taskSetName, $advanceProgressBar = false) + { + $tasks = $this->getTaskSet($taskSetName); + + if (!$tasks) { + throw new \Exception('Could not find task set `' . $taskSetName . '`'); + } + foreach ($this->taskPriority[$taskSetName] as $message) { + $task = $tasks[$message]; + // Create file in web root for execution + $this->runTask($message, $task, $advanceProgressBar); + } + } + + /** + * Return the full path of a given file + * + * @author Phil Burton + * @param string $path + * @return string + */ + protected function getFullSitesPath($path) + { + return SITES_ROOT . "/" . $path; + } + + /** + * Run a task + * + * @author Phil Burton + * @param mixed $task + * @param string $message + * @return string + */ + protected function runTask(string $message, TaskInterface $task, $advanceProgressBar = false) + { + if ($advanceProgressBar) { + $this->progressBar->advance(); + $this->progressBar->setMessage($message); + } + $task->execute(); + } +} diff --git a/src/Manager/Script/Console.php b/src/Manager/Script/Console.php new file mode 100644 index 0000000..5f5d90d --- /dev/null +++ b/src/Manager/Script/Console.php @@ -0,0 +1,24 @@ + + */ + public function __construct() + { + $application = new Application(); + $application->add(new Create()); + $application->run(); + } +} diff --git a/src/Manager/Task/TaskInterface.php b/src/Manager/Task/TaskInterface.php new file mode 100644 index 0000000..407e6c8 --- /dev/null +++ b/src/Manager/Task/TaskInterface.php @@ -0,0 +1,11 @@ + - */ -class Create extends SyCommand -{ - /** - * Symfony Progress Bar for command - * - * @var Symfony\Component\Console\Helper\ProgressBar - */ - protected $progressBar; - - /** - * Array of task sets containing tasks - * - * @var mixed - */ - protected $tasks; - - /** - * Name of the deployment - * - * @var string - */ - protected $name; - - /** - * Domain name of the deployment - * - * @var string - */ - protected $domain; - - /** - * Configure the command - * - * @author Phil Burton - */ - protected function configure() - { - $this->addArgument('name', InputArgument::REQUIRED, 'The name of the new site.'); - $this->addArgument('domain', InputArgument::REQUIRED, 'The domain name of the new site.'); - - $this - ->setName('site:create') - ->setDescription('Create a new site.') - ->setHelp('This command allows you to create a new site.'); - - $this->addOption( - 'test', - 't', - InputOption::VALUE_NONE, - 'Run in test mode without any file or directory created?' - ); - - $this->addOption( - 'ignore-config', - 'c', - InputOption::VALUE_NONE, - 'Ignore writing the config?' - ); - - $this->addOption( - 'ignore-filesystem', - 'f', - InputOption::VALUE_NONE, - 'Ignore writing the deployment files and directories?' - ); - } - - /** - * Run the command - * - * @param InputInterface $input - * @param OutputInterface $output - * @author Phil Burton - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - $taskCount = 9; - if ($input->getOption('ignore-filesystem')) { - $taskCount -= 8; - } - - if ($input->getOption('ignore-config')) { - $taskCount -= 1; - } - - if ($taskCount <= 0) { - echo "Nothing to do.\n"; - exit; - } - - $this->name = $input->getArgument('name'); - $this->domain = $input->getArgument('domain'); - - $output->writeln( - 'Creating new site with name: ' - . $input->getArgument('name') - . ' and domain: ' - . $input->getArgument('domain') - ); - - // Set-up progress bar - $this->progressBar = new ProgressBar($output, $taskCount); - $this->progressBar->setFormatDefinition('custom', ' %current%/%max% -- %message%'); - $this->progressBar->setFormat('custom'); - $this->progressBar->setMessage('Starting...'); - $this->progressBar->start(); - $this->progressBar->setMessage('Running pre-flight checks'); - - // Construct the file commands so we run checks first - $this->progressBar->advance(); - - try { - $this->runPreflightChecks($input); - } catch (\Exception $e) { - $this->complete($output, "Error with pre-flight checks:\n" . $e->getMessage()); - } - try { - if (!$input->getOption('test')) { - if (!$input->getOption('ignore-config')) { - $this->createConfig($input); - } - - if (!$input->getOption('ignore-filesystem')) { - $this->createFilesystem($input); - } - } - } catch (\Exception $e) { - $this->complete($output, "Unexpected Error creating files and directories:\n " . $e->getMessage()); - } - // Finish up - $this->complete($output, 'Complete', true); - } - - /** - * Show a message and end the program - * Optionally complete the progress bar - * - * @author Phil Burton - * @param OutputInterface $output - * @param string $message - * @param boolean $completeProgressBar - */ - public function complete(OutputInterface $output, string $message, $completeProgressBar = false) - { - if ($completeProgressBar) { - $this->progressBar->advance(); - $this->progressBar->finish(); - } - - $output->writeln(''); - $output->writeln($message); - exit; - } - - /** - * Run the required tasks to check we can complete the full deployment - * - * @author Phil Burton - * @param InputInterface $input - */ - public function runPreflightChecks(InputInterface $input) - { - $ignoreFilesystem = $input->getOption('ignore-filesystem'); - $ignoreConfig = $input->getOption('ignore-config'); - - if (!$ignoreFilesystem) { - $this->addTask( - 'check-filesystem', - 'Creating site directory', - new CreateDirectory( - $this->getFullSitesPath($this->name) - ) - ); - } - - if (!$ignoreConfig) { - $this->addTask( - 'check-config', - 'Creating nginx config file', - new CreateFile( - CONFIG_ROOT . '/' . $this->name . '.conf', - include_once(DIPPER_ROOT . '/nginx/stub.php') - ) - ); - } - } - - /** - * Run the creation of the config tasks - * - * @author Phil Burton - */ - public function createConfig() - { - $this->runTaskSet('check-config'); - } - - /** - * Run the creatuion of the filesystem tasks - * - * @author Phil Burton - * @param InputInterface $input - */ - public function createFilesystem(InputInterface $input) - { - $this->runTaskSet('check-filesystem'); - - $this->runTask( - 'Creating logs directory', - new CreateDirectory( - $this->getFullSitesPath($this->name) . '/logs' - ), - true - ); - - $this->runTask( - 'Creating access log file', - new CreateFile( - $this->getFullSitesPath($this->name) . '/logs/' . $this->name . '.access_log' - ), - true - ); - - $this->runTask( - 'Creating error log file', - new CreateFile( - $this->getFullSitesPath($this->name) . '/logs/' . $this->name . '.errors_log' - ), - true - ); - - $this->runTask( - 'Creating web folder', - new CreateDirectory( - $this->getFullSitesPath($this->name) . '/web' - ), - true - ); - - $this->runTask( - 'Creating directory index.php', - new CreateFile( - $this->getFullSitesPath($this->name) . '/web/index.php' - ), - true - ); - } - - /** - * Add a task to a task set - * - * @author Phil Burton - * @param string $taskSetName - * @param string $taskName - * @param TaskInterface $task - */ - public function addTask(string $taskSetName, string $taskName, TaskInterface $task) - { - $this->taskPriority[$taskSetName][] = $taskName; - $this->tasks[$taskSetName][$taskName] = $task; - } - - /** - * Get the array of tasks from a taskset - * - * @author Phil Burton - * @param string $taskSetName - * @return TaskInterface[] - */ - public function getTaskSet(string $taskSetName) - { - $tasks = $this->tasks; - if (array_key_exists($taskSetName, $tasks)) { - return $tasks[$taskSetName]; - } - - return false; - } - - /** - * Run all the tasks in a taskset - * - * @author Phil Burton - * @param string $taskSetName] - * @param boolean $advanceProgressBar - */ - public function runTaskSet(string $taskSetName, $advanceProgressBar = false) - { - $tasks = $this->getTaskSet($taskSetName); - - if (!$tasks) { - throw new \Exception('Could not find task set `' . $taskSetName . '`'); - } - foreach ($this->taskPriority[$taskSetName] as $message) { - $task = $tasks[$message]; - // Create file in web root for execution - $this->runTask($message, $task, $advanceProgressBar); - } - } - - /** - * Return the full path of a given file - * - * @author Phil Burton - * @param string $path - * @return string - */ - protected function getFullSitesPath($path) - { - return SITES_ROOT . "/" . $path; - } - - /** - * Run a task - * - * @author Phil Burton - * @param mixed $task - * @param string $message - * @return string - */ - protected function runTask(string $message, TaskInterface $task, $advanceProgressBar = false) - { - if ($advanceProgressBar) { - $this->progressBar->advance(); - $this->progressBar->setMessage($message); - } - $task->execute(); - } -} diff --git a/src/Script/Console.php b/src/Script/Console.php deleted file mode 100644 index 5f5d90d..0000000 --- a/src/Script/Console.php +++ /dev/null @@ -1,24 +0,0 @@ - - */ - public function __construct() - { - $application = new Application(); - $application->add(new Create()); - $application->run(); - } -} diff --git a/src/Task/TaskInterface.php b/src/Task/TaskInterface.php deleted file mode 100644 index 407e6c8..0000000 --- a/src/Task/TaskInterface.php +++ /dev/null @@ -1,11 +0,0 @@ -