View Javadoc

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.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 getOverridingCredentials() {
57           return delegate.getOverridingCredentials();
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 overrideLoginUserWith(String loginUser) {
85           throw new IllegalArgumentException("loginUser is immutable");
86        }
87  
88        @Override
89        public RunScriptOptions overrideLoginCredentialWith(String loginCredential) {
90           throw new IllegalArgumentException("loginCredential is immutable");
91        }
92  
93        @Override
94        public RunScriptOptions wrapInInitScript(boolean wrapInInitScript) {
95           throw new IllegalArgumentException("wrapInInitScript is immutable");
96        }
97  
98        @Override
99        public RunScriptOptions overrideCredentialsWith(Credentials overridingCredentials) {
100          throw new IllegalArgumentException("overridingCredentials is immutable");
101       }
102 
103       @Override
104       public String getTaskName() {
105          return delegate.getTaskName();
106       }
107 
108       @Override
109       public RunScriptOptions nameTask(String name) {
110          throw new IllegalArgumentException("taskName is immutable");
111       }
112 
113       @Override
114       public RunScriptOptions blockOnPort(int port, int seconds) {
115          throw new IllegalArgumentException("port, seconds are immutable");
116       }
117 
118       @Override
119       public int getPort() {
120          return delegate.getPort();
121       }
122 
123       @Override
124       public int getSeconds() {
125          return delegate.getSeconds();
126       }
127    }
128 
129    protected int port = -1;
130    protected int seconds = -1;
131    protected String taskName;
132    protected Credentials overridingCredentials;
133    protected boolean runAsRoot = true;
134    protected boolean blockOnComplete = true;
135    protected boolean wrapInInitScript = true;
136 
137    public RunScriptOptions overrideCredentialsWith(Credentials overridingCredentials) {
138       checkNotNull(overridingCredentials, "overridingCredentials");
139       this.overridingCredentials = overridingCredentials;
140       return this;
141    }
142 
143    public RunScriptOptions overrideLoginUserWith(String loginUser) {
144       checkNotNull(loginUser, "loginUser");
145       org.jclouds.domain.Credentials.Builder<? extends Credentials> builder = overridingCredentials != null ? overridingCredentials
146             .toBuilder() : new Credentials.Builder<Credentials>();
147       this.overridingCredentials = builder.identity(loginUser).build();
148       return this;
149    }
150 
151    public RunScriptOptions overrideLoginCredentialWith(String loginCredential) {
152       checkNotNull(loginCredential, "loginCredential");
153       org.jclouds.domain.Credentials.Builder<? extends Credentials> builder = overridingCredentials != null ? overridingCredentials
154             .toBuilder() : new Credentials.Builder<Credentials>();
155       this.overridingCredentials = builder.credential(loginCredential).build();
156       return this;
157    }
158 
159    /**
160     * @return What to call the task relating to this script; default
161     *         {@code jclouds-script-timestamp} where timestamp is millis since epoch
162     * 
163     */
164    public RunScriptOptions nameTask(String name) {
165       this.taskName = name;
166       return this;
167    }
168 
169    public RunScriptOptions runAsRoot(boolean runAsRoot) {
170       this.runAsRoot = runAsRoot;
171       return this;
172    }
173 
174    /**
175     * default true
176     * <p/>
177     * 
178     * @param wrapInInitScript
179     *           if the command is long-running, use this option to ensure it is wrapInInitScripted
180     *           properly. (ex. have jclouds wrap it an init script, nohup, etc)
181     * @return
182     */
183    public RunScriptOptions wrapInInitScript(boolean wrapInInitScript) {
184       this.wrapInInitScript = wrapInInitScript;
185       return this;
186    }
187 
188    /**
189     * As of version 1.1.0, we cannot kick off a script unless a node is in
190     * RUNNING state.
191     * 
192     * @param blockOnComplete (default true)
193     *           false means kick off the script in the background, but don't
194     *           wait for it to finish. (as of version 1.1.0, implemented as
195     *           nohup)
196     */
197    public RunScriptOptions blockOnComplete(boolean blockOnComplete) {
198       this.blockOnComplete = blockOnComplete;
199       return this;
200    }
201 
202    /**
203     * When the node is started, wait until the following port is active
204     */
205    public RunScriptOptions blockOnPort(int port, int seconds) {
206       checkArgument(port > 0 && port < 65536, "port must be a positive integer < 65535");
207       checkArgument(seconds > 0, "seconds must be a positive integer");
208       this.port = port;
209       this.seconds = seconds;
210       return this;
211    }
212 
213    public String getTaskName() {
214       return taskName;
215    }
216 
217    public int getPort() {
218       return port;
219    }
220 
221    public int getSeconds() {
222       return seconds;
223    }
224 
225    /**
226     * Whether to override the credentials with ones supplied in call to
227     * {@link org.jclouds.compute.ComputeService#runScriptOnNodesWithTag}. By default, true.
228     * 
229     * @return value
230     */
231    public Credentials getOverridingCredentials() {
232       return overridingCredentials;
233    }
234 
235    /**
236     * Whether to run the script as root (or run with current privileges). By default, true.
237     * 
238     * @return value
239     */
240    public boolean shouldRunAsRoot() {
241       return runAsRoot;
242    }
243 
244    /**
245     * @see #blockOnComplete(boolean)
246     */
247    public boolean shouldBlockOnComplete() {
248       return blockOnComplete;
249    }
250 
251    /**
252     * Whether to wait until the script has completed. By default, true.
253     * 
254     * @return value
255     */
256    public boolean shouldWrapInInitScript() {
257       return wrapInInitScript;
258    }
259 
260    public static class Builder {
261 
262       public static RunScriptOptions nameTask(String name) {
263          RunScriptOptions options = new RunScriptOptions();
264          return options.nameTask(name);
265       }
266 
267       public static RunScriptOptions overrideLoginUserWith(String user) {
268          RunScriptOptions options = new RunScriptOptions();
269          return options.overrideLoginUserWith(user);
270       }
271 
272       public static RunScriptOptions overrideLoginCredentialWith(String credential) {
273          RunScriptOptions options = new RunScriptOptions();
274          return options.overrideLoginCredentialWith(credential);
275       }
276 
277       public static RunScriptOptions overrideCredentialsWith(Credentials credentials) {
278          RunScriptOptions options = new RunScriptOptions();
279          return options.overrideCredentialsWith(credentials);
280       }
281 
282       public static RunScriptOptions runAsRoot(boolean value) {
283          RunScriptOptions options = new RunScriptOptions();
284          return options.runAsRoot(value);
285       }
286       
287       /**
288        * @see RunScriptOptions#blockOnComplete(boolean)
289        */
290       public static RunScriptOptions blockOnComplete(boolean value) {
291          RunScriptOptions options = new RunScriptOptions();
292          return options.blockOnComplete(value);
293       }
294 
295       public static RunScriptOptions wrapInInitScript(boolean value) {
296          RunScriptOptions options = new RunScriptOptions();
297          return options.wrapInInitScript(value);
298       }
299 
300       public static RunScriptOptions blockOnPort(int port, int seconds) {
301          RunScriptOptions options = new RunScriptOptions();
302          return options.blockOnPort(port, seconds);
303       }
304 
305    }
306 
307    @Override
308    public String toString() {
309       return "[overridingCredentials=" + (overridingCredentials != null) + ", port:seconds=" + port + ":" + seconds
310             + ", runAsRoot=" + runAsRoot + ", blockOnComplete=" + blockOnComplete + ", wrapInInitScript="
311             + wrapInInitScript + "]";
312    }
313 
314 }