View Javadoc

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 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    public RunScriptOptions blockOnComplete(boolean blockOnComplete) {
189       this.blockOnComplete = blockOnComplete;
190       return this;
191    }
192 
193    /**
194     * When the node is started, wait until the following port is active
195     */
196    public RunScriptOptions blockOnPort(int port, int seconds) {
197       checkArgument(port > 0 && port < 65536, "port must be a positive integer < 65535");
198       checkArgument(seconds > 0, "seconds must be a positive integer");
199       this.port = port;
200       this.seconds = seconds;
201       return this;
202    }
203 
204    public String getTaskName() {
205       return taskName;
206    }
207 
208    public int getPort() {
209       return port;
210    }
211 
212    public int getSeconds() {
213       return seconds;
214    }
215 
216    /**
217     * Whether to override the credentials with ones supplied in call to
218     * {@link org.jclouds.compute.ComputeService#runScriptOnNodesWithTag}. By default, true.
219     * 
220     * @return value
221     */
222    public Credentials getOverridingCredentials() {
223       return overridingCredentials;
224    }
225 
226    /**
227     * Whether to run the script as root (or run with current privileges). By default, true.
228     * 
229     * @return value
230     */
231    public boolean shouldRunAsRoot() {
232       return runAsRoot;
233    }
234 
235    /**
236     * Whether to wait until the script has completed. By default, true.
237     * 
238     * @return value
239     */
240    public boolean shouldBlockOnComplete() {
241       return blockOnComplete;
242    }
243 
244    /**
245     * Whether to wait until the script has completed. By default, true.
246     * 
247     * @return value
248     */
249    public boolean shouldWrapInInitScript() {
250       return wrapInInitScript;
251    }
252 
253    public static class Builder {
254 
255       public static RunScriptOptions nameTask(String name) {
256          RunScriptOptions options = new RunScriptOptions();
257          return options.nameTask(name);
258       }
259 
260       public static RunScriptOptions overrideLoginUserWith(String user) {
261          RunScriptOptions options = new RunScriptOptions();
262          return options.overrideLoginUserWith(user);
263       }
264 
265       public static RunScriptOptions overrideLoginCredentialWith(String credential) {
266          RunScriptOptions options = new RunScriptOptions();
267          return options.overrideLoginCredentialWith(credential);
268       }
269 
270       public static RunScriptOptions overrideCredentialsWith(Credentials credentials) {
271          RunScriptOptions options = new RunScriptOptions();
272          return options.overrideCredentialsWith(credentials);
273       }
274 
275       public static RunScriptOptions runAsRoot(boolean value) {
276          RunScriptOptions options = new RunScriptOptions();
277          return options.runAsRoot(value);
278       }
279 
280       public static RunScriptOptions blockOnComplete(boolean value) {
281          RunScriptOptions options = new RunScriptOptions();
282          return options.blockOnComplete(value);
283       }
284 
285       public static RunScriptOptions wrapInInitScript(boolean value) {
286          RunScriptOptions options = new RunScriptOptions();
287          return options.wrapInInitScript(value);
288       }
289 
290       public static RunScriptOptions blockOnPort(int port, int seconds) {
291          RunScriptOptions options = new RunScriptOptions();
292          return options.blockOnPort(port, seconds);
293       }
294 
295    }
296 
297    @Override
298    public String toString() {
299       return "[overridingCredentials=" + (overridingCredentials != null) + ", port:seconds=" + port + ":" + seconds
300             + ", runAsRoot=" + runAsRoot + ", blockOnComplete=" + blockOnComplete + ", wrapInInitScript="
301             + wrapInInitScript + "]";
302    }
303 
304 }