UNPKG

@catladder/pipeline

Version:

Panter workflow for cloud CI/CD and DevOps

173 lines (172 loc) • 6.8 kB
"use strict"; var __assign = this && this.__assign || function () { __assign = Object.assign || function (t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = this && this.__rest || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __read = this && this.__read || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCreateScheduleScript = exports.getDeleteSchedulesScript = void 0; var bash_1 = require("../../../../bash"); var utils_1 = require("../../../../utils"); var cloudRunExecutionUrl_1 = require("../../utils/cloudRunExecutionUrl"); var createArgsString_1 = require("../../utils/createArgsString"); var jobName_1 = require("../../utils/jobName"); var cloudRunJobs_1 = require("../cloudRunJobs"); var common_1 = require("../common"); var getDeleteSchedulesScript = function (context) { var deployConfig = (0, common_1.getCloudRunDeployConfig)(context); var schedules = getSchedules(context); var argsString = (0, createArgsString_1.createArgsString)({ project: deployConfig.projectId, location: deployConfig.region }); return schedules.map(function (_a) { var fullName = _a.fullName; return ["".concat((0, common_1.gcloudSchedulerCmd)(), " jobs delete ").concat(fullName, " ").concat(argsString)]; }).flat(); }; exports.getDeleteSchedulesScript = getDeleteSchedulesScript; var getCreateScheduleScript = function (context) { var schedules = getSchedules(context); var _a = (0, common_1.getCloudRunDeployConfig)(context), location = _a.region, project = _a.projectId; return schedules.map(function (_a, jobIndex) { var _b; var fullName = _a.fullName, config = _a.config; var _c = getSchedulerArgs(config, context), uri = _c.uri, args = __rest(_c, ["uri"]); var argsString = (0, createArgsString_1.createArgsString)(__assign(__assign({ project: project, location: location, uri: "\"$current_job_uri\"" }, args), { schedule: "\"".concat(config.schedule, "\""), "max-retry-attempts": (_b = config.maxRetryAttempts) !== null && _b !== void 0 ? _b : 0 })); return [jobIndex === 0 ? "exist_scheduler_names=\"$(\n ".concat((0, common_1.gcloudSchedulerCmd)(), " jobs list --filter='httpTarget.uri ~ ").concat(context.env, ".*").concat(context.name, "' --format='value(name)' --limit=999 --location='").concat(location, "' --project='").concat(project, "'\n)\"") : null, "current_job_uri=\"".concat(uri, "\""), "current_scheduler_name=\"".concat(fullName, "\""), "if echo \"$exist_scheduler_names\" | grep -Fx \"$current_scheduler_name\" >/dev/null; then", " ".concat((0, common_1.gcloudSchedulerCmd)(), " jobs update http \"$current_scheduler_name\" ").concat(argsString), "else", " ".concat((0, common_1.gcloudSchedulerCmd)(), " jobs create http \"$current_scheduler_name\" ").concat(argsString), "fi"].filter(utils_1.notNil).join("\n"); }); }; exports.getCreateScheduleScript = getCreateScheduleScript; var getSchedulerArgs = function (scheduler, context) { var _a; if (scheduler.type === "job") { var _b = (0, common_1.getCloudRunDeployConfig)(context), projectId = _b.projectId, region = _b.region; var body = scheduler.args !== undefined ? { overrides: { containerOverrides: [((_a = scheduler.args) === null || _a === void 0 ? void 0 : _a.length) > 0 ? { args: scheduler.args } : { args: [], clearArgs: true // not sure why this is neeeded, but it is }] } } : null; return { uri: (0, cloudRunExecutionUrl_1.getCloudRunJobExecuteUrl)(scheduler.job, { appFullName: context.environment.fullName, projectId: projectId, region: region }), "message-body": body ? '"' + (0, bash_1.bashEscape)(JSON.stringify(body)) + '"' : undefined, "http-method": "POST", "oauth-service-account-email": "\"$GCLOUD_PROJECT_NUMBER-compute@developer.gserviceaccount.com\"" }; } if (scheduler.type === "http") { return { uri: scheduler.url, "message-body": scheduler.body, "http-method": scheduler.method, "oidc-service-account-email": "\"$GCLOUD_PROJECT_NUMBER-compute@developer.gserviceaccount.com\"" }; } throw new Error("Unknown scheduler type: ".concat(scheduler.type)); }; var getSchedules = function (context) { var legacyScheduleJobs = getLegacyJobSchedules(context); var schedules = getScheduledExecutes(context); return Object.entries(__assign(__assign({}, legacyScheduleJobs), schedules)).map(function (_a) { var _b = __read(_a, 2), name = _b[0], config = _b[1]; return { name: name, fullName: (0, jobName_1.getFullSchedulerName)(context, name), config: config }; }); }; var getLegacyJobSchedules = function (context) { var jobsWithNames = (0, cloudRunJobs_1.getCloudRunJobsWithNames)(context); return Object.fromEntries(jobsWithNames.filter(function (entry) { return entry.job.when === "schedule"; }).map(function (_a) { var _b = _a.job, maxRetryAttempts = _b.maxRetryAttempts, schedule = _b.schedule, jobName = _a.jobName; var schedulerName = jobName.concat("-scheduler"); return [schedulerName, { type: "job", job: jobName, maxRetryAttempts: maxRetryAttempts, schedule: schedule, when: "schedule" }]; })); }; var getScheduledExecutes = function (context) { var _a; var deployConfig = (0, common_1.getCloudRunDeployConfig)(context); return Object.fromEntries(Object.entries((_a = deployConfig.execute) !== null && _a !== void 0 ? _a : {}).flatMap(function (_a) { var _b = __read(_a, 2), key = _b[0], value = _b[1]; if (!value || value.when !== "schedule") { return []; } return [[key, value]]; })); };