Skip to content

Commit

Permalink
Adds tessel2.json to the libstd bundle.
Browse files Browse the repository at this point in the history
  • Loading branch information
tcr authored and rwaldron committed Oct 13, 2016
1 parent cbd073a commit ab60289
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 82 deletions.
3 changes: 1 addition & 2 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1089,10 +1089,9 @@ controller.uninstaller = function(options) {
controller.installSdk = function(options) {
return sdk.installSdk()
.then(() => sdk.installRustlib().catch(e => {
log.warn(e.message);
log.error(e.message);
log.warn('Continuing with SDK installation...');
}))
.then(() => sdk.installRustTarget())
.then(() => {
log.info('SDK installed.');
})
Expand Down
94 changes: 14 additions & 80 deletions lib/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ var getPlatform = exports.getPlatform = () => {

var paths = {
sdk: path.join(osenv.home(), '.tessel/sdk'),
rustlib: path.join(osenv.home(), '.tessel/rust/rustlib'),
rustTarget: path.join(osenv.home(), '.tessel/rust/tessel2.json'),
rustlib: path.join(osenv.home(), '.tessel/rust'),
};

var sdkPath = {
Expand All @@ -37,11 +36,6 @@ var sdkPath = {

var rustlibUrl = 'https://builds.tessel.io/t2/sdk/t2-rustlib-VERSION.tar.gz';

function sha256digest(str) {
var sha256 = createHash('sha256');
return sha256.update(str).digest('hex');
}

function sha256stream() {
var sha256 = createHash('sha256');
var stream = new Transform();
Expand Down Expand Up @@ -89,7 +83,13 @@ function download(url) {

function downloadString(url) {
return new Promise((resolve, reject) => {
request(url, (error, response, body) => {
request({
url: url,
// We want to force Cloudfront to serve us the latest file.
headers: {
"Accept-Encoding": "gzip, deflate",
},
}, (error, response, body) => {
if (!error && response.statusCode == 200) {
resolve(body);
} else {
Expand Down Expand Up @@ -130,7 +130,7 @@ var toolchainPath = exports.toolchainPath = () => {
// This will resolve with checking that the SDK exists and matches the checksum.
var checkSdk = exports.checkSdk = (checksumVerify) => {
var dir = path.join(paths.sdk, getPlatform());
return fs.readFileAsync(path.join(dir, "CHECKSUM"))
return fs.readFileAsync(path.join(dir, "CHECKSUM"), 'utf-8')
.then(checksum => ({
exists: true,
checked: checksumVerify == checksum,
Expand All @@ -144,7 +144,7 @@ var checkSdk = exports.checkSdk = (checksumVerify) => {

var checkRustlib = exports.checkRustlib = (rustv, checksumVerify) => {
var dir = path.join(paths.rustlib, rustv)
return fs.readFileAsync(path.join(dir, "CHECKSUM"))
return fs.readFileAsync(path.join(dir, "CHECKSUM"), 'utf-8')
.then(checksum => ({
exists: true,
checked: checksumVerify == checksum,
Expand All @@ -156,29 +156,6 @@ var checkRustlib = exports.checkRustlib = (rustv, checksumVerify) => {
}))
}

var checkRustTarget = exports.checkRustTarget = (checksumVerify) => {
var file = paths.rustTarget;
return new Promise((resolve, reject) => {
fs.createReadStream(file)
.on('error', _ => {
resolve({
exists: false,
checked: false,
path: file,
})
})
.pipe(sha256stream())
.on('sha256', checksum => {
checksum = sha256file(checksum, path.basename(file));
resolve({
exists: true,
checked: checksumVerify == checksum,
path: file,
})
})
});
}

var installSdk = exports.installSdk = () => {
var url = sdkPath[getPlatform()];
var checksumVerify = null;
Expand Down Expand Up @@ -234,45 +211,11 @@ var installRustlib = exports.installRustlib = (next) => {
log.info(`Updating ${pkgname}...`)
}

return fs.mkdirpAsync(path.join(osenv.home(), '.tessel/rust/rustlib'))
return fs.mkdirpAsync(paths.rustlib)
.then(() => extractRustlib(checksumVerify, path.basename(url), download(url), rustv))
});
}

var installRustTarget = exports.installRustTarget = next => {
var url = 'http://builds.tessel.io/t2/sdk/tessel2.json';
var checksumVerify = null;

return downloadString(url + '.sha256')
.then((checksum) => {
checksumVerify = checksum;
return checkRustTarget(checksumVerify);
})
.then((check) => {
if (check.exists && check.checked) {
log.info('Latest target.json already installed.');
return;
} else if (!check.exists) {
log.info('Installing target.json...')
} else {
log.info('Updating target.json...')
}

return fs.mkdirpAsync(path.join(osenv.home(), '.tessel/rust'))
.then(() => downloadString(url))
.then(target => {
var checksum = sha256file(sha256digest(target), path.basename(url));

// Check sum.
if (checksum != checksumVerify) {
throw new Error('Checksum for downloaded target.json does not match!');
}

fs.writeFileSync(path.join(osenv.home(), '.tessel/rust/tessel2.json'), target);
});
});
}

function extract(checksumVerify, filename, sdkStream, root, strip, name, decompress) {
log.info(`Downloading ${name}...`);

Expand Down Expand Up @@ -336,12 +279,12 @@ function extract(checksumVerify, filename, sdkStream, root, strip, name, decompr
}

function extractSdk(checksumVerify, filename, sdkStream) {
var root = path.join(osenv.home(), '.tessel/sdk', 'macos');
var root = path.join(paths.sdk, 'macos');
return extract(checksumVerify, filename, sdkStream, root, 2, 'SDK', bz2());
}

function extractRustlib(checksumVerify, filename, sdkStream, rustVersion) {
var root = path.join(osenv.home(), '.tessel/rust/rustlib', rustVersion);
var root = path.join(paths.rustlib, rustVersion);
return extract(checksumVerify, filename, sdkStream, root, 0, 'MIPS libstd', zlib.createGunzip());
}

Expand All @@ -350,7 +293,6 @@ var getBuildConfig = exports.getBuildConfig = () => {
rustv: null,
toolchainPath: null,
stagingDir: null,
rustTargetPath: null,
rustlibPath: null,
name: null,
path: null,
Expand All @@ -375,14 +317,6 @@ var getBuildConfig = exports.getBuildConfig = () => {
}
config.rustlibPath = check.path;

return checkRustTarget();
})
.then(check => {
if (!check.exists) {
throw new Error('target.json not installed.');
}
config.rustTargetPath = path.dirname(check.path);

return toolchainPath();
})
.then(toolchainPath => {
Expand Down Expand Up @@ -435,7 +369,7 @@ var buildTessel = exports.buildTessel = (config) => {
var env = Object.assign({}, process.env);
Object.assign(env, {
STAGING_DIR: config.stagingDir,
RUST_TARGET_PATH: config.rustTargetPath,
RUST_TARGET_PATH: config.rustlibPath,
PATH: path.join(config.toolchainPath, "bin") + ":" + env.PATH,
RUSTFLAGS: "-L " + config.rustlibPath,
});
Expand Down

0 comments on commit ab60289

Please sign in to comment.