@codification/cutwater-build-aws
Version:
Tasks for working with AWS.
135 lines • 6.2 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrepareLambdaImageContextTask = void 0;
const cutwater_build_core_1 = require("@codification/cutwater-build-core");
const PrepareImageContextTask_1 = require("@codification/cutwater-build-docker/lib/tasks/PrepareImageContextTask");
const cutwater_node_core_1 = require("@codification/cutwater-node-core");
const path_1 = require("path");
const CliUtils_1 = require("../support/CliUtils");
class PrepareLambdaImageContextTask extends PrepareImageContextTask_1.PrepareImageContextTask {
constructor(name = 'prepare-lambda-image-context', defaultConfig = {}) {
super(name, Object.assign({ nodeVersion: '18', spawn: cutwater_build_core_1.Spawn.create(), http: new cutwater_node_core_1.HttpService() }, defaultConfig));
}
toOptions(config) {
if (!config.options) {
return '';
}
return cutwater_build_core_1.TextUtils.combineToMultilineText(cutwater_build_core_1.NodeUtils.toArray(config.options));
}
processHandlerImageConfigs() {
const configs = cutwater_build_core_1.NodeUtils.toArray(this.config.imageConfigs, [
{
name: '',
handler: 'lambda.handler',
},
]);
configs.forEach((config) => {
if (!config.dockerfile) {
config.dockerfile = (0, path_1.resolve)(__dirname, PrepareLambdaImageContextTask.DEFAULT_DOCKERFILE);
}
});
}
toAwsCommandArgs(layer) {
return CliUtils_1.CliUtils.prepareArgs({
parameters: Object.assign({}, layer),
spawnOptions: { command: 'aws' },
}, { command: 'lambda', subCommand: 'get-layer-version' });
}
toLayerUrl(layer) {
return __awaiter(this, void 0, void 0, function* () {
const args = this.toAwsCommandArgs(layer);
this.log(`Requesting layer url with args: ${args}`);
return JSON.parse((yield this.config.spawn.execute({
logger: this.logger(),
command: 'aws',
quiet: false,
ignoreErrors: false,
cwd: this.system.cwd(),
args,
})).toString('utf-8')).Content.Location;
});
}
toLayerName(layer) {
return `${layer.layerName}-${layer.versionNumber}`;
}
toLayerFileReference(layer) {
return this.system.toFileReference((0, path_1.resolve)(this.cacheFolder().path, `${this.toLayerName(layer)}.zip`));
}
toLayerDirectoryReference(layer) {
return this.system.toFileReference((0, path_1.resolve)(this.contextDirectoryReference().path, 'layers', `${this.toLayerName(layer)}`));
}
downloadLayer(layer) {
return __awaiter(this, void 0, void 0, function* () {
const layerFile = this.toLayerFileReference(layer);
if (!layerFile.exists()) {
const url = yield this.toLayerUrl(layer);
yield this.config.http.downloadToFile(url, layerFile.path);
}
return layerFile;
});
}
findRequiredLayers() {
const rval = cutwater_build_core_1.NodeUtils.toArray(this.config.imageConfigs).reduce((rval, img) => {
cutwater_build_core_1.NodeUtils.toArray(img.layers).forEach((layer) => {
rval[`${this.toLayerName(layer)}`] = layer;
});
return rval;
}, {});
return Object.values(rval);
}
prepareLayers() {
return __awaiter(this, void 0, void 0, function* () {
this.system.mkdir(this.cacheFolder().path, true);
const layers = this.findRequiredLayers();
if (layers.length < 1) {
return;
}
yield Promise.all(layers.map((layer) => this.downloadLayer(layer).then((file) => {
const layerDir = (0, path_1.resolve)(this.toLayerDirectoryReference(layer).path);
this.system.mkdir(layerDir, true);
cutwater_node_core_1.IOUtils.unzip(file.path, layerDir);
})));
});
}
toLayersCopyCommands(imageContext) {
const layers = cutwater_build_core_1.NodeUtils.toArray(imageContext.imageConfig.layers);
if (layers.length < 1) {
return '';
}
const copyCommands = layers.map((layer) => `COPY layers/${this.toLayerName(layer)}/ /`);
return cutwater_build_core_1.TextUtils.combineToMultilineText(copyCommands);
}
processDockerfiles() {
this.imageContexts.forEach((context) => {
context.dockerfile.replaceTokens({
NODE_VERSION_TAG: this.config.nodeVersion,
HANDLER_NAME: context.imageConfig.handler,
LAYERS: this.toLayersCopyCommands(context),
OPTIONS: this.toOptions(context.imageConfig),
});
});
}
executeTask() {
const _super = Object.create(null, {
executeTask: { get: () => super.executeTask }
});
return __awaiter(this, void 0, void 0, function* () {
this.processHandlerImageConfigs();
yield _super.executeTask.call(this);
yield this.prepareLayers();
this.processDockerfiles();
});
}
}
exports.PrepareLambdaImageContextTask = PrepareLambdaImageContextTask;
PrepareLambdaImageContextTask.DEFAULT_DOCKERFILE = 'AwsLambdaDockerfile';
//# sourceMappingURL=PrepareLambdaImageContextTask.js.map