@catladder/cli
Version:
Panter cli tool for cloud CI/CD and DevOps
129 lines (128 loc) • 6.72 kB
JavaScript
;
var __importDefault = this && this.__importDefault || function (mod) {
return mod && mod.__esModule ? mod : {
"default": mod
};
};
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.commandCloudSqlProxy = void 0;
const pipeline_1 = require("../../../../pipeline/src/index.js");
const clipboardy_1 = __importDefault(require("clipboardy"));
const defineCommand_1 = require("../../core/defineCommand");
const getProjectConfig_1 = require("../../config/getProjectConfig");
const startProxy_1 = require("../../gcloud/cloudSql/startProxy");
const log_1 = require("../../utils/log");
const autocompletions_1 = require("../../apps/cli/commands/project/utils/autocompletions");
const availability_1 = require("../availability");
const getProxyInfoForKubernetes = async (io, context) => {
var _a, _b;
if (!(0, pipeline_1.isOfDeployType)((_a = context.deploy) === null || _a === void 0 ? void 0 : _a.config, "kubernetes")) {
throw new Error("unsupported");
}
const envVars = await (0, getProjectConfig_1.getEnvVarsResolved)(io, context.env, context.name);
const cloudSqlValues = (0, pipeline_1.createKubernetesCloudsqlBaseValues)(context);
const DB_PASSWORD = (_b = (envVars === null || envVars === void 0 ? void 0 : envVars.DB_PASSWORD) || (envVars === null || envVars === void 0 ? void 0 : envVars.POSTGRESQL_PASSWORD)) === null || _b === void 0 ? void 0 : _b.toString();
const DB_NAME = cloudSqlValues.cloudsql.fullDbName.toString();
const instanceName = cloudSqlValues.cloudsql.instanceConnectionName;
return {
instanceName,
DB_PASSWORD,
DB_NAME,
DB_USER: "postgres"
};
};
const getProxyInfoForCloudRun = async (io, context) => {
var _a, _b, _c, _d, _e, _f;
if (!(0, pipeline_1.isOfDeployType)((_a = context.deploy) === null || _a === void 0 ? void 0 : _a.config, "google-cloudrun") || !((_b = context.deploy) === null || _b === void 0 ? void 0 : _b.config.cloudSql)) {
const deployType = (0, pipeline_1.getDeployConfigType)((_c = context.deploy) === null || _c === void 0 ? void 0 : _c.config);
const errorMessage = deployType === "google-cloudrun" ? `DeployConfig is missing the cloudSql property` : `Unsupported DeployConfig type: ${deployType}`;
(0, log_1.logError)(io, errorMessage);
throw new Error(errorMessage);
}
const envVars = await (0, getProjectConfig_1.getEnvVarsResolved)(io, context.env, context.name);
return {
instanceName: (_d = context.deploy) === null || _d === void 0 ? void 0 : _d.config.cloudSql.instanceConnectionName,
DB_PASSWORD: (_e = envVars === null || envVars === void 0 ? void 0 : envVars.DB_PASSWORD) === null || _e === void 0 ? void 0 : _e.toString(),
DB_NAME: context.environment.envVars.DB_NAME.toString(),
DB_USER: (_f = envVars === null || envVars === void 0 ? void 0 : envVars.DB_USER) === null || _f === void 0 ? void 0 : _f.toString()
};
};
const getProxyInfo = async (io, context) => {
var _a, _b, _c, _d;
if ((0, pipeline_1.isOfDeployType)((_a = context.deploy) === null || _a === void 0 ? void 0 : _a.config, "kubernetes")) {
return getProxyInfoForKubernetes(io, context);
} else if ((0, pipeline_1.isOfDeployType)((_b = context.deploy) === null || _b === void 0 ? void 0 : _b.config, "google-cloudrun")) {
return getProxyInfoForCloudRun(io, context);
}
throw new Error(`unsupported environment: ${(_d = (_c = context.deploy) === null || _c === void 0 ? void 0 : _c.config) === null || _d === void 0 ? void 0 : _d.type}`);
};
const getDbUrl = ({
DB_PASSWORD,
DB_NAME,
DB_USER
}, localPort, additional = "") => `DATABASE_URL="postgresql://${DB_USER}:${DB_PASSWORD}@localhost:${localPort}/${DB_NAME}${additional}"`;
const getJdbcUrl = ({
DB_PASSWORD,
DB_NAME,
DB_USER
}, localPort) => `DATABASE_JDBC_URL="jdbc:postgresql://localhost:${localPort}/${DB_NAME}?schema=public&user=${DB_USER}&password=${DB_PASSWORD}"`;
exports.commandCloudSqlProxy = (0, defineCommand_1.defineCommand)({
name: "project cloudsql proxy",
description: "proxy to cloud sql db",
group: "project",
isAvailable: (0, availability_1.hasCloudSql)(),
inputs: {
envComponent: {
type: "string",
message: "environment:component",
positional: true,
choices: async () => (0, autocompletions_1.envAndComponents)()
},
localPort: {
type: "number",
message: "Local port: ",
default: 54320
}
},
execute: async ctx => {
const envComponent = await ctx.get("envComponent");
const {
env,
componentName
} = (0, getProjectConfig_1.parseChoice)(envComponent);
if (!componentName) {
(0, log_1.logWarning)(ctx, "need componentName");
return;
}
const context = await (0, getProjectConfig_1.getPipelineContextByChoice)(env, componentName);
if (env === "review") {
(0, log_1.logWarning)(ctx, "connection string does not include mr information on review environments");
}
const localPort = await ctx.get("localPort");
const proxyInfo = await getProxyInfo(ctx, context);
const psqlEnvVars = {
PGPASSWORD: proxyInfo.DB_PASSWORD,
PGUSER: proxyInfo.DB_USER,
PGDATABASE: proxyInfo.DB_NAME,
PGHOST: "localhost",
PGPORT: localPort
};
const lipbqEnvVarDeclarations = Object.entries(psqlEnvVars).map(([key, value]) => `${key}="${value}"`);
const dbUrl = getDbUrl(proxyInfo, String(localPort));
const jdbcUrl = getJdbcUrl(proxyInfo, String(localPort));
clipboardy_1.default.writeSync(["", "## CloudSQL proxy connection .env variables", dbUrl, jdbcUrl, "## DATABASE_URL with schema=public parameter (e. g. Prisma requires this):", `# ${getDbUrl(proxyInfo, String(localPort), `?schema=public`)}`, "## Env variables for libpq (e.g. psql client https://www.postgresql.org/docs/current/libpq-envars.html#LIBPQ-ENVARS)", ...lipbqEnvVarDeclarations, ""].join("\n"));
(0, log_1.logLines)(ctx, null, "Connection strings env variables DATABASE_URL and those for the 'psql' client (copied to your clipboard for .env file):", dbUrl, jdbcUrl, ...lipbqEnvVarDeclarations, null, "DATABASE_URL with schema=public parameter (e. g. Prisma requires this):", getDbUrl(proxyInfo, String(localPort), `?schema=public`), null);
try {
await (0, startProxy_1.startCloudSqlProxyInCurrentShell)({
instanceName: proxyInfo.instanceName,
localPort
});
} catch (error) {
if (!(error instanceof Error)) throw error;
if (error.message !== startProxy_1.ERROR_NOT_INSTALLED) throw error;
(0, log_1.logError)(ctx, error.message, null, "Please install the Cloud SQL Auth Proxy from:", `https://cloud.google.com/sql/docs/postgres/connect-auth-proxy#install`, null);
}
}
});