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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
<?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;
use App\Task\TaskInterface;
/**
* A new Symfony command for creating a new site deployment
*
* @author Phil Burton <phil@d3r.com>
*/
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>
*/
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 <phil@d3r.com>
*/
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 <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($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);
}
}
/**
* Return the full path of a given file
*
* @author Phil Burton <phil@pgburton.com>
* @param string $path
* @return string
*/
protected function getFullSitesPath($path)
{
return SITES_ROOT . "/" . $path;
}
/**
* Run a task
*
* @author Phil Burton <phil@pgburton.com>
* @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();
}
}
|