|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.resourcemanager.appservice.samples; |
| 5 | + |
| 6 | +import com.azure.core.credential.TokenCredential; |
| 7 | +import com.azure.core.http.policy.HttpLogDetailLevel; |
| 8 | +import com.azure.core.management.AzureEnvironment; |
| 9 | +import com.azure.core.management.profile.AzureProfile; |
| 10 | +import com.azure.identity.DefaultAzureCredentialBuilder; |
| 11 | +import com.azure.resourcemanager.AzureResourceManager; |
| 12 | +import com.azure.resourcemanager.appservice.models.JavaVersion; |
| 13 | +import com.azure.resourcemanager.appservice.models.PricingTier; |
| 14 | +import com.azure.resourcemanager.appservice.models.WebContainer; |
| 15 | +import com.azure.core.management.Region; |
| 16 | +import com.azure.resourcemanager.resources.fluentcore.utils.ResourceManagerUtils; |
| 17 | +import com.azure.resourcemanager.samples.Utils; |
| 18 | +import org.eclipse.jgit.api.Git; |
| 19 | +import org.eclipse.jgit.api.PushCommand; |
| 20 | +import org.eclipse.jgit.api.errors.GitAPIException; |
| 21 | +import org.eclipse.jgit.transport.RefSpec; |
| 22 | +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; |
| 23 | +import reactor.core.publisher.Flux; |
| 24 | + |
| 25 | +import java.io.File; |
| 26 | +import java.time.Duration; |
| 27 | + |
| 28 | + |
| 29 | +/** |
| 30 | + * Azure App Service basic sample for managing web apps. |
| 31 | + * - Create 4 web apps under the same new app service plan: |
| 32 | + * - Deploy to 1 using FTP |
| 33 | + * - Deploy to 2 using local Git repository |
| 34 | + * - Deploy to 3 using a publicly available Git repository |
| 35 | + * - Deploy to 4 using a GitHub repository with continuous integration |
| 36 | + */ |
| 37 | +public final class ManageWebAppSourceControlAsync { |
| 38 | + |
| 39 | + /** |
| 40 | + * Main function which runs the actual sample. |
| 41 | + * @param azureResourceManager instance of the azure client |
| 42 | + * @return true if sample runs successfully |
| 43 | + */ |
| 44 | + public static boolean runSample(final AzureResourceManager azureResourceManager) { |
| 45 | + // New resources |
| 46 | + final String suffix = ".azurewebsites.net"; |
| 47 | + final String app1Name = Utils.randomResourceName(azureResourceManager, "webapp1-", 20); |
| 48 | + final String app2Name = Utils.randomResourceName(azureResourceManager, "webapp2-", 20); |
| 49 | + final String app3Name = Utils.randomResourceName(azureResourceManager, "webapp3-", 20); |
| 50 | + final String app4Name = Utils.randomResourceName(azureResourceManager, "webapp4-", 20); |
| 51 | + final String app1Url = app1Name + suffix; |
| 52 | + final String app2Url = app2Name + suffix; |
| 53 | + final String app3Url = app3Name + suffix; |
| 54 | + final String app4Url = app4Name + suffix; |
| 55 | + final String planName = Utils.randomResourceName(azureResourceManager, "jplan_", 15); |
| 56 | + final String rgName = Utils.randomResourceName(azureResourceManager, "rg1NEMV_", 24); |
| 57 | + |
| 58 | + try { |
| 59 | + |
| 60 | + |
| 61 | + //============================================================ |
| 62 | + // Create a web app with a new app service plan |
| 63 | + |
| 64 | + System.out.println("Creating web app " + app1Name + " in resource group " + rgName + "..."); |
| 65 | + |
| 66 | + Flux<?> app1Observable = azureResourceManager.webApps().define(app1Name) |
| 67 | + .withRegion(Region.US_WEST) |
| 68 | + .withNewResourceGroup(rgName) |
| 69 | + .withNewWindowsPlan(PricingTier.STANDARD_S1) |
| 70 | + .withJavaVersion(JavaVersion.JAVA_8_NEWEST) |
| 71 | + .withWebContainer(WebContainer.TOMCAT_8_0_NEWEST) |
| 72 | + .createAsync() |
| 73 | + .flatMapMany(app -> { |
| 74 | + System.out.println("Created web app " + app.name()); |
| 75 | + return Flux.merge( |
| 76 | + Flux.just(app), |
| 77 | + app.getPublishingProfileAsync() |
| 78 | + .map(publishingProfile -> { |
| 79 | + System.out.println("Deploying helloworld.war to " + app1Name + " through FTP..."); |
| 80 | + Utils.uploadFileViaFtp(publishingProfile, |
| 81 | + "helloworld.war", |
| 82 | + ManageWebAppSourceControlAsync.class.getResourceAsStream("/helloworld.war")); |
| 83 | + |
| 84 | + System.out.println("Deployment helloworld.war to web app " + app1Name + " completed"); |
| 85 | + return publishingProfile; |
| 86 | + })); |
| 87 | + }); |
| 88 | + |
| 89 | + System.out.println("Creating another web app " + app2Name + " in resource group " + rgName + "..."); |
| 90 | + System.out.println("Creating another web app " + app3Name + "..."); |
| 91 | + System.out.println("Creating another web app " + app4Name + "..."); |
| 92 | + |
| 93 | + Flux<?> app234Observable = azureResourceManager.appServicePlans() |
| 94 | + .getByResourceGroupAsync(rgName, planName) |
| 95 | + .flatMapMany(plan -> { |
| 96 | + return Flux.merge( |
| 97 | + azureResourceManager.webApps().define(app2Name) |
| 98 | + .withExistingWindowsPlan(plan) |
| 99 | + .withExistingResourceGroup(rgName) |
| 100 | + .withLocalGitSourceControl() |
| 101 | + .withJavaVersion(JavaVersion.JAVA_8_NEWEST) |
| 102 | + .withWebContainer(WebContainer.TOMCAT_8_0_NEWEST) |
| 103 | + .createAsync(), |
| 104 | + azureResourceManager.webApps().define(app3Name) |
| 105 | + .withExistingWindowsPlan(plan) |
| 106 | + .withNewResourceGroup(rgName) |
| 107 | + .defineSourceControl() |
| 108 | + .withPublicGitRepository( |
| 109 | + "https://github.com/Azure-Samples/app-service-web-dotnet-get-started") |
| 110 | + .withBranch("master") |
| 111 | + .attach() |
| 112 | + .createAsync(), |
| 113 | + azureResourceManager.webApps() |
| 114 | + .define(app4Name) |
| 115 | + .withExistingWindowsPlan(plan) |
| 116 | + .withExistingResourceGroup(rgName) |
| 117 | + // Uncomment the following lines to turn on 4th scenario |
| 118 | + //.defineSourceControl() |
| 119 | + // .withContinuouslyIntegratedGitHubRepository("username", "reponame") |
| 120 | + // .withBranch("master") |
| 121 | + // .withGitHubAccessToken("YOUR GITHUB PERSONAL TOKEN") |
| 122 | + // .attach() |
| 123 | + .createAsync()); |
| 124 | + }).flatMap(app -> { |
| 125 | + System.out.println("Created web app " + app.name()); |
| 126 | + if (!app.name().equals(app2Name)) { |
| 127 | + return Flux.just(app); |
| 128 | + } |
| 129 | + // for the second web app Deploy a local Tomcat |
| 130 | + return app.getPublishingProfileAsync() |
| 131 | + .map(profile -> { |
| 132 | + System.out.println("Deploying a local Tomcat source to " + app2Name + " through Git..."); |
| 133 | + Git git = null; |
| 134 | + try { |
| 135 | + git = Git |
| 136 | + .init() |
| 137 | + .setDirectory(new File( |
| 138 | + ManageWebAppSourceControlAsync.class.getResource( |
| 139 | + "/azure-samples-appservice-helloworld/") |
| 140 | + .getPath())) |
| 141 | + .call(); |
| 142 | + git.add().addFilepattern(".").call(); |
| 143 | + git.commit().setMessage("Initial commit").call(); |
| 144 | + PushCommand command = git.push(); |
| 145 | + command.setRemote(profile.gitUrl()); |
| 146 | + command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(profile.gitUsername(), profile.gitPassword())); |
| 147 | + command.setRefSpecs(new RefSpec("master:master")); |
| 148 | + command.setForce(true); |
| 149 | + command.call(); |
| 150 | + } catch (GitAPIException e) { |
| 151 | + e.printStackTrace(); |
| 152 | + } |
| 153 | + System.out.println("Deployment to web app " + app2Name + " completed"); |
| 154 | + return profile; |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + Flux.merge(app1Observable, app234Observable).blockLast(); |
| 159 | + |
| 160 | + // warm up |
| 161 | + System.out.println("Warming up " + app1Url + "/helloworld..."); |
| 162 | + Utils.sendGetRequest("http://" + app1Url + "/helloworld/"); |
| 163 | + System.out.println("Warming up " + app2Url + "/helloworld..."); |
| 164 | + Utils.sendGetRequest("http://" + app2Url + "/helloworld/"); |
| 165 | + System.out.println("Warming up " + app3Url + "..."); |
| 166 | + Utils.sendGetRequest("http://" + app3Url); |
| 167 | + System.out.println("Warming up " + app4Url + "..."); |
| 168 | + Utils.sendGetRequest("http://" + app4Url); |
| 169 | + ResourceManagerUtils.sleep(Duration.ofSeconds(5)); |
| 170 | + System.out.println("CURLing " + app1Url + "/helloworld..."); |
| 171 | + System.out.println(Utils.sendGetRequest("http://" + app1Url + "/helloworld/")); |
| 172 | + System.out.println("CURLing " + app2Url + "/helloworld..."); |
| 173 | + System.out.println(Utils.sendGetRequest("http://" + app2Url + "/helloworld/")); |
| 174 | + System.out.println("CURLing " + app3Url + "..."); |
| 175 | + System.out.println(Utils.sendGetRequest("http://" + app3Url)); |
| 176 | + System.out.println("CURLing " + app4Url + "..."); |
| 177 | + System.out.println(Utils.sendGetRequest("http://" + app4Url)); |
| 178 | + |
| 179 | + return true; |
| 180 | + } finally { |
| 181 | + try { |
| 182 | + System.out.println("Deleting Resource Group: " + rgName); |
| 183 | + azureResourceManager.resourceGroups().beginDeleteByName(rgName); |
| 184 | + System.out.println("Deleted Resource Group: " + rgName); |
| 185 | + } catch (NullPointerException npe) { |
| 186 | + System.out.println("Did not create any resources in Azure. No clean up is necessary"); |
| 187 | + } catch (Exception g) { |
| 188 | + g.printStackTrace(); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + /** |
| 193 | + * Main entry point. |
| 194 | + * @param args the parameters |
| 195 | + */ |
| 196 | + public static void main(String[] args) { |
| 197 | + try { |
| 198 | + |
| 199 | + //============================================================= |
| 200 | + // Authenticate |
| 201 | + |
| 202 | + final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE); |
| 203 | + final TokenCredential credential = new DefaultAzureCredentialBuilder() |
| 204 | + .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint()) |
| 205 | + .build(); |
| 206 | + |
| 207 | + AzureResourceManager azureResourceManager = AzureResourceManager |
| 208 | + .configure() |
| 209 | + .withLogLevel(HttpLogDetailLevel.BASIC) |
| 210 | + .authenticate(credential, profile) |
| 211 | + .withDefaultSubscription(); |
| 212 | + |
| 213 | + // Print selected subscription |
| 214 | + System.out.println("Selected subscription: " + azureResourceManager.subscriptionId()); |
| 215 | + |
| 216 | + runSample(azureResourceManager); |
| 217 | + } catch (Exception e) { |
| 218 | + System.out.println(e.getMessage()); |
| 219 | + e.printStackTrace(); |
| 220 | + } |
| 221 | + } |
| 222 | +} |
0 commit comments