|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AppBuilder\Application\Module\TaskManager\Task; |
|
6
|
|
|
|
|
7
|
|
|
use AppBuilder\Application\Configuration\ValueObject\Parameters; |
|
8
|
|
|
use AppBuilder\Application\Model\ValueObject\Ticket; |
|
9
|
|
|
use AppBuilder\Application\Utils\FileManager\FileManagerService; |
|
10
|
|
|
use FileNotFoundException; |
|
11
|
|
|
use Nette\DirectoryNotFoundException; |
|
12
|
|
|
use Symfony\Component\Filesystem\Exception\IOException; |
|
13
|
|
|
|
|
14
|
|
|
class CreateTicketDirTask implements Task |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var Ticket */ |
|
17
|
|
|
private $ticket; |
|
18
|
|
|
|
|
19
|
|
|
/** @var Parameters */ |
|
20
|
|
|
private $applicationParams; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
private $path; |
|
24
|
|
|
|
|
25
|
|
|
/** @var FileManagerService */ |
|
26
|
|
|
private $fileManager; |
|
27
|
|
|
|
|
28
|
3 |
|
public function __construct(Ticket $ticket, Parameters $applicationParams, FileManagerService $fileManager) |
|
29
|
|
|
{ |
|
30
|
3 |
|
$this->ticket = $ticket; |
|
31
|
3 |
|
$this->applicationParams = $applicationParams; |
|
32
|
3 |
|
$this->path = $applicationParams->path($ticket->key()); |
|
33
|
3 |
|
$this->fileManager = $fileManager; |
|
34
|
3 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Creates directory for application, deploys it and installs symlinks. |
|
38
|
|
|
* |
|
39
|
|
|
* @throws FileNotFoundException |
|
40
|
|
|
* @throws DirectoryNotFoundException |
|
41
|
|
|
* @throws IOException |
|
42
|
|
|
*/ |
|
43
|
3 |
|
public function execute() : bool |
|
44
|
|
|
{ |
|
45
|
3 |
|
return $this->createDirectory() |
|
46
|
2 |
|
&& $this->executeGitClone() |
|
47
|
1 |
|
&& $this->createSnapshot() |
|
48
|
1 |
|
&& $this->executeComposerInstall() |
|
49
|
1 |
|
&& $this->installSymlinks(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Creates directory for application |
|
54
|
|
|
* |
|
55
|
|
|
* @throws FileNotFoundException |
|
56
|
|
|
*/ |
|
57
|
3 |
|
private function createDirectory() : bool |
|
58
|
|
|
{ |
|
59
|
3 |
|
if ($this->fileManager->createDir($this->path . $this->applicationParams->projectsHomeDir())) { |
|
60
|
1 |
|
throw new FileNotFoundException( |
|
61
|
|
|
'Could not create directory for ' |
|
62
|
1 |
|
. $this->ticket->key() |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
2 |
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Runs git clone command in application directory. Returns true when process is finished. |
|
71
|
|
|
* Uses SSH key for authorization. |
|
72
|
|
|
* |
|
73
|
|
|
* @throws DirectoryNotFoundException |
|
74
|
|
|
*/ |
|
75
|
2 |
|
private function executeGitClone() : bool |
|
76
|
|
|
{ |
|
77
|
2 |
|
$this->fileManager->changeDir($this->path); |
|
78
|
1 |
|
exec($this->combineCloneCommand()); |
|
79
|
|
|
|
|
80
|
1 |
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns git clone bash command combined for particular application. |
|
85
|
|
|
*/ |
|
86
|
1 |
|
private function combineCloneCommand() : string |
|
87
|
|
|
{ |
|
88
|
|
|
return 'git clone ' |
|
89
|
1 |
|
. $this->applicationParams->bitbucketRepositoryHost() |
|
90
|
1 |
|
. $this->ticket->repository() |
|
91
|
1 |
|
. '.git -b ' |
|
92
|
1 |
|
. $this->ticket->branch() |
|
93
|
1 |
|
. ' ' |
|
94
|
1 |
|
. $this->path; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Creates file with tickets current status and timestamp inside ticket dir. |
|
99
|
|
|
*/ |
|
100
|
1 |
|
private function createSnapshot() : bool |
|
101
|
|
|
{ |
|
102
|
1 |
|
$snapshotPath = $this->applicationParams->snapshotPath($this->ticket->key()); |
|
103
|
1 |
|
$snap = $this->ticket->ticketStatus() . ';' . date('Y-m-d h:m:s'); |
|
104
|
1 |
|
$this->fileManager->filePutContent($snapshotPath, $snap); |
|
105
|
|
|
|
|
106
|
1 |
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Runs composer install command in ticket directory. |
|
111
|
|
|
* Returns true when process is finished. |
|
112
|
|
|
*/ |
|
113
|
1 |
|
private function executeComposerInstall() : bool |
|
114
|
|
|
{ |
|
115
|
1 |
|
exec('composer install --working-dir=' . $this->path); |
|
116
|
|
|
|
|
117
|
1 |
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Installs symlinks for application. |
|
122
|
|
|
*/ |
|
123
|
1 |
|
private function installSymlinks() : bool |
|
124
|
|
|
{ |
|
125
|
1 |
|
$ticketKey = $this->ticket->key(); |
|
126
|
|
|
|
|
127
|
1 |
|
$this->fileManager->createSymlink( |
|
128
|
1 |
|
$this->applicationParams->symlinkTarget($ticketKey), |
|
129
|
1 |
|
$this->applicationParams->symlinkSource($ticketKey) |
|
130
|
|
|
); |
|
131
|
|
|
|
|
132
|
1 |
|
return true; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|