Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vm control button for AdminWeb #483

Merged
merged 2 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api-runtime/rest-runtime/CBSpiderRuntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ func RunServer() {
{"GET", "/vmstatus/:Name", GetVMStatus},

{"GET", "/controlvm/:Name", ControlVM}, // suspend, resume, reboot
{"PUT", "/controlvm/:Name", ControlVM}, // suspend, resume, reboot

//-------------------------------------------------------------------//
//----------SSH RUN
Expand Down
37 changes: 36 additions & 1 deletion api-runtime/rest-runtime/admin-web/AdminWeb-CCM.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,9 @@ func makeVMTRList_html(connConfig string, bgcolor string, height string, fontSiz
<font size=%s>$$NUM$$</font>
</td>
<td>
<font size=%s>$$VMNAME$$</font>
<font size=%s>$$VMNAME$$</font>
<br>
<font size=%s>$$VMCONTROL$$</font>
</td>
<td>
<font size=%s>$$VMSTATUS$$</font>
Expand Down Expand Up @@ -911,6 +913,14 @@ func makeVMTRList_html(connConfig string, bgcolor string, height string, fontSiz
str = strings.ReplaceAll(str, "$$VMNAME$$", one.IId.NameId)
status := vmStatus(connConfig, one.IId.NameId)
str = strings.ReplaceAll(str, "$$VMSTATUS$$", status)

if cres.VMStatus(status) == cres.Running {
str = strings.ReplaceAll(str, "$$VMCONTROL$$", `<span id="vmcontrol-`+one.IId.NameId+`">[<a href="javascript:vmControl('`+one.IId.NameId+`','suspend')">Suspend</a> / <a href="javascript:vmControl('`+one.IId.NameId+`','reboot')">Reboot</a>]</span>`)
} else if cres.VMStatus(status) == cres.Suspended {
str = strings.ReplaceAll(str, "$$VMCONTROL$$", `<span id="vmcontrol-`+one.IId.NameId+`">[<a href="javascript:vmControl('`+one.IId.NameId+`','resume')">Resume</a>]</span>`)
} else {
str = strings.ReplaceAll(str, "$$VMCONTROL$$", `[<span style="color:brown">vm control disabed</span>] <br> you can control when Running / Suspended. <br> try refresh page...`)
}
str = strings.ReplaceAll(str, "$$LASTSTARTTIME$$", one.StartTime.Format("2006.01.02 15:04:05 Mon"))

// for Image & Spec
Expand Down Expand Up @@ -976,6 +986,30 @@ func makeVMTRList_html(connConfig string, bgcolor string, height string, fontSiz
return strData
}

// make the string of javascript function
func makeVMControlFunc_js() string {
//curl -sX GET http://localhost:1024/spider/controlvm/vm-01?action=suspend -H 'Content-Type: application/json' -d '{ "ConnectionName": "'${CONN_CONFIG}'"}'

strFunc := `
function vmControl(vmName, action) {
var connConfig = parent.frames["top_frame"].document.getElementById("connConfig").innerHTML;

document.getElementById("vmcontrol-" + vmName).innerHTML = '<span style="color:red">Waiting...</span>';
setTimeout(function(){
var xhr = new XMLHttpRequest();
xhr.open("PUT", "$$SPIDER_SERVER$$/spider/controlvm/" + vmName + "?action=" + action, false);
xhr.setRequestHeader('Content-Type', 'application/json');
sendJson = '{ "ConnectionName": "' + connConfig + '"}'
xhr.send(sendJson);

location.reload();
}, 10);
}
`
strFunc = strings.ReplaceAll(strFunc, "$$SPIDER_SERVER$$", "http://"+cr.ServiceIPorName+cr.ServicePort) // cr.ServicePort = ":1024"
return strFunc
}

// make the string of javascript function
func makePostVMFunc_js() string {

Expand Down Expand Up @@ -1096,6 +1130,7 @@ func VM(c echo.Context) error {
htmlStr += makeCheckBoxToggleFunc_js()
htmlStr += makePostVMFunc_js()
htmlStr += makeDeleteVMFunc_js()
htmlStr += makeVMControlFunc_js()

htmlStr += `
</script>
Expand Down