Search in sources :

Example 1 with MissingPlugingDescriptorFileException

use of alien4cloud.plugin.exception.MissingPlugingDescriptorFileException in project alien4cloud by alien4cloud.

the class PluginManager method uploadPlugin.

/**
 * Upload a plugin from a given path.
 *
 * @param uploadedPluginPath The path of the plugin to upload.<br>
 *            The state of the new uploaded plugin will be determined as follow:
 *            <ul>
 *            <li>plugin doesn't exists: load and enable</li>
 *            <li>plugin exists: keep the state (reload if enabled)</li>
 *            </ul>
 * @return the uploaded plugin
 * @throws IOException In case there is an issue with the access to the plugin file.
 * @throws PluginLoadingException
 * @throws AlreadyExistException if a plugin with the same id already exists in the repository
 * @throws MissingPlugingDescriptorFileException
 */
public Plugin uploadPlugin(Path uploadedPluginPath) throws PluginLoadingException, IOException, MissingPlugingDescriptorFileException {
    // load the plugin descriptor
    FileSystem fs = FileSystems.newFileSystem(uploadedPluginPath, null);
    PluginDescriptor descriptor;
    try {
        try {
            descriptor = YamlParserUtil.parseFromUTF8File(fs.getPath(PLUGIN_DESCRIPTOR_FILE), PluginDescriptor.class);
        } catch (IOException e) {
            if (e instanceof NoSuchFileException) {
                throw new MissingPlugingDescriptorFileException();
            } else {
                throw e;
            }
        }
        String pluginPathId = getPluginPathId();
        Plugin plugin = new Plugin(descriptor, pluginPathId);
        // check plugin already exists and is loaded
        if (pluginContexts.get(plugin.getId()) != null) {
            log.warn("Uploading Plugin [ {} ] impossible (already exists and enabled)", plugin.getId());
            throw new AlreadyExistException("A plugin with the given id already exists and is enabled.");
        }
        Plugin oldPlugin = alienDAO.findById(Plugin.class, plugin.getId());
        if (oldPlugin != null) {
            // remove all files for the old plugin but keep configuration.
            removePlugin(plugin.getId(), false);
        }
        Path pluginPath = getPluginPath(pluginPathId);
        FileUtil.unzip(uploadedPluginPath, pluginPath);
        // copy ui directory in case it exists
        Path pluginUiSourcePath = pluginPath.resolve(UI_DIRECTORY);
        Path pluginUiPath = getPluginUiPath(pluginPathId);
        if (Files.exists(pluginUiSourcePath)) {
            FileUtil.copy(pluginUiSourcePath, pluginUiPath);
        }
        alienDAO.save(plugin);
        if (oldPlugin == null || oldPlugin.isEnabled()) {
            enablePlugin(plugin);
        }
        return plugin;
    } finally {
        fs.close();
    }
}
Also used : Path(java.nio.file.Path) PluginDescriptor(alien4cloud.plugin.model.PluginDescriptor) FileSystem(java.nio.file.FileSystem) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) AlreadyExistException(alien4cloud.exception.AlreadyExistException) MissingPlugingDescriptorFileException(alien4cloud.plugin.exception.MissingPlugingDescriptorFileException) ManagedPlugin(alien4cloud.plugin.model.ManagedPlugin)

Example 2 with MissingPlugingDescriptorFileException

use of alien4cloud.plugin.exception.MissingPlugingDescriptorFileException in project alien4cloud by alien4cloud.

the class PluginController method upload.

@ApiOperation(value = "Upload a plugin archive.", notes = "Content of the zip file must be compliant with the expected alien 4 cloud plugin structure.", authorizations = { @Authorization("ADMIN") })
@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('ADMIN')")
@Audit
public RestResponse<Void> upload(@ApiParam(value = "Zip file that contains the plugin.", required = true) @RequestParam("file") MultipartFile pluginArchive) {
    Path pluginPath = null;
    try {
        // save the plugin archive in the temp directory
        pluginPath = Files.createTempFile(tempDirPath, null, ".zip");
        FileUploadUtil.safeTransferTo(pluginPath, pluginArchive);
        // upload the plugin archive
        Plugin plugin = pluginManager.uploadPlugin(pluginPath);
        // TODO as we do not manage many version of a same plugin, is this still relevant ?
        if (plugin.isConfigurable()) {
            tryReusePreviousVersionConf(plugin);
        }
    } catch (MissingPlugingDescriptorFileException e) {
        log.error("Your plugin don't have the META-INF/plugin.yml file.", e);
        return RestResponseBuilder.<Void>builder().error(new RestError(RestErrorCode.MISSING_PLUGIN_DESCRIPTOR_FILE_EXCEPTION.getCode(), "Your plugin don't have the META-INF/plugin.yml file.")).build();
    } catch (IOException e) {
        log.error("Unexpected IO error on plugin upload.", e);
        return RestResponseBuilder.<Void>builder().error(new RestError(RestErrorCode.INDEXING_SERVICE_ERROR.getCode(), "A technical issue occurred during the plugin upload <" + e.getMessage() + ">.")).build();
    } catch (PluginLoadingException e) {
        log.error("Fail to enable and load the plugin. The plugin will remain disabled", e);
        return RestResponseBuilder.<Void>builder().error(new RestError(RestErrorCode.ENABLE_PLUGIN_ERROR.getCode(), e.getMessage())).build();
    } finally {
        if (pluginPath != null) {
            try {
                FileUtil.delete(pluginPath);
            } catch (IOException e) {
                log.error("Failed to cleanup temporary file <" + pluginPath.toString() + ">", e);
            }
        }
    }
    return RestResponseBuilder.<Void>builder().build();
}
Also used : Path(java.nio.file.Path) PluginLoadingException(alien4cloud.plugin.exception.PluginLoadingException) RestError(alien4cloud.rest.model.RestError) IOException(java.io.IOException) Plugin(alien4cloud.plugin.Plugin) MissingPlugingDescriptorFileException(alien4cloud.plugin.exception.MissingPlugingDescriptorFileException) Audit(alien4cloud.audit.annotation.Audit) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

MissingPlugingDescriptorFileException (alien4cloud.plugin.exception.MissingPlugingDescriptorFileException)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 Audit (alien4cloud.audit.annotation.Audit)1 AlreadyExistException (alien4cloud.exception.AlreadyExistException)1 Plugin (alien4cloud.plugin.Plugin)1 PluginLoadingException (alien4cloud.plugin.exception.PluginLoadingException)1 ManagedPlugin (alien4cloud.plugin.model.ManagedPlugin)1 PluginDescriptor (alien4cloud.plugin.model.PluginDescriptor)1 RestError (alien4cloud.rest.model.RestError)1 ApiOperation (io.swagger.annotations.ApiOperation)1 FileSystem (java.nio.file.FileSystem)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1