1
|
|
|
<?php |
2
|
|
|
namespace Kunstmaan\Skylab\Skeleton; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Finder\Finder; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* MySQLSkeleton |
8
|
|
|
*/ |
9
|
|
|
class MySQLSkeleton extends AbstractSkeleton |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
const NAME = "mysql"; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @return string |
16
|
|
|
*/ |
17
|
|
|
public function getName() |
18
|
|
|
{ |
19
|
|
|
return self::NAME; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param \ArrayObject $project |
24
|
|
|
* |
25
|
|
|
* @return mixed |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
public function create(\ArrayObject $project) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$project["mysqluser"] = $this->dialogProvider->askFor("Enter a MySQL username", null, $project["name"]); |
30
|
|
|
$pwgen = new \PWGen(); |
31
|
|
|
$project["mysqlpass"] = $this->dialogProvider->askFor("Enter a MySQL password", null, $pwgen->generate()); |
32
|
|
|
$project["mysqldbname"] = $this->dialogProvider->askFor("Enter a MySQL databasename", null, $project["name"]); |
33
|
|
|
$project["mysqlserver"] = $this->dialogProvider->askFor("Enter a MySQL server host", null, "localhost"); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
public function preMaintenance() |
40
|
|
|
{ |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
|
|
public function postMaintenance() |
47
|
|
|
{ |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \ArrayObject $project |
52
|
|
|
* |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
public function maintenance(\ArrayObject $project) |
56
|
|
|
{ |
57
|
|
|
if (!isset($project["mysqlserver"]) || !isset($project["mysqldbname"]) || !isset($project["mysqluser"]) || !isset($project["mysqlpass"])){ |
58
|
|
|
$this->dialogProvider->logNotice("Required MySQL configuration is missing"); |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
try { |
62
|
|
|
new \PDO('mysql:host=' . $project["mysqlserver"] . ';dbname=' . $project["mysqldbname"], $project["mysqluser"], $project["mysqlpass"]); |
63
|
|
|
} catch (\PDOException $exLoginTest) { |
64
|
|
|
$this->dialogProvider->logNotice("Cannot connect as " . $project["mysqluser"] . ", lets test if the database exists (" . $exLoginTest->getMessage() . ")"); |
65
|
|
|
try { |
66
|
|
|
new \PDO('mysql:host=' . $project["mysqlserver"] . ';dbname=' . $project["mysqldbname"], $this->app["config"]["mysql"]["user"], $this->app["config"]["mysql"]["password"]); |
67
|
|
|
$this->dialogProvider->logNotice("Database " . $project["mysqldbname"] . " exists!"); |
68
|
|
|
} catch (\PDOException $exDBTest) { |
69
|
|
|
$this->dialogProvider->logNotice("Cannot connect to the " . $project["mysqldbname"] . " database as ".$this->app["config"]["mysql"]["user"]." as well, lets create it. (" . $exDBTest->getMessage() . ")"); |
70
|
|
|
$backupDir = $this->fileSystemProvider->getProjectDirectory($project["name"]) . "/backup/"; |
71
|
|
|
$pdo = new \PDO('mysql:host=' . $project["mysqlserver"] . ";", $this->app["config"]["mysql"]["user"], $this->app["config"]["mysql"]["password"]); |
72
|
|
|
$pdo->exec($this->dialogProvider->logQuery("create database " . $project["mysqldbname"] . " DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci")); |
73
|
|
|
$finder = new Finder(); |
74
|
|
|
$finder->files()->in($backupDir)->name("mysql.dmp.gz"); |
75
|
|
|
if (count(iterator_to_array($finder)) > 0) { |
76
|
|
|
$this->processProvider->executeCommand('gzip -dc ' . $backupDir . '/mysql.dmp.gz | mysql -h ' . $project["mysqlserver"] . ' -u root -p' . $this->app["config"]["mysql"]["password"] . ' ' . $project["mysqldbname"]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
$pdo = new \PDO('mysql:host=' . $project["mysqlserver"] . ";", $this->app["config"]["mysql"]["user"], $this->app["config"]["mysql"]["password"]); |
81
|
|
|
$pdo->exec($this->dialogProvider->logQuery("GRANT ALL PRIVILEGES ON " . $project["mysqldbname"] . ".* TO " . $project["mysqluser"] . "@localhost IDENTIFIED BY '" . $project["mysqlpass"] . "'")); |
82
|
|
|
$pdo->exec($this->dialogProvider->logQuery("GRANT ALL PRIVILEGES ON " . $project["mysqldbname"] . ".* TO " . $project["mysqluser"] . "@'%%' IDENTIFIED BY '" . $project["mysqlpass"] . "'")); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param \ArrayObject $project |
88
|
|
|
* |
89
|
|
|
* @return mixed |
90
|
|
|
*/ |
91
|
|
|
public function preBackup(\ArrayObject $project) |
92
|
|
|
{ |
93
|
|
|
$backupDir = $this->fileSystemProvider->getProjectDirectory($project["name"]) . "/backup/"; |
94
|
|
|
$this->fileSystemProvider->createDirectory($project, "backup"); |
95
|
|
|
$this->processProvider->executeSudoCommand('rm -f ' . $backupDir . '/mysql.dmp'); |
96
|
|
View Code Duplication |
if (is_file($backupDir . '/mysql.dmp.gz')) { |
|
|
|
|
97
|
|
|
$this->processProvider->executeSudoCommand('rm -f ' . $backupDir . '/mysql.dmp.previous.gz'); |
98
|
|
|
$this->processProvider->executeSudoCommand('mv ' . $backupDir . '/mysql.dmp.gz ' . $backupDir . '/mysql.dmp.previous.gz'); |
99
|
|
|
} |
100
|
|
|
$this->processProvider->executeSudoCommand("touch " . $backupDir . "/mysql.dmp"); |
101
|
|
|
$this->processProvider->executeSudoCommand('chmod -R 755 ' . $backupDir . "/mysql.dmp"); |
102
|
|
|
$this->fileSystemProvider->writeProtectedFile($backupDir . "/mysql.dmp", "SET autocommit=0;\n", true); |
103
|
|
|
$this->fileSystemProvider->writeProtectedFile($backupDir . "/mysql.dmp", "SET unique_checks=0;\n", true); |
104
|
|
|
$this->fileSystemProvider->writeProtectedFile($backupDir . "/mysql.dmp", "SET foreign_key_checks=0;\n", true); |
105
|
|
|
|
106
|
|
|
$tmpfname = tempnam(sys_get_temp_dir(), "skylab"); |
107
|
|
|
$this->processProvider->executeSudoCommand("mysqldump --skip-opt --add-drop-table --add-locks --create-options --disable-keys --single-transaction --skip-extended-insert --quick --set-charset -u " . $project["mysqluser"] . " -p" . $project["mysqlpass"] . " " . $project["mysqldbname"] . " >> " . $tmpfname); |
108
|
|
|
$this->processProvider->executeSudoCommand("cat " . $tmpfname . " | sudo tee -a " . $backupDir . "/mysql.dmp"); |
109
|
|
|
$this->processProvider->executeSudoCommand("rm -f " . $tmpfname); |
110
|
|
|
|
111
|
|
|
$this->fileSystemProvider->writeProtectedFile($backupDir . "/mysql.dmp", "COMMIT;\n", true); |
112
|
|
|
$this->fileSystemProvider->writeProtectedFile($backupDir . "/mysql.dmp", "SET autocommit=1;\n", true); |
113
|
|
|
$this->fileSystemProvider->writeProtectedFile($backupDir . "/mysql.dmp", "SET unique_checks=1;\n", true); |
114
|
|
|
$this->fileSystemProvider->writeProtectedFile($backupDir . "/mysql.dmp", "SET foreign_key_checks=1;\n", true); |
115
|
|
|
$this->processProvider->executeSudoCommand("gzip " . $backupDir . "/mysql.dmp -f"); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param \ArrayObject $project |
120
|
|
|
* |
121
|
|
|
* @return mixed |
122
|
|
|
*/ |
123
|
|
|
public function postBackup(\ArrayObject $project) |
124
|
|
|
{ |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param \ArrayObject $project |
129
|
|
|
* |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
public function preRemove(\ArrayObject $project) |
133
|
|
|
{ |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param \ArrayObject $project |
138
|
|
|
* |
139
|
|
|
* @return mixed |
140
|
|
|
*/ |
141
|
|
|
public function postRemove(\ArrayObject $project) |
142
|
|
|
{ |
143
|
|
|
$pdo = new \PDO('mysql:host=' . $project["mysqlserver"] . ";", "root", $this->app["config"]["mysql"]["password"]); |
144
|
|
|
$pdo->exec($this->dialogProvider->logQuery("drop database if exists " . $project["mysqldbname"])); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param \ArrayObject $project |
149
|
|
|
* @param \SimpleXMLElement $config |
150
|
|
|
* @return \SimpleXMLElement |
151
|
|
|
*/ |
152
|
|
|
public function writeConfig(\ArrayObject $project, \SimpleXMLElement $config) |
153
|
|
|
{ |
154
|
|
|
$config = $this->projectConfigProvider->addVar($config, 'project.mysqluser', $project["mysqluser"]); |
155
|
|
|
$config = $this->projectConfigProvider->addVar($config, 'project.mysqlpass', $project["mysqlpass"]); |
156
|
|
|
$config = $this->projectConfigProvider->addVar($config, 'project.mysqldbname', $project["mysqldbname"]); |
157
|
|
|
$config = $this->projectConfigProvider->addVar($config, 'project.mysqlserver', $project["mysqlserver"]); |
158
|
|
|
|
159
|
|
|
return $config; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return string[] |
164
|
|
|
*/ |
165
|
|
|
public function dependsOn() |
166
|
|
|
{ |
167
|
|
|
return array("base"); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.