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

Commit 0c501f0

Browse files
committed
feat(policy): add reboot command
Signed-off-by: Domingo Oropeza <[email protected]>
1 parent 89cc4d4 commit 0c501f0

File tree

5 files changed

+117
-15
lines changed

5 files changed

+117
-15
lines changed

front/agent.form.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@
6464
'_ping_request' => '',
6565
]);
6666
Html::back();
67+
} else if (isset($_POST['reboot'])) {
68+
$agent->check($_POST['id'], UPDATE);
69+
$agent->update([
70+
'id' => $_POST['id'],
71+
'_reboot' => '',
72+
]);
73+
Html::back();
6774
} else if (isset($_POST['geolocate'])) {
6875
$agent->check($_POST['id'], UPDATE);
6976
$agent->update([

inc/agent.class.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ public function showForm($ID, array $options = []) {
264264
'canUpdate' => $canUpdate,
265265
'agent' => $fields,
266266
'pingButton' => Html::submit(_x('button', 'Ping'), ['name' => 'ping']),
267+
'rebootButton' => Html::submit(_x('button', 'Reboot'), ['name' => 'reboot']),
267268
'geolocateButton' => Html::submit(_x('button', 'Geolocate'), ['name' => 'geolocate']),
268269
'inventoryButton' => Html::submit(_x('button', 'Inventory'), ['name' => 'inventory']),
269270

@@ -592,7 +593,7 @@ public function prepareInputForUpdate($input) {
592593
}
593594

594595
//Send a connection status request to the device
595-
if (isset($input['_ping_request']) || isset($input['_geolocate_request']) || isset($input['_inventory_request'])) {
596+
if (isset($input['_ping_request']) || isset($input['_reboot']) || isset($input['_geolocate_request']) || isset($input['_inventory_request'])) {
596597
if ($this->getTopic() === null) {
597598
Session::addMessageAfterRedirect(__("The device is not enrolled yet", 'flyvemdm'));
598599
return false;
@@ -608,6 +609,15 @@ public function prepareInputForUpdate($input) {
608609
}
609610
}
610611

612+
if (isset($input['_reboot'])) {
613+
try {
614+
$this->sendRebootQuery();
615+
} catch (AgentSendQueryException $exception) {
616+
Session::addMessageAfterRedirect($exception->getMessage());
617+
return false;
618+
}
619+
}
620+
611621
if (isset($input['_geolocate_request'])) {
612622
try {
613623
$this->sendGeolocationQuery();
@@ -1679,6 +1689,7 @@ public static function getTopicsToCleanup() {
16791689
return array_merge($topics, [
16801690
'Command/Subscribe',
16811691
'Command/Ping',
1692+
'Command/Reboot',
16821693
'Command/Geolocate',
16831694
'Command/Inventory',
16841695
'Command/Lock',
@@ -1781,6 +1792,44 @@ private function sendInventoryQuery() {
17811792
throw new AgentSendQueryException(__("Timeout querying the device inventory", 'flyvemdm'));
17821793
}
17831794

1795+
/**
1796+
* Sends a message on the subtopic dedicated to ping requests
1797+
*
1798+
* @return bool
1799+
* @throws AgentSendQueryException
1800+
*/
1801+
private function sendRebootQuery() {
1802+
$message = json_encode(['query' => 'Reboot'], JSON_UNESCAPED_SLASHES);
1803+
$brokerMessage = new BrokerMessage($message);
1804+
$envelopeConfig = [];
1805+
$topic = $this->getTopic();
1806+
if ($topic !== null) {
1807+
$finalTopic = $topic . "/Command/Reboot";
1808+
$envelopeConfig[] = new MqttEnvelope([
1809+
'topic' => $finalTopic,
1810+
]);
1811+
$envelopeConfig[] = new FcmEnvelope([
1812+
'topic' => $finalTopic,
1813+
'scope' => $this->getPushNotificationInfo(),
1814+
]);
1815+
}
1816+
$envelope = new BrokerEnvelope($brokerMessage, $envelopeConfig);
1817+
$this->notify($envelope);
1818+
1819+
$loopCount = 50;
1820+
$updatedAgent = new self();
1821+
while ($loopCount > 0) {
1822+
usleep(200000); // 200 milliseconds
1823+
$loopCount--;
1824+
$updatedAgent->getFromDB($this->getID());
1825+
if ($updatedAgent->getField('last_contact') != $this->fields['last_contact']) {
1826+
Session::addMessageAfterRedirect(__('The device restarted', 'flyvemdm'));
1827+
return true;
1828+
}
1829+
}
1830+
throw new AgentSendQueryException(__("Timeout querying the device", 'flyvemdm'));
1831+
}
1832+
17841833
/**
17851834
* Sends a message on the subtopic dedicated to ping requests
17861835
*

tests/src/Flyvemdm/Tests/CommonTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ public static function commandList() {
651651
return [
652652
'Command/Subscribe',
653653
'Command/Ping',
654+
'Command/Reboot',
654655
'Command/Geolocate',
655656
'Command/Inventory',
656657
'Command/Lock',

tests/suite-integration/PluginFlyvemdmAgent.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,48 @@ public function testPingRequest() {
675675
);
676676
}
677677

678+
/**
679+
* test ping message
680+
* @tags testPingRequest
681+
*/
682+
public function testRebootRequest() {
683+
list($user, $serial, $guestEmail, $invitation) = $this->createUserInvitation(\User::getForeignKeyField());
684+
$agent = $this->agentFromInvitation($user, $guestEmail, $serial,
685+
$invitation->getField('invitation_token'));
686+
687+
// Get enrolment data to enable the agent's MQTT account
688+
$this->boolean($agent->getFromDB($agent->getID()))->isTrue();
689+
690+
// Find the last existing ID of logged MQTT messages
691+
$log = new \PluginFlyvemdmMqttlog();
692+
$lastLogId = \PluginFlyvemdmCommon::getMax($log, '', 'id');
693+
694+
$updateSuccess = $agent->update([
695+
'id' => $agent->getID(),
696+
'_reboot' => '',
697+
]);
698+
699+
// Update shall fail because the ping answer will not occur
700+
$this->boolean($updateSuccess)->isFalse();
701+
702+
// Get the latest MQTT message
703+
sleep(2);
704+
705+
$logEntryFound = 0;
706+
$rows = $log->find("`direction` = 'O' AND `id` > '$lastLogId'");
707+
foreach ($rows as $row) {
708+
if ($row['topic'] == $agent->getTopic() . '/Command/Reboot') {
709+
$logEntryFound = $row['id'];
710+
break;
711+
}
712+
}
713+
$this->integer((int) $logEntryFound)->isGreaterThan(0);
714+
715+
// check the message
716+
$mqttMessage = ['query' => 'Reboot'];
717+
$this->string($row['message'])->isEqualTo(json_encode($mqttMessage, JSON_UNESCAPED_SLASHES));
718+
}
719+
678720
/**
679721
* test geolocate message
680722
* @tags testGeolocateRequest

tpl/agent.html.twig

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,21 @@
4545
<td>{{ agent.notification_type|upper }}</td>
4646
</tr>
4747
{% if canUpdate == true %}
48-
<tr class='tab_bg_1'>
49-
<td>
50-
{{ __('Actions', 'flyvemdm') }}
51-
</td>
52-
<td>
53-
{{ pingButton|raw }}
54-
</td>
55-
<td>
56-
{{ geolocateButton|raw }}
57-
</td>
58-
<td>
59-
{{ inventoryButton|raw }}
60-
</td>
61-
</tr>
48+
<tr class="tab_bg_1">
49+
<th colspan="4" class="subheader">{{ __('Actions', 'flyvemdm') }}</th>
50+
</tr>
51+
<tr class='tab_bg_2'>
52+
<td align="center">
53+
{{ pingButton|raw }}
54+
</td>
55+
<td align="center">
56+
{{ rebootButton|raw }}
57+
</td>
58+
<td align="center">
59+
{{ geolocateButton|raw }}
60+
</td>
61+
<td align="center">
62+
{{ inventoryButton|raw }}
63+
</td>
64+
</tr>
6265
{% endif %}

0 commit comments

Comments
 (0)