Skip to content

Commit 47c7a10

Browse files
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
1 parent a74971d commit 47c7a10

6 files changed

Lines changed: 89 additions & 6 deletions

File tree

core/src/com/cloud/agent/api/UpdateHostPasswordCommand.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,26 @@
2222
import com.cloud.agent.api.LogLevel.Log4jLevel;
2323

2424
public class UpdateHostPasswordCommand extends Command {
25+
2526
@LogLevel(Log4jLevel.Off)
2627
protected String username;
2728
@LogLevel(Log4jLevel.Off)
2829
protected String newPassword;
30+
@LogLevel(Log4jLevel.Off)
31+
protected String hostIp;
32+
2933

3034
protected UpdateHostPasswordCommand() {
3135
}
3236

33-
public UpdateHostPasswordCommand(String username, String newPassword) {
37+
public UpdateHostPasswordCommand(final String username, final String newPassword) {
38+
this(username, newPassword, null);
39+
}
40+
41+
public UpdateHostPasswordCommand(final String username, final String newPassword, final String hostIp) {
3442
this.username = username;
3543
this.newPassword = newPassword;
44+
this.hostIp = hostIp;
3645
}
3746

3847
public String getNewPassword() {
@@ -43,8 +52,12 @@ public String getUsername() {
4352
return username;
4453
}
4554

55+
public String getHostIp() {
56+
return hostIp;
57+
}
58+
4659
@Override
4760
public boolean executeInSequence() {
4861
return false;
4962
}
50-
}
63+
}

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
257257

258258
private String _pingTestPath;
259259

260+
private String _updateHostPasswdPath;
261+
260262
private int _dom0MinMem;
261263

262264
protected boolean _disconnected = true;
@@ -372,6 +374,10 @@ public String getPingTestPath() {
372374
return _pingTestPath;
373375
}
374376

377+
public String getUpdateHostPasswdPath() {
378+
return _updateHostPasswdPath;
379+
}
380+
375381
public int getTimeout() {
376382
return _timeout;
377383
}
@@ -516,6 +522,10 @@ protected String getDefaultStorageScriptsDir() {
516522
return "scripts/storage/qcow2";
517523
}
518524

525+
protected String getDefaultHypervisorScriptsDir() {
526+
return "scripts/vm/hypervisor";
527+
}
528+
519529
protected String getDefaultKvmScriptsDir() {
520530
return "scripts/vm/hypervisor/kvm";
521531
}
@@ -547,6 +557,11 @@ public boolean configure(final String name, final Map<String, Object> params) th
547557
domrScriptsDir = getDefaultDomrScriptsDir();
548558
}
549559

560+
final String hypervisorScriptsDir = (String)params.get("hypervisor.scripts.dir");
561+
if (hypervisorScriptsDir == null) {
562+
getDefaultHypervisorScriptsDir();
563+
}
564+
550565
String kvmScriptsDir = (String)params.get("kvm.scripts.dir");
551566
if (kvmScriptsDir == null) {
552567
kvmScriptsDir = getDefaultKvmScriptsDir();
@@ -595,6 +610,11 @@ public boolean configure(final String name, final Map<String, Object> params) th
595610

596611
_clusterId = (String)params.get("cluster");
597612

613+
_updateHostPasswdPath = Script.findScript(hypervisorScriptsDir, "update_host_passwd.sh");
614+
if (_updateHostPasswdPath == null) {
615+
throw new ConfigurationException("Unable to find update_host_passwd.sh");
616+
}
617+
598618
_modifyVlanPath = Script.findScript(networkScriptsDir, "modifyvlan.sh");
599619
if (_modifyVlanPath == null) {
600620
throw new ConfigurationException("Unable to find modifyvlan.sh");

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUpdateHostPasswordCommandWrapper.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,34 @@
1919

2020
package com.cloud.hypervisor.kvm.resource.wrapper;
2121

22+
import org.apache.log4j.Logger;
23+
2224
import com.cloud.agent.api.Answer;
2325
import com.cloud.agent.api.UpdateHostPasswordCommand;
2426
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
2527
import com.cloud.resource.CommandWrapper;
2628
import com.cloud.resource.ResourceWrapper;
29+
import com.cloud.utils.script.Script;
2730

2831
@ResourceWrapper(handles = UpdateHostPasswordCommand.class)
2932
public final class LibvirtUpdateHostPasswordCommandWrapper extends CommandWrapper<UpdateHostPasswordCommand, Answer, LibvirtComputingResource> {
3033

34+
private static final Logger s_logger = Logger.getLogger(LibvirtUpdateHostPasswordCommandWrapper.class);
35+
private static final int TIMEOUT = 10000;
36+
3137
@Override
3238
public Answer execute(final UpdateHostPasswordCommand command, final LibvirtComputingResource libvirtComputingResource) {
33-
return new Answer(command, true, null);
39+
final String hostIp = command.getHostIp();
40+
final String username = command.getUsername();
41+
final String newPassword = command.getNewPassword();
42+
43+
final Script script = new Script(libvirtComputingResource.getUpdateHostPasswdPath(), TIMEOUT, s_logger);
44+
script.add(hostIp, username, newPassword);
45+
final String result = script.execute();
46+
47+
if (result != null) {
48+
return new Answer(command, false, result);
49+
}
50+
return new Answer(command);
3451
}
3552
}

plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5076,15 +5076,18 @@ public void testStartCommandIsolationEc2() {
50765076

50775077
@Test
50785078
public void testUpdateHostPasswordCommand() {
5079+
final String hostIp = "127.0.0.1";
50795080
final String username = "root";
50805081
final String newPassword = "password";
5081-
final UpdateHostPasswordCommand command = new UpdateHostPasswordCommand(username, newPassword);
5082+
final UpdateHostPasswordCommand command = new UpdateHostPasswordCommand(username, newPassword, hostIp);
5083+
5084+
when(libvirtComputingResource.getUpdateHostPasswdPath()).thenReturn("/tmp");
50825085

50835086
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
50845087
assertNotNull(wrapper);
50855088

50865089
final Answer answer = wrapper.execute(command, libvirtComputingResource);
50875090

5088-
assertTrue(answer.getResult());
5091+
assertFalse(answer.getResult());
50895092
}
50905093
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF 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+
hostIp=$1
20+
username=$2
21+
new_passwd=$3
22+
23+
ssh -o StrictHostKeyChecking=no -p 3922 -i /root/.ssh/id_rsa.cloud root@$hostIp "echo -e "$new_passwd\n$new_passwd" | passwd --stdin $username"
24+
25+
return $?;

server/src/com/cloud/resource/ResourceManagerImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2233,7 +2233,12 @@ private boolean doUpdateHostPassword(final long hostId) {
22332233
final String username = nv.getValue();
22342234
nv = _hostDetailsDao.findDetail(hostId, ApiConstants.PASSWORD);
22352235
final String password = nv.getValue();
2236-
final UpdateHostPasswordCommand cmd = new UpdateHostPasswordCommand(username, password);
2236+
2237+
2238+
final HostVO host = _hostDao.findById(hostId);
2239+
final String hostIpAddress = host.getPrivateIpAddress();
2240+
2241+
final UpdateHostPasswordCommand cmd = new UpdateHostPasswordCommand(username, password, hostIpAddress);
22372242
final Answer answer = _agentMgr.easySend(hostId, cmd);
22382243
return answer.getResult();
22392244
}

0 commit comments

Comments
 (0)