diff options
Diffstat (limited to 'src/main/java/net/libertacasa/pubsh')
-rw-r--r-- | src/main/java/net/libertacasa/pubsh/web/SchedulerBean.java | 34 | ||||
-rw-r--r-- | src/main/java/net/libertacasa/pubsh/web/WebApplication.java | 17 |
2 files changed, 39 insertions, 12 deletions
diff --git a/src/main/java/net/libertacasa/pubsh/web/SchedulerBean.java b/src/main/java/net/libertacasa/pubsh/web/SchedulerBean.java index e0bfd24..b68a14a 100644 --- a/src/main/java/net/libertacasa/pubsh/web/SchedulerBean.java +++ b/src/main/java/net/libertacasa/pubsh/web/SchedulerBean.java @@ -1,6 +1,5 @@ package net.libertacasa.pubsh.web; -import java.time.Duration; import java.time.Instant; import org.springframework.boot.CommandLineRunner; @@ -10,20 +9,32 @@ import org.springframework.context.annotation.Configuration; import com.github.kagkarlsson.scheduler.Scheduler; import com.github.kagkarlsson.scheduler.task.Task; import com.github.kagkarlsson.scheduler.task.helper.Tasks; -import static com.github.kagkarlsson.scheduler.task.schedule.Schedules.fixedDelay; +//import static com.github.kagkarlsson.scheduler.task.schedule.Schedules.fixedDelay; @Configuration public class SchedulerBean { - @Bean - Task<Void> recurringSampleTask(CounterService counter) { - return Tasks - .recurring("recurring-sample-task", fixedDelay(Duration.ofMinutes(1))) - .execute((instance, ctx) -> { - System.out.printf("Recurring testing task. Instance: %s, ctx: %s\n", instance, ctx); - CounterService.increase(); - }); - } +// @Bean +// Task<Void> recurringSampleTask(CounterService counter) { +// return Tasks +// .recurring("recurring-sample-task", fixedDelay(Duration.ofMinutes(1))) +// .execute((instance, ctx) -> { +// System.out.printf("Recurring testing task. Instance: %s, ctx: %s\n", instance, ctx); +// CounterService.increase(); +// }); +// } + + @Bean + public static Task<Void> shellRemovalTask() { + + return Tasks.oneTime("shell-removal") + .execute((instance, ctx) -> { + System.out.printf("Running container removal task - Instance: %s, ctx: %s\n", instance, ctx); + String username = instance.getId().split("&")[0]; + String containerid = instance.getId().split("&")[1]; + Docker.deleteShell(username, containerid); + }); + } @Bean Task<Void> sampleOneTimeTask() { @@ -33,6 +44,7 @@ public class SchedulerBean { }); } + // keeping this as a quick way to check if the scheduler booted up after an application restart @Bean CommandLineRunner executeOnStartup(Scheduler scheduler, Task<Void> sampleOneTimeTask) { System.out.println("Scheduling one-shot testing task to execute now."); diff --git a/src/main/java/net/libertacasa/pubsh/web/WebApplication.java b/src/main/java/net/libertacasa/pubsh/web/WebApplication.java index e04aa48..5407256 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.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -13,6 +14,7 @@ import org.keycloak.adapters.KeycloakDeployment; import org.keycloak.adapters.RefreshableKeycloakSecurityContext; import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken; import org.keycloak.representations.IDToken; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.MediaType; @@ -28,10 +30,19 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.github.dockerjava.api.model.Container; import com.github.dockerjava.api.model.Image; +import com.github.kagkarlsson.scheduler.Scheduler; @SpringBootApplication @Controller public class WebApplication { + + //private final CounterService counter; + private static Scheduler scheduler; + + @Autowired + public void Scheduler(Scheduler scheduler) { + WebApplication.scheduler = scheduler; + } static ArrayList<String> availableOs = new ArrayList<String>(); @@ -228,15 +239,18 @@ public class WebApplication { Map<String, Object> customClaims = token.getOtherClaims(); username = String.valueOf(customClaims.get("username")); String os = (String) body.getFirst("osChoice"); + Integer expiry = (Integer) Integer.parseInt(body.getFirst("expiry")); System.out.printf("New shell with OS %s requested by %s (%s)\n", os, userid, username); Random rand = new Random(); Integer randomInt = rand.nextInt(9999999-1111); Integer count = randomInt; - + try { String containerid = Docker.createShell(username, os, count); + String instanceid = username + "&" + containerid; + scheduler.schedule(SchedulerBean.shellRemovalTask().instance(instanceid), Instant.now().plusSeconds(expiry)); String returnmessage = "Success - spawned shell " + username + "_" + os + "_" + count + " - internal ID: " + containerid; redirectAttributes.addFlashAttribute("message", returnmessage); } catch (Exception exception) { @@ -244,6 +258,7 @@ public class WebApplication { String returnmessage = "Error - failed to build image :-("; redirectAttributes.addFlashAttribute("message", returnmessage); System.out.println(exception); + //exception.printStackTrace(); } return("redirect:/portal"); |