diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Filesystem/CreateDirectory.php | 13 | ||||
| -rw-r--r-- | src/Filesystem/CreateFile.php | 17 | ||||
| -rw-r--r-- | src/Nginx/Restart.php | 21 | ||||
| -rw-r--r-- | src/Script/Command/Site/Create.php | 317 | ||||
| -rw-r--r-- | src/Task/TaskInterface.php | 11 | 
5 files changed, 264 insertions, 115 deletions
diff --git a/src/Filesystem/CreateDirectory.php b/src/Filesystem/CreateDirectory.php index 64c5fba..2621169 100644 --- a/src/Filesystem/CreateDirectory.php +++ b/src/Filesystem/CreateDirectory.php @@ -2,21 +2,16 @@  namespace App\Filesystem; +use App\Task\TaskInterface; +  /**   * Class to create a new directory   *   * @author Phil Burton <phil@d3r.com>   */ -class CreateDirectory +class CreateDirectory implements TaskInterface  {      /** -     * Thing we're creating -     * -     * @author Phil Burton <phil@d3r.com> -     */ -    protected $thing = "directory"; - -    /**       * Directory to create       *       * @var string @@ -34,7 +29,7 @@ class CreateDirectory          $this->directory = $directory;          if (!is_writable(dirname($directory))) { -            throw new \Exception('Cannot create ' . $this->thing . ' at: ' . $directory); +            throw new \Exception('Cannot create directory at: ' . $directory);          }          if (file_exists($directory)) { diff --git a/src/Filesystem/CreateFile.php b/src/Filesystem/CreateFile.php index 93c66a6..ce9537f 100644 --- a/src/Filesystem/CreateFile.php +++ b/src/Filesystem/CreateFile.php @@ -2,22 +2,27 @@  namespace App\Filesystem; +use App\Task\TaskInterface; +  /**   * Class to create a new file   *   * @author Phil Burton <phil@d3r.com>   */ -class CreateFile +class CreateFile implements TaskInterface  {      /** -     * Thing we're creating +     * Name of file to create       * -     * @author Phil Burton <phil@d3r.com> +     * @var string       */ -    protected $thing = "file"; -      protected $filename; +    /** +     * File contents to write +     * +     * @var string +     */      protected $contents;      /** @@ -35,7 +40,7 @@ class CreateFile          }          if (!is_writable(dirname($filename))) { -            throw new \Exception('Cannot create ' . $this->thing . ' at: ' . $filename); +            throw new \Exception('Cannot create file at: ' . $filename);          }          if (file_exists($filename)) { diff --git a/src/Nginx/Restart.php b/src/Nginx/Restart.php deleted file mode 100644 index cd150be..0000000 --- a/src/Nginx/Restart.php +++ /dev/null @@ -1,21 +0,0 @@ -<?php - -namespace App\Nginx; - -/** - * Restarts nginx - * - * @author Phil Burton <phil@d3r.com> - */ -class Restart -{ -    /** -     * Create the new file -     * -     * @author Phil Burton <phil@d3r.com> -     */ -    public function restart() -    { - -    } -} diff --git a/src/Script/Command/Site/Create.php b/src/Script/Command/Site/Create.php index e300ef7..df9fc18 100644 --- a/src/Script/Command/Site/Create.php +++ b/src/Script/Command/Site/Create.php @@ -10,6 +10,7 @@ use Symfony\Component\Console\Output\OutputInterface;  use Symfony\Component\Console\Helper\ProgressBar;  use App\Filesystem\CreateFile;  use App\Filesystem\CreateDirectory; +use App\Task\TaskInterface;  /**   * A new Symfony command for creating a new site deployment @@ -18,9 +19,35 @@ use App\Filesystem\CreateDirectory;   */  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 <phil@d3r.com> @@ -41,6 +68,20 @@ class Create extends SyCommand              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?' +        );      }      /** @@ -53,9 +94,21 @@ class Create extends SyCommand      protected function execute(InputInterface $input, OutputInterface $output)      {          $taskCount = 9; +        if ($input->getOption('ignore-filesystem')) { +            $taskCount -= 8; +        } -        $name = $input->getArgument('name'); -        $domain = $input->getArgument('domain'); +        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: ' @@ -75,88 +128,193 @@ class Create extends SyCommand          // Construct the file commands so we run checks first          $this->progressBar->advance(); -        $tasks = []; -          try { -            $tasks['Creating site directory'] = new \App\Filesystem\CreateDirectory( -                $this->getFullSitesPath($name) -            ); - -            $tasks['Creating nginx config file'] = new \App\Filesystem\CreateFile( -                CONFIG_ROOT . $name . '.conf', -                include_once(DIPPER_ROOT . '/nginx/stub.php') -            ); +            $this->runPreflightChecks($input);          } catch (\Exception $e) { -            $output->writeln(''); -            $output->writeln('Error with pre-flight checks'); -            $output->writeln($e->getMessage()); -            exit; +            $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("test")) { -            foreach ($tasks as $message => $task) { -                // Create file in web root for execution -                $this->runTask($task, $message); -            } - -            try { -                $tasks = []; - -                    $this->runTask( -                        new \App\Filesystem\CreateDirectory( -                            $this->getFullSitesPath($name) . '/logs' -                        ), -                        'Creating logs directory' -                    ); - -                    $this->runTask( -                        new \App\Filesystem\CreateFile( -                            $this->getFullSitesPath($name) . '/logs/' . $name . '.access_log' -                        ), -                        'Creating access log file' -                    ); - -                    $this->runTask( -                        new \App\Filesystem\CreateFile( -                            $this->getFullSitesPath($name) . '/logs/' . $name . '.errors_log' -                        ), -                        'Creating error log file' -                    ); - -                    $this->runTask( -                        new \App\Filesystem\CreateDirectory( -                            $this->getFullSitesPath($name) . '/web' -                        ), -                        'Creating web folder' -                    ); - -                    $this->runTask( -                        new \App\Filesystem\CreateFile( -                            $this->getFullSitesPath($name) . '/web/index.php' -                        ), -                        'Creating directory index.php' -                    ); - - -                foreach ($tasks as $task) { -                    // Create file in web root for execution -                    $this->progressBar->advance(); -                    $this->progressBar->setMessage($task[0]); -                    $task[1]->execute(); -                    sleep(1); +                if (!$input->getOption('ignore-filesystem')) { +                    $this->createFilesystem($input);                  } -            } catch (\Exception $e) { -                $output->writeln('Unexpected Error creating files and directories'); -                $output->writeln($e->getMessage()); -                exit;              } +        } catch (\Exception $e) { +            $this->complete($output, "Unexpected Error creating files and directories:\n " . $e->getMessage());          } -          // Finish up -        $this->progressBar->advance(); -        $this->progressBar->finish(); +        $this->complete($output, 'Complete', true); +    } + +    /** +     * Show a message and end the program +     * Optionally complete the progress bar +     * +     * @author Phil Burton <phil@pgburton.com> +     * @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('Complete'); +        $output->writeln($message); +        exit; +    } + +    /** +     * Run the required tasks to check we can complete the full deployment +     * +     * @author Phil Burton <phil@pgburton.com> +     * @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 <phil@pgburton.com> +     */ +    public function createConfig() +    { +        $this->runTaskSet('check-config'); +    } + +    /** +     * Run the creatuion of the filesystem tasks +     * +     * @author Phil Burton <phil@pgburton.com> +     * @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 <phil@pgburton.com> +     * @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 <phil@pgburton.com> +     * @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 <phil@pgburton.com> +     * @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); +        }      }      /** @@ -179,11 +337,12 @@ class Create extends SyCommand       * @param string $message       * @return string       */ -    protected function runTask($task, $message) +    protected function runTask(string $message, TaskInterface $task, $advanceProgressBar = false)      { -        $this->progressBar->advance(); -        $this->progressBar->setMessage($message); +        if ($advanceProgressBar) { +            $this->progressBar->advance(); +            $this->progressBar->setMessage($message); +        }          $task->execute(); -        sleep(1);      }  } diff --git a/src/Task/TaskInterface.php b/src/Task/TaskInterface.php new file mode 100644 index 0000000..407e6c8 --- /dev/null +++ b/src/Task/TaskInterface.php @@ -0,0 +1,11 @@ +<?php + +namespace App\Task; + +/** + * A simple task object that can be executed + */ +interface TaskInterface +{ +    public function execute(); +}  | 
