This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
/
AppEngineStandardPlugin.java
272 lines (240 loc) · 10.6 KB
/
AppEngineStandardPlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Copyright 2016 Google LLC. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.google.cloud.tools.gradle.appengine.standard;
import com.google.cloud.tools.appengine.operations.cloudsdk.CloudSdkNotFoundException;
import com.google.cloud.tools.gradle.appengine.core.AppEngineCorePluginConfiguration;
import com.google.cloud.tools.gradle.appengine.core.CloudSdkOperations;
import com.google.cloud.tools.gradle.appengine.core.DeployAllTask;
import com.google.cloud.tools.gradle.appengine.core.DeployExtension;
import com.google.cloud.tools.gradle.appengine.core.DeployTargetResolver;
import com.google.cloud.tools.gradle.appengine.core.DeployTask;
import com.google.cloud.tools.gradle.appengine.core.ToolsExtension;
import com.google.cloud.tools.gradle.appengine.util.GradleCompatibility;
import com.google.common.base.Strings;
import java.io.File;
import org.gradle.api.GradleException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.plugins.WarPlugin;
import org.gradle.api.tasks.bundling.War;
/** Plugin definition for App Engine standard environments. */
public class AppEngineStandardPlugin implements Plugin<Project> {
public static final String APP_ENGINE_STANDARD_TASK_GROUP = "App Engine Standard environment";
public static final String EXPLODE_WAR_TASK_NAME = "explodeWar";
public static final String STAGE_TASK_NAME = "appengineStage";
public static final String RUN_TASK_NAME = "appengineRun";
public static final String START_TASK_NAME = "appengineStart";
public static final String STOP_TASK_NAME = "appengineStop";
public static final String STAGED_APP_DIR_NAME = "staged-app";
public static final String DEV_APP_SERVER_OUTPUT_DIR_NAME = "dev-appserver-out";
public static final String STAGE_EXTENSION = "stage";
public static final String RUN_EXTENSION = "run";
private Project project;
private CloudSdkOperations cloudSdkOperations;
private AppEngineStandardExtension appengineExtension;
private AppEngineCorePluginConfiguration appEngineCorePluginConfiguration;
private RunExtension runExtension;
private StageStandardExtension stageExtension;
private File explodedWarDir;
@Override
public void apply(Project project) {
this.project = project;
project.getPluginManager().apply(WarPlugin.class);
appengineExtension =
project.getExtensions().create("appengine", AppEngineStandardExtension.class);
appengineExtension.createSubExtensions(project);
appEngineCorePluginConfiguration = new AppEngineCorePluginConfiguration();
appEngineCorePluginConfiguration.configureCoreProperties(
project, appengineExtension, APP_ENGINE_STANDARD_TASK_GROUP, true);
explodedWarDir = new File(project.getBuildDir(), "exploded-" + project.getName());
configureExtensions();
createExplodedWarTask();
createStageTask();
createRunTasks();
}
private void configureExtensions() {
// create the run extension and set defaults.
runExtension = appengineExtension.getRun();
runExtension.setStartSuccessTimeout(20);
runExtension.setServices(explodedWarDir);
runExtension.setServerVersion("1");
// create the stage extension and set defaults.
stageExtension = appengineExtension.getStage();
File defaultStagedAppDir = new File(project.getBuildDir(), STAGED_APP_DIR_NAME);
stageExtension.setSourceDirectory(explodedWarDir);
stageExtension.setStagingDirectory(defaultStagedAppDir);
project.afterEvaluate(
project -> {
// tools extension required to initialize cloudSdkOperations
ToolsExtension tools = appengineExtension.getTools();
try {
cloudSdkOperations =
new CloudSdkOperations(tools.getCloudSdkHome(), null, tools.getVerbosity());
} catch (CloudSdkNotFoundException ex) {
// this should be caught in AppEngineCorePluginConfig before it can ever reach here.
throw new GradleException("Could not find CloudSDK: ", ex);
}
DeployExtension deploy = appengineExtension.getDeploy();
if (deploy.getAppEngineDirectory() == null) {
deploy.setAppEngineDirectory(
new File(stageExtension.getStagingDirectory(), "WEB-INF/appengine-generated"));
}
DeployAllTask deployAllTask =
(DeployAllTask)
project
.getTasks()
.getByName(AppEngineCorePluginConfiguration.DEPLOY_ALL_TASK_NAME);
deployAllTask.setStageDirectory(stageExtension.getStagingDirectory());
deployAllTask.setDeployExtension(deploy);
DeployTask deployTask =
(DeployTask)
project.getTasks().getByName(AppEngineCorePluginConfiguration.DEPLOY_TASK_NAME);
deployTask.setDeployConfig(deploy);
deployTask.setAppYaml(stageExtension.getStagingDirectory().toPath().resolve("app.yaml"));
// configure the runExtension's project parameter
// assign the run projectId to the deploy projectId if none is specified
if (Strings.isNullOrEmpty(runExtension.getProjectId())) {
runExtension.setProjectId(deploy.getProjectId());
}
runExtension.setDeployTargetResolver(new DeployTargetResolver(cloudSdkOperations));
});
}
private void createExplodedWarTask() {
project
.getTasks()
.create(
EXPLODE_WAR_TASK_NAME,
ExplodeWarTask.class,
explodeWar -> {
explodeWar.setExplodedAppDirectory(explodedWarDir);
explodeWar.dependsOn(WarPlugin.WAR_TASK_NAME);
explodeWar.setGroup(APP_ENGINE_STANDARD_TASK_GROUP);
explodeWar.setDescription("Explode a war into a directory");
project.afterEvaluate(
project -> {
War war = (War) project.getTasks().getByPath(WarPlugin.WAR_TASK_NAME);
explodeWar.setWarFile(GradleCompatibility.getArchiveFile(war));
});
});
project.getTasks().getByName(BasePlugin.ASSEMBLE_TASK_NAME).dependsOn(EXPLODE_WAR_TASK_NAME);
}
private void createStageTask() {
project
.getTasks()
.withType(StageStandardTask.class)
.whenTaskAdded(
stageStandardTask ->
project.afterEvaluate(
ignored -> stageStandardTask.setAppCfg(cloudSdkOperations.getAppcfg())));
StageStandardTask stageTask =
project
.getTasks()
.create(
STAGE_TASK_NAME,
StageStandardTask.class,
stageTask1 -> {
stageTask1.setGroup(APP_ENGINE_STANDARD_TASK_GROUP);
stageTask1.setDescription(
"Stage an App Engine standard environment application for deployment");
stageTask1.dependsOn(BasePlugin.ASSEMBLE_TASK_NAME);
project.afterEvaluate(
project -> {
stageTask1.setStageStandardExtension(stageExtension);
});
});
// All deployment tasks depend on the stage task.
project
.getTasks()
.getByName(AppEngineCorePluginConfiguration.DEPLOY_TASK_NAME)
.dependsOn(stageTask);
project
.getTasks()
.getByName(AppEngineCorePluginConfiguration.DEPLOY_CRON_TASK_NAME)
.dependsOn(stageTask);
project
.getTasks()
.getByName(AppEngineCorePluginConfiguration.DEPLOY_DISPATCH_TASK_NAME)
.dependsOn(stageTask);
project
.getTasks()
.getByName(AppEngineCorePluginConfiguration.DEPLOY_DOS_TASK_NAME)
.dependsOn(stageTask);
project
.getTasks()
.getByName(AppEngineCorePluginConfiguration.DEPLOY_INDEX_TASK_NAME)
.dependsOn(stageTask);
project
.getTasks()
.getByName(AppEngineCorePluginConfiguration.DEPLOY_QUEUE_TASK_NAME)
.dependsOn(stageTask);
project
.getTasks()
.getByName(AppEngineCorePluginConfiguration.DEPLOY_ALL_TASK_NAME)
.dependsOn(stageTask);
}
private void createRunTasks() {
project
.getTasks()
.create(
RUN_TASK_NAME,
DevAppServerRunTask.class,
runTask -> {
runTask.setGroup(APP_ENGINE_STANDARD_TASK_GROUP);
runTask.setDescription("Run an App Engine standard environment application locally");
runTask.dependsOn(project.getTasks().findByName(BasePlugin.ASSEMBLE_TASK_NAME));
project.afterEvaluate(
project -> {
runTask.setRunConfig(runExtension);
runTask.setDevServers(cloudSdkOperations.getDevServers());
});
});
project
.getTasks()
.create(
START_TASK_NAME,
DevAppServerStartTask.class,
startTask -> {
startTask.setGroup(APP_ENGINE_STANDARD_TASK_GROUP);
startTask.setDescription(
"Run an App Engine standard environment application locally in the background");
startTask.dependsOn(project.getTasks().findByName(BasePlugin.ASSEMBLE_TASK_NAME));
project.afterEvaluate(
project -> {
startTask.setRunConfig(runExtension);
startTask.setDevServers(cloudSdkOperations.getDevServers());
startTask.setDevAppServerLoggingDir(
new File(project.getBuildDir(), DEV_APP_SERVER_OUTPUT_DIR_NAME));
});
});
project
.getTasks()
.create(
STOP_TASK_NAME,
DevAppServerStopTask.class,
stopTask -> {
stopTask.setGroup(APP_ENGINE_STANDARD_TASK_GROUP);
stopTask.setDescription(
"Stop a locally running App Engine standard environment application");
project.afterEvaluate(
project -> {
stopTask.setRunConfig(runExtension);
stopTask.setDevServers(cloudSdkOperations.getDevServers());
});
});
}
}