From 20d049ea10c14eb335014b5c8d150f55bf437377 Mon Sep 17 00:00:00 2001 From: Georg Pfuetzenreuter Date: Fri, 6 May 2022 18:25:22 +0200 Subject: Checkpoint Signed-off-by: Georg Pfuetzenreuter --- src/main/java/net/libertacasa/pubsh/web/API.java | 5 ++- .../java/net/libertacasa/pubsh/web/Docker.java | 17 +++++++++ .../net/libertacasa/pubsh/web/WebApplication.java | 40 +++++++++++++++++++++- 3 files changed, 60 insertions(+), 2 deletions(-) (limited to 'src/main/java/net/libertacasa') diff --git a/src/main/java/net/libertacasa/pubsh/web/API.java b/src/main/java/net/libertacasa/pubsh/web/API.java index 501fbc6..d14197e 100644 --- a/src/main/java/net/libertacasa/pubsh/web/API.java +++ b/src/main/java/net/libertacasa/pubsh/web/API.java @@ -14,13 +14,16 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import java.util.Random; @RestController public class API { @GetMapping("/hello") public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { - return String.format("Hello %s!", name); + Random rand = new Random(); + Integer randomInt = rand.nextInt(100-10); + return String.format("Hello %s! Your random number is %s!", name, randomInt); } @DeleteMapping("/backend/container/delete/{id}") diff --git a/src/main/java/net/libertacasa/pubsh/web/Docker.java b/src/main/java/net/libertacasa/pubsh/web/Docker.java index 536a80e..1460bb3 100644 --- a/src/main/java/net/libertacasa/pubsh/web/Docker.java +++ b/src/main/java/net/libertacasa/pubsh/web/Docker.java @@ -1,11 +1,15 @@ package net.libertacasa.pubsh.web; +import java.io.File; import java.time.Duration; import java.util.Arrays; import java.util.Collection; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.api.command.BuildImageResultCallback; import com.github.dockerjava.api.command.InspectExecResponse.Container; import com.github.dockerjava.api.command.ListContainersCmd; import com.github.dockerjava.api.model.Image; @@ -77,4 +81,17 @@ public class Docker { dockerClient.removeContainerCmd(id).exec(); } + public static String buildImage(String targetUser, String osChoice, Integer count) { + String dockerfile = "classpath:docker/Dockerfile-" + osChoice; + String tag = targetUser + ":sh" + count; + Set tags = new HashSet(); + tags.add(tag); + String imgid = dockerClient.buildImageCmd() + .withDockerfile(new File(dockerfile)) + .withPull(false).withNoCache(false).withTags(tags) + .exec(new BuildImageResultCallback()).awaitImageId(); + + return(imgid); + } + } diff --git a/src/main/java/net/libertacasa/pubsh/web/WebApplication.java b/src/main/java/net/libertacasa/pubsh/web/WebApplication.java index faef657..ca6ea7e 100644 --- a/src/main/java/net/libertacasa/pubsh/web/WebApplication.java +++ b/src/main/java/net/libertacasa/pubsh/web/WebApplication.java @@ -1,6 +1,7 @@ package net.libertacasa.pubsh.web; import java.security.Principal; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -21,12 +22,15 @@ import org.springframework.ui.Model; import org.springframework.web.bind.annotation.DeleteMapping; //import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; //import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.github.dockerjava.api.model.Container; import com.github.dockerjava.api.model.Image; +import java.util.Random; @SpringBootApplication @@ -81,7 +85,17 @@ public class WebApplication { //System.out.println(containers); model.addAttribute("docker_images", images); - model.addAttribute("docker_containers", containers); + model.addAttribute("docker_containers", containers); + + ArrayList availableOs = new ArrayList(); + availableOs.add("archlinux"); + availableOs.add("opensuse-leap"); + availableOs.add("opensuse-tumbleweed"); + availableOs.add("ubuntu"); + model.addAttribute("availableOs", availableOs); + model.addAttribute("osChoice", new String()); + + return("portal"); } @@ -104,6 +118,30 @@ public class WebApplication { return("redirect:/portal"); } + @PostMapping("/frontend/container/add") + public static String addContainer(@PathVariable String osChoice, HttpServletRequest request, Model model) { + KeycloakAuthenticationToken principal = (KeycloakAuthenticationToken) request.getUserPrincipal(); + String username= null; + String userid = principal.getName(); + IDToken token = principal.getAccount().getKeycloakSecurityContext().getIdToken(); + Map customClaims = token.getOtherClaims(); + username = String.valueOf(customClaims.get("username")); + + model.addAttribute("osChoice", osChoice); + + + System.out.printf("New container with OS %s requested by %s (%s)", osChoice, userid, username); + + Random rand = new Random(); + Integer randomInt = rand.nextInt(9999999-1111); + Integer count = randomInt; + + //Docker.buildImage(username, osChoice, count); + + + return("redirect:/portal"); + } + @GetMapping(path = "/logout") public String logout(HttpServletRequest request) throws ServletException { request.logout(); -- cgit v1.2.3