mirror of
https://github.com/unraid/webgui.git
synced 2026-04-23 18:48:49 -05:00
Merge pull request #1644 from SimonFair/Unraid-Wake-on-LAN
Remove WOL for Services
This commit is contained in:
@@ -1,125 +0,0 @@
|
||||
<?PHP
|
||||
/* Copyright 2005-2023, Lime Technology
|
||||
* Copyright 2012-2023, Bergware International.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License version 2,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*/
|
||||
?>
|
||||
<?
|
||||
global $lv;
|
||||
$docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
|
||||
require_once "$docroot/webGui/include/Helpers.php";
|
||||
require_once "$docroot/webGui/include/SysDriversHelpers.php";
|
||||
require_once "$docroot/plugins/dynamix.plugin.manager/include/PluginHelpers.php";
|
||||
require_once "$docroot/plugins/dynamix.vm.manager/include/libvirt_helpers.php";
|
||||
// add translations
|
||||
$_SERVER['REQUEST_URI'] = 'tools';
|
||||
require_once "$docroot/webGui/include/Translations.php";
|
||||
require_once "$docroot/plugins/dynamix.docker.manager/include/DockerClient.php";
|
||||
require_once "$docroot/plugins/dynamix.vm.manager/include/libvirt.php";
|
||||
|
||||
function macbyte($val) {
|
||||
if ($val < 16)
|
||||
return '0'.dechex($val);
|
||||
|
||||
return dechex($val);
|
||||
}
|
||||
|
||||
$arrEntries = [];
|
||||
$vms = $lv->get_domains() ?:[];
|
||||
sort($vms,SORT_NATURAL);
|
||||
foreach($vms as $vm){
|
||||
$arrEntries['VM'][$vm]['interfaces'] = $lv->get_nic_info($vm);
|
||||
$arrEntries['VM'][$vm]['name'] = $vm;
|
||||
}
|
||||
|
||||
$DockerClient = new DockerClient();
|
||||
$containers = $DockerClient->getDockerJSON("/containers/json?all=1");
|
||||
foreach($containers as $ct)
|
||||
$arrEntries['Docker'][substr($ct["Names"][0],1)] = [
|
||||
'interfaces' => ['0 '=> ['mac' => isset($ct["NetworkSettings"]["Networks"]["bridge"]["MacAddress"]) ? $ct["NetworkSettings"]["Networks"]["bridge"]["MacAddress"] : ""]],
|
||||
'name' => substr($ct["Names"][0],1),
|
||||
];
|
||||
|
||||
$lxc = explode("\n",shell_exec("lxc-ls -1")) ;
|
||||
$lxcpath = trim(shell_exec("lxc-config lxc.lxcpath"));
|
||||
foreach ($lxc as $lxcname) {
|
||||
if ($lxcname == "") continue;
|
||||
$value = explode("=",shell_exec("cat $lxcpath/$lxcname/config | grep 'hwaddr'"));
|
||||
$arrEntries['LXC'][$lxcname]['interfaces'][0]['mac'] = trim($value[1]);
|
||||
$arrEntries['LXC'][$lxcname]['name'] = $lxcname;
|
||||
}
|
||||
|
||||
if (is_file("/boot/config/wol.json")) $user_mac = json_decode(file_get_contents("/boot/config/wol.json"),true); else $user_mac = [];
|
||||
|
||||
foreach($arrEntries as $key => $data) {
|
||||
$type=$key;
|
||||
foreach($data as $data2){
|
||||
$name=$data2['name'];
|
||||
if (isset($user_mac[$type][$name])) {
|
||||
$name=$name;
|
||||
$arrEntries[$type][$name]['enable'] = $user_mac[$type][$name]['enable'];
|
||||
$arrEntries[$type][$name]['user_mac'] = strtolower($user_mac[$type][$name]['user_mac']);
|
||||
} else {
|
||||
$arrEntries[$type][$name]['enable'] = 'enable';
|
||||
$arrEntries[$type][$name]['user_mac'] = 'None Defined';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch ($_POST['table']) {
|
||||
|
||||
case 't1load':
|
||||
$arrMacs = $arrEntries;
|
||||
$html = "<thead><tr><th>"._('Service')."</th><th>"._('Name')."</th><th>"._('Mac Address')."</th><th>"._('Enabled')."</th><th>"._('User Mac Address')."</th></tr></thead>";
|
||||
$html .= "<tbody>";
|
||||
ksort($arrMacs);
|
||||
foreach($arrMacs as $systype => $m) {
|
||||
foreach($m as $macaddr) {
|
||||
if ($systype == "") continue;
|
||||
|
||||
$html .= "<tr id='row$systype'>";
|
||||
$macs = "";
|
||||
foreach($macaddr['interfaces'] as $intdetail)
|
||||
{
|
||||
$macs .= " {$intdetail['mac']}" ;
|
||||
}
|
||||
$html .= "<td>$systype</td>";
|
||||
$selecttypename="enable;".$systype.";".$macaddr['name'];
|
||||
$mactypename=htmlspecialchars("user_mac;".$systype.";".$macaddr['name']);
|
||||
$mactypeid=htmlspecialchars("user_mac".$systype."".$macaddr['name']);
|
||||
$user_mac_str = '<input type="text" name="'.$mactypename.'" id="'.$mactypeid.'" class="narrow" value="'.htmlspecialchars($macaddr['user_mac']).'" title="'._("random mac, you can supply your own").'" /><a><i onclick="maccreate(\''.$mactypeid.'\')" class="fa fa-refresh mac_generate" title="re-generate random mac address"></i></a>';
|
||||
$html .= "<td>{$macaddr['name']}</td><td id=\"status$systype\">$macs</td><td>";
|
||||
$html .="<select name='$selecttypename' class='audio narrow'>";
|
||||
$html .= mk_option($macaddr["enable"] , "disable", _("Disabled"));
|
||||
$html .= mk_option($macaddr["enable"] , "enable", _("Enabled"));
|
||||
$html .= mk_option($macaddr["enable"] , "shutdown", _("Enabled and Shutdown"));
|
||||
$html .= "</select></td><td>".$user_mac_str."</td></tr>";
|
||||
$text = "";
|
||||
}
|
||||
}
|
||||
if (count($arrMacs) < 1) {
|
||||
$html .= "<tr id='row'>";
|
||||
$html .= "<td></td><td></td><td>"._("No Entries")."</td><td></td><td></td></tr>";
|
||||
}
|
||||
$html .= "</tbody>";
|
||||
|
||||
$rtn = array();
|
||||
$rtn['html'] = $html;
|
||||
echo json_encode($rtn);
|
||||
break;
|
||||
|
||||
case "macaddress":
|
||||
$seed = 1;
|
||||
$prefix = '62:00:00';
|
||||
$prefix .=':'.macbyte(($seed * rand()) % 256).':'.macbyte(($seed * rand()) % 256).':'.macbyte(($seed * rand()) % 256);
|
||||
echo json_encode(['mac' => $prefix]);
|
||||
break;
|
||||
|
||||
}
|
||||
?>
|
||||
@@ -1,180 +0,0 @@
|
||||
#!/usr/bin/php
|
||||
<?php
|
||||
|
||||
|
||||
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
|
||||
require_once "$docroot/webGui/include/Helpers.php";
|
||||
require_once "$docroot/webGui/include/Custom.php";
|
||||
require_once "$docroot/plugins/dynamix.vm.manager/include/libvirt_helpers.php";
|
||||
require_once "$docroot/plugins/dynamix.vm.manager/include/libvirt.php";
|
||||
require_once "$docroot/plugins/dynamix.plugin.manager/include/PluginHelpers.php";
|
||||
require_once "$docroot/plugins/dynamix.docker.manager/include/DockerClient.php";
|
||||
|
||||
function getContainerStats($container, $option) {
|
||||
exec("lxc-info " . $container, $content);
|
||||
foreach($content as $index => $string) {
|
||||
if (strpos($string, $option) !== FALSE)
|
||||
return trim(explode(':', $string)[1]);
|
||||
}
|
||||
}
|
||||
|
||||
$mac = strtolower($argv[1]);
|
||||
|
||||
$libvirtd_running = is_file('/var/run/libvirt/libvirtd.pid') ;
|
||||
$dockerd_running = is_file('/var/run/dockerd.pid');
|
||||
$lxc_ls_exist = is_file('/usr/bin/lxc-ls');
|
||||
#$RUNDOCKER = $RUNLXC = $RUNVM = true;
|
||||
extract(parse_ini_file("/boot/config/wol.cfg")) ;
|
||||
if (!isset($RUNLXC)) $RUNLXC = "y";
|
||||
if (!isset($RUNVM)) $RUNVM = "y";
|
||||
if (!isset($RUNDocker)) $RUNDocker = "y";
|
||||
if (!isset($RUNSHUT)) $RUNSHUT = "n";
|
||||
|
||||
$arrEntries = [] ;
|
||||
if ($libvirtd_running && $RUNVM == "y") {
|
||||
$vms = $lv->get_domains();
|
||||
sort($vms,SORT_NATURAL);
|
||||
foreach($vms as $vm){
|
||||
$arrEntries['VM'][$vm]['interfaces'] = $lv->get_nic_info($vm);
|
||||
$arrEntries['VM'][$vm]['name'] = $vm;
|
||||
}
|
||||
}
|
||||
if ($dockerd_running && $RUNDOCKER == "y") {
|
||||
$DockerClient = new DockerClient();
|
||||
$containers = $DockerClient->getDockerJSON("/containers/json?all=1");
|
||||
foreach($containers as $container)
|
||||
$arrEntries['Docker'][ substr($container["Names"][0],1) ] = [
|
||||
'interfaces' => ['0' => ['mac' => isset($container["NetworkSettings"]["Networks"]["bridge"]["MacAddress"]) ? $container["NetworkSettings"]["Networks"]["bridge"]["MacAddress"]:""]],
|
||||
'name' => substr($container["Names"][0],1),
|
||||
'state' => $container["State"],
|
||||
];
|
||||
}
|
||||
|
||||
if ($lxc_ls_exist && $RUNLXC == "y") {
|
||||
$lxc = explode("\n",shell_exec("lxc-ls -1")) ;
|
||||
$lxcpath = trim(shell_exec("lxc-config lxc.lxcpath"));
|
||||
foreach ($lxc as $lxcname) {
|
||||
if ($lxcname == "") continue;
|
||||
$values = explode("=",shell_exec("cat $lxcpath/$lxcname/config | grep 'hwaddr'"));
|
||||
$arrEntries['LXC'][$lxcname]['interfaces'][0]['mac'] = trim($values[1]);
|
||||
$arrEntries['LXC'][$lxcname]['name'] = $lxcname;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_file("/boot/config/wol.json")) $user_mac = json_decode(file_get_contents("/boot/config/wol.json"),true); else $user_mac = [];
|
||||
|
||||
foreach($arrEntries as $typekey => $typedata)
|
||||
{
|
||||
foreach($typedata as $typeEntry){
|
||||
$name=$typeEntry['name'];
|
||||
if (isset($user_mac[$typekey][$name])) {
|
||||
|
||||
$name=$name;
|
||||
|
||||
$arrEntries[$typekey][$name]['enable'] = $user_mac[$typekey][$name]['enable'];
|
||||
$arrEntries[$typekey][$name]['user_mac'] = strtolower($user_mac[$typekey][$name]['user_mac']);
|
||||
|
||||
} else {
|
||||
$arrEntries[$typekey][$name]['enable'] = "enable";
|
||||
$arrEntries[$typekey][$name]['user_mac'] = 'None Defined';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$mac_list=[];
|
||||
foreach($arrEntries as $type => $detail)
|
||||
{
|
||||
foreach($detail as $name => $entryDetail)
|
||||
{
|
||||
foreach($entryDetail['interfaces'] as $interfaces)
|
||||
{
|
||||
$interfaces['mac'] = strtolower($interfaces['mac']);
|
||||
if($interfaces['mac'] == "" && $entryDetail['user_mac'] == "None Defined") continue;
|
||||
if (isset($entryDetail['state'])) $state = $entryDetail['state']; else $state = "";
|
||||
if (isset($entryDetail['enable']) && !$entryDetail['enable'] ) $enable = false; else $enable = true;
|
||||
if ($entryDetail['user_mac'] != "None Defined") {
|
||||
$mac_list[$entryDetail['user_mac']] = [
|
||||
'type' => $type,
|
||||
'name' => $name,
|
||||
'state' => $state,
|
||||
'enable' => $entryDetail['enable'],
|
||||
];
|
||||
}
|
||||
if ($interfaces['mac'] != "") {
|
||||
$mac_list[$interfaces['mac']] = [
|
||||
'type' => $type,
|
||||
'name' => $name,
|
||||
'state' => $state,
|
||||
'enable' => $entryDetail['enable'],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
$found = array_key_exists($mac,$mac_list);
|
||||
|
||||
|
||||
if ($found && $mac_list[$mac]['enable'] != "disable") {
|
||||
echo _("Found"). " " . $mac . " " .$mac_list[$mac]['type'] . " " . $mac_list[$mac]['name'];
|
||||
switch ($mac_list[$mac]['type']) {
|
||||
|
||||
case "VM":
|
||||
if ($libvirtd_running && $RUNVM == "y") {
|
||||
$res = $lv->get_domain_by_name($mac_list[$mac]['name']);
|
||||
$dom = $lv->domain_get_info($res);
|
||||
$state = $lv->domain_state_translate($dom['state']);
|
||||
switch ($state) {
|
||||
case 'running':
|
||||
if ($RUNSHUT == "y" && $mac_list[$mac]['enable'] == "shutdown") $lv->domain_shutdown("{$mac_list[$mac]['name']}");
|
||||
break;
|
||||
case 'paused':
|
||||
case 'pmsuspended':
|
||||
$lv->domain_resume("{$mac_list[$mac]['name']}");
|
||||
break;
|
||||
default:
|
||||
$lv->domain_start("{$mac_list[$mac]['name']}");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "LXC":
|
||||
if ($lxc_ls_exist && $RUNLXC == "y") {
|
||||
$state = getContainerStats($mac_list[$mac]['name'], "State");
|
||||
switch ($state) {
|
||||
case 'RUNNING':
|
||||
if ($RUNSHUT == "y" && $mac_list[$mac]['enable'] == "shutdown") shell_exec("lxc-stop {$mac_list[$mac]['name']}");
|
||||
break;
|
||||
case 'FROZEN':
|
||||
shell_exec("lxc-unfreeze {$mac_list[$mac]['name']}");
|
||||
break;
|
||||
default:
|
||||
shell_exec("lxc-start {$mac_list[$mac]['name']}");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "Docker":
|
||||
if ($dockerd_running && $RUNDOCKER == "y") {
|
||||
|
||||
switch ($mac_list[$mac]['state']) {
|
||||
case "running":
|
||||
if ($RUNSHUT == "y" && $mac_list[$mac]['enable'] == "shutdown") shell_exec("docker stop {$mac_list[$mac]['name']}");
|
||||
break;
|
||||
case "exited":
|
||||
case "created":
|
||||
shell_exec("docker start {$mac_list[$mac]['name']}");
|
||||
break;
|
||||
case "paused":
|
||||
shell_exec("docker unpause {$mac_list[$mac]['name']}");
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if ($mac_list[$mac]['enable'] == "disable") echo $mac . " " . _(" has not been actioned as set to disabled");
|
||||
else echo _("Not Found"). " " . $mac . " " . _("ignoring or Maybe actions disabled for type(Docker/VM/LXC)");
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user