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.predicates; |
20 | |
21 | import javax.annotation.Resource; |
22 | import javax.inject.Singleton; |
23 | |
24 | import org.jclouds.compute.domain.ExecResponse; |
25 | import org.jclouds.logging.Logger; |
26 | import org.jclouds.ssh.SshClient; |
27 | |
28 | import com.google.common.base.Predicate; |
29 | |
30 | /** |
31 | * |
32 | * Tests to if the runscript is still running |
33 | * |
34 | * @author Adrian Cole |
35 | */ |
36 | @Singleton |
37 | public class ScriptStatusReturnsZero implements |
38 | Predicate<ScriptStatusReturnsZero.CommandUsingClient> { |
39 | |
40 | @Resource |
41 | protected Logger logger = Logger.NULL; |
42 | |
43 | @Override |
44 | public boolean apply(CommandUsingClient commandUsingClient) { |
45 | logger.trace("looking for [%s] state on %s@%s", commandUsingClient.command, |
46 | commandUsingClient.client.getUsername(), commandUsingClient.client.getHostAddress()); |
47 | ExecResponse response = refresh(commandUsingClient); |
48 | while (response.getExitCode() == -1) |
49 | response = refresh(commandUsingClient); |
50 | logger.trace("%s@%s: looking for exit code 0: currently: %s", commandUsingClient.client |
51 | .getUsername(), commandUsingClient.client.getHostAddress(), response.getExitCode()); |
52 | return 0 == response.getExitCode(); |
53 | } |
54 | |
55 | private ExecResponse refresh(CommandUsingClient commandUsingClient) { |
56 | return commandUsingClient.client.exec(commandUsingClient.command); |
57 | } |
58 | |
59 | public static class CommandUsingClient { |
60 | |
61 | public CommandUsingClient(String command, SshClient client) { |
62 | this.command = command; |
63 | this.client = client; |
64 | } |
65 | |
66 | private final String command; |
67 | private final SshClient client; |
68 | } |
69 | } |