diff options
| -rw-r--r-- | .gitignore | 3 | ||||
| -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 | ||||
| -rw-r--r-- | src/main/resources/templates/portal.html | 5 | 
4 files changed, 47 insertions, 12 deletions
@@ -31,3 +31,6 @@ build/  ### VS Code ###  .vscode/ + +### Vim ### +.metals/ 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"); diff --git a/src/main/resources/templates/portal.html b/src/main/resources/templates/portal.html index a9319a4..524f878 100644 --- a/src/main/resources/templates/portal.html +++ b/src/main/resources/templates/portal.html @@ -18,6 +18,11 @@  			        th:attr="value=${osoption}"  			        th:text="${osoption}"></option>		  			</select> +			<select class="form-control" th:object="${expiry}" name="expiry"> +			<option value="">Select expiry ...</option> +			<option th:value="5" th:attr="value=5" th:text="5"></option> +			<option th:value="10" th:attr="value=10" th:text="10"></option> +			</select>  			<button class="btn btn-primary" th:id="request_submission" th:type="submit">Generate</button>  			</form>		  		<div th:if="${attribute01 != null}" th:text="${attribute01}"></div>  | 
