-
Notifications
You must be signed in to change notification settings - Fork 1
/
CustomModuleProvider.php
588 lines (508 loc) · 21.1 KB
/
CustomModuleProvider.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
<?php
namespace Modules\Addons\CustomModule;
use App\Providers\ModuleServiceProvider;
use Components\Front\Core\Widgets\CASmartBoxGenerator;
use Components\Helpers\Backend\MenuBuilder;
use Components\Helpers\Backend\TabsGenerator;
use Components\Libs\Widgets\SmartBoxGenerator;
use EasyDcim\Components\Provisioning\Log\LogService;
use EasyDcim\Components\Provisioning\Modules\ProvisioningModuleInterface;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Schema;
use Modules\Addons\CustomModule\Widgets\ClientArea\CustomWidgetServiceSummary;
use Modules\Addons\CustomModule\Widgets\CustomModuleDeviceWidget;
use Modules\Addons\CustomModule\Commands\CustomModuleCommand;
use Modules\Addons\CustomModule\Commands\IpmiBmcResetCommand;
use Modules\Addons\CustomModule\Model\CustomModel;
use Modules\Addons\CustomModule\Widgets\CustomModuleDashboardWidget;
/**
* Class CustomModuleProvider
* @package Modules\Addons\CustomModule
*/
class CustomModuleProvider extends ModuleServiceProvider
{
/**
* @return void
*/
public function boot()
{
parent::boot('CustomModule');
}
/**
* @return void
*/
public function register()
{
$app = $this->app;
$app['events']->listen('common.router.after', function() use($app)
{
/**
* Backend Routes
*/
$app['router']->group(['namespace' => 'Modules\Addons\CustomModule\Controller', 'prefix' => 'backend', 'before' => 'acl', 'perm' => 'core.modules.addons.custom-module'], function() use($app)
{
$app['router']->get('/custom-module/tab1', ['as' => 'backend.custom.module.tab1', 'uses' => 'PageController@showTab1']);
$app['router']->get('/custom-module/tab1/{id}/action/enable', ['as' => 'backend.custom.module.tab1.action.enable', 'uses' => 'PageController@enableAction']);
$app['router']->get('/custom-module/tab1/{id}/action/disable', ['as' => 'backend.custom.module.tab1.action.disable', 'uses' => 'PageController@disableAction']);
$app['router']->post('/custom-module/tab1/edit/quick', ['as' => 'backend.custom.module.tab1.edit.quick', 'uses' => 'PageController@quickEdit']);
$app['router']->get('/custom-module/api', ['as' => 'backend.custom.module.tab2', 'uses' => 'PageController@apiRequest']);
$app['router']->get('/custom-module/api/get-request', ['uses' => 'PageController@getRequest']);
$app['router']->get('/custom-module/api/post-request', ['uses' => 'PageController@postRequest']);
$app['router']->get('/custom-module/devices/{id}/custom-tab', [
'as' => 'backend.custom.module.device.tab',
'uses' => 'DeviceController@showCustomTab'
]);
$app['router']->get('/custom-module/custom-model-table', ['as' => 'backend.custom.module.tab3', 'uses' => 'CustomModelController@index']);
$app['router']->get('/custom-module/custom-model-table/filter={filters}{output?}', array('as' => 'backend.custom.module.tab3.filtered', 'uses' => 'CustomModelController@index'))
->where(array('filters' => '(.*)'));
});
/**
* Test API Route
*/
$app['router']->group(['namespace' => 'Modules\Addons\CustomModule\Controller\Api\v2', 'prefix' => 'backend'], function() use($app)
{
/**
* Devices
*/
$app['router']->get('/custom-module/api/device/create', ['uses' => 'DevicesController@createDevice']);
$app['router']->get('/custom-module/api/device/boot', ['uses' => 'DevicesController@bootDevice']);
$app['router']->get('/custom-module/api/device/reboot', ['uses' => 'DevicesController@rebootDevice']);
$app['router']->get('/custom-module/api/device/shutdown', ['uses' => 'DevicesController@shutdownDevice']);
$app['router']->get('/custom-module/api/device/list', ['uses' => 'DevicesController@listDevices']);
$app['router']->get('/custom-module/api/device/show', ['uses' => 'DevicesController@showDevice']);
/**
* Orders
*/
$app['router']->get('/custom-module/api/order/create', ['uses' => 'OrdersController@createOrder']);
$app['router']->get('/custom-module/api/order/update', ['uses' => 'OrdersController@updateOrder']);
$app['router']->get('/custom-module/api/order/delete', ['uses' => 'OrdersController@deleteOrder']);
/**
* Locations
*/
$app['router']->get('/custom-module/api/location/create-location', ['uses' => 'LocationsController@createLocation']);
$app['router']->get('/custom-module/api/location/create-floor', ['uses' => 'LocationsController@createFloor']);
$app['router']->get('/custom-module/api/location/create-rack', ['uses' => 'LocationsController@createRack']);
/**
* Item Types
*/
$app['router']->get('/custom-module/api/item-type/create', ['uses' => 'ItemTypesController@createType']);
$app['router']->get('/custom-module/api/item-type/update', ['uses' => 'ItemTypesController@updateType']);
/**
* Device Bays
*/
$app['router']->get('/custom-module/api/device-bay/list', ['uses' => 'DeviceBaysController@listBays']);
$app['router']->get('/custom-module/api/device-bay/create', ['uses' => 'DeviceBaysController@createBay']);
$app['router']->get('/custom-module/api/device-bay/update', ['uses' => 'DeviceBaysController@updateBay']);
$app['router']->get('/custom-module/api/device-bay/delete', ['uses' => 'DeviceBaysController@deleteBay']);
/**
* Item Ports
*/
$app['router']->get('/custom-module/api/item-port/create', ['uses' => 'ItemPortsController@createPort']);
/**
* IP Address Management Module
*/
$app['router']->get('/custom-module/api/ipam/vlan/list', ['uses' => 'IpamController@listVlans']);
$app['router']->get('/custom-module/api/ipam/vlan/show', ['uses' => 'IpamController@showVlan']);
$app['router']->get('/custom-module/api/ipam/vlan/create', ['uses' => 'IpamController@createVlan']);
$app['router']->get('/custom-module/api/ipam/vlan/update', ['uses' => 'IpamController@updateVlan']);
$app['router']->get('/custom-module/api/ipam/vlan/delete', ['uses' => 'IpamController@deleteVlan']);
$app['router']->get('/custom-module/api/ipam/ip/assign', ['uses' => 'IpamController@assignIpAddress']);
/**
* Items
*/
$app['router']->get('/custom-module/api/item/list', ['uses' => 'ItemsController@listItems']);
$app['router']->get('/custom-module/api/item/show', ['uses' => 'ItemsController@showItem']);
/**
* Graphs
*/
$app['router']->get('/custom-module/api/graphs/render', ['uses' => 'GraphsController@renderGraph']);
});
/**
* IPAM Routes
*/
$app['router']->group(['namespace' => 'Modules\Addons\CustomModule\Controller\Ipam', 'prefix' => 'backend'], function() use($app)
{
$app['router']->get('custom-module/ipam/list-subnets', [
'uses' => 'SubnetsController@listSubnets'
]);
$app['router']->get('custom-module/ipam/update-empty-gateways', [
'uses' => 'SubnetsController@updateEmptyGateways'
]);
});
/**
* Client Area Routes
*/
$app['router']->group(['namespace' => 'Modules\Addons\CustomModule\Controller'], function() use($app)
{
$app['router']->get('services/{id}/custom/page', [
'as' => 'clientarea.core.services.custom.page',
'uses' => 'ClientAreaController@showCustomPage'
])->where(array('id' => '^\d+$'));
});
});
$app['translator']->addNamespace('custom-module', base_path().'/modules/addons/CustomModule/lang');
/**
* Adds new features within the device
*/
$this->deviceEvents();
/**
* Registers new commands executed in the background
*/
// $this->registerCommand();
/**
* Registers a new page in the client area section of the service summary view
*/
// $this->clientAreaEvents();
/**
* Records new function performed during order action
*/
// $this->orderEvents();
// $this->registerOrderSettings();
// $this->serviceOrderActions();
/**
* Adds custom columns to the table
*/
// $this->tableColumns();
/**
* Adds custom scripts and CSS styles
*/
// $this->customTemplateScripts();
/**
* Blocks access to the backend section
*/
// $this->restrictBackendAccess();
/**
* Adds new widget to Dashboard section
*/
// $this->dashboardWidgets();
/**
* Adds new link to the left menu
*/
// $this->renderBackendMenu();
/**
* Database Migration
*/
// $this->databaseMigration();
/**
* Perform some actions before and after OS installation
*/
$this->registerOsInstallationActions();
parent::register('CustomModule');
}
/**
* Adds new features for the device
*
* @return void
*/
private function deviceEvents()
{
/**
* Adds widget to device summary view
*/
$this->registerWidgets();
/**
* Adds a new tab in the server view
*/
$this->registerTabs();
}
/**
* Adds new widget to Dashboard section
*
* @return void
*/
private function dashboardWidgets()
{
$this->app['events']->listen('backend.dashboard', function(SmartBoxGenerator $widgets) {
// $widgets->registerBox( new CustomModuleDashboardWidget());
});
}
/**
* If the incoming address matches against any value in the array,
* the function will deny access with a redirect header to the EasyDCIM base URL
*/
private function restrictBackendAccess()
{
if($this->app['request']->is('backend*')) {
$allowFrom = [
'192.168.56.100',
'192.168.56.200'
];
if(! in_array($_SERVER['REMOTE_ADDR'], $allowFrom)) {
header('Location: '.app_url());
exit();
}
}
}
/**
* ClientArea Events like new sidebar, widgets etc.
*/
private function clientAreaEvents()
{
$this->app['events']->listen('clientarea.core.sidebars', function(\ArrayObject $sidebars) {
$params = $this->app['ca.params'];
if($params->service instanceof \Service && $params->service->getAttribute('type') == 'Server') {
$menu = $this->app['menu.manager']->createMenu('Custom Sidebar');
$menu->addLink('External Link', [
'to' => 'https://www.easydcim.com'
], ['before' => '<i class="fa fa-plus"></i>', 'id' => 1]);
$menu->addLink('Internal Link', [
'route' => ['clientarea.core.services.custom.page', 'id' => $params->service->id]
], ['before' => '<i class="fa fa-minus"></i>', 'id' => 1]);
$sidebars['custom_sidebar'] = $menu;
}
});
$this->app['events']->listen('clientarea.core.services.summary.widgets', function(CASmartBoxGenerator $generator, \Service $service) {
if($service instanceof \Service) {
if($service->getAttribute('type') == 'Server') {
$generator->addWidget(new CustomWidgetServiceSummary($service));
}
}
});
}
/**
* @return void
*/
private function registerWidgets()
{
$this->app['events']->listen('backend.devices.summary', function(SmartBoxGenerator $widgets) {
$widgets->registerBox( new CustomModuleDeviceWidget());
});
}
/**
* @return void
*/
private function registerTabs()
{
$this->app['events']->listen('easydcim.server.tabs: render', function(TabsGenerator $tabGenerator) {
$tabGenerator->appendTab(
[
'url' => route('backend.custom.module.device.tab', array('id' => $tabGenerator->model->id)),
'title' => 'Custom Device Tab'
]
);
});
}
/**
* Events triggered during action on services
*/
private function orderEvents()
{
/**
* After activate service
*/
\Event::listen('easydcim.service.after: activate', function(\Order $order) {
/**
* @var $service \Service
*/
$service = $order->getAttribute('service');
/**
* @var $device \Device
*/
$device = $service->relatedItem();
file_put_contents(base_path('/tmp/device_dump.log'), $device->toJson());
/**
* Assign metadata to device
*/
$device->forceAssignMetadata([
'Custom VNC Username' => 'test_username new',
'Custom VNC Password' => 'test_password new'
]);
});
/**
* After terminate service
*/
\Event::listen('easydcim.service.after: terminate', function(\Order $order) {
/**
* @var $service \Service
*/
$service = $order->getAttribute('service');
echo '<h1>Service</h1>';
d($service->toArray());
/**
* @var $client \User
*/
$client = $order->getAttribute('user');
echo '<h1>Client</h1>';
d($client->toArray());
/**
* @var $device \Device
*/
$device = $service->relatedItem();
/**
* Assign metadata to device
*/
$device->forceAssignMetadata([
'Custom VNC Username' => '',
'Custom VNC Password' => ''
]);
echo '<h1>Device</h1>';
d($device->toArray());
echo '<h1>Device Metadata</h1>';
d($device->getMetaListAttribute());
});
}
/**
* Register a new command
*/
private function registerCommand()
{
$this->app['events']->listen('artisan.command.register', function() {
\Artisan::add(new CustomModuleCommand);
\Artisan::add(new IpmiBmcResetCommand);
});
}
/**
* Add additional columns to the table
*/
private function tableColumns()
{
$this->app['events']->listen('easydcim.grid: columns', function(Collection $columns, $modelName, $path) {
if($modelName == 'server') {
$columns->put('a_column', [
'label' => trans('custom-module::backend.column_1'),
'sortable' => true,
'type' => 'Key',
'value' => function($model) {
return $model->id;
}
]);
}
return $columns;
});
}
/**
* Add additional JS and CSS
*/
private function customTemplateScripts()
{
$this->app['events']->listen('easydcim.assets: template.js', function(\ArrayObject $scripts) {
$scripts[] = base_path('modules/addons/CustomModule/templates/admin/default/assets/js/custom-app.js');
return $scripts;
});
$this->app['events']->listen('easydcim.assets: template.css', function(\ArrayObject $scripts) {
$scripts[] = base_path('modules/addons/CustomModule/templates/admin/default/assets/css/style.css');
return $scripts;
});
}
/**
* Register orders additional functions
*
* @return void
*/
private function registerOrderSettings()
{
$this->app['events']->listen('easydcim.provisioning: server.service.suspend', function(\ArrayObject $options) {
$options['custom_module_action_suspend'] = '[Custom Module] Custom Suspend Action';
return $options;
});
$this->app['events']->listen('easydcim.provisioning: server.service.unsuspend', function(\ArrayObject $options) {
$options['custom_module_action_unsuspend'] = '[Custom Module] Custom Unsuspend Action';
return $options;
});
}
/**
* @return void
*/
private function serviceOrderActions()
{
$this->app['events']->listen('easydcim.provisioning: serverprovisioningmodule.suspend.after', function(
\Order $order,
\Device $device,
LogService $logger,
ProvisioningModuleInterface $module,
$configuration
) {
if(in_array('custom_module_action_suspend', $configuration)) {
module_provisioning_log($logger->getLogger(), $module, '[Custom Module] Trying to run custom suspend action');
}
});
$this->app['events']->listen('easydcim.provisioning: serverprovisioningmodule.unsuspend.after', function(
\Order $order,
\Device $device,
LogService $logger,
ProvisioningModuleInterface $module,
$configuration
) {
if(in_array('custom_module_action_unsuspend', $configuration)) {
module_provisioning_log($logger->getLogger(), $module, '[Custom Module] Trying to run custom unsuspend action');
}
});
}
/**
* Render backend menu link
*/
protected function renderBackendMenu()
{
$this->app['events']->listen('easydcim:backend.menu.render:after', function(MenuBuilder $menuBuilder, \ArrayObject $items){
$items['devices']['children'][] = [
'key' => 'devices.custom_page',
'name' => 'Custom Page',
'url' => url('backend/devices/servers')
];
return $items;
});
}
/**
* Database Migration
*
* @return void
*/
private function databaseMigration()
{
if(! Schema::hasTable('custom_module_table'))
{
Schema::create('custom_module_table', function(Blueprint $table)
{
$table->increments('id');
$table->integer('item_id');
$table->string('name', 255)->default('');
$table->string('type', 48);
$table->text('description')->default('');
$table->timestamps();
});
$items = \DB::table('items')->lists('id', 'id');
$types = \DB::table('item_types')->lists('name', 'name');
for($i = 1; $i <= 15; $i++) {
CustomModel::create([
'name' => vsprintf('Test Item %s', [$i]),
'description' => 'Auto Generated Item',
'item_id' => array_rand($items),
'type' => array_rand($types)
]);
}
}
}
/**
* Perform some actions before and after OS installation
*
* @return void
*/
protected function registerOsInstallationActions()
{
/**
* Event triggered before OS installation
*/
$this->app['events']->listen('easydcim.modules: os.remote.provisioning.start:during', function(\Device $device, array $deviceConfiguration, array $serverConfiguration) {
$mac = $device->getMetaAttribute('MAC Address');
os_update_status_for_mac($mac, '[Custom Module] Start installation');
/**
* Perform custom actions
*/
});
$this->app['events']->listen('easydcim.modules: os.remote.provisioning.cancel:during', function(\Device $device, array $deviceConfiguration, array $serverConfiguration) {
$mac = $device->getMetaAttribute('MAC Address');
os_update_status_for_mac($mac, '[Custom Module] Cancel installation');
/**
* Perform custom actions
*/
});
$this->app['events']->listen('easydcim.modules: os.remote.provisioning.end:during', function(\Device $device, array $deviceConfiguration, array $serverConfiguration) {
$mac = $device->getMetaAttribute('MAC Address');
os_update_status_for_mac($mac, '[Custom Module] Completion of the installation');
/**
* Perform custom actions
*/
});
}
}