Skip to content

Instantly share code, notes, and snippets.

@richorama
Last active April 8, 2016 14:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richorama/8052843 to your computer and use it in GitHub Desktop.
Save richorama/8052843 to your computer and use it in GitHub Desktop.
A github service hook to start a VM, and shut it down after 15 minutes of inactivity
{
"name": "vmstartstop",
"version": "0.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "MIT",
"dependencies": {
"azure": "~0.7.18"
}
}
var subscriptionId = "xxx";
var hostedService = "hosted_service";
var deploymentId = "deployment_name";
var roleInstance = "role_instance_name";
var azure = require('azure');
var sms = azure.createServiceManagementService(subscriptionId,{keyfile:"./mycert.pem",certfile:"./mycert.pem"});
require('http').createServer(function(req, res){
var path = req.url.replace('/', '');
if (path && actions[path]) actions[path](req, res);
res.end();
}).listen(process.env.port || 8080);
var timerId;
var actions = {
start: function(req, res){
sms.startRole(hostedService,deploymentId,roleInstance,function(err,data){
if (err) console.log(err);
console.log(data.body);
});
// shutdown after 15 minutes of inactivity
if (timerId){
clearTimeout(timerId);
}
timerId = setTimeout(actions.stop, 15 * 60 * 1000);
},
stop: function(req, res){
sms.shutdownRole(hostedService,deploymentId,roleInstance,true,function(err,data){
if (err) console.log(err);
console.log(data.body);
});
timerId = undefined;
}
};
@danmiser
Copy link

danmiser commented Apr 8, 2016

This used to work for me, but Azure must have updated something and this no longer works. Not exactly sure when the change of behavior occurred. I had set this up probably almost 2 years ago. Are you seeing that this still works? I'm using a Classic VM if that matters. Thanks for the gist!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment