| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.scriptbuilder.domain; |
| 20 | |
| 21 | import java.net.URI; |
| 22 | import java.util.Map; |
| 23 | |
| 24 | import com.google.common.collect.ImmutableList; |
| 25 | import com.google.common.collect.Multimap; |
| 26 | |
| 27 | /** |
| 28 | * Statements used in shell scripts. |
| 29 | * |
| 30 | * @author Adrian Cole |
| 31 | */ |
| 32 | public class Statements { |
| 33 | private static final Kill KILL = new Kill(); |
| 34 | |
| 35 | public static Statement newStatementList(Statement... statements) { |
| 36 | return new StatementList(statements); |
| 37 | } |
| 38 | |
| 39 | public static Statement switchArg(int arg, Map<String, Statement> valueToActions) { |
| 40 | return new SwitchArg(arg, valueToActions); |
| 41 | } |
| 42 | |
| 43 | public static Statement rm(final String path) { |
| 44 | return new Statement() { |
| 45 | |
| 46 | @Override |
| 47 | public Iterable<String> functionDependencies(OsFamily family) { |
| 48 | return ImmutableList.of(); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public String render(OsFamily family) { |
| 53 | if (family == OsFamily.WINDOWS) |
| 54 | return exec(String.format("{rm} %s 2{closeFd}", path)).render(family); |
| 55 | else |
| 56 | return exec(String.format("{rm} %s", path)).render(family); |
| 57 | } |
| 58 | |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | public static Statement call(String function, String... args) { |
| 63 | return new Call(function, args); |
| 64 | } |
| 65 | |
| 66 | public static Statement appendFile(String path, Iterable<String> lines) { |
| 67 | return new AppendFile(path, lines); |
| 68 | } |
| 69 | |
| 70 | public static Statement appendFile(String path, Iterable<String> lines, String marker) { |
| 71 | return new AppendFile(path, lines, marker); |
| 72 | } |
| 73 | |
| 74 | public static Statement createOrOverwriteFile(String path, Iterable<String> lines) { |
| 75 | return new CreateOrOverwriteFile(path, lines); |
| 76 | } |
| 77 | |
| 78 | public static Statement createOrOverwriteFile(String path, Iterable<String> lines, String marker) { |
| 79 | return new CreateOrOverwriteFile(path, lines, marker); |
| 80 | } |
| 81 | |
| 82 | public static CreateRunScript createRunScript(String instanceName, Iterable<String> exports, String pwd, |
| 83 | Iterable<Statement> statements) {// TODO: convert so |
| 84 | // that |
| 85 | // createRunScript |
| 86 | // can take from a |
| 87 | // variable |
| 88 | return new CreateRunScript(instanceName, exports, pwd, statements); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Stores the pid into the variable {@code FOUND_PID} if successful. |
| 93 | * |
| 94 | * @param args |
| 95 | * - what to search for in the process tree. |
| 96 | */ |
| 97 | public static Statement findPid(String args) { |
| 98 | return new Call("findPid", args); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * |
| 103 | * Runs the script in a way that it can be matched later with {@link #findPid} |
| 104 | * |
| 105 | * @param instanceName |
| 106 | * - what to match the process on |
| 107 | * @param script |
| 108 | * - what to run in the background |
| 109 | * @param logDir |
| 110 | * - where to write the following logs: |
| 111 | * <ol> |
| 112 | * <li>stdout.log</li> |
| 113 | * <li>stderr.log</li> |
| 114 | * </ol> |
| 115 | */ |
| 116 | public static Statement forget(String instanceName, String script, String logDir) { |
| 117 | return new Call("forget", instanceName, script, logDir); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Kills the pid and subprocesses related to the variable {@code FOUND_PID} if set. |
| 122 | * |
| 123 | * @see #findPid |
| 124 | */ |
| 125 | public static Statement kill() { |
| 126 | return KILL; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * statement can have multiple newlines, note you should use {@code lf} to be portable |
| 131 | * |
| 132 | * @see ShellToken |
| 133 | */ |
| 134 | public static Statement interpret(String... portableStatements) { |
| 135 | return new InterpretableStatement(portableStatements); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * interprets and adds a newline to the statement |
| 140 | */ |
| 141 | public static Statement exec(String portableStatement) { |
| 142 | return interpret(portableStatement + "{lf}"); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * untar, ungzip the data received from the request parameters. |
| 147 | * |
| 148 | * @param method |
| 149 | * http method: ex GET |
| 150 | * @param endpoint |
| 151 | * uri corresponding to the request |
| 152 | * @param headers |
| 153 | * request headers to send |
| 154 | * @param directory |
| 155 | */ |
| 156 | public static Statement extractTargzIntoDirectory(String method, URI endpoint, Multimap<String, String> headers, |
| 157 | String directory) { |
| 158 | return new PipeHttpResponseToTarxpzfIntoDirectory(method, endpoint, headers, directory); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * unzip the data received from the request parameters. |
| 163 | * |
| 164 | * @param method |
| 165 | * http method: ex GET |
| 166 | * @param endpoint |
| 167 | * uri corresponding to the request |
| 168 | * @param headers |
| 169 | * request headers to send |
| 170 | * @param directory |
| 171 | */ |
| 172 | public static Statement extractZipIntoDirectory(String method, URI endpoint, Multimap<String, String> headers, |
| 173 | String directory) { |
| 174 | return new UnzipHttpResponseIntoDirectory(method, endpoint, headers, directory); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * exec the data received from the request parameters. |
| 179 | * |
| 180 | * @param method |
| 181 | * http method: ex GET |
| 182 | * @param endpoint |
| 183 | * uri corresponding to the request |
| 184 | * @param headers |
| 185 | * request headers to send |
| 186 | */ |
| 187 | public static Statement pipeHttpResponseToBash(String method, URI endpoint, Multimap<String, String> headers) { |
| 188 | return new PipeHttpResponseToBash(method, endpoint, headers); |
| 189 | } |
| 190 | } |