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; |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame^] | 26 | import java.util.function.Function; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 27 | |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 28 | import static java.lang.String.format; |
Thomas Vachuska | 8643937 | 2015-06-05 09:21:32 -0700 | [diff] [blame] | 29 | import static org.onlab.stc.Coordinator.Status.FAILED; |
| 30 | import static org.onlab.stc.Coordinator.Status.SUCCEEDED; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 31 | import static org.onlab.stc.Coordinator.print; |
| 32 | |
| 33 | /** |
| 34 | * Manages execution of the specified step or a group. |
| 35 | */ |
| 36 | class StepProcessor implements Runnable { |
| 37 | |
Thomas Vachuska | 8643937 | 2015-06-05 09:21:32 -0700 | [diff] [blame] | 38 | private static final String IGNORE_CODE = "~"; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 39 | private static final int FAIL = -1; |
| 40 | |
| 41 | static String launcher = "stc-launcher "; |
| 42 | |
| 43 | private final Step step; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 44 | private final File logDir; |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame^] | 45 | private String command; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 46 | |
| 47 | private Process process; |
| 48 | private StepProcessListener delegate; |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame^] | 49 | private Function<String, String> substitutor; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Creates a process monitor. |
| 53 | * |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame^] | 54 | * @param step step or group to be executed |
| 55 | * @param logDir directory where step process log should be stored |
| 56 | * @param delegate process lifecycle listener |
| 57 | * @param substitutor function to substitute var reference in command |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 58 | */ |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame^] | 59 | StepProcessor(Step step, File logDir, StepProcessListener delegate, |
| 60 | Function<String, String> substitutor) { |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 61 | this.step = step; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 62 | this.logDir = logDir; |
| 63 | this.delegate = delegate; |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame^] | 64 | this.substitutor = substitutor; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void run() { |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame^] | 69 | command = substitutor != null ? substitutor.apply(command()) : command(); |
| 70 | delegate.onStart(step, command); |
Thomas Vachuska | 8643937 | 2015-06-05 09:21:32 -0700 | [diff] [blame] | 71 | int code = execute(); |
| 72 | boolean ignoreCode = step.env() != null && step.env.equals(IGNORE_CODE); |
| 73 | Status status = ignoreCode || code == 0 ? SUCCEEDED : FAILED; |
| 74 | delegate.onCompletion(step, status); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Executes the step process. |
| 79 | * |
| 80 | * @return exit code |
| 81 | */ |
| 82 | private int execute() { |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 83 | try (PrintWriter pw = new PrintWriter(logFile())) { |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame^] | 84 | process = Runtime.getRuntime().exec(command); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 85 | processOutput(pw); |
| 86 | |
| 87 | // Wait for the process to complete and get its exit code. |
| 88 | if (process.isAlive()) { |
| 89 | process.waitFor(); |
| 90 | } |
| 91 | return process.exitValue(); |
| 92 | |
| 93 | } catch (IOException e) { |
| 94 | print("Unable to run step %s using command %s", step.name(), step.command()); |
| 95 | } catch (InterruptedException e) { |
| 96 | print("Step %s interrupted", step.name()); |
| 97 | } |
| 98 | return FAIL; |
| 99 | } |
| 100 | |
| 101 | /** |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 102 | * Returns ready-to-run command for the step. |
| 103 | * |
| 104 | * @return command to execute |
| 105 | */ |
| 106 | private String command() { |
| 107 | return format("%s %s %s %s", launcher, |
| 108 | step.env() != null ? step.env() : "-", |
| 109 | step.cwd() != null ? step.cwd() : "-", |
| 110 | step.command()); |
| 111 | } |
| 112 | |
| 113 | /** |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 114 | * Captures output of the step process. |
| 115 | * |
| 116 | * @param pw print writer to send output to |
| 117 | * @throws IOException if unable to read output or write logs |
| 118 | */ |
| 119 | private void processOutput(PrintWriter pw) throws IOException { |
| 120 | InputStream out = process.getInputStream(); |
| 121 | BufferedReader br = new BufferedReader(new InputStreamReader(out)); |
| 122 | |
| 123 | // Slurp its combined stderr/stdout |
| 124 | String line; |
| 125 | while ((line = br.readLine()) != null) { |
| 126 | pw.println(line); |
| 127 | delegate.onOutput(step, line); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 132 | * Returns the log file for the step output. |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 133 | * |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 134 | * @return log file |
| 135 | */ |
Thomas Vachuska | 4bfccd54 | 2015-05-30 00:35:25 -0700 | [diff] [blame] | 136 | private File logFile() { |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 137 | return new File(logDir, step.name() + ".log"); |
| 138 | } |
| 139 | |
| 140 | } |