diff --git a/core/src/com/cloud/agent/api/UpdateHostPasswordCommand.java b/core/src/com/cloud/agent/api/UpdateHostPasswordCommand.java index e8dfa13a10a..1a75168176c 100644 --- a/core/src/com/cloud/agent/api/UpdateHostPasswordCommand.java +++ b/core/src/com/cloud/agent/api/UpdateHostPasswordCommand.java @@ -22,17 +22,26 @@ package com.cloud.agent.api; import com.cloud.agent.api.LogLevel.Log4jLevel; public class UpdateHostPasswordCommand extends Command { + @LogLevel(Log4jLevel.Off) protected String username; @LogLevel(Log4jLevel.Off) protected String newPassword; + @LogLevel(Log4jLevel.Off) + protected String hostIp; + protected UpdateHostPasswordCommand() { } - public UpdateHostPasswordCommand(String username, String newPassword) { + public UpdateHostPasswordCommand(final String username, final String newPassword) { + this(username, newPassword, null); + } + + public UpdateHostPasswordCommand(final String username, final String newPassword, final String hostIp) { this.username = username; this.newPassword = newPassword; + this.hostIp = hostIp; } public String getNewPassword() { @@ -43,8 +52,12 @@ public class UpdateHostPasswordCommand extends Command { return username; } + public String getHostIp() { + return hostIp; + } + @Override public boolean executeInSequence() { return false; } -} +} \ No newline at end of file diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java index 7bd02d09c9a..bc28f82f498 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java @@ -257,6 +257,8 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv private String _pingTestPath; + private String _updateHostPasswdPath; + private int _dom0MinMem; protected boolean _disconnected = true; @@ -372,6 +374,10 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return _pingTestPath; } + public String getUpdateHostPasswdPath() { + return _updateHostPasswdPath; + } + public int getTimeout() { return _timeout; } @@ -516,6 +522,10 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return "scripts/storage/qcow2"; } + protected String getDefaultHypervisorScriptsDir() { + return "scripts/vm/hypervisor"; + } + protected String getDefaultKvmScriptsDir() { return "scripts/vm/hypervisor/kvm"; } @@ -547,6 +557,11 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv domrScriptsDir = getDefaultDomrScriptsDir(); } + final String hypervisorScriptsDir = (String)params.get("hypervisor.scripts.dir"); + if (hypervisorScriptsDir == null) { + getDefaultHypervisorScriptsDir(); + } + String kvmScriptsDir = (String)params.get("kvm.scripts.dir"); if (kvmScriptsDir == null) { kvmScriptsDir = getDefaultKvmScriptsDir(); @@ -595,6 +610,11 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv _clusterId = (String)params.get("cluster"); + _updateHostPasswdPath = Script.findScript(hypervisorScriptsDir, "update_host_passwd.sh"); + if (_updateHostPasswdPath == null) { + throw new ConfigurationException("Unable to find update_host_passwd.sh"); + } + _modifyVlanPath = Script.findScript(networkScriptsDir, "modifyvlan.sh"); if (_modifyVlanPath == null) { throw new ConfigurationException("Unable to find modifyvlan.sh"); diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUpdateHostPasswordCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUpdateHostPasswordCommandWrapper.java index 5b7c7da2871..7ed827d8969 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUpdateHostPasswordCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUpdateHostPasswordCommandWrapper.java @@ -19,17 +19,34 @@ package com.cloud.hypervisor.kvm.resource.wrapper; +import org.apache.log4j.Logger; + import com.cloud.agent.api.Answer; import com.cloud.agent.api.UpdateHostPasswordCommand; import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; import com.cloud.resource.CommandWrapper; import com.cloud.resource.ResourceWrapper; +import com.cloud.utils.script.Script; @ResourceWrapper(handles = UpdateHostPasswordCommand.class) public final class LibvirtUpdateHostPasswordCommandWrapper extends CommandWrapper { + private static final Logger s_logger = Logger.getLogger(LibvirtUpdateHostPasswordCommandWrapper.class); + private static final int TIMEOUT = 10000; + @Override public Answer execute(final UpdateHostPasswordCommand command, final LibvirtComputingResource libvirtComputingResource) { - return new Answer(command, true, null); + final String hostIp = command.getHostIp(); + final String username = command.getUsername(); + final String newPassword = command.getNewPassword(); + + final Script script = new Script(libvirtComputingResource.getUpdateHostPasswdPath(), TIMEOUT, s_logger); + script.add(hostIp, username, newPassword); + final String result = script.execute(); + + if (result != null) { + return new Answer(command, false, result); + } + return new Answer(command); } } \ No newline at end of file diff --git a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java index d5ce9bf18cc..8ccdce5d56c 100644 --- a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java +++ b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java @@ -5076,15 +5076,18 @@ public class LibvirtComputingResourceTest { @Test public void testUpdateHostPasswordCommand() { + final String hostIp = "127.0.0.1"; final String username = "root"; final String newPassword = "password"; - final UpdateHostPasswordCommand command = new UpdateHostPasswordCommand(username, newPassword); + final UpdateHostPasswordCommand command = new UpdateHostPasswordCommand(username, newPassword, hostIp); + + when(libvirtComputingResource.getUpdateHostPasswdPath()).thenReturn("/tmp"); final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance(); assertNotNull(wrapper); final Answer answer = wrapper.execute(command, libvirtComputingResource); - assertTrue(answer.getResult()); + assertFalse(answer.getResult()); } } \ No newline at end of file diff --git a/scripts/vm/hypervisor/update_host_passwd.sh b/scripts/vm/hypervisor/update_host_passwd.sh new file mode 100755 index 00000000000..7d4eec584fe --- /dev/null +++ b/scripts/vm/hypervisor/update_host_passwd.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +hostIp=$1 +username=$2 +new_passwd=$3 + +ssh -o StrictHostKeyChecking=no -p 3922 -i /root/.ssh/id_rsa.cloud root@$hostIp "echo -e "$new_passwd\n$new_passwd" | passwd --stdin $username" + +return $?; \ No newline at end of file diff --git a/server/src/com/cloud/resource/ResourceManagerImpl.java b/server/src/com/cloud/resource/ResourceManagerImpl.java index b6f7f4e987b..c31d2a53129 100644 --- a/server/src/com/cloud/resource/ResourceManagerImpl.java +++ b/server/src/com/cloud/resource/ResourceManagerImpl.java @@ -2233,7 +2233,12 @@ public class ResourceManagerImpl extends ManagerBase implements ResourceManager, final String username = nv.getValue(); nv = _hostDetailsDao.findDetail(hostId, ApiConstants.PASSWORD); final String password = nv.getValue(); - final UpdateHostPasswordCommand cmd = new UpdateHostPasswordCommand(username, password); + + + final HostVO host = _hostDao.findById(hostId); + final String hostIpAddress = host.getPrivateIpAddress(); + + final UpdateHostPasswordCommand cmd = new UpdateHostPasswordCommand(username, password, hostIpAddress); final Answer answer = _agentMgr.easySend(hostId, cmd); return answer.getResult(); }