forked from ibi-group/datatools-server
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Deployment.java
560 lines (458 loc) · 18.4 KB
/
Deployment.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package com.conveyal.datatools.manager.models;
import com.conveyal.datatools.manager.DataManager;
import com.conveyal.datatools.manager.persistence.DataStore;
import com.conveyal.datatools.manager.utils.StringUtils;
import com.conveyal.datatools.manager.utils.json.JsonManager;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.ByteStreams;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A deployment of (a given version of) OTP on a given set of feeds.
* @author mattwigway
*
*/
@JsonInclude(Include.ALWAYS)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Deployment extends Model implements Serializable {
private static final long serialVersionUID = 1L;
private static final Logger LOG = LoggerFactory.getLogger(Deployment.class);
private static DataStore<Deployment> deploymentStore = new DataStore<Deployment>("deployments");
public String name;
/** What server is this currently deployed to? */
public String deployedTo;
public Date dateCreated;
@JsonView(JsonViews.DataDump.class)
public String projectId;
@JsonView(JsonViews.UserInterface.class)
public Project getProject () {
return Project.get(projectId);
}
public void setProject (Project project) {
this.projectId = project.id;
}
@JsonView(JsonViews.DataDump.class)
public Collection<String> feedVersionIds;
/** All of the feed versions used in this deployment */
@JsonIgnore
public List<FeedVersion> getFullFeedVersions () {
ArrayList<FeedVersion> ret = new ArrayList<FeedVersion>(feedVersionIds.size());
for (String id : feedVersionIds) {
FeedVersion v = FeedVersion.get(id);
if (v != null)
ret.add(v);
else
LOG.error("Reference integrity error for deployment {} ({}), feed version {} does not exist", this.name, this.id, id);
}
return ret;
}
/** All of the feed versions used in this deployment, summarized so that the Internet won't break */
@JsonView(JsonViews.UserInterface.class)
public List<SummarizedFeedVersion> getFeedVersions () {
ArrayList<SummarizedFeedVersion> ret = new ArrayList<SummarizedFeedVersion>(feedVersionIds.size());
for (String id : feedVersionIds) {
FeedVersion v = FeedVersion.get(id);
// should never happen but can if someone monkeyed around with dump/restore
if (v != null)
ret.add(new SummarizedFeedVersion(FeedVersion.get(id)));
else
LOG.error("Reference integrity error for deployment {} ({}), feed version {} does not exist", this.name, this.id, id);
}
return ret;
}
public void setFeedVersions (Collection<FeedVersion> versions) {
feedVersionIds = new ArrayList<String>(versions.size());
for (FeedVersion version : versions) {
feedVersionIds.add(version.id);
}
}
// future use
public String osmFileId;
/** The commit of OTP being used on this deployment */
public String otpCommit;
/**
* The routerId of this deployment
*/
public String routerId;
/**
* If this deployment is for a single feed source, the feed source this deployment is for.
*/
public String feedSourceId;
/**
* Feed sources that had no valid feed versions when this deployment was created, and ergo were not added.
*/
@JsonInclude(Include.ALWAYS)
@JsonView(JsonViews.DataDump.class)
public Collection<String> invalidFeedSourceIds;
/**
* Get all of the feed sources which could not be added to this deployment.
*/
@JsonView(JsonViews.UserInterface.class)
@JsonInclude(Include.ALWAYS)
public List<FeedSource> getInvalidFeedSources () {
if (invalidFeedSourceIds == null)
return null;
ArrayList<FeedSource> ret = new ArrayList<FeedSource>(invalidFeedSourceIds.size());
for (String id : invalidFeedSourceIds) {
ret.add(FeedSource.get(id));
}
return ret;
}
/** Create a single-agency (testing) deployment for the given feed source */
public Deployment(FeedSource feedSource) {
super();
this.feedSourceId = feedSource.id;
this.setProject(feedSource.getProject());
this.dateCreated = new Date();
this.feedVersionIds = new ArrayList<String>();
DateFormat df = new SimpleDateFormat("yyyyMMdd");
this.name = StringUtils.getCleanName(feedSource.name) + "_" + df.format(dateCreated);
// always use the latest, no matter how broken it is, so we can at least see how broken it is
this.feedVersionIds.add(feedSource.getLatestVersionId());
this.routerId = StringUtils.getCleanName(feedSource.name) + "_" + feedSourceId;
this.deployedTo = null;
}
/** Create a new deployment plan for the given feed collection */
public Deployment(Project project) {
super();
this.feedSourceId = null;
this.setProject(project);
this.dateCreated = new Date();
this.feedVersionIds = new ArrayList<String>();
this.invalidFeedSourceIds = new ArrayList<String>();
FEEDSOURCE: for (FeedSource s : project.getProjectFeedSources()) {
// only include deployable feeds
if (s.deployable) {
FeedVersion latest = s.getLatest();
// find the newest version that can be deployed
while (true) {
if (latest == null) {
invalidFeedSourceIds.add(s.id);
continue FEEDSOURCE;
}
if (!latest.hasCriticalErrors()) {
break;
}
latest = latest.getPreviousVersion();
}
// this version is the latest good version
this.feedVersionIds.add(latest.id);
}
}
this.deployedTo = null;
}
/**
* Create an empty deployment, for use with dump/restore.
*/
public Deployment() {
// do nothing.
}
/** Get a deployment by ID */
public static Deployment get (String id) {
return deploymentStore.getById(id);
}
/** Save this deployment and commit it */
public void save () {
this.save(true);
}
/** Save this deployment */
public void save (boolean commit) {
if (commit)
deploymentStore.save(id, this);
else
deploymentStore.saveWithoutCommit(id, this);
}
/**
* Delete this deployment and everything that it contains.
*/
public void delete() {
deploymentStore.delete(this.id);
}
/** Dump this deployment to the given file
* @param output the output file
* @param includeOsm should an osm.pbf file be included in the dump?
* @param includeOtpConfig should OTP build-config.json and router-config.json be included?
*/
public void dump (File output, boolean includeManifest, boolean includeOsm, boolean includeOtpConfig) throws IOException {
// create the zipfile
ZipOutputStream out;
try {
out = new ZipOutputStream(new FileOutputStream(output));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
if (includeManifest) {
// save the manifest at the beginning of the file, for read/seek efficiency
ZipEntry manifestEntry = new ZipEntry("manifest.json");
out.putNextEntry(manifestEntry);
// create the json manifest
JsonManager<Deployment> jsonManifest = new JsonManager<Deployment>(Deployment.class,
JsonViews.UserInterface.class);
// this mixin gives us full feed validation results, not summarized
jsonManifest.addMixin(Deployment.class, DeploymentFullFeedVersionMixin.class);
byte[] manifest = jsonManifest.write(this).getBytes();
out.write(manifest);
out.closeEntry();
}
// write each of the GTFS feeds
for (FeedVersion v : this.getFullFeedVersions()) {
File feed = v.getGtfsFile();
FileInputStream in;
try {
in = new FileInputStream(feed);
} catch (FileNotFoundException e1) {
LOG.error("Could not retrieve file for {}", v.name);
throw new RuntimeException(e1);
}
ZipEntry e = new ZipEntry(feed.getName());
out.putNextEntry(e);
// copy the zipfile 100k at a time
int bufSize = 100 * 1024;
byte[] buff = new byte[bufSize];
int readBytes;
while (true) {
try {
readBytes = in.read(buff);
} catch (IOException e1) {
try {
in.close();
} catch (IOException e2) {
throw new RuntimeException(e2);
}
throw new RuntimeException(e1);
}
if (readBytes == -1)
// we've copied the whole file
break;
out.write(buff, 0, readBytes);
}
try {
in.close();
} catch (IOException e1) {
throw new RuntimeException(e1);
}
out.closeEntry();
}
if (includeOsm) {
// extract OSM and insert it into the deployment bundle
ZipEntry e = new ZipEntry("osm.pbf");
out.putNextEntry(e);
InputStream is = getOsmExtract(getProjectBounds());
ByteStreams.copy(is, out);
try {
is.close();
} catch (IOException e1) {
e1.printStackTrace();
}
out.closeEntry();
}
if (includeOtpConfig) {
// write build-config.json and router-config.json
Project proj = this.getProject();
if (proj.buildConfig != null) {
ZipEntry buildConfigEntry = new ZipEntry("build-config.json");
out.putNextEntry(buildConfigEntry);
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
byte[] buildConfig = mapper.writer().writeValueAsBytes(proj.buildConfig);
out.write(buildConfig);
out.closeEntry();
}
// TODO: remove branding url root here and from config.yml
String brandingUrlRoot = DataManager.getConfigPropertyAsText("application.data.branding_public");
OtpRouterConfig routerConfig = proj.routerConfig;
if (routerConfig == null && brandingUrlRoot != null) {
routerConfig = new OtpRouterConfig();
}
if (routerConfig != null) {
routerConfig.brandingUrlRoot = brandingUrlRoot;
ZipEntry routerConfigEntry = new ZipEntry("router-config.json");
out.putNextEntry(routerConfigEntry);
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
out.write(mapper.writer().writeValueAsBytes(routerConfig));
out.closeEntry();
}
}
out.close();
}
// Get OSM extract
public static InputStream getOsmExtract(Rectangle2D bounds) {
// call vex server
URL vexUrl = null;
try {
vexUrl = new URL(String.format(Locale.ROOT,"%s/%.6f,%.6f,%.6f,%.6f.pbf",
DataManager.getConfigPropertyAsText("OSM_VEX"),
bounds.getMinY(), bounds.getMinX(), bounds.getMaxY(), bounds.getMaxX()));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
LOG.info("Getting OSM extract at " + vexUrl.toString());
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) vexUrl.openConnection();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
conn.connect();
} catch (IOException e1) {
e1.printStackTrace();
}
InputStream is = null;
try {
is = conn.getInputStream();
} catch (IOException e1) {
e1.printStackTrace();
}
return is;
}
public static Rectangle2D getFeedVersionBounds(FeedVersion version) {
return null;
}
// Get the union of the bounds of all the feeds in this deployment
@JsonView(JsonViews.UserInterface.class)
public Rectangle2D getProjectBounds() {
Project proj = this.getProject();
if(proj.useCustomOsmBounds != null && proj.useCustomOsmBounds) {
Rectangle2D bounds = new Rectangle2D.Double(proj.osmWest, proj.osmSouth,
proj.osmEast - proj.osmWest, proj.osmNorth - proj.osmSouth);
return bounds;
}
List<SummarizedFeedVersion> versions = getFeedVersions();
if (versions.size() == 0)
return null;
Rectangle2D bounds = new Rectangle2D.Double();
boolean boundsSet = false;
// i = 1 because we've already included bounds 0
for (int i = 0; i < versions.size(); i++) {
SummarizedFeedVersion version = versions.get(i);
// set version bounds from validation result
if (version.validationResult != null && version.validationResult.bounds != null) {
if (!boundsSet) {
// set the bounds, don't expand the null bounds
bounds.setRect(versions.get(0).validationResult.bounds);
boundsSet = true;
} else {
bounds.add(version.validationResult.bounds);
}
} else {
LOG.warn("Feed version {} has no bounds", version.id);
}
}
// expand the bounds by (about) 10 km in every direction
double degreesPerKmLat = 360D / 40008;
double degreesPerKmLon =
// the circumference of the chord of the earth at this latitude
360 /
(2 * Math.PI * 6371 * Math.cos(Math.toRadians(bounds.getCenterY())));
double bufferKm = 10;
if(DataManager.hasConfigProperty("modules.deployment.osm_buffer_km")) {
bufferKm = DataManager.getConfigProperty("modules.deployment.osm_buffer_km").asDouble();
}
// south-west
bounds.add(new Point2D.Double(
// lon
bounds.getMinX() - bufferKm * degreesPerKmLon,
bounds.getMinY() - bufferKm * degreesPerKmLat
));
// north-east
bounds.add(new Point2D.Double(
// lon
bounds.getMaxX() + bufferKm * degreesPerKmLon,
bounds.getMaxY() + bufferKm * degreesPerKmLat
));
return bounds;
}
/**
* Commit changes to the datastore
*/
public static void commit () {
deploymentStore.commit();
}
/**
* Get all of the deployments.
*/
public static Collection<Deployment> getAll () {
return deploymentStore.getAll();
}
/**
* Get the deployment currently deployed to a particular server.
*/
public static Deployment getDeploymentForServerAndRouterId (String server, String routerId) {
for (Deployment d : getAll()) {
if (d.deployedTo != null && d.deployedTo.equals(server)) {
if ((routerId != null && routerId.equals(d.routerId)) || d.routerId == routerId) {
return d;
}
}
}
return null;
}
public String getOrganizationId () {
Project project = getProject();
return project == null ? null : project.organizationId;
}
/**
* A summary of a FeedVersion, leaving out all of the individual validation errors.
*/
public static class SummarizedFeedVersion {
public FeedValidationResultSummary validationResult;
public FeedSource feedSource;
public String id;
public Date updated;
public String previousVersionId;
public String nextVersionId;
public int version;
public SummarizedFeedVersion (FeedVersion version) {
this.validationResult = new FeedValidationResultSummary(version.validationResult);
this.feedSource = version.getFeedSource();
this.updated = version.updated;
this.id = version.id;
this.nextVersionId = version.getNextVersionId();
this.previousVersionId = version.getPreviousVersionId();
this.version = version.version;
}
}
/**
* A MixIn to be applied to this deployment, for generating manifests, so that full feed versions appear rather than
* summarized feed versions.
*
* Usually a mixin would be used on an external class, but since we are changing one thing about a single class, it seemed
* unnecessary to define a new view just for generating deployment manifests.
*/
public abstract static class DeploymentFullFeedVersionMixin {
@JsonIgnore
public abstract Collection<SummarizedFeedVersion> getFeedVersions();
@JsonProperty("feedVersions")
@JsonIgnore(false)
public abstract Collection<FeedVersion> getFullFeedVersions ();
}
}