---
title: "Launch EI Engine"
author: "Russell Almond"
date: "July 3, 2020"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(EIEvent)
library(shiny)
source("/usr/local/share/Proc4/EIini.R")
```
## Evidence Identification Launcher

This launches evidence identification processes for _Physics Playground_.  This works according to the following steps:

1. The latest configuration information is pulled down from github (https://github.com/ralmond/PP-EI).  The branch listed in the field below is the one which will be checked out.

2. The config.json file is read to pick up the details.

3. The `appStem` field of the configuration file is consulted to find
   the applications to launch.

4. The EIEvent process is launched for each script.

## Login 

Only authorized people can make changes to the scoring models.

```{r passwordChecker, echo=FALSE}

checkPassword <- function (uid,passwd) {
    pwdline <- system2("/bin/grep",
                       c(sprintf("^%s:",uid),                                   "/usr/local/share/Proc4/p4pwds"),
                       stdout=TRUE)
    if (!is.null(attr(pwdline,"status")) || 
        !is.character(pwdline) || length(pwdline) == 0L || 
        nchar(pwdline)==0L) 
      return (FALSE)
    pwdbits <- strsplit(pwdline,"$",fixed=TRUE)[[1]]
    method <- sprintf("-%s",pwdbits[2])
    salt <- pwdbits[3]
    pwd <- system2("/usr/bin/openssl",
                     c("passwd",method, 
                       "-salt",salt, input$adminpwd),
                     stdout=TRUE)
    target <- paste("$",paste(pwdbits[-1],collapse="$"),sep="")
    return (pwd == target)
}

```


```{r login, echo=FALSE}
tags$head(tags$script(src = "message-handler.js"))
inputPanel(
  textInput("adminid", label = "Username:",
              width = 500),
  passwordInput("adminpwd", label = "Password:",
              width = 500),
  textInput("branch", label = "Vesion Branch:",
            value="PP-main",width = 500),
  actionButton("go","Go")
)

status <- reactiveVal("Ready.")
nclicks <- reactiveVal(0)
logstatus <- reactiveVal("")

observeEvent(input$go, {
  if (!checkPassword(input$adminid,input$adminpwd)) {
    showNotification("Authentication Error.")
    return(NULL)
  }
  showNotification(sprintf("Logged into application %s.", input$branch))
  if (nclicks() > 0L) {
    showNotification("Launch in progess, please wait.")
    return (NULL)
  }
  nclicks(1)
  status("Updating configuration from github.")
  setwd(config.dir)
  cat("Checking out branch ",input$branch,"\n")
  system2("git","pull")
  system2("git",c("checkout",input$branch))
  system2("git","pull")
    
  EI.config <-
    tryCatch(fromJSON(file.path(config.dir,"config.json"),FALSE),
             error=function(e) {
               status("Error.")
               nclicks(0)
               showNotification("Error parsing config.json")
               logstatus(Messages=conditionMessage(e))
               NULL
             })
  if (is.null(EI.config)) return (NULL)

  appStem <- as.character(EI.config$appStem)
  apps <- Proc4.config$apps[appStem]
  if (length(apps)==0L || any (is.na(apps))) {
    status("Error.")
    emess <- sprintf("Could not find apps for %s",
                     paste(appStem[is.na(apps)],collapse=", "))
    logstatus(emess)
    showNotification(emess)
    nclicks(0)
    return(NULL)
  }
  logfile <- file.path(logpath,EI.config$logname)
  logstatus("")
  for (sapp in appStem) {
    system2("/usr/local/share/Proc4/bin/EIEvent",
            c(sapp, sub("<app>",sapp,logfile)),
            wait=FALSE)
    logstatus(c(logstatus(),
                sprintf("Process launched for application %s.",sapp)))
  }
  status("Ready.")
  nclicks(0)
})
output$status <- renderText(status())
output$log <- renderTable(data.frame(Messages=logstatus()),colnames=FALSE)
p("EA Launcher is ",textOutput("status"))
p(tableOutput("log"))

  
```

## Links

* Status (https://pluto.coe.fsu.edu/Proc4/dongle/Status.php)
* Shutdown (https://pluto.coe.fsu.edu/Proc4/dongle/Shutdown.php)
* EI Loader (https://pluto.coe.fsu.edu/rdemos/Proc4/EILoader.Rmd)
* EI Launcher (https://pluto.coe.fsu.edu/rdemos/Proc4/EILauncher.Rmd)
* EA Builder (https://pluto.coe.fsu.edu/rdemos/Proc4/EABuilder.Rmd)
* EA Launcherr (https://pluto.coe.fsu.edu/rdemos/Proc4/EALauncher.Rmd)



