summaryrefslogtreecommitdiff
path: root/src/Script/Command/Site/Create.php
blob: 81df87e29c81d3eb429f09102f1349025b63a7d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php

namespace App\Script\Command\Site;

use Symfony\Component\Console\Command\Command as SyCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use App\Filesystem\CreateFile;
use App\Filesystem\CreateDirectory;

/**
 * A new Symfony command for creating a new site deployment
 *
 * @author Phil Burton <phil@d3r.com>
 */
class Create extends SyCommand
{
    protected $progressBar;

    /**
     * Configure the command
     *
     * @author Phil Burton <phil@d3r.com>
     */
    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?'
        );
    }

    /**
     * Run the command
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     * @author Phil Burton <phil@d3r.com>
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $taskCount = 9;

        $name = $input->getArgument('name');
        $domain = $input->getArgument('domain');

        $output->writeln(
            'Creating new site with domain: '
                . $input->getArgument('domain')
                . ' and name: '
                . $input->getArgument('name')
        );

        // 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();

        $tasks = [];

        try {
            $tasks['Creating site directory'] = new \App\Filesystem\CreateDirectory(
                SITES_ROOT . $name
            );

            $tasks['Creating nginx config file'] = new \App\Filesystem\CreateFile(
                CONFIG_ROOT . $name . '.conf',
                include_once(DIPPER_ROOT . 'nginx/stub.php')
            );
        } catch (\Exception $e) {
            $output->writeln('');
            $output->writeln('Error with pre-flight checks');
            $output->writeln($e->getMessage());
            exit;
        }

        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(
                            SITES_ROOT . $name . '/logs'
                        ),
                        'Creating logs directory'
                    );

                    $this->runTask(
                        new \App\Filesystem\CreateFile(
                            SITES_ROOT . $name . '/logs/' . $name . '.access_log'
                        ),
                        'Creating access log file'
                    );

                    $this->runTask(
                        new \App\Filesystem\CreateFile(
                            SITES_ROOT . $name . '/logs/' . $name . '.errors_log'
                        ),
                        'Creating error log file'
                    );

                    $this->runTask(
                        new \App\Filesystem\CreateDirectory(
                            SITES_ROOT . $name . '/web'
                        ),
                        'Creating web folder'
                    );

                    $this->runTask(
                        new \App\Filesystem\CreateFile(
                            SITES_ROOT . $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);
                }
            } catch (\Exception $e) {
                $output->writeln('Unexpected Error creating files and directories');
                $output->writeln($e->getMessage());
                exit;
            }
        }

        // Finish up
        $this->progressBar->advance();
        $this->progressBar->finish();
        $output->writeln('');
        $output->writeln('Complete');
    }

    public function runTask($task, $message)
    {
        $this->progressBar->advance();
        $this->progressBar->setMessage($message);
        $task->execute();
        sleep(1);
    }
}