Skip to content

Commit a2266cb

Browse files
committed
jfKVM : move snapshots to api
1 parent 3d8c07d commit a2266cb

File tree

3 files changed

+195
-27
lines changed

3 files changed

+195
-27
lines changed

projects/jfkvm/src/service/ConfigService.java

Lines changed: 94 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ private static class UI {
182182
public Runnable device_addr_complete;
183183

184184
public PopupPanel snapshots_popup;
185-
public VirtualMachine snapshots_vm;
185+
public String snapshots_vm;
186186
public Runnable snapshots_init;
187-
public Snapshot[] snapshots_list;
187+
public String[][] snapshots_list;
188188

189189
public PopupPanel snapshots_add_popup;
190190
public Runnable snapshots_add_init;
@@ -755,6 +755,11 @@ private PopupPanel vm_network_PopupPanel(UI ui) {
755755
return panel;
756756
}
757757

758+
private static final int SS_NAME = 0;
759+
private static final int SS_DESC = 1;
760+
private static final int SS_PARENT = 2;
761+
private static final int SS_CURRENT = 3;
762+
758763
private PopupPanel snapshots_PopupPanel(UI ui) {
759764
PopupPanel panel = new PopupPanel("Snapshots");
760765
panel.setPosition(256, 128);
@@ -792,16 +797,16 @@ private PopupPanel snapshots_PopupPanel(UI ui) {
792797

793798
ui.snapshots_init = () -> {
794799
if (ui.snapshots_vm == null) return;
795-
ui.snapshots_list = ui.snapshots_vm.snapshotList();
796-
Snapshot current = ui.snapshots_vm.snapshotGetCurrent();
800+
ui.snapshots_list = ui.host.snapshot_list(ui.snapshots_vm);
801+
String current = ui.host.snapshot_get_current(ui.snapshots_vm);
797802
if (current != null) {
798-
if (debug) JFLog.log("VM:snapshotList:current=" + current.name);
803+
if (debug) JFLog.log("VM:snapshotList:current=" + current);
799804
}
800805
table.removeAll();
801806
table.addRow(new String[] {"Name", "Description", "Parent", "Current"});
802807
if (ui.snapshots_list == null) return;
803-
for(Snapshot ss : ui.snapshots_list) {
804-
table.addRow(new String[] {ss.name, ss.desc, ss.parent, ss.current ? "yes" : ""});
808+
for(String[] ss : ui.snapshots_list) {
809+
table.addRow(ss);
805810
}
806811
};
807812

@@ -824,8 +829,8 @@ private PopupPanel snapshots_PopupPanel(UI ui) {
824829
Task task = new Task(createEvent("Delete Snapshot", ui)) {
825830
public void doTask() {
826831
try {
827-
Snapshot ss = ui.snapshots_list[idx];
828-
if (!ui.snapshots_vm.snapshotDelete(ss.name)) {
832+
String[] ss = ui.snapshots_list[idx];
833+
if (ui.host.snapshot_delete(ui.snapshots_vm, ss[SS_NAME]) == null) {
829834
throw new Exception("Delete failed");
830835
}
831836
setResult("Completed", true);
@@ -843,8 +848,8 @@ public void doTask() {
843848
errmsg.setText("");
844849
int idx = table.getSelectedRow();
845850
if (idx == -1) return;
846-
Snapshot ss = ui.snapshots_list[idx];
847-
if (ss.current) {
851+
String[] ss = ui.snapshots_list[idx];
852+
if (ss[SS_CURRENT].equals("yes")) {
848853
errmsg.setText("Error:snapshot already current");
849854
return;
850855
}
@@ -854,7 +859,7 @@ public void doTask() {
854859
Task task = new Task(createEvent("Restore Snapshot", ui)) {
855860
public void doTask() {
856861
try {
857-
if (!ui.snapshots_vm.snapshotRestore(ss.name)) {
862+
if (ui.host.snapshot_restore(ui.snapshots_vm, ss[SS_NAME]) == null) {
858863
throw new Exception("Restore failed");
859864
}
860865
setResult("Completed", true);
@@ -919,7 +924,8 @@ private PopupPanel snapshots_add_PopupPanel(UI ui) {
919924
desc.setText("");
920925
cb_memory.setSelected(false);
921926
if (ui.snapshots_vm != null) {
922-
int state = ui.snapshots_vm.getState();
927+
String str_state = ui.host.vm_get_state(ui.snapshots_vm);
928+
int state = VirtualMachine.getState(str_state);
923929
if (state == VirtualMachine.STATE_ON || state == VirtualMachine.STATE_SUSPEND) {
924930
cb_memory.setDisabled(false);
925931
} else {
@@ -944,7 +950,7 @@ public void doTask() {
944950
if (!cb_memory.isSelected()) {
945951
flags |= VirtualMachine.SNAPSHOT_CREATE_DISK_ONLY;
946952
}
947-
if (!ui.snapshots_vm.snapshotCreate(_name, _desc, flags)) {
953+
if (ui.host.snapshot_create(ui.snapshots_vm, _name, _desc, flags) == null) {
948954
throw new Exception("create failed");
949955
}
950956
setResult("Completed", true);
@@ -3107,7 +3113,7 @@ public void doTask() {
31073113
errmsg.setText("Error:no selection");
31083114
return;
31093115
}
3110-
ui.snapshots_vm = vms[idx];
3116+
ui.snapshots_vm = vms[idx].name;
31113117
ui.snapshots_init.run();
31123118
ui.snapshots_popup.setVisible(true);
31133119
});
@@ -3202,6 +3208,10 @@ public void doTask() {
32023208
return panel;
32033209
}
32043210

3211+
private static final int VM_NAME = 0;
3212+
private static final int VM_STATE = 1;
3213+
private static final int VM_STORAGE = 2;
3214+
32053215
private void vmsPanel_addHost(Panel panel, Host host, UI ui) {
32063216
Row row;
32073217

@@ -3221,6 +3231,8 @@ private void vmsPanel_addHost(Panel panel, Host host, UI ui) {
32213231
tools.add(restart);
32223232
Button poweroff = new Button(new Icon("power"), "PowerOff");
32233233
tools.add(poweroff);
3234+
Button snapshots = new Button(new Icon("clock"), "Snapshots");
3235+
tools.add(snapshots);
32243236

32253237
row = new Row();
32263238
panel.add(row);
@@ -3256,7 +3268,7 @@ private void vmsPanel_addHost(Panel panel, Host host, UI ui) {
32563268
errmsg.setText("Error:no selection");
32573269
return;
32583270
}
3259-
String vm_name = host_vms[idx][0];
3271+
String vm_name = host_vms[idx][VM_NAME];
32603272
Hardware hardware = ui.host.vm_load(vm_name);
32613273
if (hardware == null) {
32623274
errmsg.setText("Error:Failed to load config for vm:" + vm_name);
@@ -3274,8 +3286,8 @@ private void vmsPanel_addHost(Panel panel, Host host, UI ui) {
32743286
errmsg.setText("Error:no selection");
32753287
return;
32763288
}
3277-
String vm_name = host_vms[idx][0];
3278-
int state = VirtualMachine.getState(host_vms[idx][1]);
3289+
String vm_name = host_vms[idx][VM_NAME];
3290+
int state = VirtualMachine.getState(host_vms[idx][VM_STATE]);
32793291
if (state != VirtualMachine.STATE_OFF && state != VirtualMachine.STATE_SUSPEND) {
32803292
errmsg.setText("Error:VM is already running.");
32813293
return;
@@ -3304,8 +3316,8 @@ public void doTask() {
33043316
errmsg.setText("Error:no selection");
33053317
return;
33063318
}
3307-
String vm_name = host_vms[idx][0];
3308-
int state = VirtualMachine.getState(host_vms[idx][1]);
3319+
String vm_name = host_vms[idx][VM_NAME];
3320+
int state = VirtualMachine.getState(host_vms[idx][VM_STATE]);
33093321
if (state != VirtualMachine.STATE_ON) {
33103322
errmsg.setText("Error:VM is not running.");
33113323
return;
@@ -3334,8 +3346,8 @@ public void doTask() {
33343346
errmsg.setText("Error:no selection");
33353347
return;
33363348
}
3337-
String vm_name = host_vms[idx][0];
3338-
int state = VirtualMachine.getState(host_vms[idx][1]);
3349+
String vm_name = host_vms[idx][VM_NAME];
3350+
int state = VirtualMachine.getState(host_vms[idx][VM_STATE]);
33393351
if (state != VirtualMachine.STATE_ON) {
33403352
errmsg.setText("Error:VM is not running.");
33413353
return;
@@ -3364,8 +3376,8 @@ public void doTask() {
33643376
errmsg.setText("Error:no selection");
33653377
return;
33663378
}
3367-
String vm_name = host_vms[idx][0];
3368-
int state = VirtualMachine.getState(host_vms[idx][1]);
3379+
String vm_name = host_vms[idx][VM_NAME];
3380+
int state = VirtualMachine.getState(host_vms[idx][VM_STATE]);
33693381
if (state != VirtualMachine.STATE_ON) {
33703382
errmsg.setText("Error:VM is not running.");
33713383
return;
@@ -3394,8 +3406,8 @@ public void doTask() {
33943406
errmsg.setText("Error:no selection");
33953407
return;
33963408
}
3397-
String vm_name = host_vms[idx][0];
3398-
int state = VirtualMachine.getState(host_vms[idx][1]);
3409+
String vm_name = host_vms[idx][VM_NAME];
3410+
int state = VirtualMachine.getState(host_vms[idx][VM_STATE]);
33993411
if (state != VirtualMachine.STATE_ON && state != VirtualMachine.STATE_SUSPEND) {
34003412
errmsg.setText("Error:VM is not running.");
34013413
return;
@@ -3416,6 +3428,23 @@ public void doTask() {
34163428
};
34173429
ui.confirm_popup.setVisible(true);
34183430
});
3431+
snapshots.addClickListener((me, cmp) -> {
3432+
ui.host = host;
3433+
errmsg.setText("");
3434+
if (Linux.distro == Linux.DistroTypes.Debian && Linux.derived == Linux.DerivedTypes.Unknown) {
3435+
errmsg.setText("Debian snapshot support is broken, please use Ubuntu!");
3436+
return;
3437+
}
3438+
int idx = host_table.getSelectedRow();
3439+
if (idx == -1) {
3440+
errmsg.setText("Error:no selection");
3441+
return;
3442+
}
3443+
String vm_name = host_vms[idx][VM_NAME];
3444+
ui.snapshots_vm = vm_name;
3445+
ui.snapshots_init.run();
3446+
ui.snapshots_popup.setVisible(true);
3447+
});
34193448
}
34203449

34213450
private Panel vmAddPanel(VirtualMachine vm, Hardware hardware, UI ui) {
@@ -7307,6 +7336,8 @@ public void doTask() {
73077336
list.append(ss.desc);
73087337
list.append("\t");
73097338
list.append(ss.parent);
7339+
list.append("\t");
7340+
list.append(ss.current ? "yes": "");
73107341
list.append("\n");
73117342
}
73127343
return list.toString().getBytes();
@@ -7340,6 +7371,26 @@ public void doTask() {
73407371
Tasks.tasks.addTask(null, task);
73417372
return result.getBytes();
73427373
}
7374+
case "snapshot_get_current": {
7375+
String token = params.get("token");
7376+
if (!token.equals(Config.current.token)) return null;
7377+
String vm_name = params.get("vm");
7378+
String result = "";
7379+
try {
7380+
VirtualMachine vm = VirtualMachine.get(vm_name);
7381+
if (vm == null) {
7382+
throw new Exception("VM not found");
7383+
}
7384+
Snapshot ss = vm.snapshotGetCurrent();
7385+
if (ss != null) {
7386+
result = ss.name;
7387+
}
7388+
} catch (Exception e) {
7389+
JFLog.log(e);
7390+
result = "error";
7391+
}
7392+
return result.getBytes();
7393+
}
73437394
case "backup": {
73447395
String token = params.get("token");
73457396
if (!token.equals(Config.current.token)) return null;
@@ -7819,6 +7870,23 @@ public void doTask() {
78197870
Tasks.tasks.addTask(null, task);
78207871
return result.getBytes();
78217872
}
7873+
case "vm_get_state": {
7874+
String token = params.get("token");
7875+
if (!token.equals(Config.current.token)) return null;
7876+
String vm_name = params.get("vm");
7877+
String result = "";
7878+
try {
7879+
VirtualMachine vm = VirtualMachine.get(vm_name);
7880+
if (vm == null) {
7881+
throw new Exception("VM not found");
7882+
}
7883+
result = Integer.toString(vm.getState());
7884+
} catch (Exception e) {
7885+
JFLog.log(e);
7886+
result = "error";
7887+
}
7888+
return result.getBytes();
7889+
}
78227890
case "vm_start": {
78237891
String token = params.get("token");
78247892
if (!token.equals(Config.current.token)) return null;

projects/jfkvm/src/service/Host.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,21 @@ public String vm_power_off(String name) {
450450
}
451451
}
452452

453+
public String vm_get_state(String name) {
454+
if (!isValid(7.1f)) return null;
455+
try {
456+
HTTPS https = new HTTPS();
457+
if (!https.open(host)) throw new Exception("connect failed");
458+
byte[] data = https.get("/api/vm_get_state?token=" + token + "&vm=" + name);
459+
https.close();
460+
if (data == null || data.length == 0) return null;
461+
return new String(data);
462+
} catch (Exception e) {
463+
JFLog.log(e);
464+
return null;
465+
}
466+
}
467+
453468
public String[] browse_list(String path) {
454469
if (!isValid(7.0f)) return null;
455470
try {
@@ -466,6 +481,92 @@ public String[] browse_list(String path) {
466481
}
467482
}
468483

484+
public String[][] snapshot_list(String vm) {
485+
if (!isValid(7.1f)) return null;
486+
try {
487+
HTTPS https = new HTTPS();
488+
if (!https.open(host)) throw new Exception("connect failed");
489+
byte[] data = https.get("/api/snapshot_list?token=" + token + "&vm=" + vm);
490+
https.close();
491+
if (data == null || data.length == 0) return null;
492+
String[] sss = new String(data).split("\n");
493+
String[][] list = new String[sss.length][0];
494+
int pos = 0;
495+
for(String ss : sss) {
496+
list[pos++] = ss.split("\t", -1);
497+
}
498+
return list;
499+
} catch (Exception e) {
500+
JFLog.log(e);
501+
return null;
502+
}
503+
}
504+
505+
public String snapshot_create(String vm, String name, String desc, int flags) {
506+
if (!isValid(7.1f)) return null;
507+
try {
508+
String udesc = JF.encodeURL(desc);
509+
HTTPS https = new HTTPS();
510+
if (!https.open(host)) throw new Exception("connect failed");
511+
byte[] data = https.get("/api/snapshot_create?token=" + token + "&vm=" + vm + "&name=" + name + "&desc=" + udesc + "&flags=" + flags);
512+
https.close();
513+
if (data == null || data.length == 0) return null;
514+
String res = new String(data);
515+
return res;
516+
} catch (Exception e) {
517+
JFLog.log(e);
518+
return null;
519+
}
520+
}
521+
522+
public String snapshot_get_current(String vm) {
523+
if (!isValid(7.1f)) return null;
524+
try {
525+
HTTPS https = new HTTPS();
526+
if (!https.open(host)) throw new Exception("connect failed");
527+
byte[] data = https.get("/api/snapshot_get_current?token=" + token + "&vm=" + vm);
528+
https.close();
529+
if (data == null || data.length == 0) return null;
530+
String res = new String(data);
531+
return res;
532+
} catch (Exception e) {
533+
JFLog.log(e);
534+
return null;
535+
}
536+
}
537+
538+
public String snapshot_delete(String vm, String ss_name) {
539+
if (!isValid(7.1f)) return null;
540+
try {
541+
HTTPS https = new HTTPS();
542+
if (!https.open(host)) throw new Exception("connect failed");
543+
byte[] data = https.get("/api/snapshot_delete?token=" + token + "&vm=" + vm + "&name=" + ss_name);
544+
https.close();
545+
if (data == null || data.length == 0) return null;
546+
String res = new String(data);
547+
return res;
548+
} catch (Exception e) {
549+
JFLog.log(e);
550+
return null;
551+
}
552+
}
553+
554+
public String snapshot_restore(String vm, String ss_name) {
555+
if (!isValid(7.1f)) return null;
556+
try {
557+
HTTPS https = new HTTPS();
558+
if (!https.open(host)) throw new Exception("connect failed");
559+
byte[] data = https.get("/api/snapshot_restore?token=" + token + "&vm=" + vm + "&name=" + ss_name);
560+
https.close();
561+
if (data == null || data.length == 0) return null;
562+
String res = new String(data);
563+
return res;
564+
} catch (Exception e) {
565+
JFLog.log(e);
566+
return null;
567+
}
568+
}
569+
469570
public boolean setCephStart() {
470571
try {
471572
HTTPS https = new HTTPS();

src/javaforce/webui/static/webui.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,6 @@ function onresizeBody(event, element) {
438438

439439
//NOTE : onresize does NOT trigger if width/height are 100%
440440
function onresizeElement(event, element) {
441-
console.log('onresizeElement: id=' + element.id);
442441
var nodes = element.childNodes;
443442
var cnt = nodes.length;
444443
var node;

0 commit comments

Comments
 (0)