Skip to content

Instantly share code, notes, and snippets.

@mikaelnet
Last active August 29, 2015 14:14
Show Gist options
  • Save mikaelnet/fc96d6a17e04c2107d41 to your computer and use it in GitHub Desktop.
Save mikaelnet/fc96d6a17e04c2107d41 to your computer and use it in GitHub Desktop.
Node client for Raspberry PI Deployer
var argv = require('argv');
var https = require('https');
var xml2js = require('xml2js');
var inspect = require('eyes').inspector({maxLength: false});
var gpio = require('gpio');
var xmlParser = new xml2js.Parser();
var teamCityHost = 'your team city host';
var teamCityAuth = 'teamcity-user:teamcity-password';
var teamCityJobId = 'your team city job id string';
var LED_RED_PIN = 3; // Verify these
var LED_GREEN_PIN = 5; // Verify these
var BTN_RED_PIN = 8; // Verify these
var BTN_GREEN_BIN = 10; // Verify these
// TeamCity functions
function getTcOptions (path, method) {
return {
host: teamCityHost,
auth: teamCityAuth,
path: path,
method: method ? method : 'GET'
};
}
function startJob (buildConfiguration, callback) {
var postXml = "<build><buildType id=\""+buildConfiguration+"\"/></build>";
var postOptions = getTcOptions("/app/rest/buildQueue", "POST");
postOptions.headers = {
"Content-Type": "application/xml",
"Content-Length": postXml.length
};
var post_req = https.request(postOptions, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
parser.parseString(chunk, function (err, result) {
callback(result);
});
//console.log('Response: ' + chunk);
});
});
post_req.write(postXml);
post_req.end();
post_req.on('error', function(err) {
console.error(e);
});
}
function pollStatus (taskId, callback) {
var getOptions = getTcOptions("/app/rest/buildQueue/taskId:"+taskId);
var get_req = https.request (getOptions, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
parser.parseString(chunk, function (err, result) {
callback(result);
});
});
});
get_req.end();
post_req.on('error', function(err) {
console.error(e);
});
}
function checkStatus (taskId) {
pollStatus (taskId, function (r) {
var state = r.build.$.state;
var runningInfo = r.build["running-info"];
if (runningInfo) {
var info = runningInfo[0].$;
console.log (state + "... " + info.elapsedSeconds + "/" + info.estimatedTotalSeconds +
" (" + info.percentageComplete + "%)");
//console.log ("percentageComplete: "+info.percentageComplete);
//console.log ("elapsedSeconds: "+info.elapsedSeconds);
//console.log ("estimatedTotalSeconds: "+info.estimatedTotalSeconds);
//console.log ("currentStageText: "+info.currentStageText);
}
if (state == "running") {
setTimeout(checkStatus, 1000, taskId);
}
//inspect (r);
});
}
// Setup pins
var ledRed = gpio.export(LED_RED_PIN, {
direction: 'out',
interval: 200,
ready: function() {
ledRed.set(); // Set high
}
});
var btnRed = gpio.export(BTN_RED_PIN, {
direction: 'in',
interval: 200,
ready: function() {
btnRed.on('change', function(val) {
console.log('Red: ' + val);
}
}
});
/*startJob (teamCityJobId, function (xmlResult) {
//inspect (xmlResult);
var taskId = xmlResult.build.$.taskId;
var status = xmlResult.build.$.state;
console.log (taskId + ": " + status);
checkStatus(taskId);
});*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment