Skip to content

Instantly share code, notes, and snippets.

@jmervine
Last active March 6, 2019 19:41
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 jmervine/8907040 to your computer and use it in GitHub Desktop.
Save jmervine/8907040 to your computer and use it in GitHub Desktop.
MaxCDN purge script.
#!/usr/bin/env node
/***
* Setup:
*
* npm install maxcdn subarg async
*
***/
var MaxCDN, subarg, async;
try {
MaxCDN = require('maxcdn');
subarg = require('subarg');
async = require('async');
} catch (e) {
console.error("Setup:");
console.error(" ");
console.error("$ npm install maxcdn subarg async ");
console.error(" ");
console.trace(e);
process.exit(1);
}
var opts = subarg(process.argv.slice(2));
if (opts.help || opts.h) usage();
[ 'alias', 'key', 'secret' ].forEach(function(opt) {
// support ALIAS, KEY, SECRET from environment
opts[opt] = opts[opt] || process.env[opt.toUpperCase()];
// ensure required params exist
if (typeof opts[opt] === 'undefined') {
usage(1, "Missing required argument: "+opt); // usage with error status
}
});
// init MaxCDN
var maxcdn = new MaxCDN(opts.alias, opts.key, opts.secret);
opts.pull = opts.pull || [];
opts.push = opts.push || [];
if (opts.push == 0 && opts.pull == 0) {
async.parallel([
getZones('pull'),
getZones('push')
], function (err) {
if (err) console.trace(err);
purges = [];
opts.pull.forEach(function (zone) { purges.push(purge('pull', zone)); });
opts.push.forEach(function (zone) { purges.push(purge('push', zone)); });
async.parallelLimit(purges, 4, function (err) {
if (err) console.trace(err);
console.log('done.');
});
});
}
function purge(type, zone) {
return function (callback) {
process.stdout.write('purging '+type+' zone '+zone+'... ');
maxcdn.delete('/zones/'+type+'.json/'+zone+'/cache', function (err, res) {
console.log('complete');
callback(err);
});
};
}
function getZones(type) {
return function (callback) {
maxcdn.get('zones/'+type+'.json', function (err, res) {
res.data[type+'zones'].forEach(function (zone) {
opts[type].push(zone.id);
});
callback(err);
});
};
}
function usage(status, error) {
status = status || 0; // default to zero exit status
if (error) {
console.error("ERROR:", error);
console.error(" ");
}
console.log("Usage: purge.js --alias ALIAS --key KEY --secret SECRET [OPTIONS]");
console.log(" ");
console.log(" Required:");
console.log(" - alias: Your consumer alias.");
console.log(" - key: Your oauth consumer key.");
console.log(" - secret: Your oauth consumer secret token.");
console.log(" ");
console.log(" Note:");
console.log(" alias, key and secret can also be read from your environment");
console.log(" via exporting ALIAS, KEY, and/or SECRET with your credentials.");
console.log(" ");
console.log(" Optional:");
console.log(" - pull: One or more pull zones to purge.");
console.log(" - push: One or more push zones to purge.");
console.log(" - file: One or more files to purge.");
console.log(" ");
console.log(" Examples:");
console.log(" ");
console.log(" $ ./pruge.js --alias ALIAS --key KEY --SECRET --zone 12345 --zone 54321");
console.log(" ");
console.log(" $ ./pruge.js --alias ALIAS --key KEY --SECRET --zone [ 12345 54321 ]");
console.log(" ");
console.log(" $ ./pruge.js --alias ALIAS --key KEY --SECRET --zone 12345 \ ");
console.log(" $ --file /master.css --file /master.js ]");
console.log(" ");
process.exit(status);
}
@jmervine
Copy link
Author

jmervine commented Feb 9, 2014

Usage: purge.js --alias ALIAS --key KEY --secret SECRET [OPTIONS]

 Required:
 - alias:  Your consumer alias.
 - key:    Your oauth consumer key.
 - secret: Your oauth consumer secret token.

 Note:
   alias, key and secret can also be read from your environment
   via exporting ALIAS, KEY, and/or SECRET with your credentials.

 Optional:
 - pull:   One or more pull zones to purge.
 - push:   One or more push zones to purge.
 - file:   One or more files to purge.

 Examples:

 $ ./pruge.js --alias ALIAS --key KEY --SECRET --zone 12345 --zone 54321

 $ ./pruge.js --alias ALIAS --key KEY --SECRET --zone [ 12345 54321 ]

 $ ./pruge.js --alias ALIAS --key KEY --SECRET --zone 12345  
 $         --file /master.css --file /master.js

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