Skip to content

Commit

Permalink
Merge pull request #11 from totem/develop
Browse files Browse the repository at this point in the history
0.6.0 : Hipchat notification
  • Loading branch information
sukrit007 committed Feb 19, 2015
2 parents d707a55 + e2f8843 commit af4f0e6
Show file tree
Hide file tree
Showing 16 changed files with 752 additions and 314 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ might be removed in future releases.
| ETCD_TOTEM_BASE | Base path for totem configurations | /totem |
| HOOK_POST_URL | URL to be used for post build notification | |
| HOOK_SECRET | Secret used for github post hook and post build notification |changeit|
| HIPCHAT_TOKEN | Hipchat room notification token to be used for failed build notification ||
| HIPCHAT_ROOM | Hipchat room to be used for failed build notification ||
| BASE_URL | Base Url for Image Factory. Used for forming notification URLs | http://localhost:8080|
| DOCKER_REPO_BASE | Docker base repository url (e.g: quay.io/myorg)| quay.io/totem |


## Prerequisites (Development)
Expand Down
24 changes: 4 additions & 20 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ This document describes the API endpoints available for the Image Factory. With
},
"repo": {
"type": "string"
},
"realm": {
"type": "string"
}
},
"required": ["owner", "repo"]
Expand Down Expand Up @@ -93,8 +90,7 @@ This will return a list of all current and past Job's know by the Image Factory.
"owner": "meltmedia",
"repo": "cdp-spec-java",
"branch": "develop",
"commit": "c95fa1de94c7bee554a97f31a14a2f7de92133ee",
"realm": "dev"
"commit": "c95fa1de94c7bee554a97f31a14a2f7de92133ee"
},
"image": "u.melt.sh/meltmedia/cdp-spec-java:c95fa1de94c7bee554a97f31a14a2f7de92133ee",
"status": "running",
Expand Down Expand Up @@ -140,8 +136,7 @@ Initiate a Job by requesting a new build to occur.
"owner": "meltmedia",
"repo": "cdp-spec-java",
"branch": "develop",
"commit": "c95fa1de94c7bee554a97f31a14a2f7de92133ee",
"realm": "dev"
"commit": "c95fa1de94c7bee554a97f31a14a2f7de92133ee"
}
```

Expand All @@ -163,9 +158,6 @@ Initiate a Job by requesting a new build to occur.
},
"repo": {
"type": "string"
},
"realm": {
"type": "string"
}
},
"required": ["owner", "repo"]
Expand All @@ -189,8 +181,7 @@ Initiate a Job by requesting a new build to occur.
"owner": "meltmedia",
"repo": "cdp-spec-java",
"branch": "develop",
"commit": "c95fa1de94c7bee554a97f31a14a2f7de92133ee",
"realm": "dev"
"commit": "c95fa1de94c7bee554a97f31a14a2f7de92133ee"
},
"image": "u.melt.sh/meltmedia/cdp-spec-java:c95fa1de94c7bee554a97f31a14a2f7de92133ee",
"status": "running",
Expand Down Expand Up @@ -237,9 +228,6 @@ Initiate a Job by requesting a new build to occur.
},
"repo": {
"type": "string"
},
"realm": {
"type": "string"
}
},
"required" : ["owner", "repo"]
Expand Down Expand Up @@ -351,8 +339,7 @@ This will return the Job corisponding to the `id` provided.
"owner": "meltmedia",
"repo": "cdp-spec-java",
"branch": "develop",
"commit": "c95fa1de94c7bee554a97f31a14a2f7de92133ee",
"realm": "dev"
"commit": "c95fa1de94c7bee554a97f31a14a2f7de92133ee"
},
"image": "u.melt.sh/meltmedia/cdp-spec-java:c95fa1de94c7bee554a97f31a14a2f7de92133ee",
"status": "running",
Expand Down Expand Up @@ -399,9 +386,6 @@ This will return the Job corisponding to the `id` provided.
},
"repo": {
"type": "string"
},
"realm": {
"type": "string"
}
},
"required" : ["owner", "repo"]
Expand Down
6 changes: 4 additions & 2 deletions bin/supervisord-wrapper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ export ETCD_TOTEM_BASE='${ETCD_TOTEM_BASE:-/totem}'
export SSH_HOST_KEY='${SSH_HOST_KEY:-/root/.ssh/id_rsa}'
export ENC_PASSPHRASE='${ENC_PASSPHRASE:-password}'
export CLUSTER_NAME='${CLUSTER_NAME:-totem-local}'
export QUAY_ORGANIZATION='${QUAY_ORGANIZATION:-totem}'
export QUAY_PREFIX='${QUAY_PREFIX:-totem-}'
export DOCKER_REPO_BASE='${DOCKER_REPO_BASE:-quay.io/totem}'
export HOOK_SECRET='${HOOK_SECRET:-changeit}'
export HOOK_POST_URL='${HOOK_POST_URL}'
export BASE_URL='${BASE_URL:-http://172.17.42.1:8080}'
export HIPCHAT_TOKEN='${HIPCHAT_TOKEN}'
export HIPCHAT_ROOM='${HIPCHAT_ROOM}'
export SWF_ENABLED='${SWF_ENABLED:-false}'
export AWS_ACCESS_KEY_ID='${AWS_ACCESS_KEY_ID}'
export AWS_SECRET_ACCESS_KEY='${AWS_SECRET_ACCESS_KEY}'
Expand Down
26 changes: 2 additions & 24 deletions config/config-docker.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
{
"port": 8080,
"defaults": {
"realm": "not-production",
"branch": "develop",
"commit": "HEAD"
},
"realms": {
"production": {
"repository": "quay.io/"
},
"not-production": {
"repository": "quay.io/"
},
"totem-development": {
"repository": "quay.io/"
},
"local": {
"repository": "registry.totem.local/"
},
"public": {
"repository": ""
}
},
"tasks": {
"clone": {
"cmd": "git clone --depth 50 --recurse-submodules --branch {{branch}} git@github.com:{{owner}}/{{repo}}.git ./",
Expand All @@ -35,12 +13,12 @@
"retries": 0
},
"build": {
"cmd": "docker build --rm -t {{dockerRepo}}totem/{{owner}}-{{repo}}:{{tag}} ./",
"cmd": "docker build --rm -t {{dockerRepoBase}}/{{owner}}-{{repo}}:{{tag}} ./",
"timeout": "1800000",
"retries": 0
},
"push": {
"cmd": "docker push {{dockerRepo}}totem/{{owner}}-{{repo}}:{{tag}}",
"cmd": "docker push {{dockerRepoBase}}/{{owner}}-{{repo}}:{{tag}}",
"timeout": "1800000",
"retries": 20,
"retryWait": 20000
Expand Down
10 changes: 0 additions & 10 deletions config/config-test.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
{
"port": 8080,
"defaults": {
"realm": "dev",
"branch": "develop",
"commit": "HEAD"
},
"realms": {
"dev": {
"repository": "localhost"
}
},
"tasks": {
"clone": {
"cmd": "true",
Expand Down
26 changes: 2 additions & 24 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
{
"port": 8080,
"defaults": {
"realm": "not-production",
"branch": "develop",
"commit": "HEAD"
},
"realms": {
"production": {
"repository": "quay.io/"
},
"not-production": {
"repository": "quay.io/"
},
"totem-development": {
"repository": "quay.io/"
},
"local": {
"repository": "registry.totem.local/"
},
"public": {
"repository": ""
}
},
"tasks": {
"clone": {
"cmd": "git clone --depth 50 --recurse-submodules --branch {{branch}} git@github.com:{{owner}}/{{repo}}.git ./",
Expand All @@ -35,12 +13,12 @@
"retries": 0
},
"build": {
"cmd": "docker -H tcp://127.0.0.1:4243 build --rm -t {{dockerRepo}}totem/{{owner}}-{{repo}}:{{tag}} ./",
"cmd": "docker -H tcp://127.0.0.1:4243 build --rm -t {{dockerRepoBase}}/{{owner}}-{{repo}}:{{tag}} ./",
"timeout": "1800000",
"retries": 0
},
"push": {
"cmd": "docker -H tcp://127.0.0.1:4243 push {{dockerRepo}}totem/{{owner}}-{{repo}}:{{tag}}",
"cmd": "docker -H tcp://127.0.0.1:4243 push {{dockerRepoBase}}/{{owner}}-{{repo}}:{{tag}}",
"timeout": "1800000",
"retries": 20,
"retryWait": 20000
Expand Down
64 changes: 31 additions & 33 deletions lib/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ var winston = require('winston'),
util = require('util'),
zSchema = require('z-schema'),
path = require('path'),
PostHookNotifier = require('./posthook.js'),
PostHookNotifier = require('./notification/posthook'),
HipchatNotifier = require('./notification/hipchat'),
nunjucks = require('nunjucks'),
_ = require('lodash');

nunjucks.configure('templates', { autoescape: true });

var DEFAULT_CLONE_CMD = 'git clone --depth 100 --recurse-submodules --branch {{branch}} git@github.com:{{owner}}/{{repo}}.git ./',
DEFAULT_CHECKOUT_CMD = 'git checkout -f {{commit}}',
DEFAULT_BUILD_CMD = 'docker build -t {{dockerRepo}}/{{owner}}/{{repo}}:{{commit}} ./',
Expand All @@ -36,17 +40,10 @@ var GITHUB_HOOK_SCHEMA = require('../schema/github-hook.json');
var DEFAULT_CONFIG = {
port: 8080,
defaults: {
realm: 'dev',
branch: 'develop',
commit: 'HEAD'
},
realms: {
prod: {
repository: 'c.melt.sh'
},
dev: {
repository: 'u.melt.sh'
}
commit: 'HEAD',
github: true,
dockerRepoBase: process.env.DOCKER_REPO_BASE || 'registry.totem.local/totem'
},
tasks: {
clone: {
Expand All @@ -69,7 +66,13 @@ var DEFAULT_CONFIG = {
hook: {
secret: process.env.HOOK_SECRET || 'changeit',
postUrl: process.env.HOOK_POST_URL || ''
}
},
hipchat: {
token: process.env.HIPCHAT_TOKEN,
room: process.env.HIPCHAT_ROOM
},
baseUrl: process.env.BASE_URL || 'http://localhost:8080',
cluster: process.env.CLUSTER_NAME || 'local'
};

module.exports = Factory;
Expand All @@ -93,7 +96,10 @@ function Factory(config) {
this.jobs = new Datastore();

// Notification for job
this.notifier = new PostHookNotifier(config.hook);
this.notifiers = [
new PostHookNotifier(config.hook),
new HipchatNotifier(config.hipchat)
];

// Initialize the API
this.initApi();
Expand Down Expand Up @@ -287,7 +293,8 @@ Factory.prototype.initApi = function initApi() {
'owner': context.repository.owner.name,
'repo': context.repository.name,
'commit': context.after,
'branch': path.basename(context.ref)
'branch': path.basename(context.ref),
'github': true
};

_.defaults(context, _this.config.defaults);
Expand Down Expand Up @@ -320,17 +327,23 @@ Factory.prototype.stop = function stop() {
*/
Factory.prototype.newJob = function newJob(context) {
var _this = this;
context.tag = context.tag || context.commit;
var job = _this.jobRunner.newJob(context);

// Store the Job in the datastore
_this.jobs.put(job.id, job);

// Start building the image
job.start(function (err) {
var isSuccessful = (err ? false : true);
var image = _this.getImage(job);
//Notify build was successful / failed.
_this.notifier.notify(isSuccessful, context, image);
var notifyCtx = _.extend({}, context, {
baseUrl: _this.config.baseUrl,
cluster: _this.config.cluster,
id: job.id
});
_.forEach(_this.notifiers, function(notifier) {
notifier.notify(isSuccessful, notifyCtx, image);
});
});

return job;
Expand All @@ -345,12 +358,6 @@ Factory.prototype.newJobWithDefaults = function newJobWithDefaults(context, onSu
if (err) {
return onError('SCHEMA_ERROR', err.errors);
}
var realm = _this.config.realms[context.realm];
if (_.isEmpty(realm) || _.isEmpty(realm.repository)) {
return onError('REALM_ERROR', 'Unable to find a suitable docker repository for realm: ' + context.realm);
}
context.dockerRepo = realm.repository;
context.tag = context.tag || context.commit;
var job = _this.newJob(context);
return onSuccess(job);
});
Expand All @@ -372,7 +379,7 @@ Factory.prototype.toPublicJob = function toPublicJob(job) {
};

Factory.prototype.getImage = function getImage(job) {
return job.context.dockerRepo + 'totem/' + job.context.owner + '-' + job.context.repo + ':' + job.context.tag;
return job.context.dockerRepoBase + '/' + job.context.owner + '-' + job.context.repo + ':' + job.context.tag;
};

/**
Expand All @@ -385,15 +392,6 @@ Factory.prototype.getImage = function getImage(job) {
*/
Factory.prototype.createJob = function createJob(req, res, context, next) {
var _this = this;
var realm = _this.config.realms[context.realm];
if (_.isEmpty(realm) || _.isEmpty(realm.repository)) {
return next(new restify.InternalError(
'Unable to find a suitable docker repository for realm: ' + context.realm));
}

context.dockerRepo = realm.repository;
context.tag = context.tag || context.commit;

try {
// Create a new job
var job = _this.newJob(context);
Expand Down

0 comments on commit af4f0e6

Please sign in to comment.