| 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.Collections; |
| 25 | |
| 26 | import javax.annotation.Resource; |
| 27 | import javax.inject.Named; |
| 28 | |
| 29 | import org.jclouds.compute.domain.ExecResponse; |
| 30 | import org.jclouds.compute.domain.NodeMetadata; |
| 31 | import org.jclouds.compute.domain.NodeMetadataBuilder; |
| 32 | import org.jclouds.compute.options.RunScriptOptions; |
| 33 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 34 | import org.jclouds.logging.Logger; |
| 35 | import org.jclouds.scriptbuilder.InitBuilder; |
| 36 | import org.jclouds.scriptbuilder.domain.AdminAccessVisitor; |
| 37 | import org.jclouds.scriptbuilder.domain.AppendFile; |
| 38 | import org.jclouds.scriptbuilder.domain.OsFamily; |
| 39 | import org.jclouds.scriptbuilder.domain.Statement; |
| 40 | import org.jclouds.scriptbuilder.domain.Statements; |
| 41 | import org.jclouds.scriptbuilder.statements.login.AdminAccess; |
| 42 | import org.jclouds.ssh.SshClient; |
| 43 | import org.jclouds.ssh.SshException; |
| 44 | |
| 45 | import com.google.common.base.Function; |
| 46 | import com.google.common.base.Splitter; |
| 47 | import com.google.inject.assistedinject.Assisted; |
| 48 | import com.google.inject.assistedinject.AssistedInject; |
| 49 | |
| 50 | /** |
| 51 | * |
| 52 | * @author Adrian Cole |
| 53 | */ |
| 54 | public class RunScriptOnNodeAsInitScriptUsingSsh extends SudoAwareInitManager implements RunScriptOnNode { |
| 55 | @Resource |
| 56 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 57 | protected Logger logger = Logger.NULL; |
| 58 | |
| 59 | protected final String initFile; |
| 60 | |
| 61 | /** |
| 62 | * @return the absolute path to the file on disk relating to this task. |
| 63 | */ |
| 64 | public String getInitFile() { |
| 65 | return initFile; |
| 66 | } |
| 67 | |
| 68 | @AssistedInject |
| 69 | public RunScriptOnNodeAsInitScriptUsingSsh(Function<NodeMetadata, SshClient> sshFactory, |
| 70 | InitScriptConfigurationForTasks initScriptConfiguration, @Assisted NodeMetadata node, |
| 71 | @Assisted Statement script, @Assisted RunScriptOptions options) { |
| 72 | super(sshFactory, options.shouldRunAsRoot(), checkNotNull(node, "node"), |
| 73 | checkNotNull(script, "script") instanceof InitBuilder ? InitBuilder.class.cast(script) |
| 74 | : createInitScript(checkNotNull(initScriptConfiguration, "initScriptConfiguration"), options |
| 75 | .getTaskName(), script)); |
| 76 | this.initFile = String.format(initScriptConfiguration.getInitScriptPattern(), init.getInstanceName()); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public RunScriptOnNodeAsInitScriptUsingSsh init() { |
| 81 | super.init(); |
| 82 | return this; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public ExecResponse call() { |
| 87 | checkState(ssh != null, "please call init() before invoking call"); |
| 88 | try { |
| 89 | ssh.connect(); |
| 90 | return doCall(); |
| 91 | } finally { |
| 92 | if (ssh != null) |
| 93 | ssh.disconnect(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public static InitBuilder createInitScript(InitScriptConfigurationForTasks config, String name, Statement script) { |
| 98 | if (name == null) { |
| 99 | name = "jclouds-script-" + config.getAnonymousTaskSuffixSupplier().get(); |
| 100 | } |
| 101 | return new InitBuilder(name, config.getBasedir() + "/" + name, config.getBasedir() + "/" + name, Collections |
| 102 | .<String, String> emptyMap(), Collections.singleton(script)); |
| 103 | } |
| 104 | |
| 105 | protected void refreshSshIfNewAdminCredentialsConfigured(AdminAccess input) { |
| 106 | if (input.getAdminCredentials() != null && input.shouldGrantSudoToAdminUser()) { |
| 107 | ssh.disconnect(); |
| 108 | logger.debug(">> reconnecting as %s@%s", input.getAdminCredentials().identity, ssh.getHostAddress()); |
| 109 | ssh = sshFactory.apply(node = NodeMetadataBuilder.fromNodeMetadata(node).adminPassword(null).credentials( |
| 110 | input.getAdminCredentials()).build()); |
| 111 | ssh.connect(); |
| 112 | setupLinkToInitFile(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | protected ExecResponse doCall() { |
| 117 | try { |
| 118 | ssh.put(initFile, init.render(OsFamily.UNIX)); |
| 119 | } catch (SshException e) { |
| 120 | // If there's a problem with the sftp configuration, we can try via ssh exec |
| 121 | if (logger.isTraceEnabled()) |
| 122 | logger.warn(e, "<< (%s) problem using sftp [%s], attempting via sshexec", ssh.toString(), e.getMessage()); |
| 123 | else |
| 124 | logger.warn("<< (%s) problem using sftp [%s], attempting via sshexec", ssh.toString(), e.getMessage()); |
| 125 | ssh.disconnect(); |
| 126 | ssh.connect(); |
| 127 | ssh.exec("rm " + initFile); |
| 128 | ssh.exec(Statements.appendFile(initFile, Splitter.on('\n').split(init.render(OsFamily.UNIX)), |
| 129 | AppendFile.MARKER + "_" + init.getInstanceName()).render(OsFamily.UNIX)); |
| 130 | } |
| 131 | |
| 132 | ssh.exec("chmod 755 " + initFile); |
| 133 | setupLinkToInitFile(); |
| 134 | |
| 135 | runAction("init"); |
| 136 | init.getInitStatement().accept(new AdminAccessVisitor() { |
| 137 | |
| 138 | @Override |
| 139 | public void visit(AdminAccess input) { |
| 140 | refreshSshIfNewAdminCredentialsConfigured(input); |
| 141 | } |
| 142 | |
| 143 | }); |
| 144 | return runAction("start"); |
| 145 | } |
| 146 | |
| 147 | protected void setupLinkToInitFile() { |
| 148 | ssh.exec(String.format("ln -fs %s %s", initFile, init.getInstanceName())); |
| 149 | } |
| 150 | |
| 151 | } |