1 | /** |
2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
3 | * contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. jclouds licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. 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, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | package org.jclouds.compute.callables; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | import static com.google.common.base.Preconditions.checkState; |
23 | |
24 | import java.util.concurrent.TimeUnit; |
25 | |
26 | import javax.inject.Inject; |
27 | |
28 | import org.jclouds.compute.domain.ExecResponse; |
29 | import org.jclouds.compute.domain.NodeMetadata; |
30 | import org.jclouds.compute.options.RunScriptOptions; |
31 | import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts; |
32 | import org.jclouds.scriptbuilder.domain.Statement; |
33 | import org.jclouds.ssh.SshClient; |
34 | |
35 | import com.google.common.base.Function; |
36 | import com.google.common.base.Throwables; |
37 | import com.google.inject.assistedinject.Assisted; |
38 | |
39 | /** |
40 | * |
41 | * @author Adrian Cole |
42 | */ |
43 | public class RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilComplete extends RunScriptOnNodeAsInitScriptUsingSsh { |
44 | protected final Timeouts timeouts; |
45 | protected final BlockUntilInitScriptStatusIsZeroThenReturnOutput.Factory statusFactory; |
46 | |
47 | @Inject |
48 | public RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilComplete( |
49 | BlockUntilInitScriptStatusIsZeroThenReturnOutput.Factory statusFactory, Timeouts timeouts, |
50 | Function<NodeMetadata, SshClient> sshFactory, InitScriptConfigurationForTasks initScriptConfiguration, |
51 | @Assisted NodeMetadata node, @Assisted Statement script, @Assisted RunScriptOptions options) { |
52 | super(sshFactory, initScriptConfiguration, node, script, options); |
53 | this.statusFactory = checkNotNull(statusFactory, "statusFactory"); |
54 | this.timeouts = checkNotNull(timeouts, "timeouts"); |
55 | } |
56 | |
57 | @Override |
58 | public ExecResponse doCall() { |
59 | try { |
60 | return future().get(timeouts.scriptComplete, TimeUnit.MILLISECONDS); |
61 | } catch (Exception e) { |
62 | Throwables.propagate(e); |
63 | return null; |
64 | } |
65 | } |
66 | |
67 | public BlockUntilInitScriptStatusIsZeroThenReturnOutput future() { |
68 | ExecResponse returnVal = super.doCall(); |
69 | checkState(returnVal.getExitCode() == 0, String.format("task: %s had non-zero exit status: %s", init |
70 | .getInstanceName(), returnVal)); |
71 | return statusFactory.create(this).init(); |
72 | } |
73 | |
74 | @Override |
75 | public RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilComplete init() { |
76 | return RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilComplete.class.cast(super.init()); |
77 | } |
78 | } |