Merge pull request #1693 from balena-io/convert-internal

Convert `internal scandevices`, `internal osinit` to typescript & oclif
This commit is contained in:
srlowe 2020-04-03 17:00:07 +02:00 committed by GitHub
commit 9812239862
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 142 additions and 63 deletions

View File

@ -0,0 +1,81 @@
/**
* @license
* 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 { stripIndent } from 'common-tags';
import Command from '../../command';
import { CommandHelp } from '../../utils/oclif-utils';
// 'Internal' commands are called during the execution of other commands.
// `osinit` is called during `os initialize`
// TODO: These should be refactored to modules/functions, and removed
// See previous `internal sudo` refactor:
// - https://github.com/balena-io/balena-cli/pull/1455/files
// - https://github.com/balena-io/balena-cli/pull/1455#discussion_r334308357
// - https://github.com/balena-io/balena-cli/pull/1455#discussion_r334308526
interface ArgsDef {
image: string;
type: string;
config: string;
}
export default class OsinitCmd extends Command {
public static description = stripIndent`
Do actual init of the device with the preconfigured os image.
Don't use this command directly!
Use \`balena os initialize <image>\` instead.
`;
public static args = [
{
name: 'image',
required: true,
},
{
name: 'type',
required: true,
},
{
name: 'config',
required: true,
},
];
public static usage = (
'internal osinit ' +
new CommandHelp({ args: OsinitCmd.args }).defaultUsage()
).trim();
public static hidden = true;
public static root = true;
public async run() {
const { args: params } = this.parse<{}, ArgsDef>(OsinitCmd);
const { initialize } = await import('balena-device-init');
const { getManifest, osProgressHandler } = await import(
'../../utils/helpers'
);
const config = JSON.parse(params.config);
const manifest = await getManifest(params.image, params.type);
const initializeEmitter = await initialize(params.image, manifest, config);
await osProgressHandler(initializeEmitter);
}
}

View File

@ -0,0 +1,46 @@
/**
* @license
* 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 { stripIndent } from 'common-tags';
import Command from '../../command';
// 'Internal' commands are called during the execution of other commands.
// `scandevices` is called during by `join`,`leave'.
// TODO: These should be refactored to modules/functions, and removed
// See previous `internal sudo` refactor:
// - https://github.com/balena-io/balena-cli/pull/1455/files
// - https://github.com/balena-io/balena-cli/pull/1455#discussion_r334308357
// - https://github.com/balena-io/balena-cli/pull/1455#discussion_r334308526
export default class ScandevicesCmd extends Command {
public static description = stripIndent`
Scan for local balena-enabled devices and show a picker to choose one.
Don't use this command directly!
`;
public static usage = 'internal scandevices';
public static root = true;
public static hidden = true;
public async run() {
const { forms } = await import('balena-sync');
const hostnameOrIp = await forms.selectLocalBalenaOsDevice();
return console.error(`==> Selected device: ${hostnameOrIp}`);
}
}

View File

@ -36,10 +36,12 @@ export async function getOclifHelpLinePairs() {
const cmdModule = await import(path.join(...pathComponents));
const command: typeof Command = cmdModule.default;
if (command.primary) {
primary.push(getCmdUsageDescriptionLinePair(command));
} else {
secondary.push(getCmdUsageDescriptionLinePair(command));
if (!command.hidden) {
if (command.primary) {
primary.push(getCmdUsageDescriptionLinePair(command));
} else {
secondary.push(getCmdUsageDescriptionLinePair(command));
}
}
}

View File

@ -28,7 +28,6 @@ module.exports =
os: require('./os')
config: require('./config')
ssh: require('./ssh')
internal: require('./internal')
build: require('./build')
deploy: require('./deploy')
util: require('./util')

View File

@ -1,54 +0,0 @@
###
Copyright 2016-2017 Balena
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.
###
# These are internal commands we want to be runnable from the outside
# One use-case for this is spawning the minimal operation with root priviledges
exports.osInit =
signature: 'internal osinit <image> <type> <config>'
description: 'do actual init of the device with the preconfigured os image'
help: '''
Don't use this command directly! Use `balena os initialize <image>` instead.
'''
hidden: true
root: true
action: (params) ->
Promise = require('bluebird')
init = require('balena-device-init')
helpers = require('../utils/helpers')
configPromise = Promise.try -> JSON.parse(params.config)
manifestPromise = helpers.getManifest(params.image, params.type)
Promise.join configPromise, manifestPromise, (config, manifest) ->
init.initialize(params.image, manifest, config)
.then(helpers.osProgressHandler)
exports.scanDevices =
signature: 'internal scandevices'
description: 'scan for local balena-enabled devices and show a picker to choose one'
help: '''
Don't use this command directly!
'''
hidden: true
root: true
action: ->
Promise = require('bluebird')
{ forms } = require('balena-sync')
return Promise.try ->
forms.selectLocalBalenaOsDevice()
.then (hostnameOrIp) ->
console.error("==> Selected device: #{hostnameOrIp}")

View File

@ -118,10 +118,6 @@ capitano.command(actions.scan)
# ---------- Public utils ----------
capitano.command(actions.util.availableDrives)
# ---------- Internal utils ----------
capitano.command(actions.internal.osInit)
capitano.command(actions.internal.scanDevices)
#------------ Local build and deploy -------
capitano.command(actions.build)
capitano.command(actions.deploy)

View File

@ -131,6 +131,8 @@ export const convertedCommands = [
'env:add',
'env:rename',
'env:rm',
'internal:scandevices',
'internal:osinit',
'note',
'os:configure',
'settings',

View File

@ -19,4 +19,10 @@ declare module 'balena-sync' {
import { CommandDefinition } from 'capitano';
export function capitano(tool: 'balena-cli'): CommandDefinition;
declare namespace forms {
export function selectLocalBalenaOsDevice(
timeout?: number,
): Promise<string>;
}
}

View File

@ -41,6 +41,7 @@ declare module 'capitano' {
permission?: 'user';
root?: boolean;
primary?: boolean;
hidden?: boolean;
action(params: P, options: Partial<O>, done: () => void): void;
}