@atomist/sdm
Version:
Atomist Software Delivery Machine SDK
85 lines • 3.52 kB
JavaScript
;
/*
* Copyright © 2020 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.syncRepoStartupListener = void 0;
const logger_1 = require("@atomist/automation-client/lib/util/logger");
const cluster = require("cluster");
const stringify = require("json-stringify-safe");
const createJob_1 = require("../../../api-helper/misc/job/createJob");
const fakeContext_1 = require("../../../api-helper/testsupport/fakeContext");
const modes_1 = require("../../../core/machine/modes");
const repo_1 = require("./repo");
const sync_1 = require("./sync");
/**
* If the SDM is registered with one or more workspaces and not
* running in local mode, query cortex for the sync repo and replace
* the sdm.k8s.options.sync.repo property with a RemoteRepoRef for
* that repo. If this is the cluster master, it will create a job to
* perform a sync. If this is the cluster master and the SDM
* configuration has a [[KubernetesSyncOptions]] with a positive value
* of `intervalMinutes`, it will set up an interval timer to apply the
* specs from the sync repo periodically.
*/
const syncRepoStartupListener = async (ctx) => {
var _a, _b;
if (modes_1.isInLocalMode()) {
return;
}
const sdm = ctx.sdm;
if (!((_b = (_a = sdm.configuration.sdm.k8s) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.sync)) {
return;
}
const repoRef = sdm.configuration.sdm.k8s.options.sync.repo;
if (!repoRef) {
logger_1.logger.debug("No k8s sync repo configuration provided");
return;
}
if (!repoRef.owner || !repoRef.repo) {
logger_1.logger.error(`Provided sync repo does not contain all required properties: ${stringify(repoRef)}`);
return;
}
if (!await repo_1.queryForScmProvider(sdm.configuration)) {
return;
}
if (!cluster.isMaster) {
return;
}
if (process.env.ATOMIST_ISOLATED_GOAL) {
return;
}
await sdmRepoSync(sdm);
const interval = sdm.configuration.sdm.k8s.options.sync.intervalMinutes;
if (interval && interval > 0) {
logger_1.logger.info(`Creating sync repo trigger every ${interval} minutes`);
setInterval(() => sdmRepoSync(sdm), interval * 60 * 1000);
}
return;
};
exports.syncRepoStartupListener = syncRepoStartupListener;
/**
* Create the trappings required for executing a command and then
* create a job to execute the [[kubernetesSync]] command.
*/
async function sdmRepoSync(sdm) {
const workspaceId = sdm.configuration.workspaceIds[0];
const context = fakeContext_1.fakeContext(workspaceId);
context.graphClient = sdm.configuration.graphql.client.factory.create(workspaceId, sdm.configuration);
logger_1.logger.info("Creating sync repo job");
await createJob_1.createJob({ command: sync_1.KubernetesSync, parameters: [{}] }, context);
return;
}
//# sourceMappingURL=startup.js.map