-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathService.php
665 lines (564 loc) · 20.9 KB
/
Service.php
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
<?php
/**
* FOSSBilling.
*
* @copyright FOSSBilling (https://www.fossbilling.org)
* @license Apache-2.0
*
* Copyright FOSSBilling 2022
* This software may contain code previously used in the BoxBilling project.
* Copyright BoxBilling, Inc 2011-2021
*
* This source file is subject to the Apache-2.0 License that is bundled
* with this source code in the file LICENSE
*/
namespace Box\Mod\Servicesolusvm;
use Box\InjectionAwareInterface;
class Service implements InjectionAwareInterface
{
protected $di;
public function setDi($di)
{
$this->di = $di;
}
public function getDi()
{
return $this->di;
}
public function install()
{
$sql = '
CREATE TABLE IF NOT EXISTS `service_solusvm` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`cluster_id` bigint(20) DEFAULT NULL,
`client_id` bigint(20) DEFAULT NULL,
`vserverid` varchar(255) DEFAULT NULL,
`virtid` varchar(255) DEFAULT NULL,
`nodeid` varchar(255) DEFAULT NULL,
`type` varchar(255) DEFAULT NULL,
`node` varchar(255) DEFAULT NULL,
`nodegroup` varchar(255) DEFAULT NULL,
`hostname` varchar(255) DEFAULT NULL,
`rootpassword` varchar(255) DEFAULT NULL,
`username` varchar(255) DEFAULT NULL,
`plan` varchar(255) DEFAULT NULL,
`template` varchar(255) DEFAULT NULL,
`ips` varchar(255) DEFAULT NULL,
`hvmt` varchar(255) DEFAULT NULL,
`custommemory` varchar(255) DEFAULT NULL,
`customdiskspace` varchar(255) DEFAULT NULL,
`custombandwidth` varchar(255) DEFAULT NULL,
`customcpu` varchar(255) DEFAULT NULL,
`customextraip` varchar(255) DEFAULT NULL,
`issuelicense` varchar(255) DEFAULT NULL,
`mainipaddress` varchar(255) DEFAULT NULL,
`extraipaddress` varchar(255) DEFAULT NULL,
`consoleuser` varchar(255) DEFAULT NULL,
`consolepassword` varchar(255) DEFAULT NULL,
`config` text,
`created_at` varchar(35) DEFAULT NULL,
`updated_at` varchar(35) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `client_id_idx` (`client_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;';
$this->di['db']->exec($sql);
}
public function uninstall()
{
$this->di['db']->exec('DROP TABLE IF EXISTS `service_solusvm`');
}
public function getCartProductTitle($product, array $data)
{
return __('Virtual private server :title',
[
':title' => $product->title,
':template' => $data['template'],
':hostname' => $data['hostname'], ]);
}
public function validateOrderData(array $data)
{
if (!isset($data['hostname']) || empty($data['hostname'])) {
throw new \Box_Exception('Please enter VPS hostname.', null, 7101);
}
$ValidHostnameRegex = "/^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/";
if (!preg_match($ValidHostnameRegex, $data['hostname'])) {
throw new \Box_Exception('Hostname :hostname is not valid.', [':hostname' => $data['hostname']], 7102);
}
if (!isset($data['template']) || empty($data['template'])) {
throw new \Box_Exception('Please select VPS template.', null, 7103);
}
}
public function updateMasterConfig($cluster_id, $data)
{
if (!isset($data['id'])) {
throw new \Box_Exception('SolusVM API ID is missing.', null, 7201);
}
if (!isset($data['key'])) {
throw new \Box_Exception('SolusVM API KEY is missing.', null, 7202);
}
if (!isset($data['ipaddress'])) {
throw new \Box_Exception('SolusVM API ipaddress is missing.', null, 7203);
}
if (!isset($data['secure'])) {
throw new \Box_Exception('SolusVM API secure is missing.', null, 7204);
}
if (!isset($data['port'])) {
throw new \Box_Exception('SolusVM API port is missing.', null, 7204);
}
$sql = "
UPDATE extension_meta
SET meta_value = :config
WHERE rel_type = 'cluster'
AND extension = 'mod_servicesolusvm'
AND rel_id = :cluster_id
AND meta_key = 'config'
";
$c = [
'id' => $data['id'],
'key' => $data['key'],
'ipaddress' => $data['ipaddress'],
'secure' => $data['secure'],
'port' => $data['port'],
'usertype' => 'admin',
];
$params = [
'config' => json_encode($c),
'cluster_id' => $cluster_id,
];
$this->di['db']->exec($sql, $params);
}
/**
* @param int $cluster_id
*/
public function getMasterConfig($cluster_id)
{
$sql = "
SELECT meta_value
FROM extension_meta
WHERE extension = 'mod_servicesolusvm'
AND rel_type = 'cluster'
AND rel_id = :cluster_id
AND meta_key = 'config'
";
$config = $this->di['db']->getCell($sql, ['cluster_id' => $cluster_id]);
if (!$config) {
$config = [];
$meta = $this->di['db']->dispense('extension_meta');
$meta->extension = 'mod_servicesolusvm';
$meta->rel_type = 'cluster';
$meta->rel_id = $cluster_id;
$meta->meta_key = 'config';
$meta->meta_value = json_encode($config);
$meta->created_at = date('Y-m-d H:i:s');
$meta->updated_at = date('Y-m-d H:i:s');
$this->di['db']->store($meta);
} else {
$config = json_decode($config, 1);
}
if (!isset($config['secure'])) {
$config['secure'] = null;
}
if (!isset($config['port'])) {
$config['port'] = null;
}
return $config;
}
public function setSolusUserPassword($client, $username, $password)
{
$meta = $this->di['db']->dispense('extension_meta');
$meta->extension = 'mod_servicesolusvm';
$meta->client_id = $client->id;
$meta->meta_key = 'solusvm_username';
$meta->meta_value = $username;
$meta->created_at = date('Y-m-d H:i:s');
$meta->updated_at = date('Y-m-d H:i:s');
$this->di['db']->store($meta);
$meta = $this->di['db']->dispense('extension_meta');
$meta->extension = 'mod_servicesolusvm';
$meta->client_id = $client->id;
$meta->meta_key = 'solusvm_password';
$meta->meta_value = $password;
$meta->created_at = date('Y-m-d H:i:s');
$meta->updated_at = date('Y-m-d H:i:s');
$this->di['db']->store($meta);
}
public function getSolusUserPassword($client)
{
$sql = "
SELECT meta_value
FROM extension_meta
WHERE client_id = :cid
AND extension = 'mod_servicesolusvm'
AND meta_key = :key
";
$username = $this->di['db']->getCell($sql, ['cid' => $client->id, 'key' => 'solusvm_username']);
$password = $this->di['db']->getCell($sql, ['cid' => $client->id, 'key' => 'solusvm_password']);
if (!$username) {
$username = $client->email;
$password = $this->di['tools']->generatePassword(8, 3);
$this->setSolusUserPassword($client, $username, $password);
}
return [$username, $password];
}
/**
* @return void
*/
public function create($order)
{
$c = json_decode($order->config, 1);
$this->validateOrderData($c);
$model = $this->di['db']->dispense('service_solusvm');
$model->client_id = $order->client_id;
$model->hostname = strtolower($c['hostname']);
$model->template = $c['template'];
$model->created_at = date('Y-m-d H:i:s');
$model->updated_at = date('Y-m-d H:i:s');
$this->di['db']->store($model);
return $model;
}
/**
* @return void
*/
public function activate($order, $model)
{
if (!is_object($model)) {
throw new \Box_Exception('Could not activate order. Service was not created', null, 7456);
}
$client = $this->di['db']->load('client', $order->client_id);
$product = $this->di['db']->load('product', $order->product_id);
if (!$product) {
throw new \Box_Exception('Could not activate order because ordered product does not exists', null, 7457);
}
$pconfig = json_decode($product->config, 1);
[$username, $password] = $this->getSolusUserPassword($client);
try {
$this->_getApi()->client_checkexists($username);
} catch (\Exception $exc) {
$this->_getApi()->client_create($username, $password, $client->email, $client->first_name, $client->last_name, $client->company);
}
if (!isset($pconfig['node'])) {
throw new \Box_Exception('Could not activate order. Product :id configuration is missing node parameter', [':id' => $product->id], 7458);
}
if (!isset($pconfig['plan'])) {
throw new \Box_Exception('Could not activate order. Product :id configuration is missing plan parameter', [':id' => $product->id], 7459);
}
if (!isset($pconfig['vtype'])) {
throw new \Box_Exception('Could not activate order. Product :id configuration is missing virtualization type parameter', [':id' => $product->id], 7460);
}
if (!isset($pconfig['ips'])) {
throw new \Box_Exception('Could not activate order. Product :id configuration is missing ips amount parameter', [':id' => $product->id], 7461);
}
$type = $pconfig['vtype'];
$node = $pconfig['node'];
$nodegroup = $this->di['array_get']($pconfig, 'nodegroup', null);
$template = $model->template;
$hostname = $model->hostname;
$plan = $pconfig['plan'];
$ips = $pconfig['ips'];
$hvmt = $this->di['array_get']($pconfig, 'hvmt', null);
$custommemory = $this->di['array_get']($pconfig, 'custommemory', null);
$customdiskspace = $this->di['array_get']($pconfig, 'customdiskspace', null);
$custombandwidth = $this->di['array_get']($pconfig, 'custombandwidth', null);
$customcpu = $this->di['array_get']($pconfig, 'customcpu', null);
$customextraip = $this->di['array_get']($pconfig, 'customextraip', null);
$issuelicense = $this->di['array_get']($pconfig, 'issuelicense', null);
$result = $this->_getApi()->vserver_create($type, $node, $nodegroup, $hostname, $password, $username, $plan, $template, $ips, $hvmt, $custommemory, $customdiskspace, $custombandwidth, $customcpu, $customextraip, $issuelicense);
$model->cluster_id = 1; // for future if ever FOSSBilling supports multiple master servers
$model->vserverid = $result['vserverid'];
$model->virtid = $result['virtid'];
$model->nodeid = $result['nodeid'];
$model->type = $type;
$model->plan = $plan;
$model->node = $node;
$model->nodegroup = $nodegroup;
$model->hostname = $result['hostname'];
$model->rootpassword = ''; // $result['rootpassword'];
$model->username = $username;
$model->ips = $ips;
$model->hvmt = $hvmt;
$model->consoleuser = $result['consoleuser'];
$model->consolepassword = ''; // $result['consolepassword'];
$model->custommemory = $custommemory;
$model->customdiskspace = $customdiskspace;
$model->custombandwidth = $custombandwidth;
$model->customcpu = $customcpu;
$model->customextraip = $customextraip;
$model->issuelicense = $issuelicense;
$model->mainipaddress = $result['mainipaddress'];
$model->extraipaddress = $this->di['array_get']($result, 'extraipaddress', null);
$model->updated_at = date('Y-m-d H:i:s');
$this->di['db']->store($model);
// pass params to event hook
return [
'rootpassword' => $result['rootpassword'],
'consolepassword' => $result['consolepassword'],
];
}
/**
* Suspend VPS.
*
* @return bool
*/
public function suspend($order, $model)
{
$this->_getApi()->vserver_suspend($model->vserverid);
$model->updated_at = date('Y-m-d H:i:s');
$this->di['db']->store($model);
return true;
}
/**
* @return bool
*/
public function unsuspend($order, $model)
{
$this->_getApi()->vserver_unsuspend($model->vserverid);
$model->updated_at = date('Y-m-d H:i:s');
$this->di['db']->store($model);
return true;
}
/**
* @return bool
*/
public function cancel($order, $model)
{
return $this->suspend($order, $model);
}
/**
* @return bool
*/
public function uncancel($order, $model)
{
return $this->unsuspend($order, $model);
}
/**
* @return bool
*/
public function delete($order, $model)
{
if (is_object($model)) {
$this->_getApi()->vserver_terminate($model->vserverid);
$this->di['db']->trash($model);
}
return true;
}
public function reboot($order, $model)
{
$this->_getApi()->vserver_reboot($model->vserverid);
return true;
}
public function boot($order, $model)
{
$this->_getApi()->vserver_boot($model->vserverid);
return true;
}
public function shutdown($order, $model)
{
$this->_getApi()->vserver_shutdown($model->vserverid);
return true;
}
public function status($order, $model)
{
$result = $this->_getApi()->vserver_status($model->vserverid);
return $result['statusmsg'];
}
public function info($vserverid)
{
$result = $this->_getApi()->vserver_infoall($vserverid);
return $result;
}
public function set_root_password($order, $model, $params = [])
{
if (!isset($params['password']) || strlen($params['password']) < 4) {
throw new \Box_Exception('Root password must be longer than 4 symbols', null, 8789);
}
$pass = $params['password'];
$this->_getApi()->vserver_rootpassword($model->vserverid, $pass);
$model->rootpassword = ''; // $pass;
$this->di['db']->store($model);
return true;
}
public function set_plan($order, $model, $params = [])
{
if (!isset($params['plan']) || empty($params['plan'])) {
throw new \Box_Exception('Plan name must not be empty', null, 8790);
}
$this->_getApi()->vserver_change($model->vserverid, $params['plan']);
$model->plan = $params['plan'];
$this->di['db']->store($model);
return true;
}
public function set_hostname($order, $model, $params = [])
{
if (!isset($params['hostname']) || empty($params['hostname'])) {
throw new \Box_Exception('Hostname must not be empty', null, 8791);
}
$this->_getApi()->vserver_hostname($model->vserverid, $params['hostname']);
$model->hostname = $params['hostname'];
$this->di['db']->store($model);
return true;
}
public function rebuild($order, $model, $params = [])
{
if (!isset($params['template']) || empty($params['template'])) {
throw new \Box_Exception('Template must not be empty', null, 8792);
}
$this->_getApi()->vserver_rebuild($model->vserverid, $params['template']);
$model->template = $params['template'];
$this->di['db']->store($model);
return true;
}
public function addip($order, $model, $params = [])
{
$this->_getApi()->vserver_addip($model->vserverid);
return true;
}
public function network_disable($order, $model, $params = [])
{
return $this->_getApi()->vserver_network_disable($model->vserverid);
}
public function network_enable($order, $model, $params = [])
{
return $this->_getApi()->vserver_network_enable($model->vserverid);
}
public function tun_disable($order, $model, $params = [])
{
return $this->_getApi()->vserver_tun_disable($model->vserverid);
}
public function tun_enable($order, $model, $params = [])
{
return $this->_getApi()->vserver_tun_enable($model->vserverid);
}
public function pae_disable($order, $model, $params = [])
{
return $this->_getApi()->vserver_pae($model->vserverid, 'off');
}
public function pae_enable($order, $model, $params = [])
{
return $this->_getApi()->vserver_pae($model->vserverid, 'on');
}
public function client_list()
{
return $this->_getApi()->client_list();
}
public function node_virtualservers($nodeid)
{
return $this->_getApi()->node_virtualservers($nodeid);
}
public function client_change_password($order, $model, $params = [])
{
if (!isset($params['password']) || strlen($params['password']) < 4) {
throw new \Box_Exception('Password must be longer than 4 symbols', null, 8790);
}
$this->_getApi()->client_updatepassword($model->username, $params['password']);
$sql = "
UPDATE extension_meta
SET meta_value = :pass
WHERE client_id = :cid
AND extension = 'mod_servicesolusvm'
AND meta_key = :key
";
$this->di['db']->exec($sql, ['cid' => $model->client_id, 'key' => 'solusvm_password', 'pass' => $params['password']]);
return true;
}
public function getVirtualizationTypes($data)
{
return [
'openvz' => 'OpenVz',
'xen' => 'Xen',
'xen hvm' => 'Xen HVM',
'kvm' => 'KVM',
];
}
public function getNodes($type, $by)
{
if ('id' == $by) {
$result = $this->_getApi()->node_idlist($type);
} else {
$result = $this->_getApi()->listnodes($type);
}
$list = explode(',', $result['nodes']);
$res = [];
foreach ($list as $p) {
$res[$p] = $p;
}
return $res;
}
public function getTemplates($type)
{
$templates = $this->_getApi()->listtemplates($type);
$list = explode(',', $templates['templates']);
if ('kvm' == $type && isset($templates['templateskvm'])) {
$list = explode(',', $templates['templateskvm']);
}
if ('xen hvm' == $type && isset($templates['templateshvm'])) {
$list = explode(',', $templates['templateshvm']);
}
$res = [];
foreach ($list as $p) {
$res[$p] = $p;
}
return $res;
}
public function getPlans($type = 'openvz')
{
$plans = $this->_getApi()->listplans($type);
$list = explode(',', $plans['plans']);
$res = [];
foreach ($list as $p) {
$res[$p] = $p;
}
return $res;
}
public function testConnection()
{
$this->_getApi()->listnodegroups();
return true;
}
private function _getApi()
{
if (APPLICATION_ENV == 'testing') {
return new \Vps_SolusvmMock($this->getMasterConfig(1));
}
return $this->di['solusvm']($this->getMasterConfig(1));
}
public function toApiArray($model, $deep = false, $identity = null)
{
$c = $this->getMasterConfig(1);
if ($c['secure']) {
if ($c['port']) {
$cport = $c['port'];
} else {
$cport = '5656';
}
$url = 'https://' . $c['ipaddress'] . ':' . $cport;
} else {
if ($c['port']) {
$cport = $c['port'];
} else {
$cport = '5353';
}
$url = 'http://' . $c['ipaddress'] . ':' . $cport;
}
$client = $this->di['db']->load('client', $model->client_id);
[$username, $password] = $this->getSolusUserPassword($client);
return [
'id' => $model->id,
'vserverid' => $model->vserverid,
'virtid' => $model->virtid,
'hostname' => $model->hostname,
'plan' => $model->plan,
'template' => $model->template,
'mainipaddress' => $model->mainipaddress,
'rootpassword' => $model->rootpassword,
'consoleuser' => $model->consoleuser,
'consolepassword' => $model->consolepassword,
'custommemory' => $model->custommemory,
'customdiskspace' => $model->customdiskspace,
'custombandwidth' => $model->custombandwidth,
'customcpu' => $model->customcpu,
'master_url' => $url,
'username' => $username,
'password' => $password,
];
}
}