Java tutorial
/** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for * license information. */ package com.microsoft.azure.management.appservice.samples; import com.microsoft.azure.management.Azure; import com.microsoft.azure.management.appservice.AppServicePlan; import com.microsoft.azure.management.appservice.PricingTier; import com.microsoft.azure.management.appservice.PublishingProfile; import com.microsoft.azure.management.appservice.WebApp; import com.microsoft.azure.management.resources.fluentcore.arm.Region; import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext; import com.microsoft.azure.management.samples.Utils; import com.microsoft.rest.LogLevel; import okhttp3.OkHttpClient; import okhttp3.Request; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.PushCommand; import org.eclipse.jgit.transport.RefSpec; import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; /** * Azure App Service basic sample for managing web apps. * - Create 4 web apps under the same new app service plan: * - Deploy to 1 using FTP * - Deploy to 2 using local Git repository * - Deploy to 3 using a publicly available Git repository * - Deploy to 4 using a GitHub repository with continuous integration */ public final class ManageLinuxWebAppSourceControl { private static OkHttpClient httpClient; /** * 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.uploadFileToWebApp(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") // Uncomment the following lines to turn on 4th scenario //.defineSourceControl() // .withContinuouslyIntegratedGitHubRepository("username", "reponame") // .withBranch("master") // .withGitHubAccessToken("YOUR GITHUB PERSONAL TOKEN") // .attach() .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; } /** * Main entry point. * @param args the parameters */ public static void main(String[] args) { try { //============================================================= // Authenticate final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION")); Azure azure = Azure.configure().withLogLevel(LogLevel.BASIC).authenticate(credFile) .withDefaultSubscription(); // Print selected subscription System.out.println("Selected subscription: " + azure.subscriptionId()); runSample(azure); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } private static String curl(String url) { Request request = new Request.Builder().url(url).get().build(); try { return httpClient.newCall(request).execute().body().string(); } catch (IOException e) { return null; } } static { httpClient = new OkHttpClient.Builder().readTimeout(1, TimeUnit.MINUTES).build(); } }