-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathcontainer-image.ts
101 lines (90 loc) · 3.34 KB
/
container-image.ts
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
/* eslint-disable import/order */
import { Construct } from 'constructs';
import { ContainerDefinition } from './container-definition';
import { CfnTaskDefinition } from './ecs.generated';
import * as ecr from '../../aws-ecr';
import { DockerImageAsset, TarballImageAsset } from '../../aws-ecr-assets';
/**
* Constructs for types of container images
*/
export abstract class ContainerImage {
/**
* Reference an image on DockerHub or another online registry
*/
public static fromRegistry(name: string, props: RepositoryImageProps = {}) {
return new RepositoryImage(name, props);
}
/**
* Reference an image in an ECR repository
*/
public static fromEcrRepository(repository: ecr.IRepository, tag: string = 'latest') {
return new EcrImage(repository, tag);
}
/**
* Reference an image that's constructed directly from sources on disk.
*
* If you already have a `DockerImageAsset` instance, you can use the
* `ContainerImage.fromDockerImageAsset` method instead.
*
* @param directory The directory containing the Dockerfile
*/
public static fromAsset(directory: string, props: AssetImageProps = {}) {
return new AssetImage(directory, props);
}
/**
* Use an existing `DockerImageAsset` for this container image.
*
* @param asset The `DockerImageAsset` to use for this container definition.
*/
public static fromDockerImageAsset(asset: DockerImageAsset): ContainerImage {
return {
bind(_scope: Construct, containerDefinition: ContainerDefinition): ContainerImageConfig {
asset.repository.grantPull(containerDefinition.taskDefinition.obtainExecutionRole());
return {
imageName: asset.imageUri,
};
},
};
}
/**
* Use an existing tarball for this container image.
*
* Use this method if the container image has already been created by another process (e.g. jib)
* and you want to add it as a container image asset.
*
* @param tarballFile Absolute path to the tarball. You can use language-specific idioms (such as `__dirname` in Node.js)
* to create an absolute path based on the current script running directory.
*/
public static fromTarball(tarballFile: string): ContainerImage {
return {
bind(scope: Construct, containerDefinition: ContainerDefinition): ContainerImageConfig {
const asset = new TarballImageAsset(scope, 'Tarball', { tarballFile });
asset.repository.grantPull(containerDefinition.taskDefinition.obtainExecutionRole());
return {
imageName: asset.imageUri,
};
},
};
}
/**
* Called when the image is used by a ContainerDefinition
*/
public abstract bind(scope: Construct, containerDefinition: ContainerDefinition): ContainerImageConfig;
}
/**
* The configuration for creating a container image.
*/
export interface ContainerImageConfig {
/**
* Specifies the name of the container image.
*/
readonly imageName: string;
/**
* Specifies the credentials used to access the image repository.
*/
readonly repositoryCredentials?: CfnTaskDefinition.RepositoryCredentialsProperty;
}
// These imports have to be at the end to prevent circular imports
import { AssetImage, AssetImageProps } from './images/asset-image';
import { EcrImage } from './images/ecr';
import { RepositoryImage, RepositoryImageProps } from './images/repository';