Skip to content
This repository was archived by the owner on Aug 9, 2021. It is now read-only.

Commit 6edf83e

Browse files
committed
feature(policy): add reboot command
Signed-off-by: Domingo Oropeza <[email protected]>
1 parent 46ccb5a commit 6edf83e

File tree

5 files changed

+103
-15
lines changed

5 files changed

+103
-15
lines changed

front/agent.form.php

+7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@
6565
'_ping' => '',
6666
]);
6767
Html::back();
68+
} else if (isset($_POST['reboot'])) {
69+
$agent->check($_POST['id'], UPDATE);
70+
$agent->update([
71+
'id' => $_POST['id'],
72+
'_reboot' => '',
73+
]);
74+
Html::back();
6875
} else if (isset($_POST['geolocate'])) {
6976
$agent->check($_POST['id'], UPDATE);
7077
$agent->update([

inc/agent.class.php

+36-1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ public function showForm($ID, array $options = []) {
246246
'canUpdate' => $canUpdate,
247247
'agent' => $fields,
248248
'pingButton' => Html::submit(_x('button', 'Ping'), ['name' => 'ping']),
249+
'rebootButton' => Html::submit(_x('button', 'Reboot'), ['name' => 'reboot']),
249250
'geolocateButton' => Html::submit(_x('button', 'Geolocate'), ['name' => 'geolocate']),
250251
'inventoryButton' => Html::submit(_x('button', 'Inventory'), ['name' => 'inventory']),
251252

@@ -505,7 +506,7 @@ public function prepareInputForUpdate($input) {
505506
}
506507

507508
//Send a connection status request to the device
508-
if (isset($input['_ping']) || isset($input['_geolocate']) || isset($input['_inventory'])) {
509+
if (isset($input['_ping']) || isset($input['_reboot']) || isset($input['_geolocate']) || isset($input['_inventory'])) {
509510
if ($this->getTopic() === null) {
510511
Session::addMessageAfterRedirect(__("The device is not enrolled yet", 'flyvemdm'));
511512
return false;
@@ -521,6 +522,15 @@ public function prepareInputForUpdate($input) {
521522
}
522523
}
523524

525+
if (isset($input['_reboot'])) {
526+
try {
527+
$this->sendRebootQuery();
528+
} catch (AgentSendQueryException $exception) {
529+
Session::addMessageAfterRedirect($exception->getMessage());
530+
return false;
531+
}
532+
}
533+
524534
if (isset($input['_geolocate'])) {
525535
try {
526536
$this->sendGeolocationQuery();
@@ -1505,6 +1515,7 @@ public static function getTopicsToCleanup() {
15051515
return array_merge($topics, [
15061516
'Command/Subscribe',
15071517
'Command/Ping',
1518+
'Command/Reboot',
15081519
'Command/Geolocate',
15091520
'Command/Inventory',
15101521
'Command/Lock',
@@ -1579,6 +1590,30 @@ private function sendInventoryQuery() {
15791590
throw new AgentSendQueryException(__("Timeout querying the device inventory", 'flyvemdm'));
15801591
}
15811592

1593+
/**
1594+
* Sends a message on the subtopic dedicated to ping requests
1595+
*
1596+
* @return bool
1597+
* @throws AgentSendQueryException
1598+
*/
1599+
private function sendRebootQuery() {
1600+
$this->notify($this->topic . "/Command/Reboot",
1601+
json_encode(['query' => 'Reboot'], JSON_UNESCAPED_SLASHES), 0, 0);
1602+
1603+
$loopCount = 25;
1604+
$updatedAgent = new self();
1605+
while ($loopCount > 0) {
1606+
usleep(200000); // 200 milliseconds
1607+
$loopCount--;
1608+
$updatedAgent->getFromDB($this->getID());
1609+
if ($updatedAgent->getField('last_contact') != $this->fields['last_contact']) {
1610+
Session::addMessageAfterRedirect(__('The device restarted', 'flyvemdm'));
1611+
return true;
1612+
}
1613+
}
1614+
throw new AgentSendQueryException(__("Timeout querying the device", 'flyvemdm'));
1615+
}
1616+
15821617
/**
15831618
* Sends a message on the subtopic dedicated to ping requests
15841619
*

tests/src/Flyvemdm/Tests/CommonTestCase.php

+1
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ public static function commandList() {
532532
return [
533533
'Command/Subscribe',
534534
'Command/Ping',
535+
'Command/Reboot',
535536
'Command/Geolocate',
536537
'Command/Inventory',
537538
'Command/Lock',

tests/suite-integration/PluginFlyvemdmAgent.php

+42
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,48 @@ public function testPingRequest() {
623623
$this->string($row['message'])->isEqualTo(json_encode($mqttMessage, JSON_UNESCAPED_SLASHES));
624624
}
625625

626+
/**
627+
* test ping message
628+
* @tags testPingRequest
629+
*/
630+
public function testRebootRequest() {
631+
list($user, $serial, $guestEmail, $invitation) = $this->createUserInvitation(\User::getForeignKeyField());
632+
$agent = $this->agentFromInvitation($user, $guestEmail, $serial,
633+
$invitation->getField('invitation_token'));
634+
635+
// Get enrolment data to enable the agent's MQTT account
636+
$this->boolean($agent->getFromDB($agent->getID()))->isTrue();
637+
638+
// Find the last existing ID of logged MQTT messages
639+
$log = new \PluginFlyvemdmMqttlog();
640+
$lastLogId = \PluginFlyvemdmCommon::getMax($log, '', 'id');
641+
642+
$updateSuccess = $agent->update([
643+
'id' => $agent->getID(),
644+
'_reboot' => '',
645+
]);
646+
647+
// Update shall fail because the ping answer will not occur
648+
$this->boolean($updateSuccess)->isFalse();
649+
650+
// Get the latest MQTT message
651+
sleep(2);
652+
653+
$logEntryFound = 0;
654+
$rows = $log->find("`direction` = 'O' AND `id` > '$lastLogId'");
655+
foreach ($rows as $row) {
656+
if ($row['topic'] == $agent->getTopic() . '/Command/Reboot') {
657+
$logEntryFound = $row['id'];
658+
break;
659+
}
660+
}
661+
$this->integer((int) $logEntryFound)->isGreaterThan(0);
662+
663+
// check the message
664+
$mqttMessage = ['query' => 'Reboot'];
665+
$this->string($row['message'])->isEqualTo(json_encode($mqttMessage, JSON_UNESCAPED_SLASHES));
666+
}
667+
626668
/**
627669
* test geolocate message
628670
* @tags testGeolocateRequest

tpl/agent.html.twig

+17-14
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,21 @@
3939
</td>
4040
</tr>
4141
{% if canUpdate == true %}
42-
<tr class='tab_bg_1'>
43-
<td>
44-
{{ __('Actions', 'flyvemdm') }}
45-
</td>
46-
<td>
47-
{{ pingButton|raw }}
48-
</td>
49-
<td>
50-
{{ geolocateButton|raw }}
51-
</td>
52-
<td>
53-
{{ inventoryButton|raw }}
54-
</td>
55-
</tr>
42+
<tr class="tab_bg_1">
43+
<th colspan="4" class="subheader">{{ __('Actions', 'flyvemdm') }}</th>
44+
</tr>
45+
<tr class='tab_bg_2'>
46+
<td align="center">
47+
{{ pingButton|raw }}
48+
</td>
49+
<td align="center">
50+
{{ rebootButton|raw }}
51+
</td>
52+
<td align="center">
53+
{{ geolocateButton|raw }}
54+
</td>
55+
<td align="center">
56+
{{ inventoryButton|raw }}
57+
</td>
58+
</tr>
5659
{% endif %}

0 commit comments

Comments
 (0)