Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): invalidate cloudfront cache when updating tileset information #554

Merged
merged 2 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions packages/cog/src/cli/basemaps/action.tileset.tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import {
CommandLineStringParameter,
} from '@rushstack/ts-command-line';
import { TileSetBaseAction } from './tileset.action';
import * as AWS from 'aws-sdk';
import { EPSG } from '@basemaps/geo';

// Coudfront has to be defined in us-east-1
const cloudFormation = new AWS.CloudFormation({ region: 'us-east-1' });
const cloudFront = new AWS.CloudFront({ region: 'us-east-1' });

export class TileSetUpdateTagAction extends TileSetBaseAction {
private version: CommandLineIntegerParameter;
Expand Down Expand Up @@ -62,10 +68,45 @@ export class TileSetUpdateTagAction extends TileSetBaseAction {

LogConfig.get().info({ version, tag, name, projection }, 'Tagging');

if (this.commit.value!) {
if (this.commit.value) {
await Aws.tileMetadata.TileSet.tag(name, projection, tag, version);
} else {
}
await this.invalidateCache(name, projection, tag);

if (!this.commit.value) {
LogConfig.get().warn('DryRun:Done');
}
}

async invalidateCache(name: string, projection: EPSG, tag: TileSetTag): Promise<void> {
const nameStr = tag == TileSetTag.Production ? name : `${name}@${tag}`;
const path = `/v1/tiles/${nameStr}/${projection}/*`;

const stackInfo = await cloudFormation.describeStacks({ StackName: 'Edge' }).promise();
if (stackInfo.Stacks?.[0].Outputs == null) {
LogConfig.get().warn('Unable to find cloud front distribution');
return;
}
const cloudFrontDomain = stackInfo.Stacks[0].Outputs.find((f) => f.OutputKey == 'CloudFrontDomain');

const cloudFrontDistributions = await cloudFront.listDistributions().promise();
const cf = cloudFrontDistributions.DistributionList?.Items?.find(
(f) => f.DomainName == cloudFrontDomain?.OutputValue,
);

if (cloudFrontDomain == null || cf == null) {
LogConfig.get().warn('Unable to find cloud front distribution');
return;
}

LogConfig.get().info({ path, cfId: cf.Id }, 'Invalidating');
if (this.commit.value) {
await cloudFront
.createInvalidation({
DistributionId: cf.Id,
InvalidationBatch: { Paths: { Quantity: 1, Items: [path] }, CallerReference: this.id },
})
.promise();
}
}
}
2 changes: 2 additions & 0 deletions packages/cog/src/cli/basemaps/tileset.action.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { CommandLineAction, CommandLineStringParameter, CommandLineIntegerParameter } from '@rushstack/ts-command-line';
import * as ulid from 'ulid';

export abstract class TileSetBaseAction extends CommandLineAction {
id = ulid.ulid();
tileSet: CommandLineStringParameter;
projection: CommandLineIntegerParameter;

Expand Down