-
-
Notifications
You must be signed in to change notification settings - Fork 655
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Checks versions against https://version.unleash.run - Generates a unique instance id (uuid)
- Loading branch information
Christopher Kolstad
committed
Feb 19, 2021
1 parent
4902161
commit b83387a
Showing
9 changed files
with
246 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import fetch from 'node-fetch'; | ||
|
||
const TWO_DAYS = 48 * 60 * 60 * 1000; | ||
class VersionService { | ||
constructor( | ||
{ settingStore }, | ||
{ getLogger, versionCheck, version, enterpriseVersion }, | ||
) { | ||
this.logger = getLogger('lib/services/version-service.js'); | ||
this.settingStore = settingStore; | ||
this.current = { | ||
oss: version, | ||
enterprise: enterpriseVersion, | ||
}; | ||
if (versionCheck) { | ||
if (versionCheck.url) { | ||
this.versionCheckUrl = versionCheck.url; | ||
} | ||
|
||
if (versionCheck.enable === 'true') { | ||
this.enabled = true; | ||
this.checkLatestVersion(); | ||
setInterval(this.checkLatestVersion, TWO_DAYS); | ||
} else { | ||
this.enabled = false; | ||
} | ||
} | ||
} | ||
|
||
async checkLatestVersion() { | ||
if (this.enabled) { | ||
const { id } = await this.settingStore.get('instanceInfo'); | ||
try { | ||
const data = await fetch(this.versionCheckUrl, { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
versions: this.current, | ||
id, | ||
}), | ||
headers: { 'Content-Type': 'application/json' }, | ||
}).then(res => res.json()); | ||
this.latest = { | ||
oss: data.versions.oss, | ||
enterprise: data.versions.enterprise, | ||
}; | ||
this.isLatest = data.latest; | ||
} catch (err) { | ||
this.logger.info('Could not check newest version', err); | ||
} | ||
} | ||
} | ||
|
||
getVersionInfo() { | ||
return { | ||
current: this.current, | ||
latest: this.latest || {}, | ||
isLatest: this.isLatest || false, | ||
}; | ||
} | ||
} | ||
|
||
module.exports = VersionService; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
const test = require('ava'); | ||
const proxyquire = require('proxyquire').noCallThru(); | ||
const fetchMock = require('fetch-mock').sandbox(); | ||
const stores = require('../../test/fixtures/store'); | ||
const getLogger = require('../../test/fixtures/no-logger'); | ||
const version = require('../util/version'); | ||
|
||
const VersionService = proxyquire('./version-service', { | ||
'node-fetch': fetchMock, | ||
}); | ||
|
||
test.serial('yields current versions', async t => { | ||
const testurl = 'https://version.test'; | ||
const { settingStore } = stores.createStores(); | ||
await settingStore.insert({ | ||
name: 'instanceInfo', | ||
content: { id: '1234abc' }, | ||
}); | ||
const latest = { | ||
oss: '4.0.0', | ||
enterprise: '4.0.0', | ||
}; | ||
fetchMock.mock( | ||
{ url: testurl, method: 'POST' }, | ||
{ | ||
latest: false, | ||
versions: latest, | ||
}, | ||
); | ||
const service = new VersionService( | ||
{ settingStore }, | ||
{ getLogger, versionCheck: { url: testurl, enable: 'true' }, version }, | ||
); | ||
await service.checkLatestVersion(); | ||
fetchMock.done(); | ||
const versionInfo = service.getVersionInfo(); | ||
t.is(versionInfo.current.oss, version); | ||
t.falsy(versionInfo.current.enterprise); | ||
t.is(versionInfo.latest.oss, latest.oss); | ||
t.is(versionInfo.latest.enterprise, latest.enterprise); | ||
}); | ||
|
||
test.serial('supports setting enterprise version as well', async t => { | ||
const testurl = `https://version.test${Math.random() * 1000}`; | ||
const { settingStore } = stores.createStores(); | ||
const enterpriseVersion = '3.7.0'; | ||
await settingStore.insert({ | ||
name: 'instanceInfo', | ||
content: { id: '1234abc' }, | ||
}); | ||
const latest = { | ||
oss: '4.0.0', | ||
enterprise: '4.0.0', | ||
}; | ||
fetchMock.mock( | ||
{ url: testurl, method: 'POST' }, | ||
{ | ||
latest: false, | ||
versions: latest, | ||
}, | ||
); | ||
const service = new VersionService( | ||
{ settingStore }, | ||
{ | ||
getLogger, | ||
versionCheck: { url: testurl, enable: 'true' }, | ||
version, | ||
enterpriseVersion, | ||
}, | ||
); | ||
await service.checkLatestVersion(); | ||
fetchMock.done(); | ||
const versionInfo = service.getVersionInfo(); | ||
t.is(versionInfo.current.oss, version); | ||
t.is(versionInfo.current.enterprise, enterpriseVersion); | ||
t.is(versionInfo.latest.oss, latest.oss); | ||
t.is(versionInfo.latest.enterprise, latest.enterprise); | ||
}); | ||
|
||
test.serial( | ||
'if version check is not enabled should not make any calls', | ||
async t => { | ||
const testurl = `https://version.test${Math.random() * 1000}`; | ||
const { settingStore } = stores.createStores(); | ||
const enterpriseVersion = '3.7.0'; | ||
await settingStore.insert({ | ||
name: 'instanceInfo', | ||
content: { id: '1234abc' }, | ||
}); | ||
const latest = { | ||
oss: '4.0.0', | ||
enterprise: '4.0.0', | ||
}; | ||
fetchMock.mock( | ||
{ url: testurl, method: 'POST' }, | ||
{ | ||
latest: false, | ||
versions: latest, | ||
}, | ||
); | ||
const service = new VersionService( | ||
{ settingStore }, | ||
{ | ||
getLogger, | ||
versionCheck: { url: testurl, enable: false }, | ||
version, | ||
enterpriseVersion, | ||
}, | ||
); | ||
await service.checkLatestVersion(); | ||
t.false(fetchMock.called(testurl)); | ||
const versionInfo = service.getVersionInfo(); | ||
t.is(versionInfo.current.oss, version); | ||
t.is(versionInfo.current.enterprise, enterpriseVersion); | ||
t.falsy(versionInfo.latest.oss, latest.oss); | ||
t.falsy(versionInfo.latest.enterprise, latest.enterprise); | ||
}, | ||
); |
19 changes: 19 additions & 0 deletions
19
src/migrations/20210218090213-generate-server-identifier.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
exports.up = function(db, cb) { | ||
db.runSql( | ||
` | ||
INSERT INTO settings(name, content) VALUES ('instanceInfo', json_build_object('id', gen_random_uuid())); | ||
`, | ||
cb, | ||
); | ||
}; | ||
|
||
exports.down = function(db, cb) { | ||
db.runSql( | ||
` | ||
DROP FROM settings WHERE name = 'instanceInfo' | ||
`, | ||
cb, | ||
); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,18 @@ | ||
'use strict'; | ||
|
||
module.exports = () => ({ | ||
insert: () => Promise.resolve(), | ||
get: () => Promise.resolve(), | ||
}); | ||
module.exports = () => { | ||
const _settings = []; | ||
return { | ||
insert: setting => { | ||
_settings.push(setting); | ||
return Promise.resolve(); | ||
}, | ||
get: name => { | ||
const setting = _settings.find(s => s.name === name); | ||
if (setting) { | ||
return Promise.resolve(setting.content); | ||
} | ||
return Promise.reject(new Error('Could not find setting')); | ||
}, | ||
}; | ||
}; |