Skip to content

Commit 333b272

Browse files
committed
CLOUDSTACK-4073: fix s3 on vmware
1 parent c83fd94 commit 333b272

10 files changed

Lines changed: 158 additions & 6 deletions

File tree

core/src/com/cloud/storage/resource/StorageSubsystemCommandHandlerBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
public class StorageSubsystemCommandHandlerBase implements StorageSubsystemCommandHandler {
4141
private static final Logger s_logger = Logger.getLogger(StorageSubsystemCommandHandlerBase.class);
42-
private StorageProcessor processor;
42+
protected StorageProcessor processor;
4343
public StorageSubsystemCommandHandlerBase(StorageProcessor processor) {
4444
this.processor = processor;
4545
}

engine/api/src/org/apache/cloudstack/storage/command/CopyCommand.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public DataTO getDestTO() {
3838
return this.destTO;
3939
}
4040

41+
public void setSrcTO(DataTO srcTO) {
42+
this.srcTO = srcTO;
43+
}
44+
45+
public void setDestTO(DataTO destTO) {
46+
this.destTO = destTO;
47+
}
48+
4149
public DataTO getSrcTO() {
4250
return this.srcTO;
4351
}

engine/api/src/org/apache/cloudstack/storage/to/SnapshotObjectTO.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public DataStoreTO getDataStore() {
6666
return this.dataStore;
6767
}
6868

69+
public void setDataStore(DataStoreTO store) {
70+
this.dataStore = store;
71+
}
72+
6973
@Override
7074
public String getPath() {
7175
return this.path;

plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/manager/VmwareStorageManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ public interface VmwareStorageManager {
3737
Answer execute(VmwareHostService hostService, DeleteVMSnapshotCommand cmd);
3838
Answer execute(VmwareHostService hostService, RevertToVMSnapshotCommand cmd);
3939
boolean execute(VmwareHostService hostService, CreateEntityDownloadURLCommand cmd);
40+
public void createOva(String path, String name);
4041
}

plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/manager/VmwareStorageManagerImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,21 @@ public boolean execute(VmwareHostService hostService, CreateEntityDownloadURLCom
109109
return true;
110110
}
111111

112+
@Override
113+
public void createOva(String path, String name) {
114+
Script commandSync = new Script(true, "sync", 0, s_logger);
115+
commandSync.execute();
116+
117+
Script command = new Script(false, "tar", 0, s_logger);
118+
command.setWorkDir(path);
119+
command.add("-cf", name + ".ova");
120+
command.add(name + ".ovf"); // OVF file should be the first file in OVA archive
121+
command.add(name + "-disk0.vmdk");
122+
123+
s_logger.info("Package OVA with commmand: " + command.toString());
124+
command.execute();
125+
}
126+
112127
private static final Logger s_logger = Logger.getLogger(VmwareStorageManagerImpl.class);
113128

114129
private final VmwareStorageMount _mountService;

plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import javax.naming.ConfigurationException;
4646

4747
import com.cloud.agent.api.to.DhcpTO;
48+
import com.cloud.storage.resource.VmwareStorageSubsystemCommandHandler;
4849
import org.apache.log4j.Logger;
4950
import org.apache.log4j.NDC;
5051

@@ -6331,9 +6332,9 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
63316332
int timeout = NumbersUtil.parseInt(value, 1440) * 1000;
63326333
VmwareManager mgr = context.getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
63336334
VmwareStorageProcessor storageProcessor = new VmwareStorageProcessor((VmwareHostService)this, _fullCloneFlag, (VmwareStorageMount)mgr,
6334-
timeout, this, _shutdown_waitMs
6335+
timeout, this, _shutdown_waitMs, null
63356336
);
6336-
storageHandler = new StorageSubsystemCommandHandlerBase(storageProcessor);
6337+
storageHandler = new VmwareStorageSubsystemCommandHandler(storageProcessor);
63376338

63386339
return true;
63396340
}

plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareSecondaryStorageResourceHandler.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ public VmwareSecondaryStorageResourceHandler(PremiumSecondaryStorageResource res
6868
_gson = GsonHelper.getGsonLogger();
6969

7070
VmwareStorageProcessor storageProcessor = new VmwareStorageProcessor(this, true, this, resource.getTimeout(),
71-
null, null);
72-
storageSubsystemHandler = new StorageSubsystemCommandHandlerBase(storageProcessor);
71+
null, null, _resource);
72+
VmwareStorageSubsystemCommandHandler vmwareStorageSubsystemCommandHandler = new VmwareStorageSubsystemCommandHandler(storageProcessor);
73+
vmwareStorageSubsystemCommandHandler.setStorageResource(_resource);
74+
vmwareStorageSubsystemCommandHandler.setStorageManager(_storageMgr);
75+
storageSubsystemHandler = vmwareStorageSubsystemCommandHandler;
76+
7377
}
7478

7579
@Override

plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareStorageProcessor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,21 @@ public class VmwareStorageProcessor implements StorageProcessor {
8989
protected Integer _shutdown_waitMs;
9090
private final Gson _gson;
9191
private final StorageLayer _storage = new JavaStorageLayer();
92+
private final PremiumSecondaryStorageResource storageResource;
9293

9394
public VmwareStorageProcessor(VmwareHostService hostService, boolean fullCloneFlag, VmwareStorageMount mountService,
9495
Integer timeout,
9596
VmwareResource resource,
96-
Integer shutdownWaitMs) {
97+
Integer shutdownWaitMs,
98+
PremiumSecondaryStorageResource storageResource) {
9799
this.hostService = hostService;
98100
this._fullCloneFlag = fullCloneFlag;
99101
this.mountService = mountService;
100102
this._timeout = timeout;
101103
this.resource = resource;
102104
this._shutdown_waitMs = shutdownWaitMs;
103105
_gson = GsonHelper.getGsonLogger();
106+
this.storageResource = storageResource;
104107
}
105108

106109
private String getOVFFilePath(String srcOVAFileName) {
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
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+
package com.cloud.storage.resource;
20+
21+
import com.cloud.agent.api.Answer;
22+
import com.cloud.agent.api.to.DataObjectType;
23+
import com.cloud.agent.api.to.DataStoreTO;
24+
import com.cloud.agent.api.to.DataTO;
25+
import com.cloud.agent.api.to.NfsTO;
26+
import com.cloud.agent.api.to.S3TO;
27+
import com.cloud.agent.api.to.SwiftTO;
28+
import com.cloud.hypervisor.vmware.manager.VmwareStorageManager;
29+
import com.cloud.storage.DataStoreRole;
30+
import org.apache.cloudstack.storage.command.CopyCmdAnswer;
31+
import org.apache.cloudstack.storage.command.CopyCommand;
32+
import org.apache.cloudstack.storage.to.SnapshotObjectTO;
33+
34+
import java.io.File;
35+
36+
public class VmwareStorageSubsystemCommandHandler extends StorageSubsystemCommandHandlerBase {
37+
38+
private VmwareStorageManager storageManager;
39+
private PremiumSecondaryStorageResource storageResource;
40+
41+
public PremiumSecondaryStorageResource getStorageResource() {
42+
return storageResource;
43+
}
44+
45+
public void setStorageResource(PremiumSecondaryStorageResource storageResource) {
46+
this.storageResource = storageResource;
47+
}
48+
49+
public VmwareStorageManager getStorageManager() {
50+
return storageManager;
51+
}
52+
53+
public void setStorageManager(VmwareStorageManager storageManager) {
54+
this.storageManager = storageManager;
55+
}
56+
57+
public VmwareStorageSubsystemCommandHandler(StorageProcessor processor
58+
) {
59+
super(processor);
60+
}
61+
62+
63+
@Override
64+
protected Answer execute(CopyCommand cmd) {
65+
DataTO srcData = cmd.getSrcTO();
66+
DataTO destData = cmd.getDestTO();
67+
DataStoreTO srcDataStore = srcData.getDataStore();
68+
DataStoreTO destDataStore = destData.getDataStore();
69+
//if copied between s3 and nfs cache, go to resource
70+
boolean needDelegation = false;
71+
if (destDataStore instanceof NfsTO
72+
&& destDataStore.getRole() == DataStoreRole.ImageCache) {
73+
if (srcDataStore instanceof S3TO || srcDataStore instanceof SwiftTO) {
74+
needDelegation = true;
75+
}
76+
}
77+
78+
if (srcDataStore.getRole() == DataStoreRole.ImageCache && destDataStore.getRole() == DataStoreRole.Image) {
79+
needDelegation = true;
80+
}
81+
82+
if (srcData.getObjectType() == DataObjectType.SNAPSHOT && srcData.getDataStore().getRole() == DataStoreRole.Primary) {
83+
//for back up snapshot, we need to do backup to cache, then to object store if object store is used.
84+
if (cmd.getCacheTO() != null) {
85+
cmd.setDestTO(cmd.getCacheTO());
86+
87+
CopyCmdAnswer answer = (CopyCmdAnswer)processor.backupSnapshot(cmd);
88+
if (!answer.getResult()) {
89+
return answer;
90+
}
91+
NfsTO cacheStore = (NfsTO)cmd.getCacheTO().getDataStore();
92+
String parentPath = storageResource.getRootDir(cacheStore.getUrl());
93+
SnapshotObjectTO newSnapshot = (SnapshotObjectTO)answer.getNewData();
94+
String path = newSnapshot.getPath();
95+
int index = path.lastIndexOf(File.separator);
96+
String name = path.substring(index + 1);
97+
String dir = path.substring(0, index);
98+
storageManager.createOva(parentPath + File.separator + dir, name);
99+
newSnapshot.setPath(newSnapshot.getPath() + ".ova");
100+
newSnapshot.setDataStore(cmd.getCacheTO().getDataStore());
101+
CopyCommand newCmd = new CopyCommand(newSnapshot, destData, cmd.getWait(), cmd.executeInSequence());
102+
return storageResource.defaultAction(newCmd);
103+
}
104+
}
105+
106+
if (needDelegation) {
107+
return storageResource.defaultAction(cmd);
108+
} else {
109+
return super.execute(cmd);
110+
}
111+
}
112+
}

services/secondary-storage/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,10 @@ protected Answer copyFromNfsToS3(CopyCommand cmd) {
739739
newVol.setPath(key);
740740
newVol.setSize(srcFile.length());
741741
retObj = newVol;
742+
} else if (destData.getObjectType() == DataObjectType.SNAPSHOT) {
743+
SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
744+
newSnapshot.setPath(key);
745+
retObj = newSnapshot;
742746
}
743747

744748
return new CopyCmdAnswer(retObj);

0 commit comments

Comments
 (0)