1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Server module for HiPanel |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/hipanel-module-server |
7
|
|
|
* @package hipanel-module-server |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hipanel\modules\server\models; |
13
|
|
|
|
14
|
|
|
use hipanel\modules\hosting\models\Ip; |
15
|
|
|
use hipanel\modules\server\helpers\ServerHelper; |
16
|
|
|
use hipanel\validators\EidValidator; |
17
|
|
|
use hipanel\validators\RefValidator; |
18
|
|
|
use Yii; |
19
|
|
|
use yii\base\NotSupportedException; |
20
|
|
|
|
21
|
|
|
class Server extends \hipanel\base\Model |
22
|
|
|
{ |
23
|
|
|
use \hipanel\base\ModelTrait; |
24
|
|
|
|
25
|
|
|
const STATE_OK = 'ok'; |
26
|
|
|
const STATE_DISABLED = 'disabled'; |
27
|
|
|
const STATE_BLOCKED = 'blocked'; |
28
|
|
|
const STATE_DELETED = 'deleted'; |
29
|
|
|
|
30
|
|
|
public function rules() |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
[['id', 'tariff_id', 'client_id', 'seller_id'], 'integer'], |
34
|
|
|
[['osimage'], EidValidator::class], |
35
|
|
|
[['panel'], RefValidator::class], |
36
|
|
|
[ |
37
|
|
|
[ |
38
|
|
|
'name', |
39
|
|
|
'dc', |
40
|
|
|
'seller', |
41
|
|
|
'client', |
42
|
|
|
'panel', |
43
|
|
|
'parent_tariff', |
44
|
|
|
'tariff', |
45
|
|
|
'tariff_note', |
46
|
|
|
'discounts', |
47
|
|
|
'request_state', |
48
|
|
|
'request_state_label', |
49
|
|
|
'state_label', |
50
|
|
|
'autorenewal', |
51
|
|
|
'state', |
52
|
|
|
'type', |
53
|
|
|
'block_reason_label', |
54
|
|
|
'ip', |
55
|
|
|
'os', |
56
|
|
|
'rcp', |
57
|
|
|
'vnc', |
58
|
|
|
'statuses', |
59
|
|
|
'running_task', |
60
|
|
|
'note', |
61
|
|
|
'label', |
62
|
|
|
'hwsummary', |
63
|
|
|
], |
64
|
|
|
'safe', |
65
|
|
|
], |
66
|
|
|
[['switches', 'rack'], 'safe'], |
67
|
|
|
[['last_expires', 'expires', 'status_time', 'sale_time'], 'date'], |
68
|
|
|
[ |
69
|
|
|
['state'], |
70
|
|
|
'isOperable', |
71
|
|
|
'on' => [ |
72
|
|
|
'reinstall', |
73
|
|
|
'reboot', |
74
|
|
|
'reset', |
75
|
|
|
'shutdown', |
76
|
|
|
'power-off', |
77
|
|
|
'power-on', |
78
|
|
|
'boot-live', |
79
|
|
|
'regen-root-password', |
80
|
|
|
'reset-password', |
81
|
|
|
], |
82
|
|
|
], |
83
|
|
|
[ |
84
|
|
|
['id'], |
85
|
|
|
'required', |
86
|
|
|
'on' => [ |
87
|
|
|
'refuse', 'delete', 'enable-autorenewal', |
88
|
|
|
'enable-vnc', 'set-note', 'set-label', |
89
|
|
|
'enable-block', 'disable-block', |
90
|
|
|
], |
91
|
|
|
], |
92
|
|
|
[ |
93
|
|
|
['id'], |
94
|
|
|
'integer', |
95
|
|
|
'on' => [ |
96
|
|
|
'refuse', 'delete', 'enable-autorenewal', |
97
|
|
|
'enable-vnc', 'set-note', 'set-label', |
98
|
|
|
'enable-block', 'disable-block', |
99
|
|
|
], |
100
|
|
|
], |
101
|
|
|
[['id', 'osimage', 'panel'], 'required', 'on' => ['reinstall']], |
102
|
|
|
[['id', 'osimage'], 'required', 'on' => ['boot-live']], |
103
|
|
|
[['type', 'comment'], 'required', 'on' => ['enable-block']], |
104
|
|
|
[['comment'], 'safe', 'on' => ['disable-block']], |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Determine good server states. |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
public function goodStates() |
114
|
|
|
{ |
115
|
|
|
return [static::STATE_OK, static::STATE_DISABLED]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function isOperable() |
122
|
|
|
{ |
123
|
|
|
if ($this->running_task || !in_array($this->state, $this->goodStates(), true)) { |
|
|
|
|
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Checks whether server supports VNC. |
131
|
|
|
* |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
public function isVNCSupported() |
135
|
|
|
{ |
136
|
|
|
return $this->type !== 'ovds'; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Checks whether server supports root password change. |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function isPwChangeSupported() |
145
|
|
|
{ |
146
|
|
|
return $this->type === 'ovds'; |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Checks whether server supports LiveCD. |
151
|
|
|
* |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
|
|
public function isLiveCDSupported() |
155
|
|
|
{ |
156
|
|
|
return $this->type !== 'ovds'; |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function getIsBlocked() |
160
|
|
|
{ |
161
|
|
|
return $this->state === static::STATE_BLOCKED; |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Checks whether server can be operated not. |
166
|
|
|
* |
167
|
|
|
* @throws NotSupportedException |
168
|
|
|
* @return bool |
169
|
|
|
* @see isOperable() |
170
|
|
|
*/ |
171
|
|
|
public function checkOperable() |
172
|
|
|
{ |
173
|
|
|
if (!$this->isOperable()) { |
174
|
|
|
throw new NotSupportedException(Yii::t('hipanel/server', 'Server already has a running task. Can not start new.')); |
175
|
|
|
} |
176
|
|
|
return true; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
|
|
public function scenarioCommands() |
183
|
|
|
{ |
184
|
|
|
return [ |
185
|
|
|
'reinstall' => 'resetup', |
186
|
|
|
'reset-password' => 'regenRootPassword', |
187
|
|
|
]; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* During 5 days after the last expiration client is able to refuse server with full refund. |
192
|
|
|
* Method checks, whether 5 days passed. |
193
|
|
|
* @return bool |
194
|
|
|
*/ |
195
|
|
|
public function canFullRefuse() |
196
|
|
|
{ |
197
|
|
|
return (time() - Yii::$app->formatter->asTimestamp($this->last_expires)) / 3600 / 24 < 5; |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function groupUsesForCharts() |
201
|
|
|
{ |
202
|
|
|
return ServerHelper::groupUsesForChart($this->uses); |
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function getUses() |
206
|
|
|
{ |
207
|
|
|
return $this->hasMany(ServerUse::class, ['object_id' => 'id']); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function getIps() |
211
|
|
|
{ |
212
|
|
|
return $this->hasMany(Ip::class, ['device_id' => 'id'])->join('links'); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* {@inheritdoc} |
217
|
|
|
*/ |
218
|
|
|
public function attributeLabels() |
219
|
|
|
{ |
220
|
|
|
return $this->mergeAttributeLabels([ |
221
|
|
|
'remoteid' => Yii::t('hipanel/server', 'Remote ID'), |
222
|
|
|
'name_like' => Yii::t('hipanel/server', 'Name'), |
223
|
|
|
'name' => Yii::t('hipanel/server', 'Name'), |
224
|
|
|
'dc' => Yii::t('hipanel/server', 'DC'), |
225
|
|
|
'rack' => Yii::t('hipanel/server', 'Rack'), |
226
|
|
|
'status_time' => Yii::t('hipanel/server', 'Status update time'), |
227
|
|
|
'block_reason_label' => Yii::t('hipanel/server', 'Block reason label'), |
228
|
|
|
'request_state_label' => Yii::t('hipanel/server', 'Request state label'), |
229
|
|
|
'ips' => Yii::t('hipanel/server', 'IP addresses'), |
230
|
|
|
'label' => Yii::t('hipanel/server', 'Internal note'), |
231
|
|
|
'os' => Yii::t('hipanel/server', 'OS'), |
232
|
|
|
'comment' => Yii::t('hipanel/server', 'Comment'), |
233
|
|
|
]); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.