feat: some safety checks
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
|||||||
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/types/mount"
|
||||||
"github.com/docker/docker/api/types/network"
|
"github.com/docker/docker/api/types/network"
|
||||||
"github.com/docker/docker/api/types/swarm"
|
"github.com/docker/docker/api/types/swarm"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
@@ -149,6 +150,7 @@ func listServices(w http.ResponseWriter, r *http.Request){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
SERVICES:
|
||||||
for _, service := range list{
|
for _, service := range list{
|
||||||
var settings map[string]string = service.Spec.Labels;
|
var settings map[string]string = service.Spec.Labels;
|
||||||
|
|
||||||
@@ -159,10 +161,28 @@ func listServices(w http.ResponseWriter, r *http.Request){
|
|||||||
|
|
||||||
targetVolumes := strings.Split(settings["blazena.volumes"], ",");
|
targetVolumes := strings.Split(settings["blazena.volumes"], ",");
|
||||||
|
|
||||||
|
var validVolumeNames []string;
|
||||||
|
for _, mnt := range service.Spec.TaskTemplate.ContainerSpec.Mounts{
|
||||||
|
if mnt.Type != mount.TypeVolume {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
validVolumeNames = append(validVolumeNames, mnt.Source);
|
||||||
|
}
|
||||||
|
|
||||||
if !contains(validNodeHostnames, settings["blazena.node"]) {
|
if !contains(validNodeHostnames, settings["blazena.node"]) {
|
||||||
errMsg := "node with hostname:'"+ settings["blazena.node"] +"' does not exist.";
|
errMsg := "Node with hostname:'"+ settings["blazena.node"] +"' does not exist.";
|
||||||
slog.Warn("Invalid Service Config!", slog.String("serviceId", service.ID), slog.String("errMessage", errMsg));
|
slog.Warn("Invalid Service Config!", slog.String("serviceId", service.ID), slog.String("errMessage", errMsg));
|
||||||
continue;
|
continue SERVICES;
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, volume := range targetVolumes{
|
||||||
|
if contains(validVolumeNames, volume){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
errMsg := "Volume name '"+ volume + "' is not in the service spec!";
|
||||||
|
slog.Warn("Invalid Service Config!", slog.String("serviceId", service.ID), slog.String("errMessage", errMsg));
|
||||||
|
continue SERVICES;
|
||||||
}
|
}
|
||||||
|
|
||||||
services = append(services, aService{
|
services = append(services, aService{
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
package host
|
package host
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"bytes"
|
"os"
|
||||||
|
|
||||||
cfg "github.com/rony5394/blazena/config"
|
cfg "github.com/rony5394/blazena/config"
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -19,12 +22,15 @@ func exchangeKeys(Config cfg.Config, sshKeyPem string)string{
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Failed to marshal body."+ err.Error());
|
panic("Failed to marshal body."+ err.Error());
|
||||||
|
slog.Error("Failed to marshal body.", slog.Any("propagatedError", err), slog.String("note", "Input for this marshal operation is that ssh pk. So the kebab is going on!"))
|
||||||
|
os.Exit(42);
|
||||||
}
|
}
|
||||||
|
|
||||||
rq, err := http.NewRequest("POST", Config.DockerManagerBaseUrl + "/keys", bytes.NewBuffer(bodyEncoded));
|
rq, err := http.NewRequest("POST", Config.DockerManagerBaseUrl + "/keys", bytes.NewBuffer(bodyEncoded));
|
||||||
|
|
||||||
if err != nil{
|
if err != nil{
|
||||||
panic("Failed to create http request"+ err.Error());
|
slog.Error("Failed to create request", slog.Any("propagatedError", err), slog.String("note", "not send just create the object"));
|
||||||
|
os.Exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
rq.Header.Set("Authorization", "Bearer "+ token);
|
rq.Header.Set("Authorization", "Bearer "+ token);
|
||||||
@@ -32,7 +38,8 @@ func exchangeKeys(Config cfg.Config, sshKeyPem string)string{
|
|||||||
rs, err := http.DefaultClient.Do(rq);
|
rs, err := http.DefaultClient.Do(rq);
|
||||||
|
|
||||||
if err != nil{
|
if err != nil{
|
||||||
panic("Failed to send http request"+ err.Error());
|
slog.Error("Failed to send http request", slog.Any("propagatedError", err));
|
||||||
|
os.Exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
defer rs.Body.Close();
|
defer rs.Body.Close();
|
||||||
@@ -40,7 +47,8 @@ func exchangeKeys(Config cfg.Config, sshKeyPem string)string{
|
|||||||
rsBodyRaw, err := io.ReadAll(rs.Body);
|
rsBodyRaw, err := io.ReadAll(rs.Body);
|
||||||
|
|
||||||
if err != nil{
|
if err != nil{
|
||||||
panic("Failed to read response's body!"+err.Error());
|
slog.Error("Failed to read response body!" , slog.Any("propagatedError", err));
|
||||||
|
os.Exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
var rsBody struct{
|
var rsBody struct{
|
||||||
@@ -49,7 +57,8 @@ func exchangeKeys(Config cfg.Config, sshKeyPem string)string{
|
|||||||
|
|
||||||
err = json.Unmarshal(rsBodyRaw, &rsBody);
|
err = json.Unmarshal(rsBodyRaw, &rsBody);
|
||||||
if err != nil{
|
if err != nil{
|
||||||
panic("Failed to unmarshal rsBodyRaw!"+ err.Error());
|
slog.Error("Failed to unmarshal rsBodyRaw!", slog.Any("propagatedError", err));
|
||||||
|
os.Exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -102,6 +102,8 @@ func Run(Config cfg.Config) {
|
|||||||
slog.Error("Failed to shutdown docker api!");
|
slog.Error("Failed to shutdown docker api!");
|
||||||
os.Exit(1);
|
os.Exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Info("Finished whole backup run.");
|
||||||
}
|
}
|
||||||
|
|
||||||
func getServices(Config cfg.Config)[]aService{
|
func getServices(Config cfg.Config)[]aService{
|
||||||
|
|||||||
12
main.go
12
main.go
@@ -8,6 +8,18 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/*
|
||||||
|
If the exit code is X then it means Y:
|
||||||
|
|
||||||
|
| X | Y |
|
||||||
|
|----|------------------------------------------------------------------------------------------|
|
||||||
|
| 0 | Everything should be good, normal exit. |
|
||||||
|
| 1 | Some common error, but that still mean it is going to crash. |
|
||||||
|
| 3 | Ask yourself if you are not using dev version in prod. If not then spam the developer. |
|
||||||
|
| 42 | WHAT THE ACTUAL ***** IS HAPPENING. or assume something is very wrong in the app itself. |
|
||||||
|
| 69 | [INSERT HERE] |
|
||||||
|
*/
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if(len(os.Args) < 2){
|
if(len(os.Args) < 2){
|
||||||
panic("Usage: blazena <mode>");
|
panic("Usage: blazena <mode>");
|
||||||
|
|||||||
Reference in New Issue
Block a user