Search in sources :

Example 1 with FunctionConfiguration

use of com.microsoft.azure.toolkit.lib.legacy.function.configurations.FunctionConfiguration in project azure-tools-for-java by Microsoft.

the class FunctionUtils method prepareStagingFolder.

@AzureOperation(name = "function.prepare_staging_folder", type = AzureOperation.Type.TASK)
public static Map<String, FunctionConfiguration> prepareStagingFolder(Path stagingFolder, Path hostJson, Module module, PsiMethod[] methods) throws AzureExecutionException, IOException {
    final Map<String, FunctionConfiguration> configMap = generateConfigurations(methods);
    if (stagingFolder.toFile().isDirectory()) {
        FileUtils.cleanDirectory(stagingFolder.toFile());
    }
    final Path jarFile = JarUtils.buildJarFileToStagingPath(stagingFolder.toString(), module);
    final String scriptFilePath = "../" + jarFile.getFileName().toString();
    configMap.values().forEach(config -> config.setScriptFile(scriptFilePath));
    for (final Map.Entry<String, FunctionConfiguration> config : configMap.entrySet()) {
        if (StringUtils.isNotBlank(config.getKey())) {
            final File functionJsonFile = Paths.get(stagingFolder.toString(), config.getKey(), FUNCTION_JSON).toFile();
            writeFunctionJsonFile(functionJsonFile, config.getValue());
        }
    }
    final File hostJsonFile = new File(stagingFolder.toFile(), "host.json");
    copyFilesWithDefaultContent(hostJson, hostJsonFile, DEFAULT_HOST_JSON);
    final List<File> jarFiles = new ArrayList<>();
    OrderEnumerator.orderEntries(module).productionOnly().forEachLibrary(lib -> {
        if (StringUtils.isNotEmpty(lib.getName()) && ArrayUtils.contains(lib.getName().split("\\:"), FUNCTION_JAVA_LIBRARY_ARTIFACT_ID)) {
            return true;
        }
        if (lib != null) {
            for (final VirtualFile virtualFile : lib.getFiles(OrderRootType.CLASSES)) {
                final File file = new File(stripExtraCharacters(virtualFile.getPath()));
                if (file.exists()) {
                    jarFiles.add(file);
                }
            }
        }
        return true;
    });
    final File libFolder = new File(stagingFolder.toFile(), "lib");
    for (final File file : jarFiles) {
        FileUtils.copyFileToDirectory(file, libFolder);
    }
    return configMap;
}
Also used : Path(java.nio.file.Path) VirtualFile(com.intellij.openapi.vfs.VirtualFile) FunctionConfiguration(com.microsoft.azure.toolkit.lib.legacy.function.configurations.FunctionConfiguration) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Example 2 with FunctionConfiguration

use of com.microsoft.azure.toolkit.lib.legacy.function.configurations.FunctionConfiguration in project azure-tools-for-java by Microsoft.

the class FunctionUtils method generateConfiguration.

private static FunctionConfiguration generateConfiguration(PsiMethod method) throws AzureExecutionException {
    final FunctionConfiguration config = new FunctionConfiguration();
    final List<Binding> bindings = new ArrayList<>();
    processParameterAnnotations(method, bindings);
    processMethodAnnotations(method, bindings);
    patchStorageBinding(method, bindings);
    config.setEntryPoint(method.getContainingClass().getQualifiedName() + "." + method.getName());
    // Todo: add set bindings method in tools-common
    config.setBindings(bindings);
    return config;
}
Also used : Binding(com.microsoft.azure.toolkit.lib.legacy.function.bindings.Binding) FunctionConfiguration(com.microsoft.azure.toolkit.lib.legacy.function.configurations.FunctionConfiguration) ArrayList(java.util.ArrayList)

Example 3 with FunctionConfiguration

use of com.microsoft.azure.toolkit.lib.legacy.function.configurations.FunctionConfiguration in project azure-tools-for-java by Microsoft.

the class FunctionDeploymentState method prepareStagingFolder.

@AzureOperation(name = "function.prepare_staging_folder_detail", params = { "stagingFolder.getName()", "this.deployModel.getFunctionAppConfig().getName()" }, type = AzureOperation.Type.TASK)
private void prepareStagingFolder(File stagingFolder, RunProcessHandler processHandler, @NotNull final Operation operation) {
    AzureTaskManager.getInstance().read(() -> {
        final Path hostJsonPath = FunctionUtils.getDefaultHostJson(project);
        final PsiMethod[] methods = FunctionUtils.findFunctionsByAnnotation(functionDeployConfiguration.getModule());
        final Path folder = stagingFolder.toPath();
        try {
            final Map<String, FunctionConfiguration> configMap = FunctionUtils.prepareStagingFolder(folder, hostJsonPath, functionDeployConfiguration.getModule(), methods);
            operation.trackProperty(TelemetryConstants.TRIGGER_TYPE, StringUtils.join(FunctionUtils.getFunctionBindingList(configMap), ","));
        } catch (final AzureExecutionException | IOException e) {
            final String error = String.format("failed prepare staging folder[%s]", folder);
            throw new AzureToolkitRuntimeException(error, e);
        }
    });
}
Also used : Path(java.nio.file.Path) PsiMethod(com.intellij.psi.PsiMethod) AzureExecutionException(com.microsoft.azure.toolkit.lib.common.exception.AzureExecutionException) FunctionConfiguration(com.microsoft.azure.toolkit.lib.legacy.function.configurations.FunctionConfiguration) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) IOException(java.io.IOException) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Example 4 with FunctionConfiguration

use of com.microsoft.azure.toolkit.lib.legacy.function.configurations.FunctionConfiguration in project azure-tools-for-java by Microsoft.

the class FunctionUtils method generateConfigurations.

private static Map<String, FunctionConfiguration> generateConfigurations(final PsiMethod[] methods) throws AzureExecutionException {
    final Map<String, FunctionConfiguration> configMap = new HashMap<>();
    for (final PsiMethod method : methods) {
        final PsiAnnotation annotation = AnnotationUtil.findAnnotation(method, FunctionUtils.AZURE_FUNCTION_ANNOTATION_CLASS);
        final String functionName = AnnotationUtil.getDeclaredStringAttributeValue(annotation, "value");
        configMap.put(functionName, generateConfiguration(method));
    }
    return configMap;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PsiMethod(com.intellij.psi.PsiMethod) FunctionConfiguration(com.microsoft.azure.toolkit.lib.legacy.function.configurations.FunctionConfiguration) PsiAnnotation(com.intellij.psi.PsiAnnotation)

Example 5 with FunctionConfiguration

use of com.microsoft.azure.toolkit.lib.legacy.function.configurations.FunctionConfiguration in project azure-tools-for-java by Microsoft.

the class FunctionRunState method prepareStagingFolder.

@AzureOperation(name = "function.prepare_staging_folder_detail", params = { "stagingFolder.getName()", "this.functionRunConfiguration.getFuncPath()" }, type = AzureOperation.Type.SERVICE)
private void prepareStagingFolder(File stagingFolder, RunProcessHandler processHandler, @NotNull final Operation operation) throws Exception {
    AzureTaskManager.getInstance().read(() -> {
        final Path hostJsonPath = FunctionUtils.getDefaultHostJson(project);
        final Path localSettingsJson = Paths.get(functionRunConfiguration.getLocalSettingsJsonPath());
        final PsiMethod[] methods = FunctionUtils.findFunctionsByAnnotation(functionRunConfiguration.getModule());
        final Path folder = stagingFolder.toPath();
        try {
            Map<String, FunctionConfiguration> configMap = FunctionUtils.prepareStagingFolder(folder, hostJsonPath, functionRunConfiguration.getModule(), methods);
            operation.trackProperty(TelemetryConstants.TRIGGER_TYPE, StringUtils.join(FunctionUtils.getFunctionBindingList(configMap), ","));
            final Map<String, String> appSettings = FunctionUtils.loadAppSettingsFromSecurityStorage(functionRunConfiguration.getAppSettingsKey());
            FunctionUtils.copyLocalSettingsToStagingFolder(folder, localSettingsJson, appSettings);
            final Set<BindingEnum> bindingClasses = getFunctionBindingEnums(configMap);
            if (isInstallingExtensionNeeded(bindingClasses, processHandler)) {
                installProcess = getRunFunctionCliExtensionInstallProcessBuilder(stagingFolder).start();
            }
        } catch (AzureExecutionException | IOException e) {
            final String error = String.format("failed prepare staging folder[%s]", folder);
            throw new AzureToolkitRuntimeException(error, e);
        }
    });
    if (installProcess != null) {
        readInputStreamByLines(installProcess.getErrorStream(), inputLine -> {
            if (processHandler.isProcessRunning()) {
                processHandler.println(inputLine, ProcessOutputTypes.STDERR);
            }
        });
        readInputStreamByLines(installProcess.getInputStream(), inputLine -> {
            if (processHandler.isProcessRunning()) {
                processHandler.setText(inputLine);
            }
        });
        int exitCode = installProcess.waitFor();
        if (exitCode != 0) {
            throw new AzureExecutionException(message("function.run.error.installFuncFailed"));
        }
    }
}
Also used : Path(java.nio.file.Path) BindingEnum(com.microsoft.azure.toolkit.lib.legacy.function.bindings.BindingEnum) PsiMethod(com.intellij.psi.PsiMethod) FunctionConfiguration(com.microsoft.azure.toolkit.lib.legacy.function.configurations.FunctionConfiguration) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) IOException(java.io.IOException) AzureExecutionException(com.microsoft.azure.toolkit.lib.common.exception.AzureExecutionException) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Aggregations

FunctionConfiguration (com.microsoft.azure.toolkit.lib.legacy.function.configurations.FunctionConfiguration)5 PsiMethod (com.intellij.psi.PsiMethod)3 AzureOperation (com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)3 Path (java.nio.file.Path)3 AzureExecutionException (com.microsoft.azure.toolkit.lib.common.exception.AzureExecutionException)2 AzureToolkitRuntimeException (com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiAnnotation (com.intellij.psi.PsiAnnotation)1 AzureString (com.microsoft.azure.toolkit.lib.common.bundle.AzureString)1 Binding (com.microsoft.azure.toolkit.lib.legacy.function.bindings.Binding)1 BindingEnum (com.microsoft.azure.toolkit.lib.legacy.function.bindings.BindingEnum)1 File (java.io.File)1 Map (java.util.Map)1