-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
392 additions
and
414 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package challengedeployerservice | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"regexp" | ||
"strconv" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
archiver "github.com/mholt/archiver/v3" | ||
g "github.com/sdslabs/katana/configs" | ||
"github.com/sdslabs/katana/lib/utils" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func DeployChallenge(c *fiber.Ctx) error { | ||
|
||
challengeType := "web" | ||
folderName := "" | ||
patch := false | ||
replicas := g.KatanaConfig.TeamDeployment | ||
log.Println("Starting") | ||
if form, err := c.MultipartForm(); err == nil { | ||
|
||
files := form.File["challenge"] | ||
|
||
// Loops through all challenges, if multiple uploaded : | ||
for _, file := range files { | ||
|
||
//creates folders for each challenge | ||
pattern := `([^/]+)\.tar\.gz$` | ||
regex := regexp.MustCompile(pattern) | ||
match := regex.FindStringSubmatch(file.Filename) | ||
folderName = match[1] | ||
|
||
response, challengePath := createFolder(folderName) | ||
if response == 1 { | ||
log.Println("Directory already exists with same name") | ||
return c.SendString("Directory already exists with same name") | ||
} else if response == 2 { | ||
log.Println("Issue with creating chall directory.Check permissions") | ||
return c.SendString("Issue with creating chall directory.Check permissions") | ||
} | ||
|
||
//save to disk in that directory | ||
if err := c.SaveFile(file, fmt.Sprintf("./challenges/%s/%s", folderName, file.Filename)); err != nil { | ||
return err | ||
} | ||
|
||
//Create folder inside the challenge folder | ||
err = os.Mkdir(challengePath+"/"+folderName, 0777) | ||
if err != nil { | ||
log.Println("Error in creating folder inside challenge folder") | ||
return c.SendString("Error in creating folder inside challenge folder") | ||
} | ||
|
||
//extract the tar.gz file | ||
err := archiver.Unarchive("./challenges/"+folderName+"/"+file.Filename, "./challenges/"+folderName) | ||
if err != nil { | ||
log.Println("Error in unarchiving", err) | ||
return c.SendString("Error in unarchiving") | ||
} | ||
|
||
//Update challenge path to get dockerfile | ||
challengePath = challengePath + "/" + folderName | ||
|
||
utils.BuildDockerImage(folderName, challengePath) | ||
|
||
//Get no.of teams and DEPLOY CHALLENGE to each namespace (assuming they exist and /createTeams has been called) | ||
clusterConfig := g.ClusterConfig | ||
numberOfTeams := clusterConfig.TeamCount | ||
res := make([][]string, 0) | ||
for i := 0; i < int(numberOfTeams); i++ { | ||
log.Println("-----------Deploying challenge for team: " + strconv.Itoa(i) + " --------") | ||
teamName := "katana-team-" + strconv.Itoa(i) | ||
utils.DeployChallenge(folderName, teamName, patch, replicas) | ||
url, err := createServiceAndIngressForChallenge(folderName, teamName, 3000) | ||
if err != nil { | ||
res = append(res, []string{teamName, err.Error()}) | ||
} else { | ||
res = append(res, []string{teamName, url}) | ||
} | ||
} | ||
|
||
//Copy challenge in pods and etc. | ||
copyChallengeIntoTsuka(challengePath, folderName, challengeType) | ||
|
||
return c.JSON(res) | ||
} | ||
} | ||
log.Println("Ending") | ||
|
||
return c.SendString("Wrong file") | ||
} | ||
|
||
func DeleteChallenge(c *fiber.Ctx) error { | ||
|
||
challengeName := c.Params("challengeName") | ||
//Delete chall directory also ? | ||
log.Println("Challenge name is : " + challengeName) | ||
|
||
dirPath, _ := os.Getwd() | ||
challengePath := dirPath + "/challenges/" + challengeName | ||
log.Println("Deleting challenge folder :", challengePath) | ||
err := os.RemoveAll(challengePath) | ||
if err != nil { | ||
log.Println("Error in deleting challenge folder") | ||
return err | ||
} | ||
|
||
totalTeams := utils.GetTeamNumber() | ||
|
||
for i := 0; i < totalTeams; i++ { | ||
|
||
teamName := "team-" + strconv.Itoa(i) | ||
|
||
teamNamespace := "katana-" + teamName + "-ns" | ||
kubeclient, err := utils.GetKubeClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Println("---------------Deleting challenge for team: ", teamNamespace) | ||
serviceClient := kubeclient.CoreV1().Services(teamNamespace) | ||
deploymentsClient := kubeclient.AppsV1().Deployments(teamNamespace) | ||
|
||
//Get deployment | ||
deps, err := deploymentsClient.Get(context.TODO(), challengeName, metav1.GetOptions{}) | ||
if err != nil { | ||
log.Println(" Error in getting deployments associated with the challenge. ") | ||
continue | ||
//panic(err) | ||
} | ||
|
||
//Delete deployments | ||
if deps.Name != challengeName { | ||
log.Println("Deployment does not exist. Create one using /deploy route.") | ||
return nil | ||
} else { | ||
log.Println("Deleting deployment...") | ||
deletePolicy := metav1.DeletePropagationForeground | ||
err = deploymentsClient.Delete(context.TODO(), challengeName, metav1.DeleteOptions{PropagationPolicy: &deletePolicy}) | ||
if err != nil { | ||
log.Println("Error in deleting deployment.") | ||
continue | ||
} | ||
} | ||
|
||
//Check if service exists | ||
services, err := serviceClient.List(context.TODO(), metav1.ListOptions{}) | ||
if err != nil { | ||
log.Println(" Error in getting services for" + challengeName + "in the namespace " + teamNamespace) | ||
continue | ||
//panic(err) | ||
} | ||
|
||
flag := 0 | ||
for _, service := range services.Items { | ||
if service.Name == challengeName { | ||
flag = 1 | ||
} | ||
} | ||
if flag == 0 { | ||
log.Println("Service does not exist for the " + challengeName + " in namespace " + teamNamespace) | ||
continue | ||
} | ||
|
||
//Delete service | ||
log.Println("Deleting services associated with this challenge...") | ||
err = serviceClient.Delete(context.TODO(), challengeName, metav1.DeleteOptions{}) | ||
if err != nil { | ||
log.Println("Error in deleting service for "+challengeName+" in namespace "+teamNamespace, err) | ||
continue | ||
} | ||
|
||
} | ||
|
||
log.Println("Process completed") | ||
|
||
return c.SendString("Deleted challenge" + challengeName + "in all namespaces.") | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.