EMMA Coverage Report (generated Wed Jun 22 19:47:49 EDT 2011)
[all classes][org.jclouds.compute.internal]

COVERAGE SUMMARY FOR SOURCE FILE [BaseComputeService.java]

nameclass, %method, %block, %line, %
BaseComputeService.java12%  (1/8)4%   (2/57)13%  (169/1309)16%  (30/187)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BaseComputeService$10%   (0/1)0%   (0/2)0%   (0/42)0%   (0/8)
BaseComputeService$1 (BaseComputeService, String, AtomicReference): void 0%   (0/1)0%   (0/12)0%   (0/1)
apply (String): boolean 0%   (0/1)0%   (0/30)0%   (0/7)
     
class BaseComputeService$20%   (0/1)0%   (0/2)0%   (0/16)0%   (0/2)
BaseComputeService$2 (BaseComputeService): void 0%   (0/1)0%   (0/6)0%   (0/1)
apply (NodeMetadata): Future 0%   (0/1)0%   (0/10)0%   (0/1)
     
class BaseComputeService$2$10%   (0/1)0%   (0/3)0%   (0/32)0%   (0/4)
BaseComputeService$2$1 (BaseComputeService$2, NodeMetadata): void 0%   (0/1)0%   (0/9)0%   (0/1)
call (): NodeMetadata 0%   (0/1)0%   (0/10)0%   (0/2)
toString (): String 0%   (0/1)0%   (0/13)0%   (0/1)
     
class BaseComputeService$30%   (0/1)0%   (0/2)0%   (0/14)0%   (0/3)
BaseComputeService$3 (BaseComputeService): void 0%   (0/1)0%   (0/6)0%   (0/1)
apply (NodeMetadata): Future 0%   (0/1)0%   (0/8)0%   (0/2)
     
class BaseComputeService$40%   (0/1)0%   (0/2)0%   (0/14)0%   (0/3)
BaseComputeService$4 (BaseComputeService): void 0%   (0/1)0%   (0/6)0%   (0/1)
apply (NodeMetadata): Future 0%   (0/1)0%   (0/8)0%   (0/2)
     
class BaseComputeService$50%   (0/1)0%   (0/2)0%   (0/14)0%   (0/3)
BaseComputeService$5 (BaseComputeService): void 0%   (0/1)0%   (0/6)0%   (0/1)
apply (NodeMetadata): Future 0%   (0/1)0%   (0/8)0%   (0/2)
     
class BaseComputeService$TransformNodesIntoInitializedScriptRunners0%   (0/1)0%   (0/3)0%   (0/97)0%   (0/15)
BaseComputeService$TransformNodesIntoInitializedScriptRunners (BaseComputeSer... 0%   (0/1)0%   (0/24)0%   (0/5)
BaseComputeService$TransformNodesIntoInitializedScriptRunners (BaseComputeSer... 0%   (0/1)0%   (0/7)0%   (0/1)
apply (NodeMetadata): Future 0%   (0/1)0%   (0/66)0%   (0/9)
     
class BaseComputeService100% (1/1)5%   (2/41)16%  (169/1080)20%  (30/151)
access$000 (BaseComputeService): DestroyNodeStrategy 0%   (0/1)0%   (0/3)0%   (0/1)
access$100 (BaseComputeService): ExecutorService 0%   (0/1)0%   (0/3)0%   (0/1)
access$300 (BaseComputeService): InitializeRunScriptOnNodeOrPlaceInBadMap$Fac... 0%   (0/1)0%   (0/3)0%   (0/1)
createNodesInGroup (String, int): Set 0%   (0/1)0%   (0/7)0%   (0/1)
createNodesInGroup (String, int, Template): Set 0%   (0/1)0%   (0/138)0%   (0/16)
createNodesInGroup (String, int, TemplateOptions): Set 0%   (0/1)0%   (0/11)0%   (0/1)
destroyNode (String): void 0%   (0/1)0%   (0/80)0%   (0/9)
destroyNodesMatching (Predicate): Set 0%   (0/1)0%   (0/49)0%   (0/4)
detailsOnAllNodes (): Set 0%   (0/1)0%   (0/6)0%   (0/1)
getContext (): ComputeServiceContext 0%   (0/1)0%   (0/3)0%   (0/1)
getNodeMetadata (String): NodeMetadata 0%   (0/1)0%   (0/9)0%   (0/2)
listAssignableLocations (): Set 0%   (0/1)0%   (0/5)0%   (0/1)
listHardwareProfiles (): Set 0%   (0/1)0%   (0/5)0%   (0/1)
listImages (): Set 0%   (0/1)0%   (0/5)0%   (0/1)
listNodesDetailsMatching (Predicate): Set 0%   (0/1)0%   (0/34)0%   (0/5)
nodesMatchingFilterAndNotTerminated (Predicate): Iterable 0%   (0/1)0%   (0/11)0%   (0/1)
nodesMatchingFilterAndNotTerminatedExceptionIfNotFound (Predicate): Iterable 0%   (0/1)0%   (0/21)0%   (0/4)
rebootNode (String): void 0%   (0/1)0%   (0/40)0%   (0/6)
rebootNodesMatching (Predicate): void 0%   (0/1)0%   (0/41)0%   (0/4)
resumeNode (String): void 0%   (0/1)0%   (0/40)0%   (0/6)
resumeNodesMatching (Predicate): void 0%   (0/1)0%   (0/41)0%   (0/4)
runNodesWithTag (String, int): Set 0%   (0/1)0%   (0/7)0%   (0/1)
runNodesWithTag (String, int, Template): Set 0%   (0/1)0%   (0/6)0%   (0/1)
runNodesWithTag (String, int, TemplateOptions): Set 0%   (0/1)0%   (0/11)0%   (0/1)
runScriptOnNode (String, Statement): ExecResponse 0%   (0/1)0%   (0/6)0%   (0/1)
runScriptOnNode (String, Statement, RunScriptOptions): ExecResponse 0%   (0/1)0%   (0/41)0%   (0/6)
runScriptOnNode (String, String): ExecResponse 0%   (0/1)0%   (0/6)0%   (0/1)
runScriptOnNode (String, String, RunScriptOptions): ExecResponse 0%   (0/1)0%   (0/10)0%   (0/1)
runScriptOnNodesMatching (Predicate, Payload): Map 0%   (0/1)0%   (0/6)0%   (0/1)
runScriptOnNodesMatching (Predicate, Payload, RunScriptOptions): Map 0%   (0/1)0%   (0/18)0%   (0/4)
runScriptOnNodesMatching (Predicate, Statement): Map 0%   (0/1)0%   (0/6)0%   (0/1)
runScriptOnNodesMatching (Predicate, Statement, RunScriptOptions): Map 0%   (0/1)0%   (0/110)0%   (0/19)
runScriptOnNodesMatching (Predicate, String): Map 0%   (0/1)0%   (0/9)0%   (0/1)
runScriptOnNodesMatching (Predicate, String, RunScriptOptions): Map 0%   (0/1)0%   (0/10)0%   (0/1)
suspendNode (String): void 0%   (0/1)0%   (0/40)0%   (0/6)
suspendNodesMatching (Predicate): void 0%   (0/1)0%   (0/41)0%   (0/4)
templateBuilder (): TemplateBuilder 0%   (0/1)0%   (0/5)0%   (0/1)
templateOptions (): TemplateOptions 0%   (0/1)0%   (0/5)0%   (0/1)
transformNodesIntoInitializedScriptRunners (Iterable, Statement, RunScriptOpt... 0%   (0/1)0%   (0/19)0%   (0/1)
BaseComputeService (ComputeServiceContext, Map, Supplier, Supplier, Supplier,... 100% (1/1)100% (144/144)100% (26/26)
listNodes (): Set 100% (1/1)100% (25/25)100% (4/4)

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 */
19package org.jclouds.compute.internal;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22import static com.google.common.base.Predicates.and;
23import static com.google.common.base.Predicates.not;
24import static com.google.common.base.Predicates.notNull;
25import static com.google.common.collect.Iterables.filter;
26import static com.google.common.collect.Maps.newLinkedHashMap;
27import static com.google.common.collect.Sets.filter;
28import static com.google.common.collect.Sets.newLinkedHashSet;
29import static com.google.common.util.concurrent.Futures.immediateFuture;
30import static org.jclouds.compute.predicates.NodePredicates.TERMINATED;
31import static org.jclouds.compute.predicates.NodePredicates.all;
32import static org.jclouds.concurrent.FutureIterables.awaitCompletion;
33import static org.jclouds.concurrent.FutureIterables.transformParallel;
34 
35import java.io.IOException;
36import java.util.Map;
37import java.util.NoSuchElementException;
38import java.util.Set;
39import java.util.concurrent.Callable;
40import java.util.concurrent.ExecutorService;
41import java.util.concurrent.Future;
42import java.util.concurrent.TimeUnit;
43import java.util.concurrent.atomic.AtomicReference;
44 
45import javax.annotation.Resource;
46import javax.inject.Inject;
47import javax.inject.Named;
48import javax.inject.Provider;
49import javax.inject.Singleton;
50 
51import org.jclouds.Constants;
52import org.jclouds.collect.Memoized;
53import org.jclouds.compute.ComputeService;
54import org.jclouds.compute.ComputeServiceContext;
55import org.jclouds.compute.RunNodesException;
56import org.jclouds.compute.RunScriptOnNodesException;
57import org.jclouds.compute.callables.RunScriptOnNode;
58import org.jclouds.compute.config.CustomizationResponse;
59import org.jclouds.compute.domain.ComputeMetadata;
60import org.jclouds.compute.domain.ExecResponse;
61import org.jclouds.compute.domain.Hardware;
62import org.jclouds.compute.domain.Image;
63import org.jclouds.compute.domain.NodeMetadata;
64import org.jclouds.compute.domain.NodeMetadataBuilder;
65import org.jclouds.compute.domain.NodeState;
66import org.jclouds.compute.domain.Template;
67import org.jclouds.compute.domain.TemplateBuilder;
68import org.jclouds.compute.options.RunScriptOptions;
69import org.jclouds.compute.options.TemplateOptions;
70import org.jclouds.compute.reference.ComputeServiceConstants;
71import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts;
72import org.jclouds.compute.strategy.CreateNodesInGroupThenAddToSet;
73import org.jclouds.compute.strategy.DestroyNodeStrategy;
74import org.jclouds.compute.strategy.GetNodeMetadataStrategy;
75import org.jclouds.compute.strategy.InitializeRunScriptOnNodeOrPlaceInBadMap;
76import org.jclouds.compute.strategy.ListNodesStrategy;
77import org.jclouds.compute.strategy.RebootNodeStrategy;
78import org.jclouds.compute.strategy.ResumeNodeStrategy;
79import org.jclouds.compute.strategy.RunScriptOnNodeAndAddToGoodMapOrPutExceptionIntoBadMap;
80import org.jclouds.compute.strategy.SuspendNodeStrategy;
81import org.jclouds.domain.Credentials;
82import org.jclouds.domain.Location;
83import org.jclouds.domain.Credentials.Builder;
84import org.jclouds.io.Payload;
85import org.jclouds.logging.Logger;
86import org.jclouds.predicates.RetryablePredicate;
87import org.jclouds.scriptbuilder.domain.Statement;
88import org.jclouds.scriptbuilder.domain.Statements;
89import org.jclouds.scriptbuilder.functions.InitAdminAccess;
90import org.jclouds.util.Maps2;
91import org.jclouds.util.Strings2;
92 
93import com.google.common.base.Function;
94import com.google.common.base.Predicate;
95import com.google.common.base.Supplier;
96import com.google.common.base.Throwables;
97import com.google.common.collect.ImmutableMap;
98import com.google.common.collect.ImmutableSet;
99import com.google.common.collect.Iterables;
100import com.google.common.collect.LinkedHashMultimap;
101import com.google.common.collect.Multimap;
102 
103/**
104 * 
105 * @author Adrian Cole
106 */
107@Singleton
108public class BaseComputeService implements ComputeService {
109 
110   @Resource
111   @Named(ComputeServiceConstants.COMPUTE_LOGGER)
112   protected Logger logger = Logger.NULL;
113 
114   protected final ComputeServiceContext context;
115   protected final Map<String, Credentials> credentialStore;
116 
117   private final Supplier<Set<? extends Image>> images;
118   private final Supplier<Set<? extends Hardware>> hardwareProfiles;
119   private final Supplier<Set<? extends Location>> locations;
120   private final ListNodesStrategy listNodesStrategy;
121   private final GetNodeMetadataStrategy getNodeMetadataStrategy;
122   private final CreateNodesInGroupThenAddToSet runNodesAndAddToSetStrategy;
123   private final RebootNodeStrategy rebootNodeStrategy;
124   private final DestroyNodeStrategy destroyNodeStrategy;
125   private final ResumeNodeStrategy resumeNodeStrategy;
126   private final SuspendNodeStrategy suspendNodeStrategy;
127   private final Provider<TemplateBuilder> templateBuilderProvider;
128   private final Provider<TemplateOptions> templateOptionsProvider;
129   private final Predicate<NodeMetadata> nodeRunning;
130   private final Predicate<NodeMetadata> nodeTerminated;
131   private final Predicate<NodeMetadata> nodeSuspended;
132   private final InitializeRunScriptOnNodeOrPlaceInBadMap.Factory initScriptRunnerFactory;
133   private final Timeouts timeouts;
134   private final InitAdminAccess initAdminAccess;
135   private final PersistNodeCredentials persistNodeCredentials;
136   private final RunScriptOnNode.Factory runScriptOnNodeFactory;
137   private final ExecutorService executor;
138 
139   @Inject
140   protected BaseComputeService(ComputeServiceContext context, Map<String, Credentials> credentialStore,
141            @Memoized Supplier<Set<? extends Image>> images,
142            @Memoized Supplier<Set<? extends Hardware>> hardwareProfiles,
143            @Memoized Supplier<Set<? extends Location>> locations, ListNodesStrategy listNodesStrategy,
144            GetNodeMetadataStrategy getNodeMetadataStrategy,
145            CreateNodesInGroupThenAddToSet runNodesAndAddToSetStrategy, RebootNodeStrategy rebootNodeStrategy,
146            DestroyNodeStrategy destroyNodeStrategy, ResumeNodeStrategy resumeNodeStrategy,
147            SuspendNodeStrategy suspendNodeStrategy, Provider<TemplateBuilder> templateBuilderProvider,
148            Provider<TemplateOptions> templateOptionsProvider,
149            @Named("NODE_RUNNING") Predicate<NodeMetadata> nodeRunning,
150            @Named("NODE_TERMINATED") Predicate<NodeMetadata> nodeTerminated,
151            @Named("NODE_SUSPENDED") Predicate<NodeMetadata> nodeSuspended,
152            InitializeRunScriptOnNodeOrPlaceInBadMap.Factory initScriptRunnerFactory, InitAdminAccess initAdminAccess,
153            RunScriptOnNode.Factory runScriptOnNodeFactory, PersistNodeCredentials persistNodeCredentials,
154            Timeouts timeouts, @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor) {
155      this.context = checkNotNull(context, "context");
156      this.credentialStore = checkNotNull(credentialStore, "credentialStore");
157      this.images = checkNotNull(images, "images");
158      this.hardwareProfiles = checkNotNull(hardwareProfiles, "hardwareProfiles");
159      this.locations = checkNotNull(locations, "locations");
160      this.listNodesStrategy = checkNotNull(listNodesStrategy, "listNodesStrategy");
161      this.getNodeMetadataStrategy = checkNotNull(getNodeMetadataStrategy, "getNodeMetadataStrategy");
162      this.runNodesAndAddToSetStrategy = checkNotNull(runNodesAndAddToSetStrategy, "runNodesAndAddToSetStrategy");
163      this.rebootNodeStrategy = checkNotNull(rebootNodeStrategy, "rebootNodeStrategy");
164      this.resumeNodeStrategy = checkNotNull(resumeNodeStrategy, "resumeNodeStrategy");
165      this.suspendNodeStrategy = checkNotNull(suspendNodeStrategy, "suspendNodeStrategy");
166      this.destroyNodeStrategy = checkNotNull(destroyNodeStrategy, "destroyNodeStrategy");
167      this.templateBuilderProvider = checkNotNull(templateBuilderProvider, "templateBuilderProvider");
168      this.templateOptionsProvider = checkNotNull(templateOptionsProvider, "templateOptionsProvider");
169      this.nodeRunning = checkNotNull(nodeRunning, "nodeRunning");
170      this.nodeTerminated = checkNotNull(nodeTerminated, "nodeTerminated");
171      this.nodeSuspended = checkNotNull(nodeSuspended, "nodeSuspended");
172      this.initScriptRunnerFactory = checkNotNull(initScriptRunnerFactory, "initScriptRunnerFactory");
173      this.timeouts = checkNotNull(timeouts, "timeouts");
174      this.initAdminAccess = checkNotNull(initAdminAccess, "initAdminAccess");
175      this.runScriptOnNodeFactory = checkNotNull(runScriptOnNodeFactory, "runScriptOnNodeFactory");
176      this.persistNodeCredentials = checkNotNull(persistNodeCredentials, "persistNodeCredentials");
177      this.executor = checkNotNull(executor, "executor");
178   }
179 
180   /**
181    * {@inheritDoc}
182    */
183   @Override
184   public ComputeServiceContext getContext() {
185      return context;
186   }
187 
188   /**
189    * {@inheritDoc}
190    */
191   @Override
192   public Set<? extends NodeMetadata> runNodesWithTag(String group, int count, Template template)
193            throws RunNodesException {
194      return createNodesInGroup(group, count, template);
195   }
196 
197   /**
198    * {@inheritDoc}
199    */
200   @Override
201   public Set<? extends NodeMetadata> runNodesWithTag(String group, int count, TemplateOptions templateOptions)
202            throws RunNodesException {
203      return createNodesInGroup(group, count, templateBuilder().any().options(templateOptions).build());
204   }
205 
206   /**
207    * {@inheritDoc}
208    */
209   @Override
210   public Set<? extends NodeMetadata> runNodesWithTag(String group, int count) throws RunNodesException {
211      return createNodesInGroup(group, count, templateOptions());
212   }
213 
214   @Override
215   public Set<? extends NodeMetadata> createNodesInGroup(String group, int count, Template template)
216            throws RunNodesException {
217      checkNotNull(group, "group cannot be null");
218      checkNotNull(template.getLocation(), "location");
219      logger.debug(">> running %d node%s group(%s) location(%s) image(%s) hardwareProfile(%s) options(%s)", count,
220               count > 1 ? "s" : "", group, template.getLocation().getId(), template.getImage().getId(), template
221                        .getHardware().getId(), template.getOptions());
222      Set<NodeMetadata> goodNodes = newLinkedHashSet();
223      Map<NodeMetadata, Exception> badNodes = newLinkedHashMap();
224      Multimap<NodeMetadata, CustomizationResponse> customizationResponses = LinkedHashMultimap.create();
225 
226      if (template.getOptions().getRunScript() != null)
227         template.getOptions().runScript(initAdminAccess.apply(template.getOptions().getRunScript()));
228 
229      Map<?, Future<Void>> responses = runNodesAndAddToSetStrategy.execute(group, count, template, goodNodes, badNodes,
230               customizationResponses);
231      Map<?, Exception> executionExceptions = awaitCompletion(responses, executor, null, logger, "runNodesWithTag("
232               + group + ")");
233      Function<NodeMetadata, NodeMetadata> fn = persistNodeCredentials.always(template.getOptions().getRunScript());
234      badNodes = Maps2.transformKeys(badNodes, fn);
235      goodNodes = ImmutableSet.copyOf(Iterables.transform(goodNodes, fn));
236      if (executionExceptions.size() > 0 || badNodes.size() > 0) {
237         throw new RunNodesException(group, count, template, goodNodes, executionExceptions, badNodes);
238      }
239      return goodNodes;
240   }
241 
242   @Override
243   public Set<? extends NodeMetadata> createNodesInGroup(String group, int count, TemplateOptions templateOptions)
244            throws RunNodesException {
245      return createNodesInGroup(group, count, templateBuilder().any().options(templateOptions).build());
246   }
247 
248   @Override
249   public Set<? extends NodeMetadata> createNodesInGroup(String group, int count) throws RunNodesException {
250      return createNodesInGroup(group, count, templateOptions());
251   }
252 
253   /**
254    * {@inheritDoc}
255    */
256   @Override
257   public void destroyNode(final String id) {
258      checkNotNull(id, "id");
259      logger.debug(">> destroying node(%s)", id);
260      final AtomicReference<NodeMetadata> node = new AtomicReference<NodeMetadata>();
261      RetryablePredicate<String> tester = new RetryablePredicate<String>(new Predicate<String>() {
262 
263         @Override
264         public boolean apply(String input) {
265            try {
266               NodeMetadata md = destroyNodeStrategy.destroyNode(id);
267               if (md != null)
268                  node.set(md);
269               return true;
270            } catch (IllegalStateException e) {
271               logger.warn("<< illegal state destroying node(%s)", id);
272               return false;
273            }
274         }
275 
276      }, timeouts.nodeRunning, 1000, TimeUnit.MILLISECONDS);
277      boolean successful = tester.apply(id) && (node.get() == null || nodeTerminated.apply(node.get()));
278      if (successful)
279         credentialStore.remove("node#" + id);
280      logger.debug("<< destroyed node(%s) success(%s)", id, successful);
281   }
282 
283   /**
284    * {@inheritDoc}
285    */
286   @Override
287   public Set<? extends NodeMetadata> destroyNodesMatching(Predicate<NodeMetadata> filter) {
288      logger.debug(">> destroying nodes matching(%s)", filter);
289      Set<NodeMetadata> set = newLinkedHashSet(transformParallel(nodesMatchingFilterAndNotTerminated(filter),
290               new Function<NodeMetadata, Future<NodeMetadata>>() {
291 
292                  // TODO make an async interface instead of re-wrapping
293                  @Override
294                  public Future<NodeMetadata> apply(final NodeMetadata from) {
295                     return executor.submit(new Callable<NodeMetadata>() {
296 
297                        @Override
298                        public NodeMetadata call() throws Exception {
299                           destroyNode(from.getId());
300                           return from;
301                        }
302 
303                        @Override
304                        public String toString() {
305                           return "destroyNode(" + from.getId() + ")";
306                        }
307                     });
308                  }
309 
310               }, executor, null, logger, "destroyNodesMatching(" + filter + ")"));
311      logger.debug("<< destroyed(%d)", set.size());
312      return set;
313   }
314 
315   Iterable<? extends NodeMetadata> nodesMatchingFilterAndNotTerminated(Predicate<NodeMetadata> filter) {
316      return filter(detailsOnAllNodes(), and(checkNotNull(filter, "filter"), not(TERMINATED)));
317   }
318 
319   /**
320    * @throws NoSuchElementException
321    *            if none found
322    */
323   Iterable<? extends NodeMetadata> nodesMatchingFilterAndNotTerminatedExceptionIfNotFound(
324            Predicate<NodeMetadata> filter) {
325      Iterable<? extends NodeMetadata> nodes = nodesMatchingFilterAndNotTerminated(filter);
326      if (Iterables.size(nodes) == 0)
327         throw new NoSuchElementException("no nodes matched filter: " + filter);
328      return nodes;
329   }
330 
331   /**
332    * {@inheritDoc}
333    */
334   @Override
335   public Set<ComputeMetadata> listNodes() {
336      logger.debug(">> listing nodes");
337      Set<ComputeMetadata> set = newLinkedHashSet(listNodesStrategy.listNodes());
338      logger.debug("<< list(%d)", set.size());
339      return set;
340   }
341 
342   /**
343    * {@inheritDoc}
344    */
345   @Override
346   public Set<? extends NodeMetadata> listNodesDetailsMatching(Predicate<ComputeMetadata> filter) {
347      checkNotNull(filter, "filter");
348      logger.debug(">> listing node details matching(%s)", filter);
349      Set<NodeMetadata> set = newLinkedHashSet(listNodesStrategy.listDetailsOnNodesMatching(filter));
350      logger.debug("<< list(%d)", set.size());
351      return set;
352   }
353 
354   /**
355    * {@inheritDoc}
356    */
357   @Override
358   public Set<? extends Hardware> listHardwareProfiles() {
359      return hardwareProfiles.get();
360   }
361 
362   /**
363    * {@inheritDoc}
364    */
365   @Override
366   public Set<? extends Image> listImages() {
367      return images.get();
368   }
369 
370   /**
371    * {@inheritDoc}
372    */
373   @Override
374   public Set<? extends Location> listAssignableLocations() {
375      return locations.get();
376   }
377 
378   /**
379    * {@inheritDoc}
380    */
381   @Override
382   public TemplateBuilder templateBuilder() {
383      return templateBuilderProvider.get();
384   }
385 
386   /**
387    * {@inheritDoc}
388    */
389   @Override
390   public NodeMetadata getNodeMetadata(String id) {
391      checkNotNull(id, "id");
392      return getNodeMetadataStrategy.getNode(id);
393   }
394 
395   /**
396    * {@inheritDoc}
397    */
398   @Override
399   public void rebootNode(String id) {
400      checkNotNull(id, "id");
401      logger.debug(">> rebooting node(%s)", id);
402      NodeMetadata node = rebootNodeStrategy.rebootNode(id);
403      boolean successful = nodeRunning.apply(node);
404      logger.debug("<< rebooted node(%s) success(%s)", id, successful);
405   }
406 
407   /**
408    * {@inheritDoc}
409    */
410   @Override
411   public void rebootNodesMatching(Predicate<NodeMetadata> filter) {
412      logger.debug(">> rebooting nodes matching(%s)", filter);
413      transformParallel(nodesMatchingFilterAndNotTerminatedExceptionIfNotFound(filter),
414               new Function<NodeMetadata, Future<Void>>() {
415                  // TODO use native async
416                  @Override
417                  public Future<Void> apply(NodeMetadata from) {
418                     rebootNode(from.getId());
419                     return immediateFuture(null);
420                  }
421 
422               }, executor, null, logger, "rebootNodesMatching(" + filter + ")");
423      logger.debug("<< rebooted");
424   }
425 
426   /**
427    * {@inheritDoc}
428    */
429   @Override
430   public void resumeNode(String id) {
431      checkNotNull(id, "id");
432      logger.debug(">> resuming node(%s)", id);
433      NodeMetadata node = resumeNodeStrategy.resumeNode(id);
434      boolean successful = nodeRunning.apply(node);
435      logger.debug("<< resumed node(%s) success(%s)", id, successful);
436   }
437 
438   /**
439    * {@inheritDoc}
440    */
441   @Override
442   public void resumeNodesMatching(Predicate<NodeMetadata> filter) {
443      logger.debug(">> resuming nodes matching(%s)", filter);
444      transformParallel(nodesMatchingFilterAndNotTerminatedExceptionIfNotFound(filter),
445               new Function<NodeMetadata, Future<Void>>() {
446                  // TODO use native async
447                  @Override
448                  public Future<Void> apply(NodeMetadata from) {
449                     resumeNode(from.getId());
450                     return immediateFuture(null);
451                  }
452 
453               }, executor, null, logger, "resumeNodesMatching(" + filter + ")");
454      logger.debug("<< resumed");
455   }
456 
457   /**
458    * {@inheritDoc}
459    */
460   @Override
461   public void suspendNode(String id) {
462      checkNotNull(id, "id");
463      logger.debug(">> suspending node(%s)", id);
464      NodeMetadata node = suspendNodeStrategy.suspendNode(id);
465      boolean successful = nodeSuspended.apply(node);
466      logger.debug("<< suspended node(%s) success(%s)", id, successful);
467   }
468 
469   /**
470    * {@inheritDoc}
471    */
472   @Override
473   public void suspendNodesMatching(Predicate<NodeMetadata> filter) {
474      logger.debug(">> suspending nodes matching(%s)", filter);
475      transformParallel(nodesMatchingFilterAndNotTerminatedExceptionIfNotFound(filter),
476               new Function<NodeMetadata, Future<Void>>() {
477                  // TODO use native async
478                  @Override
479                  public Future<Void> apply(NodeMetadata from) {
480                     suspendNode(from.getId());
481                     return immediateFuture(null);
482                  }
483 
484               }, executor, null, logger, "suspendNodesMatching(" + filter + ")");
485      logger.debug("<< suspended");
486   }
487 
488   /**
489    * {@inheritDoc}
490    */
491   @Override
492   public Map<NodeMetadata, ExecResponse> runScriptOnNodesMatching(Predicate<NodeMetadata> filter, Payload runScript)
493            throws RunScriptOnNodesException {
494      return runScriptOnNodesMatching(filter, runScript, RunScriptOptions.NONE);
495   }
496 
497   /**
498    * {@inheritDoc}
499    */
500   @Override
501   public Map<NodeMetadata, ExecResponse> runScriptOnNodesMatching(Predicate<NodeMetadata> filter, Payload runScript,
502            RunScriptOptions options) throws RunScriptOnNodesException {
503      try {
504         return runScriptOnNodesMatching(filter, Statements.exec(Strings2.toStringAndClose(checkNotNull(runScript,
505                  "runScript").getInput())), options);
506      } catch (IOException e) {
507         Throwables.propagate(e);
508         return null;
509      }
510   }
511 
512   /**
513    * {@inheritDoc}
514    */
515   @Override
516   public Map<NodeMetadata, ExecResponse> runScriptOnNodesMatching(Predicate<NodeMetadata> filter, String runScript)
517            throws RunScriptOnNodesException {
518      return runScriptOnNodesMatching(filter, Statements.exec(checkNotNull(runScript, "runScript")));
519   }
520 
521   /**
522    * {@inheritDoc}
523    */
524   @Override
525   public Map<NodeMetadata, ExecResponse> runScriptOnNodesMatching(Predicate<NodeMetadata> filter, Statement runScript)
526            throws RunScriptOnNodesException {
527      return runScriptOnNodesMatching(filter, runScript, RunScriptOptions.NONE);
528   }
529 
530   @Override
531   public Map<? extends NodeMetadata, ExecResponse> runScriptOnNodesMatching(Predicate<NodeMetadata> filter,
532            String runScript, RunScriptOptions options) throws RunScriptOnNodesException {
533      return runScriptOnNodesMatching(filter, Statements.exec(checkNotNull(runScript, "runScript")),
534               RunScriptOptions.NONE);
535   }
536 
537   /**
538    * {@inheritDoc}
539    */
540   @Override
541   public Map<NodeMetadata, ExecResponse> runScriptOnNodesMatching(Predicate<NodeMetadata> filter, Statement runScript,
542            RunScriptOptions options) throws RunScriptOnNodesException {
543 
544      checkNotNull(filter, "filter");
545      checkNotNull(runScript, "runScript");
546      checkNotNull(options, "options");
547 
548      Map<NodeMetadata, ExecResponse> goodNodes = newLinkedHashMap();
549      Map<NodeMetadata, Exception> badNodes = newLinkedHashMap();
550      Map<NodeMetadata, Future<ExecResponse>> responses = newLinkedHashMap();
551      Map<?, Exception> exceptions = ImmutableMap.<Object, Exception> of();
552 
553      runScript = initAdminAccess.apply(runScript);
554 
555      Iterable<? extends RunScriptOnNode> scriptRunners = transformNodesIntoInitializedScriptRunners(
556               nodesMatchingFilterAndNotTerminatedExceptionIfNotFound(filter), runScript, options, badNodes);
557      if (Iterables.size(scriptRunners) > 0) {
558         for (RunScriptOnNode runner : scriptRunners) {
559            responses.put(runner.getNode(), executor.submit(new RunScriptOnNodeAndAddToGoodMapOrPutExceptionIntoBadMap(
560                     runner, goodNodes, badNodes)));
561         }
562         exceptions = awaitCompletion(responses, executor, null, logger, "runScriptOnNodesMatching(" + filter + ")");
563      }
564 
565      Function<NodeMetadata, NodeMetadata> fn = persistNodeCredentials.ifAdminAccess(runScript);
566      badNodes = Maps2.transformKeys(badNodes, fn);
567      goodNodes = Maps2.transformKeys(goodNodes, fn);
568 
569      if (exceptions.size() > 0 || badNodes.size() > 0) {
570         throw new RunScriptOnNodesException(runScript, options, goodNodes, exceptions, badNodes);
571      }
572      return goodNodes;
573   }
574   
575   /**
576    * {@inheritDoc}
577    */
578   @Override
579   public ExecResponse runScriptOnNode(String id, String runScript) {
580      return runScriptOnNode(id, runScript, RunScriptOptions.NONE);
581   }
582   
583   /**
584    * {@inheritDoc}
585    */
586   @Override
587   public ExecResponse runScriptOnNode(String id, String runScript, RunScriptOptions options) {
588      return runScriptOnNode(id, Statements.exec(checkNotNull(runScript, "runScript")), options);
589   }
590   
591   /**
592    * {@inheritDoc}
593    */
594   @Override
595   public ExecResponse runScriptOnNode(String id, Statement runScript) {
596      return runScriptOnNode(id, runScript, RunScriptOptions.NONE);
597   }
598   
599   /**
600    * {@inheritDoc}
601    */
602   @Override
603   public ExecResponse runScriptOnNode(String id, Statement runScript, RunScriptOptions options) {
604      NodeMetadata node = this.getNodeMetadata(id);
605      if (node == null)
606         throw new NoSuchElementException(id);
607      if (node.getState() != NodeState.RUNNING)
608         throw new IllegalStateException("node " + id
609                  + " needs to be running before executing a script on it. current state: " + node.getState());
610      return runScriptOnNodeFactory.create(node, runScript, options).init().call();
611   }
612 
613   private Iterable<? extends RunScriptOnNode> transformNodesIntoInitializedScriptRunners(
614            Iterable<? extends NodeMetadata> nodes, Statement script, RunScriptOptions options,
615            Map<NodeMetadata, Exception> badNodes) {
616      return filter(transformParallel(nodes, new TransformNodesIntoInitializedScriptRunners(script, options, badNodes),
617               executor, null, logger, "initialize script runners"), notNull());
618   }
619 
620   private Set<? extends NodeMetadata> detailsOnAllNodes() {
621      return newLinkedHashSet(listNodesStrategy.listDetailsOnNodesMatching(all()));
622   }
623 
624   @Override
625   public TemplateOptions templateOptions() {
626      return templateOptionsProvider.get();
627   }
628 
629   private final class TransformNodesIntoInitializedScriptRunners implements
630            Function<NodeMetadata, Future<RunScriptOnNode>> {
631      private final Map<NodeMetadata, Exception> badNodes;
632      private final Statement script;
633      private final RunScriptOptions options;
634 
635      private TransformNodesIntoInitializedScriptRunners(Statement script, RunScriptOptions options,
636               Map<NodeMetadata, Exception> badNodes) {
637         this.badNodes = checkNotNull(badNodes, "badNodes");
638         this.script = checkNotNull(script, "script");
639         this.options = checkNotNull(options, "options");
640      }
641 
642      @Override
643      public Future<RunScriptOnNode> apply(NodeMetadata node) {
644         checkNotNull(node, "node");
645         if (options.getOverridingCredentials() != null) {
646            Builder<? extends Credentials> builder = node.getCredentials() != null ? node.getCredentials().toBuilder()
647                     : new Credentials.Builder<Credentials>();
648            if (options.getOverridingCredentials().identity != null)
649               builder.identity(options.getOverridingCredentials().identity);
650            if (options.getOverridingCredentials().credential != null)
651               builder.credential(options.getOverridingCredentials().credential);
652            node = NodeMetadataBuilder.fromNodeMetadata(node).credentials(builder.build()).build();
653         }
654         return executor.submit(initScriptRunnerFactory.create(node, script, options, badNodes));
655      }
656   }
657 
658}

[all classes][org.jclouds.compute.internal]
EMMA 2.0.5312 (C) Vladimir Roubtsov