1
- /**
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the MIT License. See License.txt in the project root for
4
- * license information.
5
- */
6
-
7
- package com .microsoft .azure .management .appservice .samples ;
8
-
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 .identity .DefaultAzureCredentialBuilder ;
10
+ import com .azure .resourcemanager .AzureResourceManager ;
11
+ import com .azure .resourcemanager .appservice .models .PricingTier ;
12
+ import com .azure .resourcemanager .appservice .models .WebApp ;
13
+ import com .azure .resourcemanager .containerregistry .models .AccessKeyType ;
14
+ import com .azure .resourcemanager .containerregistry .models .Registry ;
15
+ import com .azure .resourcemanager .containerregistry .models .RegistryCredentials ;
16
+ import com .azure .core .management .Region ;
17
+ import com .azure .core .management .profile .AzureProfile ;
18
+ import com .azure .resourcemanager .resources .fluentcore .utils .ResourceManagerUtils ;
19
+ import com .azure .resourcemanager .samples .DockerUtils ;
20
+ import com .azure .resourcemanager .samples .Utils ;
9
21
import com .github .dockerjava .api .DockerClient ;
10
22
import com .github .dockerjava .api .command .CreateContainerResponse ;
23
+ import com .github .dockerjava .api .command .PullImageResultCallback ;
11
24
import com .github .dockerjava .api .exception .NotFoundException ;
25
+ import com .github .dockerjava .api .model .AuthConfig ;
12
26
import com .github .dockerjava .api .model .Image ;
13
- import com .github .dockerjava .core .command .PullImageResultCallback ;
14
27
import com .github .dockerjava .core .command .PushImageResultCallback ;
15
- import com .microsoft .azure .management .Azure ;
16
- import com .microsoft .azure .management .appservice .PricingTier ;
17
- import com .microsoft .azure .management .appservice .WebApp ;
18
- import com .microsoft .azure .management .containerregistry .AccessKeyType ;
19
- import com .microsoft .azure .management .containerregistry .Registry ;
20
- import com .microsoft .azure .management .containerregistry .RegistryCredentials ;
21
- import com .microsoft .azure .management .resources .fluentcore .arm .Region ;
22
- import com .microsoft .azure .management .resources .fluentcore .utils .SdkContext ;
23
- import com .microsoft .azure .management .samples .DockerUtils ;
24
- import com .microsoft .azure .management .samples .Utils ;
25
- import com .microsoft .rest .LogLevel ;
26
- import okhttp3 .OkHttpClient ;
27
- import okhttp3 .Request ;
28
-
29
- import java .io .File ;
28
+
30
29
import java .io .IOException ;
30
+ import java .time .Duration ;
31
31
import java .util .Date ;
32
32
import java .util .List ;
33
- import java .util .concurrent .TimeUnit ;
34
33
35
34
/**
36
35
* Azure App Service sample for deploying from an Azure Container Registry.
44
43
*/
45
44
public class ManageLinuxWebAppWithContainerRegistry {
46
45
47
- private static OkHttpClient httpClient ;
48
-
49
46
/**
50
47
* Main function which runs the actual sample.
51
48
*
52
- * @param azure instance of the azure client
49
+ * @param azureResourceManager instance of the azure client
53
50
* @return true if sample runs successfully
54
51
*/
55
- public static boolean runSample (Azure azure ) {
56
- final String rgName = SdkContext .randomResourceName ("rgACR" , 15 );
57
- final String acrName = SdkContext .randomResourceName ("acrsample" , 20 );
58
- final String appName = SdkContext .randomResourceName ("webapp" , 20 );
52
+ public static boolean runSample (AzureResourceManager azureResourceManager ) throws IOException , InterruptedException {
53
+ final String rgName = Utils .randomResourceName (azureResourceManager , "rgACR" , 15 );
54
+ final String acrName = Utils .randomResourceName (azureResourceManager , "acrsample" , 20 );
55
+ final String appName = Utils .randomResourceName (azureResourceManager , "webapp" , 20 );
59
56
final String appUrl = appName + ".azurewebsites.net" ;
60
57
final Region region = Region .US_EAST ;
61
58
final String dockerImageName = "tomcat" ;
@@ -70,7 +67,7 @@ public static boolean runSample(Azure azure) {
70
67
71
68
Date t1 = new Date ();
72
69
73
- Registry azureRegistry = azure .containerRegistries ().define (acrName )
70
+ Registry azureRegistry = azureResourceManager .containerRegistries ().define (acrName )
74
71
.withRegion (region )
75
72
.withNewResourceGroup (rgName )
76
73
.withBasicSku ()
@@ -86,7 +83,7 @@ public static boolean runSample(Azure azure) {
86
83
// Create a Docker client that will be used to push/pull images to/from the Azure Container Registry
87
84
88
85
RegistryCredentials acrCredentials = azureRegistry .getCredentials ();
89
- DockerClient dockerClient = DockerUtils .createDockerClient (azure , rgName , region ,
86
+ DockerClient dockerClient = DockerUtils .createDockerClient (azureResourceManager , rgName , region ,
90
87
azureRegistry .loginServerUrl (), acrCredentials .username (), acrCredentials .accessKeys ().get (AccessKeyType .PRIMARY ));
91
88
92
89
//=============================================================
@@ -95,12 +92,13 @@ public static boolean runSample(Azure azure) {
95
92
96
93
dockerClient .pullImageCmd (dockerImageName )
97
94
.withTag (dockerImageTag )
95
+ .withAuthConfig (new AuthConfig ())
98
96
.exec (new PullImageResultCallback ())
99
- .awaitSuccess ();
97
+ .awaitCompletion ();
100
98
System .out .println ("List local Docker images:" );
101
99
List <Image > images = dockerClient .listImagesCmd ().withShowAll (true ).exec ();
102
100
for (Image image : images ) {
103
- System .out .format ("\t Found Docker image %s (%s)\ n " , image .getRepoTags ()[0 ], image .getId ());
101
+ System .out .format ("\t Found Docker image %s (%s)% n" , image .getRepoTags ()[0 ], image .getId ());
104
102
}
105
103
106
104
CreateContainerResponse dockerContainerInstance = dockerClient .createContainerCmd (dockerImageName + ":" + dockerImageTag )
@@ -111,9 +109,9 @@ public static boolean runSample(Azure azure) {
111
109
// Commit the new container
112
110
113
111
String privateRepoUrl = azureRegistry .loginServerUrl () + "/samples/" + dockerContainerName ;
114
- String dockerImageId = dockerClient .commitCmd (dockerContainerInstance .getId ())
115
- .withRepository (privateRepoUrl )
116
- .withTag ("latest" ).exec ();
112
+ dockerClient .commitCmd (dockerContainerInstance .getId ())
113
+ .withRepository (privateRepoUrl )
114
+ .withTag ("latest" ).exec ();
117
115
118
116
// We can now remove the temporary container instance
119
117
dockerClient .removeContainerCmd (dockerContainerInstance .getId ())
@@ -139,7 +137,7 @@ public static boolean runSample(Azure azure) {
139
137
140
138
System .out .println ("Creating web app " + appName + " in resource group " + rgName + "..." );
141
139
142
- WebApp app = azure .webApps ().define (appName )
140
+ WebApp app = azureResourceManager .webApps ().define (appName )
143
141
.withRegion (Region .US_WEST )
144
142
.withExistingResourceGroup (rgName )
145
143
.withNewLinuxPlan (PricingTier .STANDARD_S1 )
@@ -153,27 +151,23 @@ public static boolean runSample(Azure azure) {
153
151
154
152
// warm up
155
153
System .out .println ("Warming up " + appUrl + "..." );
156
- curl ("http://" + appUrl );
157
- SdkContext .sleep (5000 );
154
+ Utils . sendGetRequest ("http://" + appUrl );
155
+ ResourceManagerUtils .sleep (Duration . ofSeconds ( 5 ) );
158
156
System .out .println ("CURLing " + appUrl + "..." );
159
- System .out .println (curl ("http://" + appUrl ));
157
+ System .out .println (Utils . sendGetRequest ("http://" + appUrl ));
160
158
161
159
return true ;
162
- } catch (Exception f ) {
163
- System .out .println (f .getMessage ());
164
- f .printStackTrace ();
165
160
} finally {
166
161
try {
167
162
System .out .println ("Deleting Resource Group: " + rgName );
168
- azure .resourceGroups ().beginDeleteByName (rgName );
163
+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
169
164
System .out .println ("Deleted Resource Group: " + rgName );
170
165
} catch (NullPointerException npe ) {
171
166
System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
172
167
} catch (Exception g ) {
173
168
g .printStackTrace ();
174
169
}
175
170
}
176
- return false ;
177
171
}
178
172
179
173
/**
@@ -186,33 +180,24 @@ public static void main(String[] args) {
186
180
//=============================================================
187
181
// Authenticate
188
182
189
- final File credFile = new File (System .getenv ("AZURE_AUTH_LOCATION" ));
183
+ final AzureProfile profile = new AzureProfile (AzureEnvironment .AZURE );
184
+ final TokenCredential credential = new DefaultAzureCredentialBuilder ()
185
+ .authorityHost (profile .getEnvironment ().getActiveDirectoryEndpoint ())
186
+ .build ();
190
187
191
- Azure azure = Azure .configure ()
192
- .withLogLevel (LogLevel .BODY )
193
- .authenticate (credFile )
194
- .withDefaultSubscription ();
188
+ AzureResourceManager azureResourceManager = AzureResourceManager
189
+ .configure ()
190
+ .withLogLevel (HttpLogDetailLevel .BASIC )
191
+ .authenticate (credential , profile )
192
+ .withDefaultSubscription ();
195
193
196
194
// Print selected subscription
197
- System .out .println ("Selected subscription: " + azure .subscriptionId ());
195
+ System .out .println ("Selected subscription: " + azureResourceManager .subscriptionId ());
198
196
199
- runSample (azure );
197
+ runSample (azureResourceManager );
200
198
} catch (Exception e ) {
201
199
System .out .println (e .getMessage ());
202
200
e .printStackTrace ();
203
201
}
204
202
}
205
-
206
- private static String curl (String url ) {
207
- Request request = new Request .Builder ().url (url ).get ().build ();
208
- try {
209
- return httpClient .newCall (request ).execute ().body ().string ();
210
- } catch (IOException e ) {
211
- return null ;
212
- }
213
- }
214
-
215
- static {
216
- httpClient = new OkHttpClient .Builder ().readTimeout (1 , TimeUnit .MINUTES ).build ();
217
- }
218
- }
203
+ }
0 commit comments