Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onlab.stc; |
| 17 | |
Thomas Vachuska | 8643937 | 2015-06-05 09:21:32 -0700 | [diff] [blame^] | 18 | import org.onlab.stc.Coordinator.Status; |
| 19 | |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 20 | import java.io.BufferedReader; |
| 21 | import java.io.File; |
| 22 | import java.io.IOException; |
| 23 | import java.io.InputStream; |
| 24 | import java.io.InputStreamReader; |
| 25 | import java.io.PrintWriter; |
| 26 | |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 27 | import static java.lang.String.format; |
Thomas Vachuska | 8643937 | 2015-06-05 09:21:32 -0700 | [diff] [blame^] | 28 | import static org.onlab.stc.Coordinator.Status.FAILED; |
| 29 | import static org.onlab.stc.Coordinator.Status.SUCCEEDED; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 30 | import static org.onlab.stc.Coordinator.print; |
| 31 | |
| 32 | /** |
| 33 | * Manages execution of the specified step or a group. |
| 34 | */ |
| 35 | class StepProcessor implements Runnable { |
| 36 | |
Thomas Vachuska | 8643937 | 2015-06-05 09:21:32 -0700 | [diff] [blame^] | 37 | private static final String IGNORE_CODE = "~"; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 38 | private static final int FAIL = -1; |
| 39 | |
| 40 | static String launcher = "stc-launcher "; |
| 41 | |
| 42 | private final Step step; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 43 | private final File logDir; |
| 44 | |
| 45 | private Process process; |
| 46 | private StepProcessListener delegate; |
| 47 | |
| 48 | /** |
| 49 | * Creates a process monitor. |
| 50 | * |
| 51 | * @param step step or group to be executed |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 52 | * @param logDir directory where step process log should be stored |
| 53 | * @param delegate process lifecycle listener |
| 54 | */ |
Thomas Vachuska | 8643937 | 2015-06-05 09:21:32 -0700 | [diff] [blame^] | 55 | StepProcessor(Step step, File logDir, StepProcessListener delegate) { |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 56 | this.step = step; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 57 | this.logDir = logDir; |
| 58 | this.delegate = delegate; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void run() { |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 63 | delegate.onStart(step); |
Thomas Vachuska | 8643937 | 2015-06-05 09:21:32 -0700 | [diff] [blame^] | 64 | int code = execute(); |
| 65 | boolean ignoreCode = step.env() != null && step.env.equals(IGNORE_CODE); |
| 66 | Status status = ignoreCode || code == 0 ? SUCCEEDED : FAILED; |
| 67 | delegate.onCompletion(step, status); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Executes the step process. |
| 72 | * |
| 73 | * @return exit code |
| 74 | */ |
| 75 | private int execute() { |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 76 | try (PrintWriter pw = new PrintWriter(logFile())) { |
| 77 | process = Runtime.getRuntime().exec(command()); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 78 | processOutput(pw); |
| 79 | |
| 80 | // Wait for the process to complete and get its exit code. |
| 81 | if (process.isAlive()) { |
| 82 | process.waitFor(); |
| 83 | } |
| 84 | return process.exitValue(); |
| 85 | |
| 86 | } catch (IOException e) { |
| 87 | print("Unable to run step %s using command %s", step.name(), step.command()); |
| 88 | } catch (InterruptedException e) { |
| 89 | print("Step %s interrupted", step.name()); |
| 90 | } |
| 91 | return FAIL; |
| 92 | } |
| 93 | |
| 94 | /** |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 95 | * Returns ready-to-run command for the step. |
| 96 | * |
| 97 | * @return command to execute |
| 98 | */ |
| 99 | private String command() { |
| 100 | return format("%s %s %s %s", launcher, |
| 101 | step.env() != null ? step.env() : "-", |
| 102 | step.cwd() != null ? step.cwd() : "-", |
| 103 | step.command()); |
| 104 | } |
| 105 | |
| 106 | /** |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 107 | * Captures output of the step process. |
| 108 | * |
| 109 | * @param pw print writer to send output to |
| 110 | * @throws IOException if unable to read output or write logs |
| 111 | */ |
| 112 | private void processOutput(PrintWriter pw) throws IOException { |
| 113 | InputStream out = process.getInputStream(); |
| 114 | BufferedReader br = new BufferedReader(new InputStreamReader(out)); |
| 115 | |
| 116 | // Slurp its combined stderr/stdout |
| 117 | String line; |
| 118 | while ((line = br.readLine()) != null) { |
| 119 | pw.println(line); |
| 120 | delegate.onOutput(step, line); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 125 | * Returns the log file for the step output. |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 126 | * |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 127 | * @return log file |
| 128 | */ |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 129 | private File logFile() { |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 130 | return new File(logDir, step.name() + ".log"); |
| 131 | } |
| 132 | |
| 133 | } |