firebase-tools
Version:
Command-Line Interface for Firebase
168 lines (167 loc) • 6.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DockerHelper = void 0;
exports.listGcfPaths = listGcfPaths;
exports.deleteGcfArtifacts = deleteGcfArtifacts;
const error_1 = require("../../error");
const api_1 = require("../../api");
const logger_1 = require("../../logger");
const docker = require("../../gcp/docker");
async function retry(func) {
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const MAX_RETRIES = 3;
const INITIAL_BACKOFF = 100;
const TIMEOUT_MS = 10000;
let retry = 0;
while (true) {
try {
const timeout = new Promise((resolve, reject) => {
setTimeout(() => reject(new Error("Timeout")), TIMEOUT_MS);
});
return await Promise.race([func(), timeout]);
}
catch (err) {
logger_1.logger.debug("Failed docker command with error ", err);
retry += 1;
if (retry >= MAX_RETRIES) {
throw new error_1.FirebaseError("Failed to clean up artifacts", { original: err });
}
await sleep(Math.pow(INITIAL_BACKOFF, retry - 1));
}
}
}
function getHelper(cache, subdomain) {
if (!cache[subdomain]) {
cache[subdomain] = new DockerHelper(`https://${subdomain}.${(0, api_1.containerRegistryDomain)()}`);
}
return cache[subdomain];
}
async function listGcfPaths(projectId, locations, dockerHelpers = {}) {
if (!locations) {
locations = Object.keys(docker.GCR_SUBDOMAIN_MAPPING);
}
const invalidRegion = locations.find((loc) => !docker.GCR_SUBDOMAIN_MAPPING[loc]);
if (invalidRegion) {
throw new error_1.FirebaseError(`Invalid region ${invalidRegion} supplied`);
}
const locationsSet = new Set(locations);
const subdomains = new Set(Object.values(docker.GCR_SUBDOMAIN_MAPPING));
const failedSubdomains = [];
const listAll = [];
for (const subdomain of subdomains) {
listAll.push((async () => {
try {
return getHelper(dockerHelpers, subdomain).ls(`${projectId}/gcf`);
}
catch (err) {
failedSubdomains.push(subdomain);
logger_1.logger.debug(err);
const stat = {
children: [],
digests: [],
tags: [],
};
return Promise.resolve(stat);
}
})());
}
const gcfDirs = (await Promise.all(listAll))
.map((results) => results.children)
.reduce((acc, val) => [...acc, ...val], [])
.filter((loc) => locationsSet.has(loc));
if (failedSubdomains.length === subdomains.size) {
throw new error_1.FirebaseError("Failed to search all subdomains.");
}
else if (failedSubdomains.length > 0) {
throw new error_1.FirebaseError(`Failed to search the following subdomains: ${failedSubdomains.join(",")}`);
}
return gcfDirs.map((loc) => {
return `${docker.GCR_SUBDOMAIN_MAPPING[loc]}.${(0, api_1.containerRegistryDomain)()}/${projectId}/gcf/${loc}`;
});
}
async function deleteGcfArtifacts(projectId, locations, dockerHelpers = {}) {
if (!locations) {
locations = Object.keys(docker.GCR_SUBDOMAIN_MAPPING);
}
const invalidRegion = locations.find((loc) => !docker.GCR_SUBDOMAIN_MAPPING[loc]);
if (invalidRegion) {
throw new error_1.FirebaseError(`Invalid region ${invalidRegion} supplied`);
}
const subdomains = new Set(Object.values(docker.GCR_SUBDOMAIN_MAPPING));
const failedSubdomains = [];
const deleteLocations = locations.map((loc) => {
const subdomain = docker.GCR_SUBDOMAIN_MAPPING[loc];
try {
return getHelper(dockerHelpers, subdomain).rm(`${projectId}/gcf/${loc}`);
}
catch (err) {
failedSubdomains.push(subdomain);
logger_1.logger.debug(err);
}
});
await Promise.all(deleteLocations);
if (failedSubdomains.length === subdomains.size) {
throw new error_1.FirebaseError("Failed to search all subdomains.");
}
else if (failedSubdomains.length > 0) {
throw new error_1.FirebaseError(`Failed to search the following subdomains: ${failedSubdomains.join(",")}`);
}
}
class DockerHelper {
constructor(origin) {
this.cache = {};
this.client = new docker.Client(origin);
}
async ls(path) {
if (!(path in this.cache)) {
this.cache[path] = retry(() => this.client.listTags(path)).then((res) => {
return {
tags: res.tags,
digests: Object.keys(res.manifest),
children: res.child,
};
});
}
return this.cache[path];
}
async rm(path) {
let toThrowLater = undefined;
const stat = await this.ls(path);
const recursive = stat.children.map(async (child) => {
try {
await this.rm(`${path}/${child}`);
stat.children.splice(stat.children.indexOf(child), 1);
}
catch (err) {
toThrowLater = err;
}
});
const deleteTags = stat.tags.map(async (tag) => {
try {
await retry(() => this.client.deleteTag(path, tag));
stat.tags.splice(stat.tags.indexOf(tag), 1);
}
catch (err) {
logger_1.logger.debug("Got error trying to remove docker tag:", err);
toThrowLater = err;
}
});
await Promise.all(deleteTags);
const deleteImages = stat.digests.map(async (digest) => {
try {
await retry(() => this.client.deleteImage(path, digest));
stat.digests.splice(stat.digests.indexOf(digest), 1);
}
catch (err) {
logger_1.logger.debug("Got error trying to remove docker image:", err);
toThrowLater = err;
}
});
await Promise.all(deleteImages);
await Promise.all(recursive);
if (toThrowLater) {
throw toThrowLater;
}
}
}
exports.DockerHelper = DockerHelper;