-
Notifications
You must be signed in to change notification settings - Fork 24
/
AppYamlProjectStaging.java
304 lines (279 loc) · 11.7 KB
/
AppYamlProjectStaging.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Copyright 2016 Google LLC.
*
* 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.appengine.operations;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import com.google.cloud.tools.appengine.AppEngineException;
import com.google.cloud.tools.appengine.configuration.AppYamlProjectStageConfiguration;
import com.google.cloud.tools.io.FileUtil;
import com.google.cloud.tools.project.AppYaml;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.logging.Logger;
import javax.annotation.Nullable;
/** Application stager for app.yaml based applications before deployment. */
public class AppYamlProjectStaging {
private static final Logger log = Logger.getLogger(AppYamlProjectStaging.class.getName());
private static final String APP_YAML = "app.yaml";
@VisibleForTesting
static final ImmutableList<String> OTHER_YAMLS =
ImmutableList.of("cron.yaml", "dos.yaml", "dispatch.yaml", "index.yaml", "queue.yaml");
/**
* Stages an app.yaml based App Engine project for deployment. Copies app.yaml, the project
* artifact and any user defined extra files. Will also copy the Docker directory for flex
* projects.
*
* @param config Specifies artifacts and staging destination
* @throws AppEngineException When staging fails
*/
public void stageArchive(AppYamlProjectStageConfiguration config) throws AppEngineException {
Preconditions.checkNotNull(config);
Path stagingDirectory = config.getStagingDirectory();
if (!Files.exists(stagingDirectory)) {
throw new AppEngineException(
"Staging directory does not exist. Location: " + stagingDirectory);
}
if (!Files.isDirectory(stagingDirectory)) {
throw new AppEngineException(
"Staging location is not a directory. Location: " + stagingDirectory);
}
try {
String env = findEnv(config);
String runtime = findRuntime(config);
if ("flex".equals(env)) {
stageFlexibleArchive(config, runtime);
return;
}
if ("java11".equals(runtime)) {
boolean isJar = config.getArtifact().getFileName().toString().endsWith(".jar");
if (isJar) {
stageStandardArchive(config);
return;
}
if (hasCustomEntrypoint(config)) {
stageStandardBinary(config);
return;
}
// I cannot deploy non-jars without custom entrypoints
throw new AppEngineException(
"Cannot process application with runtime: java11."
+ " A custom entrypoint must be defined in your app.yaml for non-jar artifact: "
+ config.getArtifact().toString());
}
// I don't know how to deploy this
throw new AppEngineException(
"Cannot process application with runtime: "
+ runtime
+ (Strings.isNullOrEmpty(env) ? "" : " and env: " + env));
} catch (IOException ex) {
throw new AppEngineException(ex);
}
}
@VisibleForTesting
void stageFlexibleArchive(AppYamlProjectStageConfiguration config, @Nullable String runtime)
throws IOException, AppEngineException {
CopyService copyService = new CopyService();
copyDockerContext(config, copyService, runtime);
copyExtraFiles(config, copyService);
copyAppEngineContext(config, copyService);
copyArtifact(config, copyService);
}
@VisibleForTesting
void stageStandardArchive(AppYamlProjectStageConfiguration config)
throws IOException, AppEngineException {
CopyService copyService = new CopyService();
copyExtraFiles(config, copyService);
copyAppEngineContext(config, copyService);
copyArtifact(config, copyService);
copyArtifactJarClasspath(config, copyService);
}
@VisibleForTesting
void stageStandardBinary(AppYamlProjectStageConfiguration config)
throws IOException, AppEngineException {
CopyService copyService = new CopyService();
copyExtraFiles(config, copyService);
copyAppEngineContext(config, copyService);
copyArtifact(config, copyService);
}
@VisibleForTesting
@Nullable
static String findEnv(AppYamlProjectStageConfiguration config)
throws AppEngineException, IOException {
Path appEngineDirectory = config.getAppEngineDirectory();
if (appEngineDirectory == null) {
throw new AppEngineException("Invalid Staging Configuration: missing App Engine directory");
}
Path appYaml = appEngineDirectory.resolve(APP_YAML);
try (InputStream input = Files.newInputStream(appYaml)) {
return AppYaml.parse(input).getEnvironmentType();
}
}
@VisibleForTesting
@Nullable
static String findRuntime(AppYamlProjectStageConfiguration config)
throws IOException, AppEngineException {
// verify that app.yaml that contains runtime:java
Path appEngineDirectory = config.getAppEngineDirectory();
if (appEngineDirectory == null) {
throw new AppEngineException("Invalid Staging Configuration: missing App Engine directory");
}
Path appYaml = appEngineDirectory.resolve(APP_YAML);
try (InputStream input = Files.newInputStream(appYaml)) {
return AppYaml.parse(input).getRuntime();
}
}
@VisibleForTesting
static void copyDockerContext(
AppYamlProjectStageConfiguration config, CopyService copyService, @Nullable String runtime)
throws IOException, AppEngineException {
Path dockerDirectory = config.getDockerDirectory();
if (dockerDirectory != null) {
if (Files.exists(dockerDirectory)) {
if ("java".equals(runtime)) {
log.warning(
"WARNING: runtime 'java' detected, any docker configuration in "
+ dockerDirectory
+ " will be ignored. If you wish to specify a docker configuration, please use "
+ "'runtime: custom'.");
} else {
// Copy docker context to staging
if (!Files.isRegularFile(dockerDirectory.resolve("Dockerfile"))) {
throw new AppEngineException(
"Docker directory " + dockerDirectory + " does not contain Dockerfile.");
} else {
Path stagingDirectory = config.getStagingDirectory();
copyService.copyDirectory(dockerDirectory, stagingDirectory);
}
}
}
}
}
@VisibleForTesting
static void copyAppEngineContext(AppYamlProjectStageConfiguration config, CopyService copyService)
throws IOException, AppEngineException {
Path appYaml = config.getAppEngineDirectory().resolve(APP_YAML);
if (!Files.exists(appYaml)) {
throw new AppEngineException(APP_YAML + " not found in the App Engine directory.");
}
Path stagingDirectory = config.getStagingDirectory();
copyService.copyFileAndReplace(appYaml, stagingDirectory.resolve(APP_YAML));
}
@VisibleForTesting
static void copyExtraFiles(AppYamlProjectStageConfiguration config, CopyService copyService)
throws IOException, AppEngineException {
List<Path> extraFilesDirectories = config.getExtraFilesDirectory();
if (extraFilesDirectories == null) {
return;
}
for (Path extraFilesDirectory : extraFilesDirectories) {
if (!Files.exists(extraFilesDirectory)) {
throw new AppEngineException(
"Extra files directory does not exist. Location: " + extraFilesDirectory);
}
if (!Files.isDirectory(extraFilesDirectory)) {
throw new AppEngineException(
"Extra files location is not a directory. Location: " + extraFilesDirectory);
}
Path stagingDirectory = config.getStagingDirectory();
copyService.copyDirectory(extraFilesDirectory, stagingDirectory);
}
}
private static void copyArtifact(AppYamlProjectStageConfiguration config, CopyService copyService)
throws IOException, AppEngineException {
Path artifact = config.getArtifact();
if (Files.exists(artifact)) {
Path stagingDirectory = config.getStagingDirectory();
Path destination = stagingDirectory.resolve(artifact.getFileName());
copyService.copyFileAndReplace(artifact, destination);
} else {
throw new AppEngineException("Artifact doesn't exist at '" + artifact + "'.");
}
}
@VisibleForTesting
// Copies files referenced in "Class-Path" of Jar's MANIFEST.MF to the target directory. Assumes
// files are present at relative paths and that relative path should be preserved in the staged
// directory.
static void copyArtifactJarClasspath(
AppYamlProjectStageConfiguration config, CopyService copyService) throws IOException {
Path artifact = config.getArtifact();
Path targetDirectory = config.getStagingDirectory();
try (JarFile jarFile = new JarFile(artifact.toFile())) {
String jarClassPath =
jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
if (jarClassPath == null) {
return;
}
Iterable<String> classpathEntries = Splitter.onPattern("\\s+").split(jarClassPath.trim());
for (String classpathEntry : classpathEntries) {
// classpath entries are relative to artifact's position and relativeness should be
// preserved
// in the target directory
Path jarSrc = artifact.getParent().resolve(classpathEntry);
if (!Files.isRegularFile(jarSrc)) {
log.warning("Could not copy 'Class-Path' jar: " + jarSrc + " referenced in MANIFEST.MF");
continue;
}
Path jarTarget = targetDirectory.resolve(classpathEntry);
if (Files.exists(jarTarget)) {
log.fine(
"Overwriting 'Class-Path' jar: "
+ jarTarget
+ " with "
+ jarSrc
+ " referenced in MANIFEST.MF");
}
copyService.copyFileAndReplace(jarSrc, jarTarget);
}
}
}
@VisibleForTesting
// for non jar artifacts we want to ensure the entrypoint is custom
static boolean hasCustomEntrypoint(AppYamlProjectStageConfiguration config)
throws IOException, AppEngineException {
// verify that app.yaml that contains entrypoint:
if (config.getAppEngineDirectory() == null) {
throw new AppEngineException("Invalid Staging Configuration: missing App Engine directory");
}
Path appYamlFile = config.getAppEngineDirectory().resolve(APP_YAML);
try (InputStream input = Files.newInputStream(appYamlFile)) {
return AppYaml.parse(input).getEntrypoint() != null;
}
}
@VisibleForTesting
static class CopyService {
void copyDirectory(Path src, Path dest, List<Path> excludes) throws IOException {
FileUtil.copyDirectory(src, dest, excludes);
}
void copyDirectory(Path src, Path dest) throws IOException {
FileUtil.copyDirectory(src, dest);
}
void copyFileAndReplace(Path src, Path dest) throws IOException {
if (!Files.exists(dest.getParent())) {
Files.createDirectories(dest.getParent());
}
Files.copy(src, dest, REPLACE_EXISTING);
}
}
}