CLOUDSTACK-8607 - Adding update_host_passwd.sh script

- Modifying the LibvirtUpdateHostPasswordCommandWrapper in order to execute the script on the host
   - Adding the script path to LibvirtComputingResource
   - Adding the host IP address as an instance variable on UpdateHostPasswordCommand
   - Improving the Unit Test (LibvirtComputingResourceTest) to get it covering the new code
This commit is contained in:
wilderrodrigues 2015-07-02 14:54:51 +02:00
parent a74971df06
commit 47c7a1083f
6 changed files with 89 additions and 6 deletions

View File

@ -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;
}
}
}

View File

@ -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");

View File

@ -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<UpdateHostPasswordCommand, Answer, LibvirtComputingResource> {
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);
}
}

View File

@ -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());
}
}

View File

@ -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 $?;

View File

@ -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();
}