use of com.microsoft.azure.management.appservice.PublishingProfile in project azure-sdk-for-java by Azure.
the class ManageFunctionAppSourceControl method runSample.
/**
* Main function which runs the actual sample.
* @param azure instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(Azure azure) {
// New resources
final String suffix = ".azurewebsites.net";
final String app1Name = SdkContext.randomResourceName("webapp1-", 20);
final String app2Name = SdkContext.randomResourceName("webapp2-", 20);
final String app3Name = SdkContext.randomResourceName("webapp3-", 20);
final String app4Name = SdkContext.randomResourceName("webapp4-", 20);
final String app1Url = app1Name + suffix;
final String app2Url = app2Name + suffix;
final String app3Url = app3Name + suffix;
final String app4Url = app4Name + suffix;
final String rgName = SdkContext.randomResourceName("rg1NEMV_", 24);
try {
//============================================================
// Create a function app with a new app service plan
System.out.println("Creating function app " + app1Name + " in resource group " + rgName + "...");
FunctionApp app1 = azure.appServices().functionApps().define(app1Name).withRegion(Region.US_WEST).withNewResourceGroup(rgName).create();
System.out.println("Created function app " + app1.name());
Utils.print(app1);
//============================================================
// Deploy to app 1 through FTP
System.out.println("Deploying a function app to " + app1Name + " through FTP...");
Utils.uploadFileToFtp(app1.getPublishingProfile(), "host.json", ManageFunctionAppSourceControl.class.getResourceAsStream("/square-function-app/host.json"));
Utils.uploadFileToFtp(app1.getPublishingProfile(), "square/function.json", ManageFunctionAppSourceControl.class.getResourceAsStream("/square-function-app/square/function.json"));
Utils.uploadFileToFtp(app1.getPublishingProfile(), "square/index.js", ManageFunctionAppSourceControl.class.getResourceAsStream("/square-function-app/square/index.js"));
System.out.println("Deployment square app to function app " + app1.name() + " completed");
Utils.print(app1);
// warm up
System.out.println("Warming up " + app1Url + "/api/square...");
post("http://" + app1Url + "/api/square", "625");
Thread.sleep(5000);
System.out.println("CURLing " + app1Url + "/api/square...");
System.out.println("Square of 625 is " + post("http://" + app1Url + "/api/square", "625"));
//============================================================
// Create a second function app with local git source control
System.out.println("Creating another function app " + app2Name + " in resource group " + rgName + "...");
AppServicePlan plan = azure.appServices().appServicePlans().getById(app1.appServicePlanId());
FunctionApp app2 = azure.appServices().functionApps().define(app2Name).withExistingAppServicePlan(plan).withExistingResourceGroup(rgName).withExistingStorageAccount(app1.storageAccount()).withLocalGitSourceControl().create();
System.out.println("Created function app " + app2.name());
Utils.print(app2);
//============================================================
// Deploy to app 2 through local Git
System.out.println("Deploying a local Tomcat source to " + app2Name + " through Git...");
PublishingProfile profile = app2.getPublishingProfile();
Git git = Git.init().setDirectory(new File(ManageFunctionAppSourceControl.class.getResource("/square-function-app/").getPath())).call();
git.add().addFilepattern(".").call();
git.commit().setMessage("Initial commit").call();
PushCommand command = git.push();
command.setRemote(profile.gitUrl());
command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(profile.gitUsername(), profile.gitPassword()));
command.setRefSpecs(new RefSpec("master:master"));
command.setForce(true);
command.call();
System.out.println("Deployment to function app " + app2.name() + " completed");
Utils.print(app2);
// warm up
System.out.println("Warming up " + app2Url + "/api/square...");
post("http://" + app2Url + "/api/square", "725");
Thread.sleep(5000);
System.out.println("CURLing " + app2Url + "/api/square...");
System.out.println("Square of 725 is " + post("http://" + app2Url + "/api/square", "725"));
//============================================================
// Create a 3rd function app with a public GitHub repo in Azure-Samples
System.out.println("Creating another function app " + app3Name + "...");
FunctionApp app3 = azure.appServices().functionApps().define(app3Name).withExistingAppServicePlan(plan).withNewResourceGroup(rgName).withExistingStorageAccount(app2.storageAccount()).defineSourceControl().withPublicGitRepository("https://github.com/jianghaolu/square-function-app-sample").withBranch("master").attach().create();
System.out.println("Created function app " + app3.name());
Utils.print(app3);
// warm up
System.out.println("Warming up " + app3Url + "/api/square...");
post("http://" + app3Url + "/api/square", "825");
Thread.sleep(5000);
System.out.println("CURLing " + app3Url + "/api/square...");
System.out.println("Square of 825 is " + post("http://" + app3Url + "/api/square", "825"));
//============================================================
// Create a 4th function app with a personal GitHub repo and turn on continuous integration
System.out.println("Creating another function app " + app4Name + "...");
FunctionApp app4 = azure.appServices().functionApps().define(app4Name).withExistingAppServicePlan(plan).withExistingResourceGroup(rgName).withExistingStorageAccount(app3.storageAccount()).create();
System.out.println("Created function app " + app4.name());
Utils.print(app4);
// warm up
System.out.println("Warming up " + app4Url + "...");
curl("http://" + app4Url);
Thread.sleep(5000);
System.out.println("CURLing " + app4Url + "...");
System.out.println(curl("http://" + app4Url));
return true;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().beginDeleteByName(rgName);
System.out.println("Deleted Resource Group: " + rgName);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}
return false;
}
use of com.microsoft.azure.management.appservice.PublishingProfile in project azure-tools-for-java by Microsoft.
the class WebAppDeployDialog method deploy.
private void deploy(String artifactName, String artifactPath) {
int selectedRow = table.getSelectionIndex();
String appServiceName = table.getItems()[selectedRow].getText(0);
WebAppDetails wad = webAppDetailsMap.get(appServiceName);
WebApp webApp = wad.webApp;
boolean isDeployToRoot = btnDeployToRoot.getSelection();
String errTitle = "Deploy Web App Error";
String sitePath = buildSiteLink(wad.webApp, isDeployToRoot ? null : artifactName);
//Map<String, String> threadParams = new HashMap<>();
//threadParams.put("sitePath", sitePath);
String jobDescription = String.format("Web App '%s' deployment", webApp.name());
String deploymentName = UUID.randomUUID().toString();
AzureDeploymentProgressNotification.createAzureDeploymentProgressNotification(deploymentName, jobDescription);
Job job = new Job(jobDescription) {
@Override
protected IStatus run(IProgressMonitor monitor) {
String message = "Deploying Web App...";
String cancelMessage = "Interrupted by user";
String successMessage = "";
String errorMessage = "Error";
Map<String, String> postEventProperties = new HashMap<String, String>();
postEventProperties.put("Java App Name", project.getName());
monitor.beginTask(message, IProgressMonitor.UNKNOWN);
try {
AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, sitePath, 5, message);
PublishingProfile pp = webApp.getPublishingProfile();
WebAppUtils.deployArtifact(artifactName, artifactPath, pp, isDeployToRoot, new UpdateProgressIndicator(monitor));
if (monitor.isCanceled()) {
AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, null, -1, cancelMessage);
return Status.CANCEL_STATUS;
}
message = "Checking Web App availability...";
monitor.setTaskName(message);
//monitor.subTask("Link: " + sitePath);
AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, sitePath, 75, message);
// to make warn up cancelable
int stepLimit = 5;
int sleepMs = 1000;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
for (int step = 0; step < stepLimit; ++step) {
if (WebAppUtils.isUrlAccessible(sitePath)) {
// warm up
break;
}
Thread.sleep(sleepMs);
}
} catch (IOException ex) {
ex.printStackTrace();
LOG.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "run@Thread@run@ProgressDialog@deploy@AppServiceCreateDialog@SingInDialog", ex));
} catch (InterruptedException ex) {
System.out.println("The thread is interupted");
}
}
});
thread.start();
while (thread.isAlive()) {
if (monitor.isCanceled()) {
// it's published but not warmed up yet - consider as success
AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, sitePath, 100, successMessage);
return Status.CANCEL_STATUS;
} else
Thread.sleep(sleepMs);
}
monitor.done();
AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, sitePath, 100, successMessage);
} catch (IOException | InterruptedException ex) {
//threadParams.put("sitePath", null);
postEventProperties.put("PublishError", ex.getMessage());
ex.printStackTrace();
LOG.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "run@ProgressDialog@deploy@AppServiceCreateDialog", ex));
AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, null, -1, errorMessage);
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
ErrorWindow.go(parentShell, ex.getMessage(), errTitle);
;
}
});
}
AppInsightsClient.create("Deploy as WebApp", "", postEventProperties);
return Status.OK_STATUS;
}
};
job.schedule();
// try {
// ProgressDialog.get(this.getShell(), "Deploy Web App Progress").run(true, true, new IRunnableWithProgress() {
// @Override
// public void run(IProgressMonitor monitor) {
// monitor.beginTask("Deploying Web App...", IProgressMonitor.UNKNOWN);
// try {
// PublishingProfile pp = webApp.getPublishingProfile();
// WebAppUtils.deployArtifact(artifactName, artifactPath,
// pp, isDeployToRoot, new UpdateProgressIndicator(monitor));
// monitor.setTaskName("Checking Web App availability...");
// monitor.subTask("Link: " + sitePath);
//
// // to make warn up cancelable
// int stepLimit = 5;
// int sleepMs = 2000;
// Thread thread = new Thread(new Runnable() {
// @Override
// public void run() {
// try {
// for (int step = 0; step < stepLimit; ++step) {
// if (WebAppUtils.isUrlAccessible(sitePath)) { // warm up
// break;
// }
// Thread.sleep(sleepMs);
// }
// } catch (IOException | InterruptedException ex) {
// ex.printStackTrace();
// LOG.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "run@Thread@run@ProgressDialog@deploy@AppServiceCreateDialog@SingInDialog", ex));
// }
// }
// });
// thread.run();
// while (thread.isAlive()) {
// if (monitor.isCanceled()) return;
// else Thread.sleep(sleepMs);
// }
//
// monitor.done();
// } catch (IOException | InterruptedException ex) {
// threadParams.put("sitePath", null);
// ex.printStackTrace();
// LOG.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "run@ProgressDialog@deploy@AppServiceCreateDialog", ex));
// Display.getDefault().asyncExec(new Runnable() {
// @Override
// public void run() {
// ErrorWindow.go(parentShell, ex.getMessage(), errTitle);;
// }
// });
// }
// }
// });
// } catch (InvocationTargetException | InterruptedException ex) {
// threadParams.put("sitePath", null);
// ex.printStackTrace();
// LOG.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "deploy@AppServiceCreateDialog", ex));
// ErrorWindow.go(getShell(), ex.getMessage(), errTitle);;
// }
//return threadParams.get("sitePath");
}
use of com.microsoft.azure.management.appservice.PublishingProfile in project azure-tools-for-java by Microsoft.
the class WebAppDeployDialog method deploy.
private void deploy() {
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
int selectedRow = table.getSelectedRow();
WebAppDetails wad = webAppWebAppDetailsMap.get(tableModel.getValueAt(selectedRow, 0));
WebApp webApp = wad.webApp;
boolean isDeployToRoot = deployToRootCheckBox.isSelected();
ProgressManager.getInstance().run(new Task.Backgroundable(project, "Deploy Web App Progress", true) {
@Override
public void run(@NotNull ProgressIndicator progressIndicator) {
AzureDeploymentProgressNotification azureDeploymentProgressNotification = new AzureDeploymentProgressNotification(project);
try {
progressIndicator.setIndeterminate(true);
PublishingProfile pp = webApp.getPublishingProfile();
Date startDate = new Date();
azureDeploymentProgressNotification.notifyProgress(webApp.name(), startDate, null, 5, "Deploying Web App...");
WebAppUtils.deployArtifact(artifact.getName(), artifact.getOutputFilePath(), pp, isDeployToRoot, new UpdateProgressIndicator(progressIndicator));
String sitePath = buildSiteLink(wad.webApp, isDeployToRoot ? null : artifact.getName());
progressIndicator.setText("Checking Web App availability...");
progressIndicator.setText2("Link: " + sitePath);
azureDeploymentProgressNotification.notifyProgress(webApp.name(), startDate, sitePath, 75, "Checking Web App availability...");
int stepLimit = 5;
int sleepMs = 2000;
// to make warn up cancelable
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
for (int step = 0; step < stepLimit; ++step) {
if (WebAppUtils.isUrlAccessible(sitePath)) {
// warm up
break;
}
Thread.sleep(sleepMs);
}
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("deploy::warmup", e);
}
}
});
thread.start();
while (thread.isAlive()) {
if (progressIndicator.isCanceled())
return;
else
Thread.sleep(sleepMs);
}
azureDeploymentProgressNotification.notifyProgress(webApp.name(), startDate, sitePath, 100, message("runStatus"));
showLink(sitePath);
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
//LOGGER.error("deploy", ex);
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
ErrorWindow.show(project, ex.getMessage(), "Deploy Web App Error");
}
});
}
}
});
}
Aggregations