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 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
132
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
147
148
149
150
151
152
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
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
189
190
191
192
193 public Credentials getOverrideCredentials() {
194 return overridingCredentials;
195 }
196
197
198
199
200
201
202 public boolean shouldRunAsRoot() {
203 return runAsRoot;
204 }
205
206
207
208
209
210
211 public boolean shouldBlockOnComplete() {
212 return blockOnComplete;
213 }
214
215
216
217
218
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 }