Skip to content

Commit

Permalink
adds canIDeploy script
Browse files Browse the repository at this point in the history
  • Loading branch information
suraj-keshri committed Feb 25, 2020
1 parent b1c80ff commit d2f0a30
Show file tree
Hide file tree
Showing 11 changed files with 899 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/*
.git
.git
.env
14 changes: 14 additions & 0 deletions __tests__/deploy-kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
resources:
- ../../base

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: gcr.io/kollate-218719/auth-service
newTag: 4284bc991c815591eddaf6034d0dfc171fdfa77d
- name: gcr.io/kollate-218719/frontend
newTag: 27c177e79511aa74a43a5dd319274910c8a3cc1d
- name: gcr.io/kollate-218719/gateway
newTag: a4349c2c93535f9db51e73e701f6aadb229e8ac3
- name: gcr.io/kollate-218719/group-service
newTag: d45454cab2ff92c6e60e7af731639fbbfc3f79d9
45 changes: 45 additions & 0 deletions canIDeploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require("dotenv").config();
const path = require("path");
const { getPacticipant } = require("./getPacticipant");
const fs = require("fs");
const yaml = require("js-yaml");
const _ = require("lodash");
const pact = require("@pact-foundation/pact-node");

exports.canIDeploy = function({
kustomizeFile,
retryTimes,
retryInterval,
brokerUrl,
brokerToken
// verbose
}) {
const yamlFile = path.resolve(kustomizeFile);
const doc = yaml.safeLoad(fs.readFileSync(yamlFile, "utf8"));

const imageTagPairs = _.fromPairs(
doc.images.map(a => [getPacticipant(a.name), a.newTag])
);

const pacticipants = Object.entries(imageTagPairs).map(([name, version]) => ({
name,
version
}));

const canDeployOptions = {
pacticipants,
pactBroker: brokerUrl || process.env.PACT_BROKER_URL,
pactBrokerToken: brokerToken || process.env.PACT_BROKER_TOKEN,
retryWhileUnknown: retryTimes || 5,
retryInterval: retryInterval || 30
};

// console.log(!!verbose);

return pact
.canDeploy(canDeployOptions)
.then(console.log)
.catch(e => {
throw e;
});
};
17 changes: 17 additions & 0 deletions canIDeploy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const path = require("path");
const { canIDeploy } = require("./canIDeploy");

jest.setTimeout(60000);
describe("canIDeploy", () => {
test("canIDeploy", async () => {
const filePath = path.resolve(
__dirname,
"./__tests__/deploy-kustomization.yaml"
);
await canIDeploy({
kustomizeFile: filePath,
retryTimes: 1,
retryInterval: 10
});
});
});
44 changes: 44 additions & 0 deletions createHooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require("dotenv").config();

const { getPacticipant } = require("./getPacticipant");
const _ = require("lodash");
const util = require("util");
const exec = util.promisify(require("child_process").exec);

const JENKINS_USERNAME = process.env.JENKINS_USERNAME;
const JENKINS_USERTOKEN = process.env.JENKINS_USERTOKEN;
const JENKINS_HTTPS_URL = process.env.JENKINS_HTTPS_URL;
const PACT_BROKER_TOKEN = process.env.PACT_BROKER_TOKEN;
const PACT_BROKER_URL = process.env.PACT_BROKER_URL;

const providerMap = {
"auth-service": ["group-service"],
frontend: ["gateway"],
gateway: ["auth-service", "group-service"]
};

async function createWebhook(consumer, provider) {
const { stdout, stderr } = await exec(`
pact-broker create-webhook \
${JENKINS_HTTPS_URL}/job/${provider}/job/master/buildWithParameters?PACT_VERIFY=true \
--request=POST \
--contract-content-changed \
--consumer=${getPacticipant(consumer)} \
--provider=${getPacticipant(provider)} \
--user=${JENKINS_USERNAME}:${JENKINS_USERTOKEN} \
--broker-base-url="${PACT_BROKER_URL}" \
--broker-token="${PACT_BROKER_TOKEN}"
`);
if (stderr) throw stderr;
console.log(stdout);
}

(async () => {
await Promise.all(
_.flatten(
Object.entries(providerMap).map(([consumer, providers]) => {
return providers.map(p => createWebhook(consumer, p));
})
)
);
})();
12 changes: 12 additions & 0 deletions getPacticipant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const capitalize = require("capitalize");
const _ = require("lodash");

exports.getPacticipant = service =>
capitalize
.words(
_.last(service.split("/"))
.split("-")
.join(" ")
)
.split(" ")
.join("");
44 changes: 43 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const yargs = require("yargs");
const loadImages = require("./loadImages");
const getImageTag = require("./getImageTag");
const temp = require("./temp");
const { canIDeploy } = require("./canIDeploy");
// const temp = require("./temp");

// yargs.command(temp).help().argv;

Expand Down Expand Up @@ -42,6 +43,47 @@ yargs
);
}
)
.command(
"canIDeploy <filepath>",
"Check if the pacticipants in the kustomization file with tags pass the deployment test",
yargs => {
return yargs
.option("brokerToken", {
alias: "t",
type: "strring",
describe: "broker auth token"
})
.demand(["brokerToken"])
.option("brokerUrl", {
alias: "b",
type: "string",
describe: "pact broker url",
default: "https://crewhood.pact.dius.com.au"
})
.option("retryTimes", {
alias: "N",
type: "number",
describe:
"number of times pact broker should check if the pacticipants are compatible",
default: 5
})
.option("retryInterval", {
alias: "I",
type: "number",
describe: "interval (in seconds) between checks",
default: 30
});
},
argv => {
return canIDeploy({
kustomizeFile: argv.filepath,
retryInterval: argv.retryInterval,
retryTimes: argv.retryTimes,
brokerToken: argv.brokerToken,
brokerUrl: argv.brokerUrl
});
}
)
.demandCommand(1, "You must select one of the commands")
.help().argv;

Expand Down
Loading

0 comments on commit d2f0a30

Please sign in to comment.