diff --git a/include/json/send.h b/include/json/send.h new file mode 100644 index 0000000..3945644 --- /dev/null +++ b/include/json/send.h @@ -0,0 +1,8 @@ +#ifndef JSON_SEND_H +#define JSON_SEND_H +#include +void restartVM(String node, int vmid); +void restartContainer(String node, int containerid); +void restartNode(String node); + +#endif /* JSON_SEND_H */ diff --git a/include/manage.h b/include/manage.h new file mode 100644 index 0000000..8be4185 --- /dev/null +++ b/include/manage.h @@ -0,0 +1,5 @@ +#ifndef MANAGE_H +#define MANAGE_H + + +#endif /* MANAGE_H */ diff --git a/src/json/send.cpp b/src/json/send.cpp index e69de29..bb7ef22 100644 --- a/src/json/send.cpp +++ b/src/json/send.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include +#include + +void restartVM(String node, int vmid) +{ + HTTPClient http; + String apiAddress = PROXMOX_ADDRESS + "/api2/json/nodes/" + node + "/qemu/" + vmid + "/status/reboot"; + String auth = "PVEAPIToken=" + PROXMOX_TOKEN_USER + "!" + PROXMOX_TOKEN_NAME + "=" + PROXMOX_TOKEN_SECRET; + char authc[auth.length() + 1]; + auth.toCharArray(authc, auth.length() + 1); + http.begin(apiAddress); + http.setAuthorization(authc); + int httpCode = http.POST(""); + http.end(); + + if(httpCode == 0) { + throw std::runtime_error("Can't restart VM. Error code: " + httpCode); + } +} + +void restartContainer(String node, int containerid) +{ + HTTPClient http; + String apiAddress = PROXMOX_ADDRESS + "/api2/json/nodes/" + node + "/lxc/" + containerid + "/status/reboot"; + String auth = "PVEAPIToken=" + PROXMOX_TOKEN_USER + "!" + PROXMOX_TOKEN_NAME + "=" + PROXMOX_TOKEN_SECRET; + char authc[auth.length() + 1]; + auth.toCharArray(authc, auth.length() + 1); + http.begin(apiAddress); + http.setAuthorization(authc); + int httpCode = http.POST(""); + http.end(); + + if(httpCode == 0) { + throw std::runtime_error("Can't restart container. Error code: " + httpCode); + } +} + +void restartNode(String node) +{ + HTTPClient http; + String apiAddress = PROXMOX_ADDRESS + "/api2/json/nodes/" + node + "/status/reboot"; + String auth = "PVEAPIToken=" + PROXMOX_TOKEN_USER + "!" + PROXMOX_TOKEN_NAME + "=" + PROXMOX_TOKEN_SECRET; + char authc[auth.length() + 1]; + auth.toCharArray(authc, auth.length() + 1); + http.begin(apiAddress); + http.setAuthorization(authc); + int httpCode = http.POST(""); + http.end(); + + if(httpCode == 0) { + throw std::runtime_error("Can't restart container. Error code: " + httpCode); + } +} + diff --git a/src/manage.cpp b/src/manage.cpp index e69de29..0e1ce99 100644 --- a/src/manage.cpp +++ b/src/manage.cpp @@ -0,0 +1,159 @@ +#include +#include +#include +#include +#include +#include +#include + +void nodeRestart() +{ + Serial.println("node restart"); + selectedItem = 0; + + restartNode(selectedNode); + + GO.lcd.clearDisplay(); + GO.lcd.println("done"); + + delay(2000); +} + +void vmRestart() +{ + Serial.println("vm restart"); + selectedItem = 0; + int numVMs; + VM *vms = getVMInfo(&numVMs, selectedNode); + if (vms != NULL) + { + listVMs(vms, numVMs); + + restartVM(selectedNode, selectedVM); + + GO.lcd.clearDisplay(); + GO.lcd.println("done"); + + delay(2000); + } +} + +void containerRestart() +{ + Serial.println("lxc restart"); + selectedItem = 0; + int numContainers; + Container *containers = getContainerInfo(&numContainers, selectedNode); + if (containers != NULL) + { + listContainers(containers, numContainers); + + restartContainer(selectedNode, selectedLXC); + GO.lcd.clearDisplay(); + GO.lcd.println("done"); + + delay(2000); + } +} + + +// void nodeStart() +// { +// Serial.println("node start"); +// selectedItem = 0; + +// startNode(selectedNode); + +// GO.lcd.clearDisplay(); +// GO.lcd.println("done"); + +// delay(2000); +// } + +// void vmStart() +// { +// Serial.println("vm start"); +// selectedItem = 0; +// int numVMs; +// VM *vms = getVMInfo(&numVMs, selectedNode); +// if (vms != NULL) +// { +// listVMs(vms, numVMs); + +// startVM(selectedNode, selectedVM); + +// GO.lcd.clearDisplay(); +// GO.lcd.println("done"); + +// delay(2000); +// } +// } + +// void containerStart() +// { +// Serial.println("lxc start"); +// selectedItem = 0; +// int numContainers; +// Container *containers = getContainerInfo(&numContainers, selectedNode); +// if (containers != NULL) +// { +// listContainers(containers, numContainers); + +// startContainer(selectedNode, selectedLXC); +// GO.lcd.clearDisplay(); +// GO.lcd.println("done"); + +// delay(2000); +// } +// } + + +// void nodeStop() +// { +// Serial.println("node stop"); +// selectedItem = 0; + +// stopNode(selectedNode); + +// GO.lcd.clearDisplay(); +// GO.lcd.println("done"); + +// delay(2000); +// } + +// void vmStop() +// { +// Serial.println("vm stop"); +// selectedItem = 0; +// int numVMs; +// VM *vms = getVMInfo(&numVMs, selectedNode); +// if (vms != NULL) +// { +// listVMs(vms, numVMs); + +// stopVM(selectedNode, selectedVM); + +// GO.lcd.clearDisplay(); +// GO.lcd.println("done"); + +// delay(2000); +// } +// } + +// void containerStop() +// { +// Serial.println("lxc stop"); +// selectedItem = 0; +// int numContainers; +// Container *containers = getContainerInfo(&numContainers, selectedNode); +// if (containers != NULL) +// { +// listContainers(containers, numContainers); + +// stopContainer(selectedNode, selectedLXC); +// GO.lcd.clearDisplay(); +// GO.lcd.println("done"); + +// delay(2000); +// } +// } \ No newline at end of file