use of com.microsoft.azure.management.appservice.WebApp in project azure-sdk-for-java by Azure.
the class ManageWebAppBasic 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 app1Name = SdkContext.randomResourceName("webapp1-", 20);
final String app2Name = SdkContext.randomResourceName("webapp2-", 20);
final String app3Name = SdkContext.randomResourceName("webapp3-", 20);
final String rg1Name = SdkContext.randomResourceName("rg1NEMV_", 24);
final String rg2Name = SdkContext.randomResourceName("rg2NEMV_", 24);
try {
//============================================================
// Create a web app with a new app service plan
System.out.println("Creating web app " + app1Name + " in resource group " + rg1Name + "...");
WebApp app1 = azure.webApps().define(app1Name).withRegion(Region.US_WEST).withNewResourceGroup(rg1Name).withNewWindowsPlan(PricingTier.STANDARD_S1).create();
System.out.println("Created web app " + app1.name());
Utils.print(app1);
//============================================================
// Create a second web app with the same app service plan
System.out.println("Creating another web app " + app2Name + " in resource group " + rg1Name + "...");
AppServicePlan plan = azure.appServices().appServicePlans().getById(app1.appServicePlanId());
WebApp app2 = azure.webApps().define(app2Name).withExistingWindowsPlan(plan).withExistingResourceGroup(rg1Name).create();
System.out.println("Created web app " + app2.name());
Utils.print(app2);
//============================================================
// Create a third web app with the same app service plan, but
// in a different resource group
System.out.println("Creating another web app " + app3Name + " in resource group " + rg2Name + "...");
WebApp app3 = azure.webApps().define(app3Name).withExistingWindowsPlan(plan).withNewResourceGroup(rg2Name).create();
System.out.println("Created web app " + app3.name());
Utils.print(app3);
//============================================================
// stop and start app1, restart app 2
System.out.println("Stopping web app " + app1.name());
app1.stop();
System.out.println("Stopped web app " + app1.name());
Utils.print(app1);
System.out.println("Starting web app " + app1.name());
app1.start();
System.out.println("Started web app " + app1.name());
Utils.print(app1);
System.out.println("Restarting web app " + app2.name());
app2.restart();
System.out.println("Restarted web app " + app2.name());
Utils.print(app2);
//============================================================
// Configure app 3 to have Java 8 enabled
System.out.println("Adding Java support to web app " + app3Name + "...");
app3.update().withJavaVersion(JavaVersion.JAVA_8_NEWEST).withWebContainer(WebContainer.TOMCAT_8_0_NEWEST).apply();
System.out.println("Java supported on web app " + app3Name + "...");
//=============================================================
// List web apps
System.out.println("Printing list of web apps in resource group " + rg1Name + "...");
for (WebApp webApp : azure.webApps().listByResourceGroup(rg1Name)) {
Utils.print(webApp);
}
System.out.println("Printing list of web apps in resource group " + rg2Name + "...");
for (WebApp webApp : azure.webApps().listByResourceGroup(rg2Name)) {
Utils.print(webApp);
}
//=============================================================
// Delete a web app
System.out.println("Deleting web app " + app1Name + "...");
azure.webApps().deleteByResourceGroup(rg1Name, app1Name);
System.out.println("Deleted web app " + app1Name + "...");
System.out.println("Printing list of web apps in resource group " + rg1Name + " again...");
for (WebApp webApp : azure.webApps().listByResourceGroup(rg1Name)) {
Utils.print(webApp);
}
return true;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rg1Name);
azure.resourceGroups().beginDeleteByName(rg1Name);
System.out.println("Deleted Resource Group: " + rg1Name);
System.out.println("Deleting Resource Group: " + rg2Name);
azure.resourceGroups().beginDeleteByName(rg2Name);
System.out.println("Deleted Resource Group: " + rg2Name);
} 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.WebApp in project azure-sdk-for-java by Azure.
the class ManageWebAppSlots 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 resourceGroupName = SdkContext.randomResourceName("rg", 24);
final String app1Name = SdkContext.randomResourceName("webapp1-", 20);
final String app2Name = SdkContext.randomResourceName("webapp2-", 20);
final String app3Name = SdkContext.randomResourceName("webapp3-", 20);
final String slotName = "staging";
try {
azure.resourceGroups().define(resourceGroupName).withRegion(Region.US_EAST).create();
//============================================================
// Create 3 web apps with 3 new app service plans in different regions
WebApp app1 = createWebApp(azure, app1Name, Region.US_EAST, resourceGroupName);
WebApp app2 = createWebApp(azure, app2Name, Region.EUROPE_WEST, resourceGroupName);
WebApp app3 = createWebApp(azure, app3Name, Region.ASIA_EAST, resourceGroupName);
//============================================================
// Create a deployment slot under each web app with auto swap
DeploymentSlot slot1 = createSlot(slotName, app1);
DeploymentSlot slot2 = createSlot(slotName, app2);
DeploymentSlot slot3 = createSlot(slotName, app3);
//============================================================
// Deploy the staging branch to the slot
deployToStaging(slot1);
deployToStaging(slot2);
deployToStaging(slot3);
// swap back
swapProductionBacktoSlot(slot1);
swapProductionBacktoSlot(slot2);
swapProductionBacktoSlot(slot3);
return true;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + resourceGroupName);
azure.resourceGroups().beginDeleteByName(resourceGroupName);
System.out.println("Deleted Resource Group: " + resourceGroupName);
} 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.WebApp in project azure-sdk-for-java by Azure.
the class ManageLinuxWebAppBasic 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 app1Name = SdkContext.randomResourceName("webapp1-", 20);
final String app2Name = SdkContext.randomResourceName("webapp2-", 20);
final String app3Name = SdkContext.randomResourceName("webapp3-", 20);
final String rg1Name = SdkContext.randomResourceName("rg1NEMV_", 24);
final String rg2Name = SdkContext.randomResourceName("rg2NEMV_", 24);
try {
//============================================================
// Create a web app with a new app service plan
System.out.println("Creating web app " + app1Name + " in resource group " + rg1Name + "...");
WebApp app1 = azure.webApps().define(app1Name).withRegion(Region.US_WEST).withNewResourceGroup(rg1Name).withNewLinuxPlan(PricingTier.STANDARD_S1).withBuiltInImage(RuntimeStack.NODEJS_6_9_3).create();
System.out.println("Created web app " + app1.name());
Utils.print(app1);
//============================================================
// Create a second web app with the same app service plan
System.out.println("Creating another web app " + app2Name + " in resource group " + rg1Name + "...");
AppServicePlan plan = azure.appServices().appServicePlans().getById(app1.appServicePlanId());
WebApp app2 = azure.webApps().define(app2Name).withExistingLinuxPlan(plan).withExistingResourceGroup(rg1Name).withBuiltInImage(RuntimeStack.NODEJS_6_9_3).create();
System.out.println("Created web app " + app2.name());
Utils.print(app2);
//============================================================
// Create a third web app with the same app service plan, but
// in a different resource group
System.out.println("Creating another web app " + app3Name + " in resource group " + rg2Name + "...");
WebApp app3 = azure.webApps().define(app3Name).withExistingLinuxPlan(plan).withNewResourceGroup(rg2Name).withBuiltInImage(RuntimeStack.NODEJS_6_9_3).create();
System.out.println("Created web app " + app3.name());
Utils.print(app3);
//============================================================
// stop and start app1, restart app 2
System.out.println("Stopping web app " + app1.name());
app1.stop();
System.out.println("Stopped web app " + app1.name());
Utils.print(app1);
System.out.println("Starting web app " + app1.name());
app1.start();
System.out.println("Started web app " + app1.name());
Utils.print(app1);
System.out.println("Restarting web app " + app2.name());
app2.restart();
System.out.println("Restarted web app " + app2.name());
Utils.print(app2);
//============================================================
// Configure app 3 to have Java 8 enabled
System.out.println("Adding Java support to web app " + app3Name + "...");
app3.update().withJavaVersion(JavaVersion.JAVA_8_NEWEST).withWebContainer(WebContainer.TOMCAT_8_0_NEWEST).apply();
System.out.println("Java supported on web app " + app3Name + "...");
//=============================================================
// List web apps
System.out.println("Printing list of web apps in resource group " + rg1Name + "...");
for (WebApp webApp : azure.webApps().listByResourceGroup(rg1Name)) {
Utils.print(webApp);
}
System.out.println("Printing list of web apps in resource group " + rg2Name + "...");
for (WebApp webApp : azure.webApps().listByResourceGroup(rg2Name)) {
Utils.print(webApp);
}
//=============================================================
// Delete a web app
System.out.println("Deleting web app " + app1Name + "...");
azure.webApps().deleteByResourceGroup(rg1Name, app1Name);
System.out.println("Deleted web app " + app1Name + "...");
System.out.println("Printing list of web apps in resource group " + rg1Name + " again...");
for (WebApp webApp : azure.webApps().listByResourceGroup(rg1Name)) {
Utils.print(webApp);
}
return true;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rg1Name);
azure.resourceGroups().beginDeleteByName(rg1Name);
System.out.println("Deleted Resource Group: " + rg1Name);
System.out.println("Deleting Resource Group: " + rg2Name);
azure.resourceGroups().beginDeleteByName(rg2Name);
System.out.println("Deleted Resource Group: " + rg2Name);
} 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.WebApp in project azure-sdk-for-java by Azure.
the class ManageLinuxWebAppSourceControl 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 web app with a new app service plan
System.out.println("Creating web app " + app1Name + " in resource group " + rgName + "...");
WebApp app1 = azure.webApps().define(app1Name).withRegion(Region.US_WEST).withNewResourceGroup(rgName).withNewLinuxPlan(PricingTier.STANDARD_S1).withPublicDockerHubImage("tomcat:8-jre8").withStartUpCommand("/bin/bash -c \"sed -ie 's/appBase=\\\"webapps\\\"/appBase=\\\"\\\\/home\\\\/site\\\\/wwwroot\\\\/webapps\\\"/g' conf/server.xml && catalina.sh run\"").withAppSetting("PORT", "8080").create();
System.out.println("Created web app " + app1.name());
Utils.print(app1);
//============================================================
// Deploy to app 1 through FTP
System.out.println("Deploying helloworld.war to " + app1Name + " through FTP...");
Utils.uploadFileToFtp(app1.getPublishingProfile(), "helloworld.war", ManageLinuxWebAppSourceControl.class.getResourceAsStream("/helloworld.war"));
System.out.println("Deployment helloworld.war to web app " + app1.name() + " completed");
Utils.print(app1);
// warm up
System.out.println("Warming up " + app1Url + "/helloworld...");
curl("http://" + app1Url + "/helloworld");
Thread.sleep(5000);
System.out.println("CURLing " + app1Url + "/helloworld...");
System.out.println(curl("http://" + app1Url + "/helloworld"));
//============================================================
// Create a second web app with local git source control
System.out.println("Creating another web app " + app2Name + " in resource group " + rgName + "...");
AppServicePlan plan = azure.appServices().appServicePlans().getById(app1.appServicePlanId());
WebApp app2 = azure.webApps().define(app2Name).withExistingLinuxPlan(plan).withExistingResourceGroup(rgName).withPublicDockerHubImage("tomcat:8-jre8").withStartUpCommand("/bin/bash -c \"sed -ie 's/appBase=\\\"webapps\\\"/appBase=\\\"\\\\/home\\\\/site\\\\/wwwroot\\\\/webapps\\\"/g' conf/server.xml && catalina.sh run\"").withAppSetting("PORT", "8080").withLocalGitSourceControl().create();
System.out.println("Created web 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(ManageLinuxWebAppSourceControl.class.getResource("/azure-samples-appservice-helloworld/").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 web app " + app2.name() + " completed");
Utils.print(app2);
// warm up
System.out.println("Warming up " + app2Url + "/helloworld...");
curl("http://" + app2Url + "/helloworld");
Thread.sleep(5000);
System.out.println("CURLing " + app2Url + "/helloworld...");
System.out.println(curl("http://" + app2Url + "/helloworld"));
//============================================================
// Create a 3rd web app with a public GitHub repo in Azure-Samples
System.out.println("Creating another web app " + app3Name + "...");
WebApp app3 = azure.webApps().define(app3Name).withExistingLinuxPlan(plan).withNewResourceGroup(rgName).withPublicDockerHubImage("tomcat:8-jre8").withStartUpCommand("/bin/bash -c \"sed -ie 's/appBase=\\\"webapps\\\"/appBase=\\\"\\\\/home\\\\/site\\\\/wwwroot\\\\/webapps\\\"/g' conf/server.xml && catalina.sh run\"").withAppSetting("PORT", "8080").defineSourceControl().withPublicGitRepository("https://github.com/azure-appservice-samples/java-get-started").withBranch("master").attach().create();
System.out.println("Created web app " + app3.name());
Utils.print(app3);
// warm up
System.out.println("Warming up " + app3Url + "...");
curl("http://" + app3Url);
Thread.sleep(5000);
System.out.println("CURLing " + app3Url + "...");
System.out.println(curl("http://" + app3Url));
//============================================================
// Create a 4th web app with a personal GitHub repo and turn on continuous integration
System.out.println("Creating another web app " + app4Name + "...");
WebApp app4 = azure.webApps().define(app4Name).withExistingLinuxPlan(plan).withExistingResourceGroup(rgName).withPublicDockerHubImage("tomcat:8-jre8").withStartUpCommand("/bin/bash -c \"sed -ie 's/appBase=\\\"webapps\\\"/appBase=\\\"\\\\/home\\\\/site\\\\/wwwroot\\\\/webapps\\\"/g' conf/server.xml && catalina.sh run\"").withAppSetting("PORT", "8080").create();
System.out.println("Created web 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.WebApp in project azure-sdk-for-java by Azure.
the class ManageLinuxWebAppSqlConnection 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 appName = SdkContext.randomResourceName("webapp1-", 20);
final String appUrl = appName + suffix;
final String sqlServerName = SdkContext.randomResourceName("jsdkserver", 20);
final String sqlDbName = SdkContext.randomResourceName("jsdkdb", 20);
final String admin = "jsdkadmin";
final String password = "StrongPass!123";
final String rgName = SdkContext.randomResourceName("rg1NEMV_", 24);
try {
//============================================================
// Create a sql server
System.out.println("Creating SQL server " + sqlServerName + "...");
SqlServer server = azure.sqlServers().define(sqlServerName).withRegion(Region.US_WEST).withNewResourceGroup(rgName).withAdministratorLogin(admin).withAdministratorPassword(password).create();
System.out.println("Created SQL server " + server.name());
//============================================================
// Create a sql database for the web app to use
System.out.println("Creating SQL database " + sqlDbName + "...");
SqlDatabase db = server.databases().define(sqlDbName).create();
System.out.println("Created SQL database " + db.name());
//============================================================
// Create a web app with a new app service plan
System.out.println("Creating web app " + appName + "...");
WebApp app = azure.webApps().define(appName).withRegion(Region.US_WEST).withExistingResourceGroup(rgName).withNewLinuxPlan(PricingTier.STANDARD_S1).withBuiltInImage(RuntimeStack.PHP_5_6_23).defineSourceControl().withPublicGitRepository("https://github.com/ProjectNami/projectnami").withBranch("master").attach().withAppSetting("ProjectNami.DBHost", server.fullyQualifiedDomainName()).withAppSetting("ProjectNami.DBName", db.name()).withAppSetting("ProjectNami.DBUser", admin).withAppSetting("ProjectNami.DBPass", password).create();
System.out.println("Created web app " + app.name());
Utils.print(app);
//============================================================
// Allow web app to access the SQL server
System.out.println("Allowing web app " + appName + " to access SQL server...");
SqlServer.Update update = server.update();
for (String ip : app.outboundIPAddresses()) {
update = update.withNewFirewallRule(ip);
}
server = update.apply();
System.out.println("Firewall rules added for web app " + appName);
Utils.print(server);
System.out.println("Your WordPress app is ready.");
System.out.println("Please navigate to http://" + appUrl + " to finish the GUI setup. Press enter to exit.");
System.in.read();
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;
}
Aggregations