Skip to content

Instantly share code, notes, and snippets.

@smashwilson
Created September 2, 2015 12:57
Show Gist options
  • Save smashwilson/0e9f9f92a598e4897699 to your computer and use it in GitHub Desktop.
Save smashwilson/0e9f9f92a598e4897699 to your computer and use it in GitHub Desktop.
Updating the condition of an existing node on a Cloud Load Balancer
// Updating the condition of an existing load balancer node with pkgcloud.
var pkgcloud = require('pkgcloud');
var client = pkgcloud.loadbalancer.createClient({
provider: 'rackspace',
username: process.env.RACKSPACE_USERNAME,
apiKey: process.env.RACKSPACE_APIKEY,
region: process.env.RACKSPACE_REGION
});
var lbName = process.env.CLB_NAME;
var serverAddr = process.env.CLB_SERVER_ADDR;
// 1. Find the load balancer you wish to update.
client.getLoadBalancers(function (err, lbs) {
if (err) {
console.error("Unable to list load balancers.");
console.error(err);
return;
}
var lb;
lbs.forEach(function (each) {
if (each.name === lbName) {
lb = each;
}
});
if (!lb) {
console.error("Unable to find the load balancer.");
return;
}
console.log("Found load balancer: %s", lb.name);
// 2. Find the node that you wish to update.
lb.getNodes(function (err, nodes) {
if (err) {
console.error("Unable to list the load balancer's nodes.");
console.error(err);
return;
}
var node;
nodes.forEach(function (each) {
if (each.address === serverAddr) {
node = each;
}
});
if (!node) {
console.error("Unable to locate the server's node.");
console.error(err);
return;
}
console.log("Found node for server: %s", node.address);
// 3. Update the node's condition.
node.condition = 'DRAINING';
lb.updateNode(node, function (err) {
if (err) {
console.error("Unable to update the node's condition.");
console.error(err);
return;
}
// 4. Wait for the load balancer to become mutable again.
// Necessary before you can perform any other modifications to this load balancer.
lb.setWait({status: 'ACTIVE'}, 2500, function (err) {
if (err) {
console.error(err);
return;
}
console.log("Successfully updated the node's condition to draining.");
})
})
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment