Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ public class ApiConstants {
public static final String LB_PROVIDER = "lbprovider";
public static final String MAC_ADDRESS = "macaddress";
public static final String MAC_ADDRESSES = "macaddresses";
public static final String MANIFEST_URL = "manifesturl";
public static final String MANUAL_UPGRADE = "manualupgrade";
public static final String MAX = "max";
public static final String MAX_SNAPS = "maxsnaps";
Expand Down
5 changes: 5 additions & 0 deletions framework/extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,10 @@
<version>4.23.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>${cs.jackson.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// 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.

package org.apache.cloudstack.framework.extensions.api;

import java.util.EnumSet;

import javax.inject.Inject;

import org.apache.cloudstack.acl.RoleType;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiCommandResourceType;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.ExtensionResponse;
import org.apache.cloudstack.extension.Extension;
import org.apache.cloudstack.framework.extensions.manager.ExtensionsImportManager;
import org.apache.cloudstack.framework.extensions.manager.ExtensionsManager;

import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.NetworkRuleConflictException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.user.Account;

@APICommand(name = "importExtension",
description = "Imports an extension",
responseObject = ExtensionResponse.class,
responseHasSensitiveInfo = false,
entityType = {Extension.class},
authorized = {RoleType.Admin},
since = "4.23.0")
public class ImportExtensionCmd extends BaseCmd {

@Inject
ExtensionsManager extensionsManager;

@Inject
ExtensionsImportManager extensionsImportManager;

/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////

@Parameter(name = ApiConstants.MANIFEST_URL, type = CommandType.STRING, required = true,
description = "URL of the extension manifest import file")
private String manifestUrl;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////

public String getManifestUrl() {
return manifestUrl;
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////

@Override
public void execute() throws ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
Extension extension = extensionsImportManager.importExtension(this);
ExtensionResponse response = extensionsManager.createExtensionResponse(extension,
EnumSet.of(ApiConstants.ExtensionDetails.all));
response.setResponseName(getCommandName());
setResponseObject(response);
}

@Override
public long getEntityOwnerId() {
return Account.ACCOUNT_ID_SYSTEM;
}

@Override
public ApiCommandResourceType getApiResourceType() {
return ApiCommandResourceType.Extension;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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.

package org.apache.cloudstack.framework.extensions.manager;

import org.apache.cloudstack.extension.Extension;
import org.apache.cloudstack.framework.extensions.api.ImportExtensionCmd;

public interface ExtensionsImportManager {
Extension importExtension(ImportExtensionCmd cmd);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// 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.

package org.apache.cloudstack.framework.extensions.manager;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;

import javax.inject.Inject;

import org.apache.cloudstack.extension.Extension;
import org.apache.cloudstack.framework.extensions.api.ImportExtensionCmd;
import org.apache.cloudstack.framework.extensions.dao.ExtensionDao;
import org.apache.cloudstack.framework.extensions.util.ExtensionConfig;
import org.apache.cloudstack.framework.extensions.util.YamlParser;
import org.apache.cloudstack.framework.extensions.util.ZipExtractor;
import org.apache.cloudstack.framework.extensions.vo.ExtensionVO;
import org.apache.commons.lang3.StringUtils;

import com.cloud.hypervisor.ExternalProvisioner;
import com.cloud.utils.FileUtil;
import com.cloud.utils.HttpUtils;
import com.cloud.utils.component.ManagerBase;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.db.TransactionCallbackWithException;
import com.cloud.utils.exception.CloudRuntimeException;

public class ExtensionsImportManagerImpl extends ManagerBase implements ExtensionsImportManager {

@Inject
ExtensionsManager extensionsManager;

@Inject
ExternalProvisioner externalProvisioner;

@Inject
ExtensionDao extensionDao;

protected Extension importExtensionInternal(String manifestUrl, Path tempDir) {
Path manifestPath = tempDir.resolve("manifest.yaml");
HttpUtils.downloadFileWithProgress(manifestUrl, manifestPath.toString(), logger);
if (!Files.exists(manifestPath)) {
throw new CloudRuntimeException("Failed to download extension manifest from URL: " + manifestUrl);
}
final ExtensionConfig extensionConfig = YamlParser.parseYamlFile(manifestPath.toString());
//Parse the manifest and create the extension
final String name = extensionConfig.metadata.name;
final String extensionArchiveURL = extensionConfig.getArchiveUrl();
ExtensionVO extensionByName = extensionDao.findByName(name);
if (extensionByName != null) {
throw new CloudRuntimeException("Extension by name already exists");
}
if (StringUtils.isBlank(extensionArchiveURL)) {
throw new CloudRuntimeException("Unable to retrieve archive URL for extension source during import");
}
Path extensionArchivePath = tempDir.resolve(UUID.randomUUID() + ".zip");
HttpUtils.downloadFileWithProgress(extensionArchiveURL, extensionArchivePath.toString(), logger);
if (!Files.exists(extensionArchivePath)) {
throw new CloudRuntimeException("Failed to download extension archive from URL: " + extensionArchiveURL);
}
final String extensionRootPath = externalProvisioner.getExtensionsPath() + File.separator + name;
try {
ZipExtractor.extractZipContents(extensionArchivePath.toString(), extensionRootPath);
} catch (IOException e) {
throw new CloudRuntimeException("Failed to extract extension archive during import at: " + extensionRootPath, e);
}
return Transaction.execute((TransactionCallbackWithException<Extension, CloudRuntimeException>) status -> {
Extension extension = extensionsManager.createExtension(name, extensionConfig.metadata.description,
extensionConfig.spec.type, extensionConfig.spec.entrypoint.path, Extension.State.Enabled.name(),
false, Collections.emptyMap());

for (ExtensionConfig.CustomAction action : extensionConfig.spec.customActions) {
Map<Integer, Map<String, String>> parameters = action.getParametersAsMap();
extensionsManager.addCustomAction(action.name, action.description, extension.getId(),
action.resourcetype, action.allowedroletypes, action.timeout, true, parameters,
null, null, Collections.emptyMap());
}
return extension;
});
}

@Override
public Extension importExtension(ImportExtensionCmd cmd) {
final String manifestUrl = cmd.getManifestUrl();
final String extensionsRootPath = externalProvisioner.getExtensionsPath();

Path tempDir;
try {
Path extensionsRootDir = Paths.get(extensionsRootPath);
Files.createDirectories(extensionsRootDir);
tempDir = Files.createTempDirectory(extensionsRootDir, "import-ext-");

} catch (IOException e) {
logger.error("Failed to create working directory for import extension, {}", extensionsRootPath, e);
throw new CloudRuntimeException("Failed to create working directory for import extension", e);
}
try {
return importExtensionInternal(manifestUrl, tempDir);
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw e;
} finally {
FileUtil.deletePath(tempDir.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public interface ExtensionsManager extends Manager {

Extension createExtension(CreateExtensionCmd cmd);

Extension createExtension(String name, String description, String type, String relativePath, String state,
Boolean orchestratorRequiresPrepareVm, Map<String, String> details);

boolean prepareExtensionPathAcrossServers(Extension extension);

List<ExtensionResponse> listExtensions(ListExtensionsCmd cmd);
Expand All @@ -79,6 +82,10 @@ public interface ExtensionsManager extends Manager {

ExtensionCustomAction addCustomAction(AddCustomActionCmd cmd);

ExtensionCustomAction addCustomAction(String name, String description, long extensionId, String resourceTypeStr,
List<String> rolesStrList, int timeout , boolean enabled, Map parametersMap, String successMessage,
String errorMessage, Map<String, String> details);

boolean deleteCustomAction(DeleteCustomActionCmd cmd);

List<ExtensionCustomActionResponse> listCustomActions(ListCustomActionCmd cmd);
Expand Down
Loading
Loading