Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Make update URL configurable. #776

Merged
merged 2 commits into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ module.exports = {
certemail: 'certificate@mozilla-iot.org',
},
bcryptRounds: 2,
updateUrl: 'https://api.github.com/repos/mozilla-iot/gateway/releases',
};
9 changes: 7 additions & 2 deletions src/controllers/updates_controller.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const childProcess = require('child_process');
const config = require('config');
const fs = require('fs');
const fetch = require('node-fetch');
const semver = require('semver');
const PromiseRouter = require('express-promise-router');

const pkg = require('../../package.json');

const UpdatesController = PromiseRouter();

function readVersion(packagePath) {
Expand Down Expand Up @@ -65,8 +68,10 @@ UpdatesController.get('/latest', async function(request, response) {
}
}

const res = await
fetch('https://api.github.com/repos/mozilla-iot/gateway/releases');
const res = await fetch(
config.get('updateUrl'),
{headers: {'User-Agent': `mozilla-iot-gateway/${pkg.version}`}});

const releases = await res.json();
if (!releases || !releases.filter) {
console.warn('API returned invalid releases, rate limit likely exceeded');
Expand Down
16 changes: 10 additions & 6 deletions src/test/integration/updates-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

/* globals it */

const config = require('config');
const nock = require('nock');
const {URL} = require('url');

const {server, chai} = require('../common');
const Constants = require('../../constants');
Expand Down Expand Up @@ -35,15 +37,17 @@ const releases = [
},
];

const updateUrl = new URL(config.get('updateUrl'));

describe('updates/', function() {
let jwt;
beforeEach(async () => {
jwt = await createUser(server, TEST_USER);
});

it('should get /latest with a normal response', async() => {
nock('https://api.github.com')
.get('/repos/mozilla-iot/gateway/releases')
nock(updateUrl.origin)
.get(updateUrl.pathname)
.reply(200, releases);

const res = await chai.request(server)
Expand All @@ -57,8 +61,8 @@ describe('updates/', function() {
});

it('should get /latest with no good releases', async() => {
nock('https://api.github.com')
.get('/repos/mozilla-iot/gateway/releases')
nock(updateUrl.origin)
.get(updateUrl.pathname)
.reply(200, releases.slice(0, 2));

const res = await chai.request(server)
Expand All @@ -71,8 +75,8 @@ describe('updates/', function() {
});

it('should get /latest with a strange error', async() => {
nock('https://api.github.com')
.get('/repos/mozilla-iot/gateway/releases')
nock(updateUrl.origin)
.get(updateUrl.pathname)
.reply(200, {error: true});

const res = await chai.request(server)
Expand Down
106 changes: 53 additions & 53 deletions tools/check-for-update.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
const config = require('config');
const exec = require('child_process').exec;
const fetch = require('node-fetch');
const fs = require('fs');
const semver = require('semver');

const exec = require('child_process').exec;

const remote = 'mozilla-iot';

const pkg = JSON.parse(fs.readFileSync('package.json', {encoding: 'utf-8'}));
const pkg = require('../package.json');

fetch(`https://api.github.com/repos/${remote}/gateway/releases`).then(res => {
return res.json();
}).then(releases => {
// Assumes that releases are in chronological order, latest-first
return releases.filter(release => {
return !release.prerelease && !release.draft;
})[0];
}).then(latestRelease => {
if (!latestRelease) {
console.error('No releases found');
return;
}
let releaseVer = latestRelease.tag_name;
let currentVer = pkg.version;
if (semver.lt(currentVer, releaseVer)) {
// do upgrade here woo
// download latestRelease.assets[:].browser_download_url
let gatewayUrl = null;
let nodeModulesUrl = null;
let validAssetRe = new RegExp(
`^https://github.com/${remote}/gateway/releases/download/` + releaseVer
+ '/[a-z0-9_-]+.tar.gz$');
for (let asset of latestRelease.assets) {
if (!asset.browser_download_url.match(validAssetRe)) {
continue;
}
if (asset.name.match(/^gateway-[a-f0-9]+\.tar\.gz$/)) {
gatewayUrl = asset.browser_download_url;
}
if (asset.name.match(/^node_modules-[a-f0-9]+\.tar\.gz$/)) {
nodeModulesUrl = asset.browser_download_url;
}
fetch(config.get('updateUrl'),
{headers: {'User-Agent': `mozilla-iot-gateway/${pkg.version}`}})
.then(res => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should be consistent with line 13 (which means it wouldn't have the extra indentation either)

return res.json();
})
.then(releases => {
// Assumes that releases are in chronological order, latest-first
return releases.filter(release => {
return !release.prerelease && !release.draft;
})[0];
})
.then(latestRelease => {
if (!latestRelease) {
console.error('No releases found');
return;
}

if (nodeModulesUrl && gatewayUrl) {
exec(`./gateway/tools/upgrade.sh ${gatewayUrl} ${nodeModulesUrl}`,
{cwd: '..'}, function(err, stdout, stderr) {
if (err) {
console.error('Upgrade failed', err, stdout, stderr);
} else {
console.log('Upgrade succeeded');
let releaseVer = latestRelease.tag_name;
let currentVer = pkg.version;
if (semver.lt(currentVer, releaseVer)) {
// do upgrade here woo
// download latestRelease.assets[:].browser_download_url
let gatewayUrl = null;
let nodeModulesUrl = null;
let validAssetRe = new RegExp(
`/download/${releaseVer}/[a-z0-9_-]+.tar.gz$`);
for (let asset of latestRelease.assets) {
if (!asset.browser_download_url.match(validAssetRe)) {
continue;
}
if (asset.name.match(/^gateway-[a-f0-9]+\.tar\.gz$/)) {
gatewayUrl = asset.browser_download_url;
}
if (asset.name.match(/^node_modules-[a-f0-9]+\.tar\.gz$/)) {
nodeModulesUrl = asset.browser_download_url;
}
});
}

if (nodeModulesUrl && gatewayUrl) {
exec(`./gateway/tools/upgrade.sh ${gatewayUrl} ${nodeModulesUrl}`,
{cwd: '..'}, function(err, stdout, stderr) {
if (err) {
console.error('Upgrade failed', err, stdout, stderr);
} else {
console.log('Upgrade succeeded');
}
});
} else {
console.warn(`Release ${releaseVer} does not include archives`,
latestRelease.assets);
}
} else {
console.warn(`Release ${releaseVer} does not include archives`,
latestRelease.assets);
console.log(`Our version ${currentVer} >= ${releaseVer}, exiting`);
}
} else {
console.log(`Our version ${currentVer} >= ${releaseVer}, exiting`);
}
});
});