2020-04-25 10:47:23 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016-2020 Balena Ltd.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as commandOptions from './command-options';
|
|
|
|
|
|
|
|
import * as _ from 'lodash';
|
2020-06-17 13:46:25 +00:00
|
|
|
import { getBalenaSdk } from '../utils/lazy';
|
2020-04-25 10:47:23 +00:00
|
|
|
|
|
|
|
export const init = {
|
|
|
|
signature: 'device init',
|
|
|
|
description: 'initialise a device with balenaOS',
|
|
|
|
help: `\
|
|
|
|
Use this command to download the OS image of a certain application and write it to an SD Card.
|
|
|
|
|
|
|
|
Notice this command may ask for confirmation interactively.
|
|
|
|
You can avoid this by passing the \`--yes\` boolean option.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
$ balena device init
|
|
|
|
$ balena device init --application MyApp\
|
|
|
|
`,
|
|
|
|
options: [
|
|
|
|
commandOptions.optionalApplication,
|
|
|
|
commandOptions.yes,
|
|
|
|
commandOptions.advancedConfig,
|
|
|
|
_.assign({}, commandOptions.osVersionOrSemver, {
|
|
|
|
signature: 'os-version',
|
|
|
|
parameter: 'os-version',
|
|
|
|
}),
|
|
|
|
commandOptions.drive,
|
|
|
|
{
|
|
|
|
signature: 'config',
|
|
|
|
description: 'path to the config JSON file, see `balena os build-config`',
|
|
|
|
parameter: 'config',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
permission: 'user',
|
|
|
|
action(_params, options) {
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const rimraf = Promise.promisify(require('rimraf'));
|
|
|
|
const tmp = require('tmp');
|
|
|
|
const tmpNameAsync = Promise.promisify(tmp.tmpName);
|
|
|
|
tmp.setGracefulCleanup();
|
|
|
|
|
|
|
|
const balena = getBalenaSdk();
|
|
|
|
const patterns = require('../utils/patterns');
|
|
|
|
const { runCommand } = require('../utils/helpers');
|
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
return Promise.try(function () {
|
2020-04-25 10:47:23 +00:00
|
|
|
if (options.application != null) {
|
|
|
|
return options.application;
|
|
|
|
}
|
|
|
|
return patterns.selectApplication();
|
|
|
|
})
|
|
|
|
.then(balena.models.application.get)
|
2020-06-15 22:53:07 +00:00
|
|
|
.then(function (application) {
|
2020-04-25 10:47:23 +00:00
|
|
|
const download = () =>
|
|
|
|
tmpNameAsync()
|
2020-06-15 22:53:07 +00:00
|
|
|
.then(function (tempPath) {
|
2020-04-25 10:47:23 +00:00
|
|
|
const osVersion = options['os-version'] || 'default';
|
|
|
|
return runCommand(
|
|
|
|
`os download ${application.device_type} --output '${tempPath}' --version ${osVersion}`,
|
|
|
|
);
|
|
|
|
})
|
2020-06-15 22:53:07 +00:00
|
|
|
.disposer((tempPath) => rimraf(tempPath));
|
2020-04-25 10:47:23 +00:00
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
return Promise.using(download(), (tempPath) =>
|
2020-04-25 10:47:23 +00:00
|
|
|
runCommand(`device register ${application.app_name}`)
|
|
|
|
.then(balena.models.device.get)
|
2020-06-15 22:53:07 +00:00
|
|
|
.tap(function (device) {
|
2020-04-25 10:47:23 +00:00
|
|
|
let configureCommand = `os configure '${tempPath}' --device ${device.uuid}`;
|
|
|
|
if (options.config) {
|
|
|
|
configureCommand += ` --config '${options.config}'`;
|
|
|
|
} else if (options.advanced) {
|
|
|
|
configureCommand += ' --advanced';
|
|
|
|
}
|
|
|
|
return runCommand(configureCommand)
|
2020-06-15 22:53:07 +00:00
|
|
|
.then(function () {
|
2020-04-25 10:47:23 +00:00
|
|
|
let osInitCommand = `os initialize '${tempPath}' --type ${application.device_type}`;
|
|
|
|
if (options.yes) {
|
|
|
|
osInitCommand += ' --yes';
|
|
|
|
}
|
|
|
|
if (options.drive) {
|
|
|
|
osInitCommand += ` --drive ${options.drive}`;
|
|
|
|
}
|
|
|
|
return runCommand(osInitCommand);
|
|
|
|
})
|
2020-06-15 22:53:07 +00:00
|
|
|
.catch((error) =>
|
|
|
|
balena.models.device.remove(device.uuid).finally(function () {
|
2020-04-25 10:47:23 +00:00
|
|
|
throw error;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}),
|
2020-06-15 22:53:07 +00:00
|
|
|
).then(function (device) {
|
2020-04-25 10:47:23 +00:00
|
|
|
console.log('Done');
|
|
|
|
return device.uuid;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|