Skip to content

Commit cc90fa3

Browse files
committed
Add http(s) toggle for nodes, update power page
Updates power and console controls to match new daemon output and API.
1 parent 264431a commit cc90fa3

File tree

4 files changed

+79
-114
lines changed

4 files changed

+79
-114
lines changed

app/Models/Node.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static function guzzleRequest($node)
6969

7070
// @TODO: Better solution to disabling verification. Security risk.
7171
self::$guzzle[$node] = new Client([
72-
'base_uri' => sprintf('https://%s:%s/', $nodeData->fqdn, $nodeData->daemonListen),
72+
'base_uri' => sprintf('http%s://%s:%s/', ($nodeData->https === true) ? 's' : '', $nodeData->fqdn, $nodeData->daemonListen),
7373
'timeout' => 10.0,
7474
'connect_timeout' => 5.0,
7575
'verify' => false,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class AddHttpsToggle extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::table('nodes', function (Blueprint $table) {
16+
$table->boolean('https')->after('fqdn')->default(false);
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*
23+
* @return void
24+
*/
25+
public function down()
26+
{
27+
Schema::table('nodes', function (Blueprint $table) {
28+
$table->dropColumn('https');
29+
});
30+
}
31+
}

resources/views/layouts/master.blade.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
$(window).load(function () {
4141
4242
// Main Socket Object
43-
socket = io('https://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/{{ $server->uuid }}', {
43+
socket = io('http{{ ($node->https === true) ? 's' : '' }}://{{ $node->fqdn }}:{{ $node->daemonListen }}/ws/{{ $server->uuid }}', {
4444
'query': 'token={{ $server->daemonSecret }}'
4545
});
4646
@@ -67,9 +67,12 @@
6767
}
6868
});
6969
70+
socket.on('error', function (err) {
71+
alert('There was an error while attemping to connect to the websocket: ' + err + '\n\nPlease try loading this page again.');
72+
});
73+
7074
// Socket Sends Server Status on Connect
7175
socket.on('initial_status', function (data) {
72-
7376
var color = '#E33200';
7477
var selector = 'fa-times-circle';
7578
@@ -79,12 +82,10 @@
7982
}
8083
8184
$('#applyUpdate').removeClass('fa-circle-o-notch fa-spinner fa-spin fa-check-circle fa-times-circle').addClass(selector).css({ color: color });
82-
8385
});
8486
8587
// Socket Recieves New Status from Scales
8688
socket.on('status', function(data) {
87-
8889
if(data.status !== 'crashed') {
8990
9091
var newStatus, selector = 'fa-times-circle';
@@ -100,10 +101,10 @@
100101
selector = "fa-check-circle";
101102
break;
102103
case 2:
103-
newStatus = 'STOPPING';
104+
newStatus = 'STARTING';
104105
break;
105106
case 3:
106-
newStatus = 'STARTING';
107+
newStatus = 'STOPPING';
107108
break;
108109
}
109110

resources/views/server/index.blade.php

Lines changed: 40 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@
102102
</div>
103103
<div class="col-md-6" style="text-align:center;">
104104
<hr />
105-
<button class="btn btn-success btn-sm start disabled" id="server_start">Start</button>
106-
<button class="btn btn-primary btn-sm restart disabled" id="server_restart">Restart</button>
107-
<button class="btn btn-danger btn-sm stop disabled" id="server_stop">Stop</button>
108-
<button class="btn btn-danger btn-sm stop disabled" id="kill_proc"><i class="fa fa-ban" data-toggle="tooltip" data-placement="top" title="Kill Running Process"></i></button>
105+
<button class="btn btn-success btn-sm disabled" data-attr="power" data-action="start">Start</button>
106+
<button class="btn btn-primary btn-sm disabled" data-attr="power" data-action="restart">Restart</button>
107+
<button class="btn btn-danger btn-sm disabled" data-attr="power" data-action="stop">Stop</button>
108+
<button class="btn btn-danger btn-sm disabled" data-attr="power" data-action="kill"><i class="fa fa-ban" data-toggle="tooltip" data-placement="top" title="Kill Running Process"></i></button>
109109
<button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#pauseConsole" id="pause_console"><small><i class="fa fa-pause fa-fw"></i></small></button>
110110
<div id="pw_resp" style="display:none;margin-top: 15px;"></div>
111111
</div>
@@ -164,6 +164,7 @@
164164
165165
// New Console Data Recieved
166166
socket.on('console', function (data) {
167+
console.log(JSON.stringify(data));
167168
$('#live_console').val($('#live_console').val() + data.line);
168169
$('#live_console').scrollTop($('#live_console')[0].scrollHeight);
169170
});
@@ -177,16 +178,16 @@
177178
'X-Access-Token': '{{ $server->daemonSecret }}',
178179
'X-Access-Server': '{{ $server->uuid }}'
179180
},
180-
url: 'https://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/log/750',
181+
url: 'http{{ $node->https ? 's' : '' }}://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/log',
181182
timeout: 10000
182183
}).done(function(data) {
183184
$('#live_console').val(data);
184185
$('#live_console').scrollTop($('#live_console')[0].scrollHeight);
185186
}).fail(function(jqXHR, textStatus, errorThrown) {
186-
$('#pw_resp').attr('class', 'alert alert-danger').html('Unable to load initial server log.').fadeIn().delay(5000).fadeOut();
187+
alert('Unable to load initial server log, try reloading the page.');
187188
});
188189
} else {
189-
$('#live_console').val('');
190+
$('#live_console').val('Server is currently off.');
190191
}
191192
updateServerPowerControls(data.status);
192193
updatePlayerListVisibility(data.status);
@@ -225,7 +226,7 @@
225226
var cpuChart = new Chart(cty).Bar(cpuChartData,{animation:!1,showScale:!0,barShowStroke:!1,scaleOverride:!1,tooltipTemplate:"<%= value %> %",barValueSpacing:1,barStrokeWidth:1,scaleShowGridLines:!1});
226227
function updatePlayerListVisibility(data) {
227228
// Server is On or Starting
228-
if(data == 1 || data == 3) {
229+
if(data !== 0) {
229230
$('#stats_players').show();
230231
} else {
231232
$('#stats_players').hide();
@@ -248,9 +249,10 @@ function updatePlayerListVisibility(data) {
248249
'X-Access-Token': '{{ $server->daemonSecret }}',
249250
'X-Access-Server': '{{ $server->uuid }}'
250251
},
251-
url: 'https://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/console',
252+
contentType: 'application/json; charset=utf-8',
253+
url: 'http{{ $node->https ? 's' : '' }}://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/command',
252254
timeout: 10000,
253-
data: { command: ccmd }
255+
data: JSON.stringify({ command: ccmd })
254256
}).fail(function (jqXHR) {
255257
$('#sc_resp').html('Unable to process your request. Please try again.').fadeIn().delay(5000).fadeOut();
256258
}).always(function () {
@@ -265,126 +267,57 @@ function updateServerPowerControls (data) {
265267
266268
// Server is On or Starting
267269
if(data == 1 || data == 3) {
268-
$('#server_start').addClass('disabled');
269-
$('#server_stop, #server_restart').removeClass('disabled');
270+
$('[data-attr="power"][data-action="start"]').addClass('disabled');
271+
$('[data-attr="power"][data-action="stop"], [data-attr="power"][data-action="restart"]').removeClass('disabled');
270272
} else {
271-
$('#server_start').removeClass('disabled');
272-
$('#server_stop, #server_restart').addClass('disabled');
273+
$('[data-attr="power"][data-action="start"]').removeClass('disabled');
274+
$('[data-attr="power"][data-action="stop"], [data-attr="power"][data-action="restart"]').addClass('disabled');
273275
}
274276
275277
if(data !== 0) {
276-
$('#kill_proc').removeClass('disabled');
278+
$('[data-attr="power"][data-action="kill"]').removeClass('disabled');
277279
} else {
278-
$('#kill_proc').addClass('disabled');
280+
$('[data-attr="power"][data-action="kill"]').addClass('disabled');
279281
}
280282
281283
}
282284
283-
// Kill Server Process Button
284-
$('#kill_proc').click(function (e){
285-
286-
e.preventDefault();
287-
var killConfirm = confirm('WARNING: This operation will not save your server data gracefully. You should only use this if your server is failing to respond to stops.');
285+
$('[data-attr="power"]').click(function (event) {
286+
event.preventDefault();
287+
var action = $(this).data('action');
288+
if (action === 'kill') {
289+
var killConfirm = confirm('WARNING: This operation will not save your server data gracefully. You should only use this if your server is failing to respond to normal stop commands.');
290+
} else { var killConfirm = true; }
288291
289292
if(killConfirm) {
293+
if (action === 'start') {
294+
$("#live_console").val('');
295+
}
290296
$.ajax({
291-
type: 'GET',
297+
type: 'PUT',
292298
headers: {
293299
'X-Access-Token': '{{ $server->daemonSecret }}',
294300
'X-Access-Server': '{{ $server->uuid }}'
295301
},
296-
url: 'https://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/power/kill',
302+
contentType: 'application/json; charset=utf-8',
303+
data: JSON.stringify({
304+
action: action
305+
}),
306+
url: 'http{{ $node->https ? 's' : '' }}://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/power',
297307
timeout: 10000
298308
}).done(function(data) {
299-
$('#pw_resp').attr('class', 'alert alert-success').html('Server has been killed successfully.').fadeIn().delay(5000).fadeOut();
300-
}).fail(function(jqXHR, textStatus, errorThrown) {
301-
$('#pw_resp').attr('class', 'alert alert-danger').html('Unable to process your request. Please try again. ('+ errorThrown +')').fadeIn().delay(5000).fadeOut();
309+
$('#pw_resp').attr('class', 'alert alert-success').html('Server has been ' + action + 'ed successfully.').fadeIn().delay(5000).fadeOut();
310+
}).fail(function(jqXHR) {
311+
var error = 'An unknown error occured processing this request.';
312+
if (typeof jqXHR.responseJSON.error !== 'undefined') {
313+
error = jqXHR.responseJSON.error;
314+
}
315+
$('#pw_resp').attr('class', 'alert alert-danger').html('Unable to process your request. Please try again. (' + error + ')').fadeIn().delay(5000).fadeOut();
302316
});
303317
}
304318
305319
});
306320
307-
$('#server_stop').click(function (e){
308-
e.preventDefault();
309-
if(can_run) {
310-
can_run = false;
311-
$(this).append(' <i class=\'fa fa-refresh fa-spin\'></i>').toggleClass('disabled');
312-
$.ajax({
313-
type: 'GET',
314-
headers: {
315-
'X-Access-Token': '{{ $server->daemonSecret }}',
316-
'X-Access-Server': '{{ $server->uuid }}'
317-
},
318-
url: 'https://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/power/off',
319-
timeout: 10000
320-
}).done(function(data) {
321-
$('#pw_resp').attr('class', 'alert alert-success').html('Server is now stopping...').fadeIn().delay(5000).fadeOut();
322-
$('#server_stop').removeClass('disabled').html('Stop');
323-
can_run = true;
324-
return false;
325-
}).fail(function(jqXHR, textStatus, errorThrown) {
326-
$('#pw_resp').attr('class', 'alert alert-danger').html('Unable to process your request. Please try again. ('+ errorThrown +')').fadeIn().delay(5000).fadeOut();
327-
$('#server_stop').removeClass('disabled').html('Stop');
328-
can_run = true;
329-
});
330-
}
331-
});
332-
333-
$('#server_restart').click(function(e){
334-
e.preventDefault();
335-
if(can_run) {
336-
can_run = false;
337-
$(this).append(' <i class=\'fa fa-refresh fa-spin\'></i>').toggleClass('disabled');
338-
$.ajax({
339-
type: 'GET',
340-
headers: {
341-
'X-Access-Token': '{{ $server->daemonSecret }}',
342-
'X-Access-Server': '{{ $server->uuid }}'
343-
},
344-
url: 'https://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/power/restart',
345-
timeout: 10000
346-
}).done(function(data) {
347-
$('#pw_resp').attr('class', 'alert alert-success').html('Server is now restarting...').fadeIn().delay(5000).fadeOut();
348-
$('#server_restart').removeClass('disabled').html('Restart');
349-
can_run = true;
350-
return false;
351-
}).fail(function(jqXHR, textStatus, errorThrown) {
352-
$('#pw_resp').attr('class', 'alert alert-danger').html('Unable to process your request. Please try again. ('+ errorThrown +')').fadeIn().delay(5000).fadeOut();
353-
$('#server_restart').removeClass('disabled').html('Restart');
354-
can_run = true;
355-
});
356-
}
357-
});
358-
$('#server_start').click(function(e){
359-
e.preventDefault();
360-
start_server();
361-
});
362-
function start_server() {
363-
if(can_run === true){
364-
can_run = false;
365-
$('#server_start').append(' <i class=\'fa fa-refresh fa-spin\'></i>').toggleClass('disabled');
366-
$.ajax({
367-
type: 'GET',
368-
headers: {
369-
'X-Access-Token': '{{ $server->daemonSecret }}',
370-
'X-Access-Server': '{{ $server->uuid }}'
371-
},
372-
url: 'https://{{ $node->fqdn }}:{{ $node->daemonListen }}/server/power/on',
373-
timeout: 10000
374-
}).done(function(data) {
375-
$('#live_console').val('Server is starting...\n');
376-
$('#pw_resp').attr('class', 'alert alert-success').html('Server is now starting...').fadeIn().delay(5000).fadeOut();
377-
$('#server_start').toggleClass('disabled');
378-
$('#server_start').html('Start');
379-
can_run = true;
380-
}).fail(function(jqXHR, textStatus, errorThrown) {
381-
$('#pw_resp').attr('class', 'alert alert-danger').html('Unable to process your request. Please try again. ('+ errorThrown +')').fadeIn().delay(5000).fadeOut();
382-
$('#server_start').removeClass('disabled');
383-
$('#server_start').html('Start');
384-
can_run = true;
385-
});
386-
}
387-
}
388321
@endcan
389322
});
390323

0 commit comments

Comments
 (0)