| 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.compute.options; | 
| 20 |   | 
| 21 | import static com.google.common.base.Preconditions.checkArgument; | 
| 22 | import static com.google.common.base.Preconditions.checkNotNull; | 
| 23 |   | 
| 24 | import org.jclouds.domain.Credentials; | 
| 25 |   | 
| 26 | /** | 
| 27 |  * Enables additional options for running a script. | 
| 28 |  *  | 
| 29 |  * @author Oleksiy Yarmula | 
| 30 |  */ | 
| 31 | public class RunScriptOptions { | 
| 32 |   | 
| 33 |    /** | 
| 34 |     * Default options. The default settings are: | 
| 35 |     * <ul> | 
| 36 |     * <li>override the credentials with ones supplied in call to | 
| 37 |     * {@link org.jclouds.compute.ComputeService#runScriptOnNodesWithTag}</li> | 
| 38 |     * <li>run the script as root (versus running with current privileges)</li> | 
| 39 |     * </ul> | 
| 40 |     */ | 
| 41 |    public static final RunScriptOptions NONE = new ImmutableRunScriptOptions(new RunScriptOptions()); | 
| 42 |   | 
| 43 |    public static class ImmutableRunScriptOptions extends RunScriptOptions { | 
| 44 |       private final RunScriptOptions delegate; | 
| 45 |   | 
| 46 |       public ImmutableRunScriptOptions(RunScriptOptions delegate) { | 
| 47 |          this.delegate = delegate; | 
| 48 |       } | 
| 49 |   | 
| 50 |       @Override | 
| 51 |       public String toString() { | 
| 52 |          return delegate.toString(); | 
| 53 |       } | 
| 54 |   | 
| 55 |       @Override | 
| 56 |       public Credentials getOverrideCredentials() { | 
| 57 |          return delegate.getOverrideCredentials(); | 
| 58 |   | 
| 59 |       } | 
| 60 |   | 
| 61 |       @Override | 
| 62 |       public boolean shouldRunAsRoot() { | 
| 63 |          return delegate.shouldRunAsRoot(); | 
| 64 |   | 
| 65 |       } | 
| 66 |   | 
| 67 |       @Override | 
| 68 |       public RunScriptOptions runAsRoot(boolean runAsRoot) { | 
| 69 |          throw new IllegalArgumentException("runAsRoot is immutable"); | 
| 70 |       } | 
| 71 |   | 
| 72 |       @Override | 
| 73 |       public boolean shouldBlockOnComplete() { | 
| 74 |          return delegate.shouldBlockOnComplete(); | 
| 75 |   | 
| 76 |       } | 
| 77 |   | 
| 78 |       @Override | 
| 79 |       public RunScriptOptions blockOnComplete(boolean blockOnComplete) { | 
| 80 |          throw new IllegalArgumentException("blockOnComplete is immutable"); | 
| 81 |       } | 
| 82 |   | 
| 83 |       @Override | 
| 84 |       public RunScriptOptions withOverridingCredentials(Credentials overridingCredentials) { | 
| 85 |          throw new IllegalArgumentException("overridingCredentials is immutable"); | 
| 86 |       } | 
| 87 |   | 
| 88 |       @Override | 
| 89 |       public String getTaskName() { | 
| 90 |          return delegate.getTaskName(); | 
| 91 |       } | 
| 92 |   | 
| 93 |       @Override | 
| 94 |       public RunScriptOptions nameTask(String name) { | 
| 95 |          throw new IllegalArgumentException("taskName is immutable"); | 
| 96 |       } | 
| 97 |   | 
| 98 |       @Override | 
| 99 |       public RunScriptOptions blockOnPort(int port, int seconds) { | 
| 100 |          throw new IllegalArgumentException("port, seconds are immutable"); | 
| 101 |       } | 
| 102 |   | 
| 103 |       @Override | 
| 104 |       public int getPort() { | 
| 105 |          return delegate.getPort(); | 
| 106 |       } | 
| 107 |   | 
| 108 |       @Override | 
| 109 |       public int getSeconds() { | 
| 110 |          return delegate.getSeconds(); | 
| 111 |       } | 
| 112 |    } | 
| 113 |   | 
| 114 |    protected int port = -1; | 
| 115 |    protected int seconds = -1; | 
| 116 |    protected String taskName; | 
| 117 |    protected Credentials overridingCredentials; | 
| 118 |    protected boolean runAsRoot = true; | 
| 119 |    protected boolean blockOnComplete = true; | 
| 120 |    protected boolean wrapInInitScript = true; | 
| 121 |   | 
| 122 |    public RunScriptOptions withOverridingCredentials(Credentials overridingCredentials) { | 
| 123 |       checkNotNull(overridingCredentials, "overridingCredentials"); | 
| 124 |       checkNotNull(overridingCredentials.identity, "overridingCredentials.identity"); | 
| 125 |       checkNotNull(overridingCredentials.credential, "overridingCredentials.key"); | 
| 126 |       this.overridingCredentials = overridingCredentials; | 
| 127 |       return this; | 
| 128 |    } | 
| 129 |   | 
| 130 |    /** | 
| 131 |     * @return What to call the task relating to this script; default {@code | 
| 132 |     *         jclouds-script-timestamp} where timestamp is millis since epoch | 
| 133 |     *  | 
| 134 |     */ | 
| 135 |    public RunScriptOptions nameTask(String name) { | 
| 136 |       this.taskName = name; | 
| 137 |       return this; | 
| 138 |    } | 
| 139 |   | 
| 140 |    public RunScriptOptions runAsRoot(boolean runAsRoot) { | 
| 141 |       this.runAsRoot = runAsRoot; | 
| 142 |       return this; | 
| 143 |    } | 
| 144 |   | 
| 145 |    /** | 
| 146 |     * default true | 
| 147 |     * <p/> | 
| 148 |     *  | 
| 149 |     * @param wrapInInitScript | 
| 150 |     *           if the command is long-running, use this option to ensure it is wrapInInitScripted | 
| 151 |     *           properly. (ex. have jclouds wrap it an init script, nohup, etc) | 
| 152 |     * @return | 
| 153 |     */ | 
| 154 |    public RunScriptOptions wrapInInitScript(boolean wrapInInitScript) { | 
| 155 |       this.wrapInInitScript = wrapInInitScript; | 
| 156 |       return this; | 
| 157 |    } | 
| 158 |   | 
| 159 |    public RunScriptOptions blockOnComplete(boolean blockOnComplete) { | 
| 160 |       this.blockOnComplete = blockOnComplete; | 
| 161 |       return this; | 
| 162 |    } | 
| 163 |   | 
| 164 |    /** | 
| 165 |     * When the node is started, wait until the following port is active | 
| 166 |     */ | 
| 167 |    public RunScriptOptions blockOnPort(int port, int seconds) { | 
| 168 |       checkArgument(port > 0 && port < 65536, "port must be a positive integer < 65535"); | 
| 169 |       checkArgument(seconds > 0, "seconds must be a positive integer"); | 
| 170 |       this.port = port; | 
| 171 |       this.seconds = seconds; | 
| 172 |       return this; | 
| 173 |    } | 
| 174 |   | 
| 175 |    public String getTaskName() { | 
| 176 |       return taskName; | 
| 177 |    } | 
| 178 |   | 
| 179 |    public int getPort() { | 
| 180 |       return port; | 
| 181 |    } | 
| 182 |   | 
| 183 |    public int getSeconds() { | 
| 184 |       return seconds; | 
| 185 |    } | 
| 186 |   | 
| 187 |    /** | 
| 188 |     * Whether to override the credentials with ones supplied in call to | 
| 189 |     * {@link org.jclouds.compute.ComputeService#runScriptOnNodesWithTag}. By default, true. | 
| 190 |     *  | 
| 191 |     * @return value | 
| 192 |     */ | 
| 193 |    public Credentials getOverrideCredentials() { | 
| 194 |       return overridingCredentials; | 
| 195 |    } | 
| 196 |   | 
| 197 |    /** | 
| 198 |     * Whether to run the script as root (or run with current privileges). By default, true. | 
| 199 |     *  | 
| 200 |     * @return value | 
| 201 |     */ | 
| 202 |    public boolean shouldRunAsRoot() { | 
| 203 |       return runAsRoot; | 
| 204 |    } | 
| 205 |   | 
| 206 |    /** | 
| 207 |     * Whether to wait until the script has completed. By default, true. | 
| 208 |     *  | 
| 209 |     * @return value | 
| 210 |     */ | 
| 211 |    public boolean shouldBlockOnComplete() { | 
| 212 |       return blockOnComplete; | 
| 213 |    } | 
| 214 |   | 
| 215 |    /** | 
| 216 |     * Whether to wait until the script has completed. By default, true. | 
| 217 |     *  | 
| 218 |     * @return value | 
| 219 |     */ | 
| 220 |    public boolean shouldWrapInInitScript() { | 
| 221 |       return wrapInInitScript; | 
| 222 |    } | 
| 223 |   | 
| 224 |    public static class Builder { | 
| 225 |   | 
| 226 |       public static RunScriptOptions nameTask(String name) { | 
| 227 |          RunScriptOptions options = new RunScriptOptions(); | 
| 228 |          return options.nameTask(name); | 
| 229 |       } | 
| 230 |   | 
| 231 |       public static RunScriptOptions overrideCredentialsWith(Credentials credentials) { | 
| 232 |          RunScriptOptions options = new RunScriptOptions(); | 
| 233 |          return options.withOverridingCredentials(credentials); | 
| 234 |       } | 
| 235 |   | 
| 236 |       public static RunScriptOptions runAsRoot(boolean value) { | 
| 237 |          RunScriptOptions options = new RunScriptOptions(); | 
| 238 |          return options.runAsRoot(value); | 
| 239 |       } | 
| 240 |   | 
| 241 |       public static RunScriptOptions blockOnComplete(boolean value) { | 
| 242 |          RunScriptOptions options = new RunScriptOptions(); | 
| 243 |          return options.blockOnComplete(value); | 
| 244 |       } | 
| 245 |   | 
| 246 |       public static RunScriptOptions wrapInInitScript(boolean value) { | 
| 247 |          RunScriptOptions options = new RunScriptOptions(); | 
| 248 |          return options.wrapInInitScript(value); | 
| 249 |       } | 
| 250 |   | 
| 251 |       public static RunScriptOptions blockOnPort(int port, int seconds) { | 
| 252 |          RunScriptOptions options = new RunScriptOptions(); | 
| 253 |          return options.blockOnPort(port, seconds); | 
| 254 |       } | 
| 255 |   | 
| 256 |    } | 
| 257 |   | 
| 258 |    @Override | 
| 259 |    public String toString() { | 
| 260 |       return "[overridingCredentials=" + (overridingCredentials != null) + ", port:seconds=" + port + ":" + seconds | 
| 261 |                + ", runAsRoot=" + runAsRoot + ", blockOnComplete=" + blockOnComplete + ", wrapInInitScript=" + wrapInInitScript | 
| 262 |                + "]"; | 
| 263 |    } | 
| 264 |   | 
| 265 | } |