limgen
Version:
Infrastructure as Code generator
151 lines (150 loc) • 7.02 kB
JavaScript
"use strict";
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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppAzureAcs = void 0;
const pulumi = __importStar(require("@pulumi/pulumi"));
const azureNative = __importStar(require("@pulumi/azure-native"));
const docker_build = __importStar(require("@pulumi/docker-build"));
const deep_merge_1 = require("../utils/deep-merge");
const prefixed_1 = require("../utils/prefixed");
class AppAzureAcs extends pulumi.ComponentResource {
constructor(name = "App", args = {}, opts) {
super("custom:app:AppAzureAcs", name, {}, opts);
this._args = args;
this.resourceGroup = this._args.resourceGroup || new azureNative.resources.ResourceGroup(`ResourceGroup`, (0, deep_merge_1.deepMerge)({
resourceGroupName: (0, prefixed_1.prefixed)("rg"),
location: "CentralUS",
}, this._args.resourceGroupOptions));
// Create the registry if not provided
this.registry = this._args.registry || new azureNative.containerregistry.Registry(`Registry`, (0, deep_merge_1.deepMerge)({
resourceGroupName: this.resourceGroup.name,
name: (0, prefixed_1.prefixed)("acr"),
sku: {
name: "Basic",
},
adminUserEnabled: true,
}, this._args.containerAppOptions));
this.logAnalyticsWorkspace = this._args.logAnalyticsWorkspace || new azureNative.operationalinsights.Workspace(`LogAnalyticsWorkspace`, {
resourceGroupName: this.resourceGroup.name,
workspaceName: (0, prefixed_1.prefixed)("law"),
sku: {
name: "PerGB2018",
},
retentionInDays: 30,
});
const imageTag = pulumi.interpolate `${this.registry.loginServer}/${pulumi.getProject()}:latest`;
const imageArgs = {
tags: [imageTag],
context: {
location: '../../..'
},
cacheFrom: [{
registry: {
ref: imageTag,
},
}],
cacheTo: [{
inline: {},
}],
push: true,
platforms: ["linux/amd64"],
registries: [{
address: this.registry.loginServer,
}],
};
// Get the admin credentials for the Azure Container Registry
const credentials = pulumi.all([this.registry.name, this.resourceGroup.name]).apply(([registryName, rgName]) => azureNative.containerregistry.listRegistryCredentials({
resourceGroupName: rgName,
registryName: registryName,
}));
let adminUsername;
let adminPassword;
if (this.registry.adminUserEnabled) {
adminUsername = credentials.apply(creds => creds.username);
adminPassword = credentials.apply(creds => creds.passwords[0].value);
imageArgs.registries[0].username = adminUsername;
imageArgs.registries[0].password = adminPassword;
}
this.image = this._args.image || new docker_build.Image(`Image`, (0, deep_merge_1.deepMerge)(imageArgs, this._args.imageOptions));
// Get the shared key for the Log Analytics Workspace
const logAnalyticsSharedKey = pulumi.all([this.resourceGroup.name, this.logAnalyticsWorkspace.name]).apply(([rgName, workspaceName]) => azureNative.operationalinsights.getSharedKeys({
resourceGroupName: rgName,
workspaceName: workspaceName,
}).then(keys => keys.primarySharedKey));
// Create a Managed Environment for Azure Container Apps
this.managedEnv = this._args.managedEnv || new azureNative.app.ManagedEnvironment("managedEnv", (0, deep_merge_1.deepMerge)({
resourceGroupName: this.resourceGroup.name,
location: this.resourceGroup.location,
appLogsConfiguration: {
destination: "log-analytics",
logAnalyticsConfiguration: {
customerId: this.logAnalyticsWorkspace.customerId,
sharedKey: logAnalyticsSharedKey,
},
},
}, this._args.managedEnvOptions));
const containerAppArgs = {
dependsOn: [this.image],
name: (0, prefixed_1.prefixed)("app", 32),
resourceGroupName: this.resourceGroup.name,
managedEnvironmentId: this.managedEnv.id,
configuration: {
ingress: {
external: true,
targetPort: 3000,
},
},
template: {
containers: [{
name: pulumi.getProject(),
image: imageTag,
resources: {
cpu: 0.5,
memory: "1Gi",
},
}],
scale: {
minReplicas: 1,
maxReplicas: 5,
},
},
};
if (this.registry.adminUserEnabled) {
containerAppArgs.configuration.registries = [{
server: this.registry.loginServer,
username: adminUsername,
passwordSecretRef: "admin-password",
}];
containerAppArgs.configuration.secrets = [{
name: "admin-password",
value: adminPassword,
}];
}
this.app = new azureNative.app.ContainerApp((0, prefixed_1.prefixed)('app', 24), (0, deep_merge_1.deepMerge)(containerAppArgs, this._args.containerAppOptions));
this.containerAppUrl = pulumi.interpolate `https://${this.app.configuration.apply(config => { var _a; return (_a = config === null || config === void 0 ? void 0 : config.ingress) === null || _a === void 0 ? void 0 : _a.fqdn; })}`;
this.registerOutputs({});
}
}
exports.AppAzureAcs = AppAzureAcs;