From 15bc4212c06d648795c127222cc28f618bb9a3e4 Mon Sep 17 00:00:00 2001 From: Rizul Gupta Date: Fri, 22 Dec 2023 22:30:23 +0530 Subject: [PATCH] func deployChallenge fixes --- cliHelpers/chalDeployerService/helper.go | 8 ++++++- cmd/root.go | 1 + lib/wireguard/firewall.go | 2 +- .../challengedeployerservice/controller.go | 23 +++++++++++++++---- services/challengedeployerservice/helper.go | 17 +++++++++++++- services/master/server.go | 3 ++- 6 files changed, 46 insertions(+), 8 deletions(-) diff --git a/cliHelpers/chalDeployerService/helper.go b/cliHelpers/chalDeployerService/helper.go index 56cc5691..33a94bc5 100644 --- a/cliHelpers/chalDeployerService/helper.go +++ b/cliHelpers/chalDeployerService/helper.go @@ -21,8 +21,14 @@ func DeployChallenge() error { challengeType := "web" log.Println("Starting") + katanaDir, err := utils.GetKatanaRootPath() + if err != nil { + return err + } + challengesDir:=katanaDir+"/challenges" + //Read folder challenge by os - dir, err := os.Open("./challenges") + dir, err := os.Open(challengesDir) //Loop over all subfolders in the challenge folder if err != nil { diff --git a/cmd/root.go b/cmd/root.go index 80e78b4c..14f4e539 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -32,4 +32,5 @@ func init() { rootCmd.AddCommand(infraService.GitCmd) rootCmd.AddCommand(infraService.CreateTeamCmd) rootCmd.AddCommand(chalDeployerService.DelChalCmd) + rootCmd.AddCommand(chalDeployerService.DeployChalCmd) } diff --git a/lib/wireguard/firewall.go b/lib/wireguard/firewall.go index ba78798f..01925699 100644 --- a/lib/wireguard/firewall.go +++ b/lib/wireguard/firewall.go @@ -76,7 +76,7 @@ func ApplyFirewall() error { if err != nil { log.Println(err) } - filepath = filepath + "/katana-services/Wireguard/root/defaults/firewall.conf" + filepath = filepath + "/katana-services/wireguard/root/defaults/firewall.conf" err = os.WriteFile(filepath, []byte(finalIprules), 0644) if err != nil { fmt.Println("Error writing to file:", err) diff --git a/services/challengedeployerservice/controller.go b/services/challengedeployerservice/controller.go index 3b36be46..3be67f9c 100644 --- a/services/challengedeployerservice/controller.go +++ b/services/challengedeployerservice/controller.go @@ -27,8 +27,14 @@ func Deploy(c *fiber.Ctx) error { challengeType := "web" log.Println("Starting") + katanaDir, err := utils.GetKatanaRootPath() + if err != nil { + return err + } + challengesDir:=katanaDir+"/challenges" + //Read folder challenge by os - dir, err := os.Open("./challenges") + dir, err := os.Open(challengesDir) //Loop over all subfolders in the challenge folder if err != nil { @@ -80,9 +86,18 @@ func Deploy(c *fiber.Ctx) error { } } } - CopyChallengeIntoTsuka(challengePath, folderName, challengeType) - CopyFlagDataIntoKashira(challengePath, folderName) - CopyChallengeCheckerIntoKissaki(challengePath, folderName) + err = CopyChallengeIntoTsuka(challengePath, folderName, challengeType) + if err != nil { + return err + } + err = CopyChallengeIntoTsuka(challengePath, folderName, challengeType) + if err != nil { + return err + } + err = CopyChallengeIntoTsuka(challengePath, folderName, challengeType) + if err != nil { + return err + } } } return c.JSON(res) diff --git a/services/challengedeployerservice/helper.go b/services/challengedeployerservice/helper.go index c9a9720f..9664fbc7 100644 --- a/services/challengedeployerservice/helper.go +++ b/services/challengedeployerservice/helper.go @@ -5,6 +5,7 @@ import ( "log" "os" "strconv" + "strings" git "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/config" @@ -28,6 +29,18 @@ func CopyChallengeIntoTsuka(dirPath string, challengeName string, challengeType err := os.Mkdir("teams/"+path, 0755) if err != nil { log.Println(err) + if (strings.Contains(err.Error(), "file exists")) { + err = os.RemoveAll("teams/" + path) + if err != nil { + log.Println(err) + } + err = os.Mkdir("teams/"+path, 0755) + if err != nil { + log.Println(err) + } + }else{ + return err + } } git.PlainInit("teams/"+path, false) repo, err := git.PlainOpen("teams/" + path) @@ -45,7 +58,9 @@ func CopyChallengeIntoTsuka(dirPath string, challengeName string, challengeType if err != nil { log.Println(err) - return err + if (!strings.Contains(err.Error(), "remote already exists")) { + return err + } } podsInTeam, err := utils.GetPods(map[string]string{ "app": g.ClusterConfig.TeamLabel, diff --git a/services/master/server.go b/services/master/server.go index 91a16df4..5d1d6113 100644 --- a/services/master/server.go +++ b/services/master/server.go @@ -7,6 +7,7 @@ import ( "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" + cfg "github.com/sdslabs/katana/configs" challengeDeployerService "github.com/sdslabs/katana/services/challengedeployerservice" infraSetService "github.com/sdslabs/katana/services/infrasetservice" @@ -47,7 +48,7 @@ func Server() error { admin.Get("/createTeams", infraSetService.CreateTeams) admin.Post("/challengeUpdate", challengeDeployerService.ChallengeUpdate) admin.Get("/deploy", challengeDeployerService.Deploy) - admin.Post("/deployChallenge", challengeDeployerService.DeployChallenge) + admin.Post("/deployChallenge", challengeDeployerService.Deploy) admin.Get("/gitServer", infraSetService.GitServer) admin.Get("/deleteChallenge/:chall_name", challengeDeployerService.DeleteChallenge) log.Printf("Listening on %s:%d\n", cfg.APIConfig.Host, cfg.APIConfig.Port)