From 1cc87edb0f07f826d052fd4bfcaea5204e3457ca Mon Sep 17 00:00:00 2001 From: Dave Eddy Date: Wed, 2 Sep 2015 23:48:14 -0400 Subject: [PATCH] per-profile cache directory using account+url slug --- lib/common.js | 13 +++++++++++++ lib/triton.js | 17 ++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/common.js b/lib/common.js index 42be0ef..cda485c 100644 --- a/lib/common.js +++ b/lib/common.js @@ -337,6 +337,18 @@ function normShortId(s) { 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 @@ -354,6 +366,7 @@ module.exports = { humanSizeFromBytes: humanSizeFromBytes, capitalize: capitalize, normShortId: normShortId, + slug: slug, TABULA_OPTIONS: TABULA_OPTIONS }; // vim: set softtabstop=4 shiftwidth=4: diff --git a/lib/triton.js b/lib/triton.js index 1f1f772..0938db9 100644 --- a/lib/triton.js +++ b/lib/triton.js @@ -67,7 +67,15 @@ function Triton(options) { this.profile = this.getProfile( options.profileName || this.config.defaultProfile); 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); } @@ -142,9 +150,10 @@ Triton.prototype.listImages = function listImages(opts, cb) { // try to read the cache if the user wants it // if this fails for any reason we fallback to hitting the cloudapi if (opts.useCache) { + self.log.debug({file: cacheFile}, 'reading images cache file'); fs.readFile(cacheFile, 'utf8', function (err, out) { if (err) { - self.log.info({err: err, cacheFile: cacheFile}, + self.log.warn({err: err, cacheFile: cacheFile}, 'failed to read cache file'); fetch(); return; @@ -153,12 +162,13 @@ Triton.prototype.listImages = function listImages(opts, cb) { try { data = JSON.parse(out); } catch (e) { - self.log.info({err: e, cacheFile: cacheFile}, + self.log.warn({err: e, cacheFile: cacheFile}, 'failed to parse cache file'); fetch(); return; } + self.log.info('calling back with images cache data'); cb(null, data, {}); }); return; @@ -176,6 +186,7 @@ Triton.prototype.listImages = function listImages(opts, cb) { self.log.info({err: err2}, 'error caching images results'); } + self.log.trace({file: cacheFile}, 'images cache updated'); done(); }); } else {