| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 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 createRunScript(String instanceName, Iterable<String> exports, String pwd, |
| 75 | Iterable<Statement> statements) {// TODO: convert so |
| 76 | // that |
| 77 | // createRunScript |
| 78 | // can take from a |
| 79 | // variable |
| 80 | return new CreateRunScript(instanceName, exports, pwd, statements); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Stores the pid into the variable {@code FOUND_PID} if successful. |
| 85 | * |
| 86 | * @param args |
| 87 | * - what to search for in the process tree. |
| 88 | */ |
| 89 | public static Statement findPid(String args) { |
| 90 | return new Call("findPid", args); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * |
| 95 | * Runs the script in a way that it can be matched later with {@link #findPid} |
| 96 | * |
| 97 | * @param instanceName |
| 98 | * - what to match the process on |
| 99 | * @param script |
| 100 | * - what to run in the background |
| 101 | * @param logDir |
| 102 | * - where to write the following logs: |
| 103 | * <ol> |
| 104 | * <li>stdout.log</li> |
| 105 | * <li>stderr.log</li> |
| 106 | * </ol> |
| 107 | */ |
| 108 | public static Statement forget(String instanceName, String script, String logDir) { |
| 109 | return new Call("forget", instanceName, script, logDir); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Kills the pid and subprocesses related to the variable {@code FOUND_PID} if set. |
| 114 | * |
| 115 | * @see #findPid |
| 116 | */ |
| 117 | public static Statement kill() { |
| 118 | return KILL; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * statement can have multiple newlines, note you should use {@code lf} to be portable |
| 123 | * |
| 124 | * @see ShellToken |
| 125 | */ |
| 126 | public static Statement interpret(String... portableStatements) { |
| 127 | return new InterpretableStatement(portableStatements); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * interprets and adds a newline to the statement |
| 132 | */ |
| 133 | public static Statement exec(String portableStatement) { |
| 134 | return interpret(portableStatement + "{lf}"); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * untar, ungzip the data received from the request parameters. |
| 139 | * |
| 140 | * @param method |
| 141 | * http method: ex GET |
| 142 | * @param endpoint |
| 143 | * uri corresponding to the request |
| 144 | * @param headers |
| 145 | * request headers to send |
| 146 | * @param directory |
| 147 | */ |
| 148 | public static Statement extractTargzIntoDirectory(String method, URI endpoint, Multimap<String, String> headers, |
| 149 | String directory) { |
| 150 | return new PipeHttpResponseToTarxpzfIntoDirectory(method, endpoint, headers, directory); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * unzip the data received from the request parameters. |
| 155 | * |
| 156 | * @param method |
| 157 | * http method: ex GET |
| 158 | * @param endpoint |
| 159 | * uri corresponding to the request |
| 160 | * @param headers |
| 161 | * request headers to send |
| 162 | * @param directory |
| 163 | */ |
| 164 | public static Statement extractZipIntoDirectory(String method, URI endpoint, Multimap<String, String> headers, |
| 165 | String directory) { |
| 166 | return new UnzipHttpResponseIntoDirectory(method, endpoint, headers, directory); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * exec the data received from the request parameters. |
| 171 | * |
| 172 | * @param method |
| 173 | * http method: ex GET |
| 174 | * @param endpoint |
| 175 | * uri corresponding to the request |
| 176 | * @param headers |
| 177 | * request headers to send |
| 178 | */ |
| 179 | public static Statement pipeHttpResponseToBash(String method, URI endpoint, Multimap<String, String> headers) { |
| 180 | return new PipeHttpResponseToBash(method, endpoint, headers); |
| 181 | } |
| 182 | } |