|
| 1 | +package edu.wpi.grip.ui.util.deployment; |
| 2 | + |
| 3 | + |
| 4 | +import com.google.common.eventbus.EventBus; |
| 5 | +import com.jcabi.ssh.Shell; |
| 6 | +import com.jcraft.jsch.JSch; |
| 7 | +import edu.wpi.grip.core.StartStoppable; |
| 8 | +import edu.wpi.grip.core.events.StartedStoppedEvent; |
| 9 | +import edu.wpi.grip.core.events.UnexpectedThrowableEvent; |
| 10 | +import edu.wpi.grip.core.serialization.Project; |
| 11 | +import org.apache.commons.io.input.NullInputStream; |
| 12 | +import org.apache.commons.io.output.NullOutputStream; |
| 13 | +import org.apache.tools.ant.BuildException; |
| 14 | +import org.apache.tools.ant.taskdefs.optional.ssh.Scp; |
| 15 | +import org.jdeferred.DeferredCallable; |
| 16 | +import org.jdeferred.DeferredManager; |
| 17 | +import org.jdeferred.Promise; |
| 18 | +import org.jdeferred.impl.DefaultDeferredManager; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.OutputStream; |
| 23 | +import java.net.InetAddress; |
| 24 | +import java.net.URISyntaxException; |
| 25 | +import java.net.URLDecoder; |
| 26 | +import java.nio.file.Paths; |
| 27 | +import java.util.Optional; |
| 28 | +import java.util.function.Supplier; |
| 29 | + |
| 30 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | + |
| 32 | +/** |
| 33 | + * Controls an instance of GRIP running on a remote device. |
| 34 | + */ |
| 35 | +public class DeployedInstanceManager implements StartStoppable { |
| 36 | + |
| 37 | + private final EventBus eventBus; |
| 38 | + private final File coreJar; |
| 39 | + private final File projectFile; |
| 40 | + private final SecureShellDetails details; |
| 41 | + private final DeploymentCommands deploymentCommands; |
| 42 | + private final Supplier<OutputStream> stdOut; |
| 43 | + private final Supplier<OutputStream> stdErr; |
| 44 | + private Optional<Thread> sshThread; |
| 45 | + |
| 46 | + public static class Factory { |
| 47 | + private final EventBus eventBus; |
| 48 | + private final File coreJAR; |
| 49 | + private final Project project; |
| 50 | + private final SecureShellDetails.Factory secureShellDetailsFactory; |
| 51 | + private final DeploymentCommands.Factory deploymentCommandsFactory; |
| 52 | + |
| 53 | + public Factory(EventBus eventBus, Project project, SecureShellDetails.Factory secureShellDetailsFactory, DeploymentCommands.Factory deploymentCommandsFactory) { |
| 54 | + this.eventBus = eventBus; |
| 55 | + try { |
| 56 | + this.coreJAR = new File(edu.wpi.grip.core.Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()); |
| 57 | + } catch (URISyntaxException e) { |
| 58 | + throw new IllegalStateException("Could not find the main class jar file", e); |
| 59 | + } |
| 60 | + this.project = project; |
| 61 | + this.secureShellDetailsFactory = secureShellDetailsFactory; |
| 62 | + this.deploymentCommandsFactory = deploymentCommandsFactory; |
| 63 | + } |
| 64 | + |
| 65 | + public Factory(EventBus eventBus, Project project) { |
| 66 | + this(eventBus, project, new SecureShellDetails.Factory(), new DeploymentCommands.Factory()); |
| 67 | + } |
| 68 | + |
| 69 | + public DeployedInstanceManager createFRC(InetAddress address) { |
| 70 | + return createFRC(address, NullOutputStream::new, NullOutputStream::new); |
| 71 | + } |
| 72 | + |
| 73 | + public DeployedInstanceManager createFRC(InetAddress address, Supplier<OutputStream> stdOut, Supplier<OutputStream> stdErr) { |
| 74 | + final File projectFile = project.getFile().get(); |
| 75 | + return createFRC(address, projectFile, stdOut, stdErr); |
| 76 | + } |
| 77 | + |
| 78 | + public DeployedInstanceManager createFRC(InetAddress address, File projectFile) { |
| 79 | + return createFRC(address, projectFile, NullOutputStream::new, NullOutputStream::new); |
| 80 | + } |
| 81 | + |
| 82 | + public DeployedInstanceManager createFRC(InetAddress addresses, File projectFile, Supplier<OutputStream> stdOut, Supplier<OutputStream> stdErr) { |
| 83 | + return new DeployedInstanceManager(eventBus, coreJAR, projectFile, secureShellDetailsFactory.createFRC(addresses), deploymentCommandsFactory.createFRC(), stdOut, stdErr); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @param eventBus |
| 89 | + * @param coreJar The jar with all of the core. This will be copied to the destination |
| 90 | + * @param projectFile The project file to send over |
| 91 | + * @param details The details regarding connecting to the secure shell |
| 92 | + * @param deploymentCommands The commands required to start and stop GRIP |
| 93 | + * @param stdOut Supplies the stream to be used for the standard output from the ssh command |
| 94 | + * @param stdErr Supplies the stream to be used for the standard error from the ssh command |
| 95 | + */ |
| 96 | + public DeployedInstanceManager(EventBus eventBus, |
| 97 | + File coreJar, |
| 98 | + File projectFile, |
| 99 | + SecureShellDetails details, |
| 100 | + DeploymentCommands deploymentCommands, |
| 101 | + Supplier<OutputStream> stdOut, |
| 102 | + Supplier<OutputStream> stdErr) { |
| 103 | + this.eventBus = checkNotNull(eventBus, "The event bus can not be null"); |
| 104 | + this.coreJar = checkNotNull(coreJar, "The URI of the coreJar can not be null"); |
| 105 | + this.projectFile = checkNotNull(projectFile, "The project file can not be null"); |
| 106 | + this.details = checkNotNull(details, "The details can not be null"); |
| 107 | + this.deploymentCommands = checkNotNull(deploymentCommands, "The deployment commands can not be null"); |
| 108 | + this.stdOut = checkNotNull(stdOut, "The standard out stream supplier can not be null"); |
| 109 | + this.stdErr = checkNotNull(stdErr, "The standard err stream supplier can not be null"); |
| 110 | + sshThread = Optional.empty(); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Deploys the coreJar and the project file to the remote device |
| 115 | + * |
| 116 | + */ |
| 117 | + public synchronized Promise<DeployedInstanceManager, Throwable, Double> deploy() { |
| 118 | + final DeployedInstanceManager self = this; |
| 119 | + JSch.setConfig("StrictHostKeyChecking", "no"); |
| 120 | + final DeferredManager deferred = new DefaultDeferredManager(); |
| 121 | + |
| 122 | + return deferred.when(new DeferredCallable<DeployedInstanceManager, Double>() { |
| 123 | + @Override |
| 124 | + public DeployedInstanceManager call() throws Exception { |
| 125 | + notify(0.2); |
| 126 | + scpFileToTarget(coreJar); |
| 127 | + notify(0.5); |
| 128 | + scpFileToTarget(projectFile); |
| 129 | + notify(1.0); |
| 130 | + return self; |
| 131 | + } |
| 132 | + |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @param file The file to send |
| 138 | + * @throws IOException If there is a problem sending the file. |
| 139 | + */ |
| 140 | + private void scpFileToTarget(File file) throws IOException { |
| 141 | + final String localFile = URLDecoder.decode(Paths.get(file.toURI()).toString()); |
| 142 | + try { |
| 143 | + final Scp scp = details.createSCPRunner(); |
| 144 | + scp.setLocalFile(localFile); |
| 145 | + scp.execute(); |
| 146 | + } catch (BuildException e) { |
| 147 | + throw new IOException("Failed to deploy", e); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Starts GRIP running on the device specified by the secure shell details |
| 153 | + * |
| 154 | + * @throws IOException |
| 155 | + */ |
| 156 | + public synchronized void start() throws IOException { |
| 157 | + if (isStarted()) { |
| 158 | + throw new IllegalStateException("The program has already been started and must be stopped before restarting"); |
| 159 | + } |
| 160 | + // Ensure that the project isn't running from a previous instance. |
| 161 | + runStop(); |
| 162 | + Thread launcher = new Thread(() -> { |
| 163 | + try { |
| 164 | + final Shell gripShell = new Shell.Safe(details.createSSHShell()); |
| 165 | + gripShell.exec("nohup " + deploymentCommands.getJARLaunchCommand(coreJar.getName(), projectFile.getName()) + " &", |
| 166 | + new NullInputStream(0L), |
| 167 | + stdOut.get(), |
| 168 | + stdErr.get()); |
| 169 | + } catch (IOException e) { |
| 170 | + throw new IllegalStateException("The program failed to start", e); |
| 171 | + } finally { |
| 172 | + // This thread is done, shut it down. |
| 173 | + synchronized (this) { |
| 174 | + sshThread = Optional.empty(); |
| 175 | + } |
| 176 | + } |
| 177 | + }, "SSH Monitor Thread"); |
| 178 | + launcher.setUncaughtExceptionHandler((thread, exception) -> { |
| 179 | + eventBus.post(new UnexpectedThrowableEvent(exception, "Failed to start the remote instance of the application")); |
| 180 | + try { |
| 181 | + runStop(); |
| 182 | + } catch (IOException e) { |
| 183 | + eventBus.post(new UnexpectedThrowableEvent(e, "Failed to stop the remote instance of the program")); |
| 184 | + } |
| 185 | + }); |
| 186 | + launcher.setDaemon(true); |
| 187 | + launcher.start(); |
| 188 | + this.sshThread = Optional.of(launcher); |
| 189 | + eventBus.post(new StartedStoppedEvent(this)); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Stops the program running on the remote device |
| 194 | + * |
| 195 | + * @throws IOException If the command fails to be delivered |
| 196 | + */ |
| 197 | + public synchronized void stop() throws IOException { |
| 198 | + if (!isStarted()) { |
| 199 | + throw new IllegalStateException("The program hasn't started yet."); |
| 200 | + } |
| 201 | + runStop(); |
| 202 | + do { |
| 203 | + try { |
| 204 | + // Since we hold the mutex on this we can wait |
| 205 | + wait(50); |
| 206 | + } catch (InterruptedException e) { |
| 207 | + Thread.currentThread().interrupt(); |
| 208 | + //TODO: Move this into a logging framework |
| 209 | + System.err.println("Caught Exception:"); |
| 210 | + e.printStackTrace(); |
| 211 | + } |
| 212 | + runStop(); |
| 213 | + } while (isStarted() && !Thread.interrupted()); |
| 214 | + eventBus.post(new StartedStoppedEvent(this)); |
| 215 | + } |
| 216 | + |
| 217 | + private void runStop() throws IOException { |
| 218 | + final Shell.Plain gripShell = new Shell.Plain(new Shell.Safe(details.createSSHShell())); |
| 219 | + gripShell.exec(deploymentCommands.getKillCommand(coreJar.getName())); |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public synchronized boolean isStarted() { |
| 224 | + return sshThread.isPresent() && sshThread.get().isAlive(); |
| 225 | + } |
| 226 | + |
| 227 | +} |
0 commit comments