This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
RecordActivity.php
91 lines (81 loc) · 2.66 KB
/
RecordActivity.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
<?php
namespace App\Listeners\Cloudstack;
use Log;
use Auth;
use Request;
use App\Activity;
use App\Events\Cloudstack\JobFinished;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class RecordActivity implements ShouldQueue
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param JobFinished $event
* @return void
*/
public function handle(JobFinished $event)
{
// Job result is in $event->jobResult
Log::debug('RecordActivity listener fired');
if (isset($event->jobResult->jobinstancetype)) {
Log::debug("Job Instance type is {$event->jobResult->jobinstancetype}");
} else {
Log::debug("Job Instance type is not set");
}
Log::debug("Command is {$event->jobResult->cmd}");
Log::debug("VM Name is {$event->jobResult->jobresult->virtualmachine->name}");
Log::debug("Auth is " . $event->userId);
Log::debug("User IP is " . Request::server('REMOTE_ADDR'));
try {
$subject = $this->translateSubjectType($event->jobResult->jobinstancetype);
$eventType = $this->translateEvent($event->jobResult->cmd);
} catch (\Exception $e) {
return;
}
Activity::create(['subject_type' => $subject,
'subject_id' => 0,
'event' => "$eventType - {$event->jobResult->jobresult->virtualmachine->name}",
'user_id' => $event->userId,
'ip' => $event->userIpAddress
]);
}
private function translateSubjectType($jobInstanceType)
{
switch ($jobInstanceType) {
case "VirtualMachine":
return 'Instance';
default:
Log::debug('Unhandled subject translation request: ' . $jobInstanceType);
throw new \Exception('Unable to translate subject type.');
}
}
private function translateEvent($cmd)
{
switch (last(explode('.', $cmd))) {
case "StartVMCmd":
return 'Started';
case "StopVMCmd":
return 'Stopped';
case "DeployVMCmd":
return 'Created';
case "DestroyVMCmd":
return 'Destroyed';
case "RebootVMCmd":
return 'Rebooted';
default:
Log::debug('Unhandled event translation request: ' . $cmd);
throw new \Exception('Unable to translate event.');
}
}
}