Skip to content

Commit a7a41c0

Browse files
authored
Merge pull request #642 from jschoiRR/mold-main#2025
[Mold API, Agent] Glue 클러스터가 2개 이상인 경우 'rbd map' 실행시 클러스터 구분되도록 반영/가상머신 생성시 네트워크 보안 기능 활성화 여부 글로벌 설정 추가
2 parents ad62e15 + c933698 commit a7a41c0

File tree

11 files changed

+149
-82
lines changed

11 files changed

+149
-82
lines changed

api/src/main/java/com/cloud/agent/api/to/NetworkTO.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class NetworkTO {
4444
protected String ip6Dns1;
4545
protected String ip6Dns2;
4646
protected boolean linkState = true;
47+
protected boolean nwfilter = false;
4748

4849
public NetworkTO() {
4950
}
@@ -241,4 +242,11 @@ public void setLinkState(boolean linkState) {
241242
this.linkState = linkState;
242243
}
243244

245+
public boolean getNwfilter() {
246+
return nwfilter;
247+
}
248+
249+
public void setNwfilter(boolean nwfilter) {
250+
this.nwfilter = nwfilter;
251+
}
244252
}

core/src/main/java/com/cloud/resource/ServerResourceBase.java

Lines changed: 72 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
package com.cloud.resource;
2121

2222
import java.io.File;
23+
import java.io.FileWriter;
24+
import java.io.IOException;
2325
import java.io.PrintWriter;
2426
import java.io.StringWriter;
2527
import java.net.NetworkInterface;
@@ -64,6 +66,7 @@ public abstract class ServerResourceBase implements ServerResource {
6466
protected NetworkInterface storageNic;
6567
protected NetworkInterface storageNic2;
6668
protected IAgentControl agentControl;
69+
protected static final String DEFAULT_LOCAL_STORAGE_PATH = "/var/lib/libvirt/images/";
6770

6871
@Override
6972
public String getName() {
@@ -178,27 +181,27 @@ protected Answer listHostDevices() {
178181
return new ListHostDeviceAnswer(true, hostDevicesText);
179182
}
180183

181-
protected Answer createImageRbd(String names, long sizes, String poolPath) {
182-
sizes = (sizes * 1024);
183-
String cmdout = Script.runSimpleBashScript("rbd -p " + poolPath + " create -s " + sizes + " " + names);
184+
protected Answer createImageRbd(String poolUuid, String skey, String authUserName, String host, String names, long sizes, String poolPath) {
185+
createRBDSecretKeyFileIfNoExist(poolUuid, DEFAULT_LOCAL_STORAGE_PATH, skey);
186+
String cmdout = Script.runSimpleBashScript("rbd -p " + poolPath + " --id " + authUserName + " -m " + host + " -K " + DEFAULT_LOCAL_STORAGE_PATH + poolUuid + " create -s " + (sizes * 1024) + " " + names);
184187
if (cmdout == null) {
185188
logger.debug(cmdout);
186189
}else{
187190
}
188191
return new ListRbdObjectsAnswer(true, names);
189192
}
190193

191-
protected Answer deleteImageRbd(String name, String poolPath) {
192-
193-
String cmdout = Script.runSimpleBashScript("rbd -p " + poolPath + " rm " + name);
194+
protected Answer deleteImageRbd(String poolUuid, String skey, String authUserName, String host, String name, String poolPath) {
195+
createRBDSecretKeyFileIfNoExist(poolUuid, DEFAULT_LOCAL_STORAGE_PATH, skey);
196+
String cmdout = Script.runSimpleBashScript("rbd -p " + poolPath + " --id " + authUserName + " -m " + host + " -K " + DEFAULT_LOCAL_STORAGE_PATH + poolUuid + " rm " + name);
194197
if (cmdout == null) {
195198
logger.debug(cmdout);
196199
}else{
197200
}
198201
return new ListRbdObjectsAnswer(true, name);
199202
}
200203

201-
protected Answer listRbdFilesAtPath(int startIndex, int pageSize, String poolPath, String keyword) {
204+
protected Answer listRbdFilesAtPath(String poolUuid, String skey, String authUserName, String host, int startIndex, int pageSize, String poolPath, String keyword) {
202205
int count = 0;
203206
List<String> names = new ArrayList<>();
204207
List<String> paths = new ArrayList<>();
@@ -207,13 +210,15 @@ protected Answer listRbdFilesAtPath(int startIndex, int pageSize, String poolPat
207210
List<Long> sizes = new ArrayList<>();
208211
List<Long> modifiedList = new ArrayList<>();
209212

213+
createRBDSecretKeyFileIfNoExist(poolUuid, DEFAULT_LOCAL_STORAGE_PATH, skey);
214+
210215
Script listCommand = new Script("/bin/bash", logger);
211216
listCommand.add("-c");
212217

213218
if (keyword != null && !keyword.isEmpty()) {
214-
listCommand.add("rbd -p " + poolPath + " ls | grep " + keyword );
219+
listCommand.add("rbd ls -p " + poolPath + " --id " + authUserName + " -m " + host + " -K " + DEFAULT_LOCAL_STORAGE_PATH + poolUuid + " | grep " + keyword );
215220
} else {
216-
listCommand.add("rbd -p " + poolPath + " ls");
221+
listCommand.add("rbd ls -p " + poolPath + " --id " + authUserName + " -m " + host + " -K " + DEFAULT_LOCAL_STORAGE_PATH + poolUuid);
217222
}
218223
OutputInterpreter.AllLinesParser listParser = new OutputInterpreter.AllLinesParser();
219224
String listResult = listCommand.execute(listParser);
@@ -231,6 +236,9 @@ protected Answer listRbdFilesAtPath(int startIndex, int pageSize, String poolPat
231236

232237
Script infoCommand = new Script("rbd");
233238
infoCommand.add("-p", poolPath);
239+
infoCommand.add("--id", authUserName);
240+
infoCommand.add("-m", host);
241+
infoCommand.add("-K", DEFAULT_LOCAL_STORAGE_PATH + poolUuid);
234242
infoCommand.add("info", imageName.trim());
235243
OutputInterpreter.AllLinesParser infoParser = new OutputInterpreter.AllLinesParser();
236244
String infoResult = infoCommand.execute(infoParser);
@@ -265,49 +273,65 @@ protected Answer listRbdFilesAtPath(int startIndex, int pageSize, String poolPat
265273
return new ListDataStoreObjectsAnswer(true, count, names, paths, absPaths, isDirs, sizes, modifiedList);
266274
}
267275

276+
public void createRBDSecretKeyFileIfNoExist(String uuid, String localPath, String skey) {
277+
File file = new File(localPath + File.separator + uuid);
278+
try {
279+
// 파일이 존재하지 않을 때만 생성
280+
if (!file.exists()) {
281+
boolean isCreated = file.createNewFile();
282+
if (isCreated) {
283+
// 파일 생성 후 내용 작성
284+
FileWriter writer = new FileWriter(file);
285+
writer.write(skey);
286+
writer.close();
287+
}
288+
}
289+
} catch (IOException e) {}
290+
}
268291

269-
protected Answer listFilesAtPath(String nfsMountPoint, String relativePath, int startIndex, int pageSize, String keyword) {
270-
int count = 0;
271-
File file = new File(nfsMountPoint, relativePath);
272-
List<String> names = new ArrayList<>();
273-
List<String> paths = new ArrayList<>();
274-
List<String> absPaths = new ArrayList<>();
275-
List<Boolean> isDirs = new ArrayList<>();
276-
List<Long> sizes = new ArrayList<>();
277-
List<Long> modifiedList = new ArrayList<>();
278-
if (file.isFile()) {
279-
count = 1;
280-
names.add(file.getName());
281-
paths.add(file.getPath().replace(nfsMountPoint, ""));
282-
absPaths.add(file.getPath());
283-
isDirs.add(file.isDirectory());
284-
sizes.add(file.length());
285-
modifiedList.add(file.lastModified());
286-
} else if (file.isDirectory()) {
287-
String[] files = file.list();
288-
List<String> filteredFiles = new ArrayList<>();
289-
if (keyword != null && !"".equals(keyword)) {
290-
for (String fileName : files) {
291-
if (fileName.contains(keyword)) {
292-
filteredFiles.add(fileName);
292+
protected Answer listFilesAtPath(String nfsMountPoint, String relativePath, int startIndex, int pageSize, String keyword) {
293+
int count = 0;
294+
File file = new File(nfsMountPoint, relativePath);
295+
List<String> names = new ArrayList<>();
296+
List<String> paths = new ArrayList<>();
297+
List<String> absPaths = new ArrayList<>();
298+
List<Boolean> isDirs = new ArrayList<>();
299+
List<Long> sizes = new ArrayList<>();
300+
List<Long> modifiedList = new ArrayList<>();
301+
if (file.isFile()) {
302+
count = 1;
303+
names.add(file.getName());
304+
paths.add(file.getPath().replace(nfsMountPoint, ""));
305+
absPaths.add(file.getPath());
306+
isDirs.add(file.isDirectory());
307+
sizes.add(file.length());
308+
modifiedList.add(file.lastModified());
309+
} else if (file.isDirectory()) {
310+
String[] files = file.list();
311+
List<String> filteredFiles = new ArrayList<>();
312+
if (keyword != null && !"".equals(keyword)) {
313+
for (String fileName : files) {
314+
if (fileName.contains(keyword)) {
315+
filteredFiles.add(fileName);
316+
}
293317
}
318+
} else {
319+
filteredFiles.addAll(Arrays.asList(files));
320+
}
321+
count = filteredFiles.size();
322+
for (int i = startIndex; i < startIndex + pageSize && i < count; i++) {
323+
File f = new File(nfsMountPoint, relativePath + '/' + filteredFiles.get(i));
324+
names.add(f.getName());
325+
paths.add(f.getPath().replace(nfsMountPoint, ""));
326+
absPaths.add(f.getPath());
327+
isDirs.add(f.isDirectory());
328+
sizes.add(f.length());
329+
modifiedList.add(f.lastModified());
294330
}
295-
} else {
296-
filteredFiles.addAll(Arrays.asList(files));
297-
}
298-
count = filteredFiles.size();
299-
for (int i = startIndex; i < startIndex + pageSize && i < count; i++) {
300-
File f = new File(nfsMountPoint, relativePath + '/' + filteredFiles.get(i));
301-
names.add(f.getName());
302-
paths.add(f.getPath().replace(nfsMountPoint, ""));
303-
absPaths.add(f.getPath());
304-
isDirs.add(f.isDirectory());
305-
sizes.add(f.length());
306-
modifiedList.add(f.lastModified());
307331
}
332+
return new ListDataStoreObjectsAnswer(file.exists(), count, names, paths, absPaths, isDirs, sizes, modifiedList);
308333
}
309-
return new ListDataStoreObjectsAnswer(file.exists(), count, names, paths, absPaths, isDirs, sizes, modifiedList);
310-
}
334+
311335
protected Answer listFilesAtPath(String nfsMountPoint, String relativePath, int startIndex, int pageSize) {
312336
int count = 0;
313337
File file = new File(nfsMountPoint, relativePath);
@@ -340,6 +364,7 @@ protected Answer listFilesAtPath(String nfsMountPoint, String relativePath, int
340364
}
341365
return new ListDataStoreObjectsAnswer(file.exists(), count, names, paths, absPaths, isDirs, sizes, modifiedList);
342366
}
367+
343368
protected void fillNetworkInformation(final StartupCommand cmd) {
344369
String[] info = null;
345370
if (privateNic != null) {

core/src/main/java/org/apache/cloudstack/storage/command/browser/CreateRbdObjectsCommand.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,27 @@
2020
package org.apache.cloudstack.storage.command.browser;
2121

2222
import com.cloud.agent.api.storage.StorageCommand;
23+
import com.cloud.agent.api.to.DataStoreTO;
2324

2425
public class CreateRbdObjectsCommand extends StorageCommand {
2526

26-
private String names;
27+
private DataStoreTO store;
2728

28-
private long sizes;
29+
private String names;
2930

30-
private String poolType;
31+
private long sizes;
3132

32-
private String poolPath;
33+
private String poolType;
3334

34-
private String keyword;
35+
private String poolPath;
3536

36-
private Long poolId;
37+
private String keyword;
3738

38-
public CreateRbdObjectsCommand(String names, long sizes) {
39+
private Long poolId;
40+
41+
public CreateRbdObjectsCommand(DataStoreTO store, String names, long sizes) {
3942
super();
43+
this.store = store;
4044
this.names = names;
4145
this.sizes = sizes;
4246
}
@@ -46,6 +50,10 @@ public boolean executeInSequence() {
4650
return false;
4751
}
4852

53+
public DataStoreTO getStore() {
54+
return store;
55+
}
56+
4957
public String getNames() {
5058
return names;
5159
}

core/src/main/java/org/apache/cloudstack/storage/command/browser/DeleteRbdObjectsCommand.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@
2020
package org.apache.cloudstack.storage.command.browser;
2121

2222
import com.cloud.agent.api.storage.StorageCommand;
23+
import com.cloud.agent.api.to.DataStoreTO;
2324

2425
public class DeleteRbdObjectsCommand extends StorageCommand {
2526

26-
private String name;
27+
private DataStoreTO store;
2728

28-
private String poolType;
29+
private String name;
2930

30-
private String poolPath;
31+
private String poolType;
3132

32-
public DeleteRbdObjectsCommand(String name) {
33+
private String poolPath;
34+
35+
public DeleteRbdObjectsCommand(DataStoreTO store, String name) {
3336
super();
37+
this.store = store;
3438
this.name = name;
3539
}
3640

@@ -39,6 +43,10 @@ public boolean executeInSequence() {
3943
return false;
4044
}
4145

46+
public DataStoreTO getStore() {
47+
return store;
48+
}
49+
4250
public String getName() {
4351
return name;
4452
}

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.io.File;
2323
import java.io.FileNotFoundException;
24+
import java.io.FileWriter;
2425
import java.io.IOException;
2526
import java.io.StringReader;
2627
import java.net.InetAddress;
@@ -3768,7 +3769,7 @@ private void createVif(final LibvirtVMDef vm, final VirtualMachineTO vmSpec, fin
37683769
enableOVSDriver = true;
37693770
}
37703771

3771-
if (!nic.isSecurityGroupEnabled() && !enableOVSDriver) {
3772+
if (!nic.isSecurityGroupEnabled() && !enableOVSDriver && nic.getNwfilter()) {
37723773
interfaceDef.setFilterrefFilterTag();
37733774
}
37743775
if (vmSpec.getDetails() != null) {
@@ -5514,23 +5515,28 @@ public Answer listHostDevices(ListHostDeviceCommand command) {
55145515
public Answer listFilesAtPath(ListDataStoreObjectsCommand command) {
55155516
DataStoreTO store = command.getStore();
55165517
if(command.getPoolType().equals("RBD")) {
5517-
return listRbdFilesAtPath(command.getStartIndex(), command.getPageSize(), command.getPoolPath(), command.getKeyword());
5518+
KVMStoragePool storagePool = storagePoolManager.getStoragePool(StoragePoolType.RBD, store.getUuid());
5519+
return listRbdFilesAtPath(storagePool.getUuid(), storagePool.getAuthSecret(), storagePool.getAuthUserName(), storagePool.getSourceHost(), command.getStartIndex(), command.getPageSize(), command.getPoolPath(), command.getKeyword());
55185520
} else {
55195521
KVMStoragePool storagePool = storagePoolManager.getStoragePool(StoragePoolType.NetworkFilesystem, store.getUuid());
55205522
return listFilesAtPath(storagePool.getLocalPath(), command.getPath(), command.getStartIndex(), command.getPageSize(),command.getKeyword());
55215523
}
55225524
}
55235525

55245526
public Answer createImageRbd(CreateRbdObjectsCommand command) {
5527+
DataStoreTO store = command.getStore();
55255528
if(command.getPoolType().equals("RBD")) {
5526-
return createImageRbd(command.getNames(), command.getSizes(), command.getPoolPath());
5529+
KVMStoragePool storagePool = storagePoolManager.getStoragePool(StoragePoolType.RBD, store.getUuid());
5530+
return createImageRbd(storagePool.getUuid(), storagePool.getAuthSecret(), storagePool.getAuthUserName(), storagePool.getSourceHost(), command.getNames(), command.getSizes(), command.getPoolPath());
55275531
}
55285532
return null;
55295533
}
55305534

55315535
public Answer deleteImageRbd(DeleteRbdObjectsCommand command) {
5536+
DataStoreTO store = command.getStore();
55325537
if(command.getPoolType().equals("RBD")) {
5533-
return deleteImageRbd(command.getName(), command.getPoolPath());
5538+
KVMStoragePool storagePool = storagePoolManager.getStoragePool(StoragePoolType.RBD, store.getUuid());
5539+
return deleteImageRbd(storagePool.getUuid(), storagePool.getAuthSecret(), storagePool.getAuthUserName(), storagePool.getSourceHost(),command.getName(), command.getPoolPath());
55345540
}
55355541
return null;
55365542
}
@@ -5754,8 +5760,9 @@ public String mapRbdDevice(final KVMPhysicalDisk disk, boolean kvdoEnable){
57545760
final String[] splitPoolImage = disk.getPath().split("/");
57555761
String device = Script.runSimpleBashScript("rbd showmapped | grep \""+splitPoolImage[0]+"[ ]*"+splitPoolImage[1]+"\" | grep -o \"[^ ]*[ ]*$\"");
57565762
if(device == null) {
5763+
createRBDSecretKeyFileIfNoExist(pool.getUuid(), DEFAULT_LOCAL_STORAGE_PATH, pool.getAuthSecret());
57575764
//If not mapped, map and return mapped device
5758-
Script.runSimpleBashScript("rbd map " + disk.getPath() + " --id " + pool.getAuthUserName());
5765+
Script.runSimpleBashScript("rbd map " + disk.getPath() + " -m " + pool.getSourceHost() + " --id " + pool.getAuthUserName() +" -K " + DEFAULT_LOCAL_STORAGE_PATH + pool.getUuid());
57595766
device = Script.runSimpleBashScript("rbd showmapped | grep \""+splitPoolImage[0]+"[ ]*"+splitPoolImage[1]+"\" | grep -o \"[^ ]*[ ]*$\"");
57605767
}
57615768
if(kvdoEnable){
@@ -5767,7 +5774,6 @@ public String mapRbdDevice(final KVMPhysicalDisk disk, boolean kvdoEnable){
57675774
logger.info("createKvdoCmdLine Action Error : "+e);
57685775
}
57695776
}
5770-
57715777
return device;
57725778
}
57735779

@@ -5788,7 +5794,9 @@ public String unmapRbdDevice(final KVMPhysicalDisk disk, boolean kvdoEnable){
57885794
logger.info("unmapRbdDevice Action error : "+e);
57895795
}
57905796
}
5791-
Script.runSimpleBashScript("rbd unmap " + disk.getPath() + " --id " + pool.getAuthUserName());
5797+
createRBDSecretKeyFileIfNoExist(pool.getUuid(), DEFAULT_LOCAL_STORAGE_PATH, pool.getAuthSecret());
5798+
5799+
Script.runSimpleBashScript("rbd unmap " + disk.getPath() + " -m " + pool.getSourceHost() + " --id " + pool.getAuthUserName() +" -K " + DEFAULT_LOCAL_STORAGE_PATH + pool.getUuid());
57925800
device = Script.runSimpleBashScript("rbd showmapped | grep \""+splitPoolImage[0]+"[ ]*"+splitPoolImage[1]+"\" | grep -o \"[^ ]*[ ]*$\"");
57935801
}
57945802
return device;
@@ -6251,4 +6259,20 @@ public static String convertDiskPathToUuid(String diskPath) {
62516259
}
62526260
return uuid;
62536261
}
6262+
6263+
public void createRBDSecretKeyFileIfNoExist(String uuid, String localPath, String skey) {
6264+
File file = new File(localPath + File.separator + uuid);
6265+
try {
6266+
// 파일이 존재하지 않을 때만 생성
6267+
if (!file.exists()) {
6268+
boolean isCreated = file.createNewFile();
6269+
if (isCreated) {
6270+
// 파일 생성 후 내용 작성
6271+
FileWriter writer = new FileWriter(file);
6272+
writer.write(skey);
6273+
writer.close();
6274+
}
6275+
}
6276+
} catch (IOException e) {}
6277+
}
62546278
}

0 commit comments

Comments
 (0)