Last active
April 8, 2016 14:35
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!