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 public RunScriptOptions blockOnComplete(boolean blockOnComplete) {
189 this.blockOnComplete = blockOnComplete;
190 return this;
191 }
192
193
194
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
218
219
220
221
222 public Credentials getOverridingCredentials() {
223 return overridingCredentials;
224 }
225
226
227
228
229
230
231 public boolean shouldRunAsRoot() {
232 return runAsRoot;
233 }
234
235
236
237
238
239
240 public boolean shouldBlockOnComplete() {
241 return blockOnComplete;
242 }
243
244
245
246
247
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 }