1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
28
29
30
31 public class RunScriptOptions {
32
33
34
35
36
37
38
39
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
161
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
176
177
178
179
180
181
182
183 public RunScriptOptions wrapInInitScript(boolean wrapInInitScript) {
184 this.wrapInInitScript = wrapInInitScript;
185 return this;
186 }
187
188
189
190
191
192
193
194
195
196
197 public RunScriptOptions blockOnComplete(boolean blockOnComplete) {
198 this.blockOnComplete = blockOnComplete;
199 return this;
200 }
201
202
203
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
227
228
229
230
231 public Credentials getOverridingCredentials() {
232 return overridingCredentials;
233 }
234
235
236
237
238
239
240 public boolean shouldRunAsRoot() {
241 return runAsRoot;
242 }
243
244
245
246
247 public boolean shouldBlockOnComplete() {
248 return blockOnComplete;
249 }
250
251
252
253
254
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
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 }