UNPKG

@pulumi/awsx

Version:

[![Actions Status](https://github.com/pulumi/pulumi-awsx/workflows/master/badge.svg)](https://github.com/pulumi/pulumi-awsx/actions) [![Slack](http://www.pulumi.com/images/docs/badges/slack.svg)](https://slack.pulumi.com) [![NPM version](https://badge.fur

188 lines • 7.69 kB
"use strict"; // Copyright 2016-2018, Pulumi Corporation. // // 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. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Image = void 0; exports.computeImageFromAsset = computeImageFromAsset; const aws = __importStar(require("@pulumi/aws")); const docker = __importStar(require("docker-classic")); const pulumi = __importStar(require("@pulumi/pulumi")); const utils = __importStar(require("../utils")); class Image { static fromPath(nameOrRepository, path) { if (path === undefined) { throw new Error("'build' was undefined"); } return new AssetImage(nameOrRepository, path); } static fromDockerBuild(nameOrRepository, build) { if (build === undefined) { throw new Error("'path' was undefined"); } return new AssetImage(nameOrRepository, build); } /** * Creates an [Image] given function code to use as the implementation of the container. */ static fromFunction(func) { return new FunctionImage(func); } } exports.Image = Image; class FunctionImage extends Image { func; constructor(func) { super(); this.func = func; } image() { // TODO[pulumi/pulumi-cloud#85]: move this to a Pulumi Docker Hub account. return "lukehoban/nodejsrunner"; } environment() { return pulumi.runtime.serializeFunction(this.func).then(value => [{ name: "PULUMI_SRC", value: value.text, }]); } } class AssetImage extends Image { nameOrRepository; pathOrBuild; // Computed and cached on demand. imageResult; constructor(nameOrRepository, pathOrBuild) { super(); this.nameOrRepository = nameOrRepository; this.pathOrBuild = pulumi.output(pathOrBuild); } environment(name, parent) { return []; } image(name, parent) { if (!this.imageResult) { const repository = typeof this.nameOrRepository === "string" ? AssetImage.createRepository(this.nameOrRepository, { parent }) : this.nameOrRepository; const { repositoryUrl, registryId } = repository; this.imageResult = pulumi.all([this.pathOrBuild, repositoryUrl, registryId]) .apply(([pathOrBuild, repositoryUrl, registryId]) => computeImageFromAsset(pathOrBuild, repositoryUrl, registryId, parent)); } return this.imageResult; } // getOrCreateRepository returns the ECR repository for this image, lazily allocating if necessary. static createRepository(name, opts) { const repository = new aws.ecr.Repository(name.toLowerCase(), {}, opts); // Set a default lifecycle policy such that at most a single untagged image is retained. // We tag all cached build layers as well as the final image, so those images will never expire. const lifecyclePolicyDocument = { rules: [{ rulePriority: 10, description: "remove untagged images", selection: { tagStatus: "untagged", countType: "imageCountMoreThan", countNumber: 1, }, action: { type: "expire", }, }], }; const lifecyclePolicy = new aws.ecr.LifecyclePolicy(name.toLowerCase(), { policy: JSON.stringify(lifecyclePolicyDocument), repository: repository.name, }, opts); return repository; } } function getImageName(pathOrBuild) { // Produce a hash of the build context and use that for the image name. let buildSig; if (typeof pathOrBuild === "string") { buildSig = pathOrBuild; } else { buildSig = pathOrBuild.context || "."; if (pathOrBuild.dockerfile) { buildSig += `;dockerfile=${pathOrBuild.dockerfile}`; } if (pathOrBuild.args) { for (const arg of Object.keys(pathOrBuild.args)) { buildSig += `;arg[${arg}]=${pathOrBuild.args[arg]}`; } } } buildSig += pulumi.getStack(); return `${utils.sha1hash(buildSig)}-container`; } /** @internal */ function computeImageFromAsset(pathOrBuild, repositoryUrl, registryId, parent) { pulumi.log.debug(`Building container image at '${JSON.stringify(pathOrBuild)}'`, parent); const imageName = getImageName(pathOrBuild); // If we haven't, build and push the local build context to the ECR repository. Then return // the unique image name we pushed to. The name will change if the image changes ensuring // the TaskDefinition get's replaced IFF the built image changes. const uniqueImageName = docker.buildAndPushImage(imageName, pathOrBuild, repositoryUrl, parent, async () => { // Construct Docker registry auth data by getting the short-lived authorizationToken from ECR, and // extracting the username/password pair after base64-decoding the token. // // See: http://docs.aws.amazon.com/cli/latest/reference/ecr/get-authorization-token.html if (!registryId) { throw new Error("Expected registry ID to be defined during push"); } const credentials = await aws.ecr.getAuthorizationToken({ registryId: registryId }, { parent, async: true }); return { registry: credentials.proxyEndpoint, username: credentials.userName, password: credentials.password, }; }); uniqueImageName.apply((d) => pulumi.log.debug(` build complete: ${imageName} (${d})`, parent)); return uniqueImageName; } //# sourceMappingURL=image.js.map