|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Dedipanel project |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2010-2015 Dedipanel <http://www.dedicated-panel.net> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace DP\Core\CoreBundle\Model; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
15
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
|
16
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata; |
|
17
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
|
18
|
|
|
|
|
19
|
|
|
abstract class AbstractServer implements ServerInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var integer $installationStatus |
|
23
|
|
|
* |
|
24
|
|
|
* @ORM\Column(name="installation_status", type="integer", nullable=true) |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $installationStatus; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var boolean $alreadyInstalled Used by the creation process |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $alreadyInstalled; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array $core |
|
35
|
|
|
* |
|
36
|
|
|
* @ORM\Column(name="core", type="simple_array", nullable=true) |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $core = array(); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string $dir |
|
42
|
|
|
* |
|
43
|
|
|
* @ORM\Column(name="dir", type="string", length=64) |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $dir; |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** {@inheritdoc} */ |
|
49
|
|
|
abstract public function getName(); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Set whether is already already installed |
|
53
|
|
|
* |
|
54
|
|
|
* @param boolean $alreadyInstalled |
|
55
|
|
|
* |
|
56
|
|
|
* @return AbstractServer |
|
57
|
|
|
*/ |
|
58
|
|
|
public function setAlreadyInstalled($alreadyInstalled) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->alreadyInstalled = $alreadyInstalled; |
|
61
|
|
|
|
|
62
|
|
|
if ($alreadyInstalled) { |
|
63
|
|
|
$this->installationStatus = 100; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Is already installed ? (from form) |
|
71
|
|
|
* |
|
72
|
|
|
* @return boolean |
|
73
|
|
|
*/ |
|
74
|
|
|
public function isAlreadyInstalled() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->alreadyInstalled || $this->isInstallationEnded(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setInstallationStatus($installationStatus) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->installationStatus = $installationStatus; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getInstallationStatus() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->installationStatus; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
|
|
public function isInstallationEnded() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->installationStatus >= 101; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritdoc} |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getMachine() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->machine; |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritdoc} |
|
113
|
|
|
*/ |
|
114
|
|
|
public function deleteServer() |
|
115
|
|
|
{ |
|
116
|
|
|
$this->changeState('stop'); |
|
117
|
|
|
|
|
118
|
|
|
return $this->deleteInstallDir(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function deleteInstallDir() |
|
122
|
|
|
{ |
|
123
|
|
|
$conn = $this->getMachine()->getConnection(); |
|
124
|
|
|
$installDir = $this->getAbsoluteDir(); |
|
125
|
|
|
|
|
126
|
|
|
if ($conn->dirExists($installDir)) { |
|
127
|
|
|
return (bool) $conn->getSFTP()->delete($installDir); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return true; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Try to retrieve the most recent percentage in $installLog |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $installLog Will seperate log by lines |
|
137
|
|
|
* @return null|string |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function getPercentFromInstallLog($installLog) |
|
140
|
|
|
{ |
|
141
|
|
|
// On recherche dans chaque ligne en commencant par la fin |
|
142
|
|
|
// Un signe "%" afin de connaître le % le plus à jour |
|
143
|
|
|
$lines = array_reverse(explode("\n", $installLog)); |
|
144
|
|
|
|
|
145
|
|
|
foreach ($lines AS $line) { |
|
146
|
|
|
$percentPos = strpos($line, '%'); |
|
147
|
|
|
|
|
148
|
|
|
if ($percentPos !== false) { |
|
149
|
|
|
$line = substr($line, 0, $percentPos); |
|
150
|
|
|
$spacePos = strrpos($line, ' ')+1; |
|
151
|
|
|
|
|
152
|
|
|
return substr($line, $spacePos); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return null; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** {@inheritdoc} */ |
|
160
|
|
|
public function getFullName() |
|
161
|
|
|
{ |
|
162
|
|
|
return '[DediPanel] ' . $this->getName(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @var array $core |
|
167
|
|
|
* @return array |
|
168
|
|
|
*/ |
|
169
|
|
|
public function setCore(array $core = array()) |
|
170
|
|
|
{ |
|
171
|
|
|
$this->core = $core; |
|
172
|
|
|
|
|
173
|
|
|
return $core; |
|
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** {@inheritdoc} */ |
|
177
|
|
|
public function getCore() |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->core; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** {@inheritdoc} */ |
|
183
|
|
|
public function setDir($dir) |
|
184
|
|
|
{ |
|
185
|
|
|
$this->dir = trim($dir, '/ '); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** {@inheritdoc} */ |
|
189
|
|
|
public function getDir() |
|
190
|
|
|
{ |
|
191
|
|
|
return $this->dir; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Get absolute path of server installation directory |
|
196
|
|
|
* |
|
197
|
|
|
* @return string |
|
198
|
|
|
*/ |
|
199
|
|
|
public function getAbsoluteDir() |
|
200
|
|
|
{ |
|
201
|
|
|
return rtrim($this->getMachine()->getHome(), '/') . '/' . $this->getDir() . '/'; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public static function loadValidatorMetadata(ClassMetadata $metadata) |
|
205
|
|
|
{ |
|
206
|
|
|
$metadata->addConstraint(new Assert\Callback('validateServer')); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
public function validateServer(ExecutionContextInterface $context) |
|
210
|
|
|
{ |
|
211
|
|
|
if ($this->getMachine() !== null && $this->getMachine()->getConnection() !== null) { |
|
212
|
|
|
$relDir = $this->getDir(); |
|
213
|
|
|
$absDir = $this->getAbsoluteDir(); |
|
214
|
|
|
|
|
215
|
|
|
if (!$this->getMachine()->getConnection()->testSSHConnection()) { |
|
216
|
|
|
$context->buildViolation('gameServer.assert.machine_unavailable') |
|
217
|
|
|
->atPath('machine') |
|
218
|
|
|
->addViolation(); |
|
219
|
|
|
} |
|
220
|
|
View Code Duplication |
elseif (!$this->isAlreadyInstalled() && !empty($relDir) |
|
|
|
|
|
|
221
|
|
|
&& $this->getMachine()->getConnection()->dirExists($absDir)) { |
|
222
|
|
|
$context->buildViolation('gameServer.assert.directory_exists') |
|
223
|
|
|
->atPath('dir') |
|
224
|
|
|
->addViolation(); |
|
225
|
|
|
} |
|
226
|
|
View Code Duplication |
elseif ($this->isAlreadyInstalled() && !empty($relDir) |
|
|
|
|
|
|
227
|
|
|
&& !$this->getMachine()->getConnection()->dirExists($absDir)) { |
|
228
|
|
|
$context->buildViolation('gameServer.assert.directory_not_exists') |
|
229
|
|
|
->atPath('dir') |
|
230
|
|
|
->addViolation(); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: