| 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.Inject; |
| 48 | import com.google.inject.assistedinject.Assisted; |
| 49 | import com.google.inject.assistedinject.AssistedInject; |
| 50 | |
| 51 | /** |
| 52 | * |
| 53 | * @author Adrian Cole |
| 54 | */ |
| 55 | public class RunScriptOnNodeAsInitScriptUsingSsh extends SudoAwareInitManager implements RunScriptOnNode { |
| 56 | public static final String PROPERTY_INIT_SCRIPT_PATTERN = "jclouds.compute.init-script-pattern"; |
| 57 | @Resource |
| 58 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 59 | protected Logger logger = Logger.NULL; |
| 60 | protected final String initFile; |
| 61 | |
| 62 | /** |
| 63 | * |
| 64 | * determines the naming convention of init scripts. |
| 65 | * |
| 66 | * ex. {@code /tmp/init-%s} |
| 67 | */ |
| 68 | @Inject(optional = true) |
| 69 | @Named(PROPERTY_INIT_SCRIPT_PATTERN) |
| 70 | private String initScriptPattern = "/tmp/init-%s"; |
| 71 | |
| 72 | @AssistedInject |
| 73 | public RunScriptOnNodeAsInitScriptUsingSsh(Function<NodeMetadata, SshClient> sshFactory, |
| 74 | @Assisted NodeMetadata node, @Assisted Statement script, @Assisted RunScriptOptions options) { |
| 75 | super(sshFactory, options.shouldRunAsRoot(), checkNotNull(node, "node"), init(script, options.getTaskName())); |
| 76 | this.initFile = String.format(initScriptPattern, options.getTaskName()); |
| 77 | } |
| 78 | |
| 79 | private static InitBuilder init(Statement script, String name) { |
| 80 | if (name == null) { |
| 81 | if (checkNotNull(script, "script") instanceof InitBuilder) |
| 82 | name = InitBuilder.class.cast(script).getInstanceName(); |
| 83 | else |
| 84 | name = "jclouds-script-" + System.currentTimeMillis(); |
| 85 | } |
| 86 | return checkNotNull(script, "script") instanceof InitBuilder ? InitBuilder.class.cast(script) : createInitScript( |
| 87 | name, script); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public RunScriptOnNodeAsInitScriptUsingSsh init() { |
| 92 | super.init(); |
| 93 | return this; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public ExecResponse call() { |
| 98 | checkState(ssh != null, "please call init() before invoking call"); |
| 99 | try { |
| 100 | ssh.connect(); |
| 101 | return doCall(); |
| 102 | } finally { |
| 103 | if (ssh != null) |
| 104 | ssh.disconnect(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | public static InitBuilder createInitScript(String name, Statement script) { |
| 109 | String path = "/tmp/" + name; |
| 110 | return new InitBuilder(name, path, path, Collections.<String, String> emptyMap(), Collections.singleton(script)); |
| 111 | } |
| 112 | |
| 113 | protected void refreshSshIfNewAdminCredentialsConfigured(AdminAccess input) { |
| 114 | if (input.getAdminCredentials() != null && input.shouldGrantSudoToAdminUser()) { |
| 115 | ssh.disconnect(); |
| 116 | logger.debug(">> reconnecting as %s@%s", input.getAdminCredentials().identity, ssh.getHostAddress()); |
| 117 | ssh = sshFactory.apply(node = NodeMetadataBuilder.fromNodeMetadata(node).adminPassword(null).credentials( |
| 118 | input.getAdminCredentials()).build()); |
| 119 | ssh.connect(); |
| 120 | setupLinkToInitFile(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | protected ExecResponse doCall() { |
| 125 | try { |
| 126 | ssh.put(initFile, init.render(OsFamily.UNIX)); |
| 127 | } catch (SshException e) { |
| 128 | // If there's a problem with the sftp configuration, we can try via ssh exec |
| 129 | if (logger.isTraceEnabled()) |
| 130 | logger.warn(e, "<< (%s) problem using sftp [%s], attempting via sshexec", ssh.toString(), e.getMessage()); |
| 131 | else |
| 132 | logger.warn("<< (%s) problem using sftp [%s], attempting via sshexec", ssh.toString(), e.getMessage()); |
| 133 | ssh.disconnect(); |
| 134 | ssh.connect(); |
| 135 | ssh.exec("rm " + initFile); |
| 136 | ssh.exec(Statements.appendFile(initFile, Splitter.on('\n').split(init.render(OsFamily.UNIX)), |
| 137 | AppendFile.MARKER + "_" + init.getInstanceName()).render(OsFamily.UNIX)); |
| 138 | } |
| 139 | |
| 140 | ssh.exec("chmod 755 " + initFile); |
| 141 | setupLinkToInitFile(); |
| 142 | |
| 143 | runAction("init"); |
| 144 | init.getInitStatement().accept(new AdminAccessVisitor() { |
| 145 | |
| 146 | @Override |
| 147 | public void visit(AdminAccess input) { |
| 148 | refreshSshIfNewAdminCredentialsConfigured(input); |
| 149 | } |
| 150 | |
| 151 | }); |
| 152 | return runAction("start"); |
| 153 | } |
| 154 | |
| 155 | protected void setupLinkToInitFile() { |
| 156 | ssh.exec(String.format("ln -fs %s %s", initFile, init.getInstanceName())); |
| 157 | } |
| 158 | |
| 159 | } |