Skip to content

Commit

Permalink
Remove staging folder from function run/deploy configuration (#4534)
Browse files Browse the repository at this point in the history
* Remove staging folder

* Clean up staging folder after function run configuration done
  • Loading branch information
Flanker32 authored Aug 10, 2020
1 parent 907fa81 commit c0c0555
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.InvalidPathException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
Expand All @@ -73,21 +73,22 @@ public class FunctionUtils {
"{\"id\":\"Microsoft.Azure.Functions.ExtensionBundle\",\"version\":\"[1.*, 2.0.0)\"}}\n";
private static final String DEFAULT_LOCAL_SETTINGS_JSON = "{ \"IsEncrypted\": false, \"Values\": " +
"{ \"FUNCTIONS_WORKER_RUNTIME\": \"java\" } }";
private static final String AZURE_FUNCTIONS = "azure-functions";

public static boolean isValidStagingFolderPath(String stagingFolderPath) {
if (StringUtils.isEmpty(stagingFolderPath)) {
return false;
}
final File target = new File(stagingFolderPath);
if (target.exists()) {
return target.isDirectory();
} else {
try {
Paths.get(stagingFolderPath);
} catch (InvalidPathException | NullPointerException ex) {
return false;
public static File getTempStagingFolder() throws IOException {
final Path path = Files.createTempDirectory(AZURE_FUNCTIONS);
final File file = path.toFile();
FileUtils.forceDeleteOnExit(file);
return file;
}

public static void cleanUpStagingFolder(File stagingFolder) {
try {
if (stagingFolder != null) {
FileUtils.deleteDirectory(stagingFolder);
}
return true;
} catch (IOException e) {
// swallow exceptions while clean up
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,6 @@ public void validate() throws ConfigurationException {
if (StringUtils.isEmpty(this.getFunctionId())) {
throw new ConfigurationException(NEED_SPECIFY_TARGET_FUNCTION);
}
if (!FunctionUtils.isValidStagingFolderPath(getDeploymentStagingDirectory())) {
throw new ConfigurationException(NEED_SPECIFY_VALID_STAGING_DIRECTORY_PATH);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class FunctionDeploymentState extends AzureRunProfileState<WebAppBase> {

private FunctionDeployConfiguration functionDeployConfiguration;
private final FunctionDeployModel deployModel;
private File stagingFolder;

/**
* Place to execute the Web App deployment task.
Expand All @@ -77,7 +78,8 @@ public WebAppBase executeSteps(@NotNull RunProcessHandler processHandler
functionDeployConfiguration.setRuntime(runtimeConfiguration);
functionDeployConfiguration.setPricingTier(appServicePlan.pricingTier().toSkuDescription().size());
// Deploy function to Azure
final File stagingFolder = new File(functionDeployConfiguration.getDeploymentStagingDirectory());
stagingFolder = FunctionUtils.getTempStagingFolder();
deployModel.setDeploymentStagingDirectoryPath(stagingFolder.getPath());
prepareStagingFolder(stagingFolder, processHandler);
final DeployFunctionHandler deployFunctionHandler = new DeployFunctionHandler(deployModel, message -> {
if (processHandler.isProcessRunning()) {
Expand All @@ -87,14 +89,15 @@ public WebAppBase executeSteps(@NotNull RunProcessHandler processHandler
return deployFunctionHandler.execute();
}

private void prepareStagingFolder(File stagingFolder, RunProcessHandler processHandler) {
private void prepareStagingFolder(File stagingFolder, RunProcessHandler processHandler)
throws AzureExecutionException {
ReadAction.run(() -> {
final Path hostJsonPath = FunctionUtils.getDefaultHostJson(project);
final PsiMethod[] methods = FunctionUtils.findFunctionsByAnnotation(functionDeployConfiguration.getModule());
try {
FunctionUtils.prepareStagingFolder(stagingFolder.toPath(), hostJsonPath, functionDeployConfiguration.getModule(), methods);
} catch (AzureExecutionException | IOException e) {
processHandler.println(String.format("Failed to prepare staging folder, %s", e.getMessage()), ProcessOutputTypes.STDERR);
throw new AzureExecutionException("Failed to prepare staging folder");
}
});
}
Expand All @@ -108,12 +111,14 @@ protected Operation createOperation() {
protected void onSuccess(WebAppBase result, @NotNull RunProcessHandler processHandler) {
processHandler.setText("Deploy succeed");
processHandler.notifyComplete();
FunctionUtils.cleanUpStagingFolder(stagingFolder);
}

@Override
protected void onFail(String errMsg, @NotNull RunProcessHandler processHandler) {
processHandler.println(errMsg, ProcessOutputTypes.STDERR);
processHandler.notifyComplete();
FunctionUtils.cleanUpStagingFolder(stagingFolder);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.microsoft.intellij.runner.functions.deploy.ui.FunctionDeploymentPanel">
<grid id="27dc6" binding="pnlRoot" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="pnlRoot" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="591" height="391"/>
Expand Down Expand Up @@ -44,38 +44,21 @@
<grid id="76234" binding="pnlAppSettings" custom-create="true" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<component id="82c52" class="javax.swing.JLabel">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="9" fill="0" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="9" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="f9a4"/>
<text value="App Settings:"/>
</properties>
</component>
<component id="2408e" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Staging Folder:"/>
</properties>
</component>
<component id="ae808" class="com.intellij.openapi.ui.TextFieldWithBrowseButton" binding="txtStagingFolder">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<editable value="true"/>
<enabled value="true"/>
</properties>
</component>
<component id="82222" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@

import com.intellij.icons.AllIcons;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.util.Comparing;
import com.intellij.packaging.artifacts.Artifact;
import com.intellij.ui.HyperlinkLabel;
Expand All @@ -43,7 +41,6 @@
import com.microsoft.intellij.runner.functions.core.FunctionUtils;
import com.microsoft.intellij.runner.functions.deploy.FunctionDeployConfiguration;
import com.microsoft.intellij.runner.functions.deploy.ui.creation.FunctionCreationDialog;
import com.microsoft.intellij.ui.util.UIUtils;
import org.apache.commons.collections.MapUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.maven.project.MavenProject;
Expand Down Expand Up @@ -72,7 +69,6 @@ public class FunctionDeploymentPanel extends AzureSettingPanel<FunctionDeployCon
private JComboBox cbxFunctionApp;
private HyperlinkLabel lblCreateFunctionApp;
private JPanel pnlAppSettings;
private TextFieldWithBrowseButton txtStagingFolder;
private JComboBox<Module> cbFunctionModule;
private AppSettingsTable appSettingsTable;

Expand Down Expand Up @@ -107,15 +103,6 @@ public void customize(JList list, Module module, int i, boolean b, boolean b1) {
}
});

cbFunctionModule.addItemListener(itemEvent -> {
final String targetFolder = FunctionUtils.getTargetFolder((Module) cbFunctionModule.getSelectedItem());
txtStagingFolder.setText(targetFolder);
});

txtStagingFolder.addActionListener(
UIUtils.createFileChooserListenerWithTextPath(txtStagingFolder, project,
FileChooserDescriptorFactory.createSingleFolderDescriptor()));

fillModules();
}

Expand Down Expand Up @@ -227,7 +214,6 @@ protected void resetFromConfig(@NotNull FunctionDeployConfiguration configuratio
protected void apply(@NotNull FunctionDeployConfiguration configuration) {
configuration.saveTargetModule((Module) cbFunctionModule.getSelectedItem());
configuration.setAppSettings(appSettingsTable.getAppSettings());
configuration.setDeploymentStagingDirectory(txtStagingFolder.getText());
if (selectedFunctionApp == null || selectedFunctionApp.getResource() == null) {
// Use previous configuration when function is not loaded
configuration.setSubscription(previousDeployConfiguration.getSubscriptionId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,6 @@ public void validate() throws ConfigurationException {
if (!func.exists() || !func.isFile() || !func.getName().contains("func")) {
throw new ConfigurationException("Please specify correct function cli path");
}
if (!FunctionUtils.isValidStagingFolderPath(getStagingFolder())) {
throw new ConfigurationException("Please specify correct staging folder path");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class FunctionRunState extends AzureRunProfileState<FunctionApp> {
private static final int DEFAULT_DEBUG_PORT = 5005;
private static final int MAX_PORT = 65535;

private File stagingFolder;
private Process process;
private Executor executor;
private FunctionRunConfiguration functionRunConfiguration;
Expand Down Expand Up @@ -106,7 +107,7 @@ private void launchDebugger(final Project project, int debugPort) {
protected FunctionApp executeSteps(@NotNull RunProcessHandler processHandler, @NotNull Map<String, String> telemetryMap) throws Exception {
// Prepare staging Folder
updateTelemetryMap(telemetryMap);
final File stagingFolder = new File(functionRunConfiguration.getStagingFolder());
stagingFolder = FunctionUtils.getTempStagingFolder();
prepareStagingFolder(stagingFolder, processHandler);
// Run Function Host
runFunctionCli(processHandler, stagingFolder);
Expand Down Expand Up @@ -167,7 +168,7 @@ private ProcessBuilder getRunFunctionCliProcessBuilder(File stagingFolder, int f
return processBuilder;
}

private void prepareStagingFolder(File stagingFolder, RunProcessHandler processHandler) {
private void prepareStagingFolder(File stagingFolder, RunProcessHandler processHandler) throws AzureExecutionException {
ReadAction.run(() -> {
final Path hostJsonPath = FunctionUtils.getDefaultHostJson(project);
final Path localSettingsJson = Paths.get(functionRunConfiguration.getLocalSettingsJsonPath());
Expand All @@ -176,7 +177,7 @@ private void prepareStagingFolder(File stagingFolder, RunProcessHandler processH
FunctionUtils.prepareStagingFolder(stagingFolder.toPath(), hostJsonPath, functionRunConfiguration.getModule(), methods);
FunctionUtils.copyLocalSettingsToStagingFolder(stagingFolder.toPath(), localSettingsJson, functionRunConfiguration.getAppSettings());
} catch (AzureExecutionException | IOException e) {
processHandler.println(String.format("Failed to prepare staging folder, %s", e.getMessage()), ProcessOutputTypes.STDERR);
throw new AzureExecutionException("Failed to prepare staging folder", e);
}
});
}
Expand Down Expand Up @@ -225,6 +226,7 @@ protected void onSuccess(FunctionApp result, RunProcessHandler processHandler) {
processHandler.setText("Succeed!");
processHandler.notifyComplete();
}
FunctionUtils.cleanUpStagingFolder(stagingFolder);
}

@Override
Expand All @@ -236,5 +238,6 @@ protected void onFail(String errMsg, RunProcessHandler processHandler) {
processHandler.setText("Fail!");
processHandler.notifyComplete();
}
FunctionUtils.cleanUpStagingFolder(stagingFolder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,25 @@
<properties/>
<border type="none"/>
<children>
<grid id="6573" binding="settings" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="6573" binding="settings" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="43173" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Staging Folder:"/>
</properties>
</component>
<component id="540ef" class="com.intellij.openapi.ui.TextFieldWithBrowseButton" binding="txtStagingFolder">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<editable value="true"/>
</properties>
</component>
<component id="841e9" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Function CLI:"/>
</properties>
</component>
<component id="3e77f" class="com.intellij.openapi.ui.TextFieldWithBrowseButton" binding="txtFunc">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
Expand All @@ -62,7 +44,7 @@
</component>
<component id="ae00f" class="javax.swing.JLabel">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="9" fill="0" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="9" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="App Settings:"/>
Expand All @@ -85,7 +67,7 @@
<grid id="c7fd0" binding="pnlAppSettings" custom-create="true" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public class FunctionRunPanel extends AzureSettingPanel<FunctionRunConfiguration

private JPanel settings;
private JPanel pnlMain;
private TextFieldWithBrowseButton txtStagingFolder;
private TextFieldWithBrowseButton txtFunc;
private JPanel pnlAppSettings;
private JComboBox<Module> cbFunctionModule;
Expand All @@ -74,15 +73,6 @@ public void customize(JList list, Module module, int i, boolean b, boolean b1) {
}
});

cbFunctionModule.addItemListener(itemEvent -> {
final String targetFolder = FunctionUtils.getTargetFolder((Module) cbFunctionModule.getSelectedItem());
txtStagingFolder.setText(targetFolder);
});

txtStagingFolder.addActionListener(
UIUtils.createFileChooserListenerWithTextPath(txtStagingFolder, project,
FileChooserDescriptorFactory.createSingleFolderDescriptor()));

txtFunc.addActionListener(
UIUtils.createFileChooserListenerWithTextPath(txtFunc, project,
FileChooserDescriptorFactory.createSingleFileDescriptor()));
Expand Down Expand Up @@ -126,7 +116,6 @@ protected void resetFromConfig(@NotNull FunctionRunConfiguration configuration)
@Override
protected void apply(@NotNull FunctionRunConfiguration configuration) {
configuration.setFuncPath(txtFunc.getText());
configuration.setStagingFolder(txtStagingFolder.getText());
configuration.setAppSettings(appSettingsTable.getAppSettings());
configuration.saveModule((Module) cbFunctionModule.getSelectedItem());
}
Expand Down

0 comments on commit c0c0555

Please sign in to comment.