Search in sources :

Example 46 with AzureOperation

use of com.microsoft.azure.toolkit.lib.common.operation.AzureOperation in project azure-tools-for-java by Microsoft.

the class FunctionRunState method validateFunctionRuntime.

@AzureOperation(name = "function.validate_runtime", params = { "this.functionRunConfiguration.getFuncPath()" }, type = AzureOperation.Type.TASK)
private void validateFunctionRuntime(RunProcessHandler processHandler) {
    try {
        final String funcPath = functionRunConfiguration.getFuncPath();
        if (StringUtils.isEmpty(funcPath)) {
            throw new AzureToolkitRuntimeException(message("function.run.error.runtimeNotFound"));
        }
        final ComparableVersion funcVersion = getFuncVersion();
        if (funcVersion == null) {
            throw new AzureToolkitRuntimeException(message("function.run.error.runtimeNotFound"));
        }
        final ComparableVersion javaVersion = getJavaVersion();
        if (javaVersion == null) {
            processHandler.setText(message("function.run.error.getJavaVersionFailed"));
            return;
        }
        if (javaVersion.compareTo(JAVA_9) < 0) {
            // No need validate function host version within java 8 or earlier
            return;
        }
        final ComparableVersion minimumVersion = funcVersion.compareTo(FUNC_3) >= 0 ? MINIMUM_JAVA_9_SUPPORTED_VERSION : MINIMUM_JAVA_9_SUPPORTED_VERSION_V2;
        if (funcVersion.compareTo(minimumVersion) < 0) {
            throw new AzureToolkitRuntimeException(message("function.run.error.funcOutOfDate"));
        }
    } catch (IOException e) {
        throw new AzureToolkitRuntimeException(message("function.run.error.validateRuntimeFailed", e.getMessage()));
    }
}
Also used : AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) IOException(java.io.IOException) ComparableVersion(org.apache.maven.artifact.versioning.ComparableVersion) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Example 47 with AzureOperation

use of com.microsoft.azure.toolkit.lib.common.operation.AzureOperation in project azure-tools-for-java by Microsoft.

the class FunctionRunState method getJavaVersion.

// Get java runtime version following the strategy of function core tools
// Get java version of JAVA_HOME first, fall back to use PATH if JAVA_HOME not exists
@AzureOperation(name = "function.validate_jre", type = AzureOperation.Type.TASK)
private static ComparableVersion getJavaVersion() throws IOException {
    final String javaHome = System.getenv("JAVA_HOME");
    final File javaFile = StringUtils.isEmpty(javaHome) ? null : Paths.get(javaHome, "bin", "java").toFile();
    final String executeFolder = javaFile == null ? null : javaFile.getParentFile().getAbsolutePath();
    final String command = javaFile == null ? "java" : javaFile.getAbsolutePath();
    final String javaVersion = CommandUtils.exec("java -version", executeFolder, true);
    if (StringUtils.isEmpty(javaVersion)) {
        return null;
    }
    final Matcher matcher = JAVA_VERSION_PATTERN.matcher(javaVersion);
    return matcher.find() ? new ComparableVersion(matcher.group(1)) : null;
}
Also used : Matcher(java.util.regex.Matcher) ComparableVersion(org.apache.maven.artifact.versioning.ComparableVersion) File(java.io.File) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Example 48 with AzureOperation

use of com.microsoft.azure.toolkit.lib.common.operation.AzureOperation 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)

Example 49 with AzureOperation

use of com.microsoft.azure.toolkit.lib.common.operation.AzureOperation in project azure-tools-for-java by Microsoft.

the class FunctionDeploymentState method executeSteps.

@Nullable
@Override
@AzureOperation(name = "function.deploy.state", type = AzureOperation.Type.ACTION)
public IFunctionApp executeSteps(@NotNull RunProcessHandler processHandler, @NotNull Operation operation) throws IOException {
    final FunctionDeploymentMessenger messenger = new FunctionDeploymentMessenger(processHandler);
    AzureMessager.getContext().setMessager(messenger);
    final IFunctionApp functionApp;
    if (StringUtils.isEmpty(functionDeployConfiguration.getFunctionId())) {
        functionApp = createFunctionApp(processHandler);
    } else {
        functionApp = Azure.az(AzureAppService.class).subscription(functionDeployConfiguration.getSubscriptionId()).functionApp(functionDeployConfiguration.getFunctionId());
        updateApplicationSettings(functionApp);
    }
    stagingFolder = FunctionUtils.getTempStagingFolder();
    prepareStagingFolder(stagingFolder, processHandler, operation);
    // deploy function to Azure
    FunctionAppService.getInstance().deployFunctionApp(functionApp, stagingFolder);
    // list triggers after deployment
    listHTTPTriggerUrls(functionApp);
    operation.trackProperties(AzureTelemetry.getActionContext().getProperties());
    return functionApp;
}
Also used : AzureAppService(com.microsoft.azure.toolkit.lib.appservice.AzureAppService) IFunctionApp(com.microsoft.azure.toolkit.lib.appservice.service.IFunctionApp) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation) Nullable(org.jetbrains.annotations.Nullable)

Example 50 with AzureOperation

use of com.microsoft.azure.toolkit.lib.common.operation.AzureOperation in project azure-tools-for-java by Microsoft.

the class CreateFunctionAppAction method actionPerformed.

@Override
@AzureOperation(name = "function.create", type = AzureOperation.Type.ACTION)
public void actionPerformed(NodeActionEvent e) {
    final Project project = (Project) functionModule.getProject();
    AzureSignInAction.requireSignedIn(project, () -> this.openDialog(project, null));
}
Also used : Project(com.intellij.openapi.project.Project) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Aggregations

AzureOperation (com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)64 AzureString (com.microsoft.azure.toolkit.lib.common.bundle.AzureString)11 AzureToolkitRuntimeException (com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)11 IOException (java.io.IOException)10 Project (com.intellij.openapi.project.Project)9 ArrayList (java.util.ArrayList)9 AzureTask (com.microsoft.azure.toolkit.lib.common.task.AzureTask)8 Operation (com.microsoft.azuretools.telemetrywrapper.Operation)6 Path (java.nio.file.Path)6 Module (com.intellij.openapi.module.Module)5 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 IWebApp (com.microsoft.azure.toolkit.lib.appservice.service.IWebApp)5 Azure (com.microsoft.azure.management.Azure)4 IFunctionApp (com.microsoft.azure.toolkit.lib.appservice.service.IFunctionApp)4 AzureUIRefreshEvent (com.microsoft.azuretools.utils.AzureUIRefreshEvent)4 Action (com.microsoft.azure.toolkit.lib.common.action.Action)3 ResourceGroup (com.microsoft.azure.toolkit.lib.common.model.ResourceGroup)3 Subscription (com.microsoft.azure.toolkit.lib.common.model.Subscription)3 AzureTaskManager (com.microsoft.azure.toolkit.lib.common.task.AzureTaskManager)3