@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
71 lines (70 loc) • 4.59 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.connectRegistry = void 0;
const log_1 = require("diginext-utils/dist/xconsole/log");
const fs_1 = require("fs");
const app_config_1 = require("../../app.config");
const plugins_1 = require("../../plugins");
const mask_sensitive_info_1 = require("../../plugins/mask-sensitive-info");
const digitalocean_1 = __importDefault(require("../providers/digitalocean"));
const gcloud_1 = __importDefault(require("../providers/gcloud"));
const docker_registry_1 = __importDefault(require("./docker-registry"));
const connectRegistry = async (registry, options) => {
var _a;
const { slug, provider, host } = registry;
const { filePath, token } = options || {};
let connectedRegistry;
const builderName = ((_a = options === null || options === void 0 ? void 0 : options.builder) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || app_config_1.Config.BUILDER.toUpperCase();
switch (provider) {
case "gcloud":
const { serviceAccount } = registry;
const serviceAccountFile = serviceAccount && !(0, mask_sensitive_info_1.isMasked)(serviceAccount) ? (0, plugins_1.createTmpFile)("gsa.json", serviceAccount) : filePath;
if (!serviceAccountFile)
throw new Error(`Service account file is required.`);
connectedRegistry = await gcloud_1.default.connectDockerRegistry({ ...options, filePath: serviceAccountFile, registry: slug, host });
if (connectedRegistry) {
(0, log_1.logSuccess)(`[CONTAINER REGISTRY] ✓ ${builderName}: Connected to Container Registry "${registry.name}".`);
}
else {
throw new Error(`[CONTAINER REGISTRY] ❌ ${builderName}: Failed to connect to this container registry (${registry.name}).`);
}
// delete temporary service account
if ((0, fs_1.existsSync)(serviceAccountFile))
(0, fs_1.unlink)(serviceAccountFile, (err) => err && (0, log_1.logError)(`[REGISTRY] Delete temporary file:`, err));
return connectedRegistry;
case "digitalocean":
const { apiAccessToken } = registry;
const key = apiAccessToken && !(0, mask_sensitive_info_1.isMasked)(apiAccessToken) ? apiAccessToken : token;
if (!key)
throw new Error(`API Access Token is required.`);
// const doAuthResult = await digitalocean.authenticate({ ...options, key });
// if (!doAuthResult) throw new Error(`Can't authenticate with Digital Ocean using this API access token.`);
connectedRegistry = await digitalocean_1.default.connectDockerRegistry({ ...options, key, registry: slug });
if (connectedRegistry) {
(0, log_1.logSuccess)(`[CONTAINER REGISTRY] ✓ ${builderName}: Connected to Container Registry "${registry.name}".`);
}
else {
throw new Error(`[CONTAINER REGISTRY] ❌ ${builderName}: Failed to connect to this container registry (${registry.name}).`);
}
return connectedRegistry;
case "dockerhub":
const { dockerUsername, dockerPassword } = registry;
const password = dockerPassword && !(0, mask_sensitive_info_1.isMasked)(dockerPassword) ? dockerPassword : token;
if (!password)
throw new Error(`Docker access token is required. Try again with:\n $ PASSWORD=<your_docker_password>\n $ dx registry connect --token=$PASSWORD`);
connectedRegistry = await docker_registry_1.default.connectDockerToRegistry({ username: dockerUsername, password: dockerPassword }, { workspaceId: options === null || options === void 0 ? void 0 : options.workspaceId, registry: slug });
if (connectedRegistry) {
(0, log_1.logSuccess)(`[CONTAINER REGISTRY] ✓ ${builderName}: Connected to Container Registry "${registry.name}".`);
}
else {
throw new Error(`[CONTAINER REGISTRY] ❌ ${builderName}: Failed to connect to this container registry (${registry.name}).`);
}
return connectedRegistry;
default:
throw new Error(`[CONTAINER REGISTRY] This container registry is not supported (${provider}), only "dockerhub", "gcloud" and "digitalocean" are supported.`);
}
};
exports.connectRegistry = connectRegistry;