per-profile cache directory using account+url slug

This commit is contained in:
Dave Eddy 2015-09-02 23:48:14 -04:00
parent da8e13b45e
commit 1cc87edb0f
2 changed files with 27 additions and 3 deletions

View File

@ -337,6 +337,18 @@ function normShortId(s) {
return shortId; return shortId;
} }
/*
* take a "profile" object and return a slug based on the account name
* and DC URL. This is currently used to create a filesystem-safe name
* to use for caching
*/
function slug(o) {
var acct = o.account.replace(/[@]/g, '_');
var url = o.url.replace(/^https?:\/\//, '');
var s = format('%s@%s', acct, url).replace(/[!#$%\^&\*:'"\?\/\\\.]/g, '_');
return s;
}
//---- exports //---- exports
@ -354,6 +366,7 @@ module.exports = {
humanSizeFromBytes: humanSizeFromBytes, humanSizeFromBytes: humanSizeFromBytes,
capitalize: capitalize, capitalize: capitalize,
normShortId: normShortId, normShortId: normShortId,
slug: slug,
TABULA_OPTIONS: TABULA_OPTIONS TABULA_OPTIONS: TABULA_OPTIONS
}; };
// vim: set softtabstop=4 shiftwidth=4: // vim: set softtabstop=4 shiftwidth=4:

View File

@ -67,7 +67,15 @@ function Triton(options) {
this.profile = this.getProfile( this.profile = this.getProfile(
options.profileName || this.config.defaultProfile); options.profileName || this.config.defaultProfile);
this.log.trace({profile: this.profile}, 'profile data'); this.log.trace({profile: this.profile}, 'profile data');
this.cacheDir = options.cacheDir;
if (options.cacheDir !== undefined) {
var slug = common.slug(this.profile);
this.cacheDir = path.join(options.cacheDir, slug);
this.log.trace({cacheDir: this.cacheDir}, 'cache dir');
try {
fs.mkdirSync(this.cacheDir);
} catch (e) {}
}
this.cloudapi = this._cloudapiFromProfile(this.profile); this.cloudapi = this._cloudapiFromProfile(this.profile);
} }
@ -142,9 +150,10 @@ Triton.prototype.listImages = function listImages(opts, cb) {
// try to read the cache if the user wants it // try to read the cache if the user wants it
// if this fails for any reason we fallback to hitting the cloudapi // if this fails for any reason we fallback to hitting the cloudapi
if (opts.useCache) { if (opts.useCache) {
self.log.debug({file: cacheFile}, 'reading images cache file');
fs.readFile(cacheFile, 'utf8', function (err, out) { fs.readFile(cacheFile, 'utf8', function (err, out) {
if (err) { if (err) {
self.log.info({err: err, cacheFile: cacheFile}, self.log.warn({err: err, cacheFile: cacheFile},
'failed to read cache file'); 'failed to read cache file');
fetch(); fetch();
return; return;
@ -153,12 +162,13 @@ Triton.prototype.listImages = function listImages(opts, cb) {
try { try {
data = JSON.parse(out); data = JSON.parse(out);
} catch (e) { } catch (e) {
self.log.info({err: e, cacheFile: cacheFile}, self.log.warn({err: e, cacheFile: cacheFile},
'failed to parse cache file'); 'failed to parse cache file');
fetch(); fetch();
return; return;
} }
self.log.info('calling back with images cache data');
cb(null, data, {}); cb(null, data, {});
}); });
return; return;
@ -176,6 +186,7 @@ Triton.prototype.listImages = function listImages(opts, cb) {
self.log.info({err: err2}, self.log.info({err: err2},
'error caching images results'); 'error caching images results');
} }
self.log.trace({file: cacheFile}, 'images cache updated');
done(); done();
}); });
} else { } else {