Skip to content

Commit

Permalink
デプロイ用のスクリプトを追加。
Browse files Browse the repository at this point in the history
  • Loading branch information
leak4mk0 committed Sep 23, 2017
1 parent 7e590da commit 005b279
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"build:script": "echo Build script.",
"build:docker": "npm shrinkwrap && node ./scripts/build-docker.js",
"push": "npm run push:gcr",
"push:gcr": "node ./scripts/push-gcr.js"
"push:gcr": "node ./scripts/push-gcr.js",
"deploy": "npm run deploy:gke",
"deploy:gke": "node ./scripts/deploy-gke.js"
},
"author": "leak4mk0",
"license": "WTFPL",
Expand Down
67 changes: 67 additions & 0 deletions scripts/deploy-gke.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const spawnSync = require('child_process').spawnSync;
const meta = require(`../package`);

process.chdir(__dirname);
process.chdir('..');

const GCR_SUBDOMAINS = ['us.', 'eu.', 'asia.', ''];

console.log('Get Kubernetes configuration.')
const kubeConfigRet = spawnSync('kubectl', ['config', 'current-context'], { encoding: 'utf8' });
if (kubeConfigRet.status) {
console.error('Error code:', kubeConfigRet.status);
return console.log();
}
const kubeConfigSplit = (kubeConfigRet.stdout || '').split('\n').join('').split('_');
const kubeContext = { project: kubeConfigSplit[1], zone: kubeConfigSplit[2], cluster: kubeConfigSplit[3] };
console.log('Kubernetes project:', kubeContext.project);
console.log('Kubernetes zone:', kubeContext.zone);
console.log('Kubernetes cluster:', kubeContext.cluster);
console.log();

console.log('Get Kubernetes deployments.')
const kubeDeploymentsRet = spawnSync('kubectl', ['get', 'deployments', '-o=json'], { encoding: 'utf8' });
if (kubeDeploymentsRet.status) {
console.error('Error code:', kubeDeploymentsRet.status);
return console.log();
}
const kubeDeployments = JSON.parse(kubeDeploymentsRet.stdout || '');
console.log();

console.log('Update Kubernetes deployments.');
const dockerImageSource = `${meta.name}:${meta.version}`;
const gcrUrl = GCR_SUBDOMAINS.find((s) => (kubeContext.zone.startsWith(s.replace(/\.$/, '')))) + 'gcr.io';
const dockerImageTarget = `${gcrUrl}/${kubeContext.project}/${dockerImageSource}`;
const dockerImageQuery = dockerImageTarget.replace(/:[^:]+$/, ':');
console.log('Docker image source:', dockerImageSource);
console.log('Docker image target:', dockerImageTarget);
console.log('Docker image query:', dockerImageQuery);
kubeDeployments.items.forEach((deployment) => {
deployment.spec.template.spec.containers.forEach((container) => {
if (!container.image.startsWith(dockerImageQuery)) {
return;
}
console.log('Deployment name:', deployment.metadata.name);
console.log('Container name:', container.name);
if (container.image !== dockerImageTarget) {
console.log('Update deployment image.');
const kubeUpdateArgs = [
'set', 'image', 'deployment', deployment.metadata.name, `${container.name}=${dockerImageTarget}`
];
const kubeUpdateRet = spawnSync('kubectl', kubeUpdateArgs, { stdio: 'inherit' });
if (kubeUpdateRet.status) {
console.warn('Error code:', kubeUpdateRet.status);
}
} else {
console.log('Restart pod.');
const labelKeys = Object.keys(deployment.spec.template.metadata.labels);
const labelValues = labelKeys.map((k) => (deployment.spec.template.metadata.labels[k]));
const kubeRestartArgs = ['delete', 'pod', '-l', `${labelKeys[0]}=${labelValues[0]}`];
const kubeRestartRet = spawnSync('kubectl', kubeRestartArgs, { stdio: 'inherit' });
if (kubeRestartRet.status) {
console.warn('Error code:', kubeRestartRet.status);
}
}
});
});
console.log();

0 comments on commit 005b279

Please sign in to comment.