feat: some safety checks
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/docker/docker/api/types/mount"
|
||||
"github.com/docker/docker/api/types/network"
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
"github.com/docker/docker/client"
|
||||
@@ -149,6 +150,7 @@ func listServices(w http.ResponseWriter, r *http.Request){
|
||||
}
|
||||
|
||||
|
||||
SERVICES:
|
||||
for _, service := range list{
|
||||
var settings map[string]string = service.Spec.Labels;
|
||||
|
||||
@@ -159,12 +161,30 @@ func listServices(w http.ResponseWriter, r *http.Request){
|
||||
|
||||
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"]) {
|
||||
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));
|
||||
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{
|
||||
ServiceId: service.ID,
|
||||
VolumeNames: targetVolumes,
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package host
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"bytes"
|
||||
"os"
|
||||
|
||||
cfg "github.com/rony5394/blazena/config"
|
||||
);
|
||||
|
||||
@@ -19,12 +22,15 @@ func exchangeKeys(Config cfg.Config, sshKeyPem string)string{
|
||||
|
||||
if err != nil {
|
||||
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));
|
||||
|
||||
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);
|
||||
@@ -32,7 +38,8 @@ func exchangeKeys(Config cfg.Config, sshKeyPem string)string{
|
||||
rs, err := http.DefaultClient.Do(rq);
|
||||
|
||||
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();
|
||||
@@ -40,7 +47,8 @@ func exchangeKeys(Config cfg.Config, sshKeyPem string)string{
|
||||
rsBodyRaw, err := io.ReadAll(rs.Body);
|
||||
|
||||
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{
|
||||
@@ -49,7 +57,8 @@ func exchangeKeys(Config cfg.Config, sshKeyPem string)string{
|
||||
|
||||
err = json.Unmarshal(rsBodyRaw, &rsBody);
|
||||
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!");
|
||||
os.Exit(1);
|
||||
}
|
||||
|
||||
slog.Info("Finished whole backup run.");
|
||||
}
|
||||
|
||||
func getServices(Config cfg.Config)[]aService{
|
||||
|
||||
12
main.go
12
main.go
@@ -8,6 +8,18 @@ import (
|
||||
"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() {
|
||||
if(len(os.Args) < 2){
|
||||
panic("Usage: blazena <mode>");
|
||||
|
||||
Reference in New Issue
Block a user