Skip to content

Commit

Permalink
updates the scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
suraj-keshri committed Feb 18, 2020
1 parent 0115745 commit b1c80ff
Show file tree
Hide file tree
Showing 12 changed files with 4,933 additions and 156 deletions.
10 changes: 10 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 2017
},
"env": {
"node": true,
"jest": true
}
}
96 changes: 96 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const yargs = require("yargs");
const loadImages = require("../loadImages");
const getImageTag = require("../getImageTag");
// const temp = require("../temp");

// test("temp", done => {
// const parser = yargs.command(temp);
// parser.parse("get shit --banana=healthy", (err, argv, output) => {
// console.log(argv);
// done();
// });
// });

describe("load images", () => {
jest.setTimeout(60 * 1000);
test("Load all Images", async done => {
try {
loadImages({
yamlFile: __dirname + "/kustomization.yaml",
cb: done
});
} catch (e) {
console.log(e);
throw e;
}
});
test("Load images with skip", async done => {
try {
loadImages({
yamlFile: __dirname + "/kustomization.yaml",
cb: done,
skipImage: "hashicorp/http-echo"
});
} catch (e) {
console.log(e);
throw e;
}
});
});

// describe("loadImages parser", () => {
// test("loadImages file", async done => {
// const parser = yargs.command(loadImages);
// parser.parse("file", (err, argv, output) => {
// expect(argv.file).toEqual("file");
// expect(argv.kind).toEqual("kind");
// done();
// });
// });
// test("loadImages file --kind=kinder", async done => {
// const parser = yargs.command(loadImages).help();
// parser.parse("file --kind=kinder", (err, argv, output) => {
// expect(argv.file).toEqual("file");
// expect(argv.kind).toEqual("kinder");
// done();
// });
// });
// test("loadImages file --kind=kinder --skip=image", async done => {
// const parser = yargs.command(loadImages).help();
// parser.parse("file --kind=kinder --skip=image", (err, argv, output) => {
// expect(argv._).toEqual(["file"]);
// expect(argv.kind).toEqual("kinder");
// expect(argv.skip).toEqual("image");
// done();
// });
// });
// });

describe("get image", () => {
// test("get image parser", done => {
// const parser = yargs.command(getImageTag);
// parser.parse("file image", (err, argv, output) => {
// expect(argv._).toEqual(["file", "image"]);
// done();
// });
// });
test("Get image", () => {
try {
expect(
getImageTag({
filePath: __dirname + "/kustomization.yaml",
imageName: "hashicorp/http-echo"
})
).toBe("0.2.3");
expect(
getImageTag({
filePath: __dirname + "/kustomization.yaml",
imageName: "alpine"
})
).toBe("3.7");
} catch (e) {
console.log(e);
throw e;
}
});
});
10 changes: 10 additions & 0 deletions __tests__/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resources:
- ../../base

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: alpine
newTag: 3.7
- name: hashicorp/http-echo
newTag: 0.2.3
36 changes: 36 additions & 0 deletions getImageTag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const path = require("path");
const yaml = require("js-yaml");
const fs = require("fs");
const _ = require("lodash");

function getImage({ filePath, imageName }) {
const yamlFile = path.resolve(filePath);
const doc = yaml.safeLoad(fs.readFileSync(yamlFile, "utf8"));
const images = _.fromPairs(doc.images.map(a => [a.name, a.newTag]));
if (!images[imageName]) {
throw new Error(`Image ${imageName} not found`);
}
return images[imageName].toString();
}

module.exports = getImage;
// exports.handler = argv => {
// getImage({ filePath: argv._[0], imageName: argv._[1] });
// };

// exports.command = "<filename> <imageName>";
// exports.describe =
// "get the tag of the imageName in the kustomization file <filename>";
// module.exports = getImage;

// if (require.main === module) {
// console.log(
// getImage("../gitops/overlays/prod-images/kustomization.yaml", "frontend")
// );
// console.log(
// getImage(
// "../gitops/overlays/prod-images/kustomization.yaml",
// "doesnot-exist"
// )
// );
// }
55 changes: 55 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const yargs = require("yargs");
const loadImages = require("./loadImages");
const getImageTag = require("./getImageTag");
const temp = require("./temp");

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

yargs
.command(
"load <filepath>",
"kustomization filename with image tags to load to kind",
yargs => {
return yargs
.option("kind", {
alias: "k",
type: "string",
describe: "the kind cluster to load file to",
default: "kind"
})
.option("skip", {
type: "string",
describe: "what image names to skip",
default: ""
});
},
({ filepath, skip, kind }) => {
loadImages({ yamlFile: filepath, skipImage: skip, kindCluster: kind });
}
)
.command(
"tag <filepath> <imageName>",
"get the image",
yargs => {
return yargs;
},
argv => {
console.log(
getImageTag({
filePath: argv.filepath,
imageName: argv.imageName
})
);
}
)
.demandCommand(1, "You must select one of the commands")
.help().argv;

// yargs
// .demandCommand(
// 2,
// "You must provide the kustomization filename with image for which you want the tag"
// )
// .help().argv;

// console.log(`kindCluster: ${kindCluster}; parallel: ${parallel}`);
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
watchPlugins: [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname"
]
};
63 changes: 0 additions & 63 deletions load-docker-kind.js

This file was deleted.

85 changes: 85 additions & 0 deletions loadImages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
const path = require("path");
const { exec } = require("child_process");
const fs = require("fs");
const yaml = require("js-yaml");

function loadImages({
yamlFile,
skipImage = "",
kindCluster = "kind",
cb = () => {}
} = {}) {
try {
const parallel = true;
const doc = yaml.safeLoad(fs.readFileSync(path.resolve(yamlFile), "utf8"));
const images = doc.images
.filter(a => a.name !== skipImage)
.map(a => `${a.name}:${a.newTag}`);
console.log(`⌛pulling images: ${images.join("\n")}`);
const pullCommand = images.map(a => `docker pull ${a}`).join(";");
const loadCommand = images
.map(a => `kind load docker-image --name=${kindCluster} ${a}`)
.join(parallel ? " & " : " && ")
.concat(parallel ? " & wait" : "");
exec(pullCommand, (error, stdout, stderr) => {
if (error) {
console.log(error);
throw error;
}
console.log("🎰 Images pull complete");
console.log("👷 loading images: ", loadCommand);
exec(loadCommand, err1 => {
if (err1) {
console.log(err1);
throw error;
}
console.log("🙋 Load complete");
cb();
});
});
} catch (e) {
console.log(e);
throw e;
}
}

module.exports = loadImages;
// module.exports = {
// name: "load",
// command: "load filename",
// description: "load images to kind cluster",
// options: {
// kind: {
// required: false,
// default: "kind",
// type: "string"
// }
// },
// callback: console.log
// };
// exports.handler = argv => {
// loadImages({ filePath: argv._[0], imageName: argv._[1] });
// };

// exports.options = {
// kind: {

// }
// }
// exports.options = y => {
// y.option("kind", {
// default: "kind"
// });
// y.option("skip", {
// default: ""
// });
// y.option("clusterName", {
// default: ""
// });
// return y;
// };

// exports.command =
// "loadToKind <filename> --kind=[clusterName] --skip=[skipImage]";
// exports.describe =
// "kustomization <filename> with image tags to load to kind cluster [cluster-name] and skipping image [imageName]";
Loading

0 comments on commit b1c80ff

Please sign in to comment.