2020-04-06 08:53:34 +00:00
|
|
|
import * as livepush from 'livepush';
|
2021-04-26 19:54:04 +00:00
|
|
|
import { promises as fs } from 'fs';
|
2020-04-06 08:53:34 +00:00
|
|
|
import * as yargs from 'yargs';
|
|
|
|
|
2021-04-26 19:54:04 +00:00
|
|
|
import * as packageJson from '../package.json';
|
2020-04-06 08:53:34 +00:00
|
|
|
import * as device from './device';
|
|
|
|
import * as init from './init';
|
|
|
|
import { startLivepush } from './livepush';
|
|
|
|
import { setupLogs } from './logs';
|
|
|
|
|
|
|
|
const helpText = `Sync changes code to a running supervisor on a device on the local network
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
npm run sync <device IP>
|
|
|
|
`;
|
|
|
|
|
2024-04-23 22:54:37 +00:00
|
|
|
interface ArgV {
|
|
|
|
'device-address': string;
|
|
|
|
'device-arch': string;
|
|
|
|
'image-name': string;
|
|
|
|
'image-tag': string;
|
|
|
|
nocache: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const argv =
|
|
|
|
// TypeScript doesn't infer `device-address` to be part of argv so we cast it
|
|
|
|
yargs
|
|
|
|
.command(
|
|
|
|
'$0 <device-address>',
|
|
|
|
'Sync changes in code to a running debug mode supervisor on a local device',
|
|
|
|
(y) =>
|
|
|
|
y.positional('device-address', {
|
|
|
|
type: 'string',
|
|
|
|
describe: 'The address of a local device',
|
|
|
|
demandOption: true,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.option('device-arch', {
|
|
|
|
alias: 'a',
|
|
|
|
type: 'string',
|
|
|
|
description:
|
|
|
|
'Specify the device architecture (use this when the automatic detection fails)',
|
|
|
|
choices: ['amd64', 'i386', 'aarch64', 'armv7hf', 'rpi'],
|
|
|
|
demandOption: true,
|
|
|
|
})
|
|
|
|
.options('image-name', {
|
|
|
|
alias: 'i',
|
|
|
|
type: 'string',
|
|
|
|
description: 'Specify the name to use for the supervisor image on device',
|
|
|
|
default: `livepush-supervisor-${packageJson.version}`,
|
|
|
|
})
|
|
|
|
.options('image-tag', {
|
|
|
|
alias: 't',
|
|
|
|
type: 'string',
|
|
|
|
description:
|
|
|
|
'Specify the tag to use for the supervisor image on device. It will not have any effect on balenaOS >= v2.89.0',
|
|
|
|
default: 'latest',
|
|
|
|
deprecated: true,
|
|
|
|
})
|
|
|
|
.options('nocache', {
|
|
|
|
description: 'Run the intial build without cache',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
|
|
|
})
|
|
|
|
.usage(helpText)
|
|
|
|
.version(false)
|
|
|
|
.scriptName('npm run sync --')
|
|
|
|
.alias('h', 'help').argv as unknown as ArgV;
|
2020-04-06 08:53:34 +00:00
|
|
|
|
2024-03-05 18:14:37 +00:00
|
|
|
void (async () => {
|
2020-04-06 08:53:34 +00:00
|
|
|
const address = argv['device-address']!;
|
2021-11-10 15:20:17 +00:00
|
|
|
const dockerfile = new livepush.Dockerfile(
|
|
|
|
await fs.readFile('Dockerfile.template'),
|
|
|
|
);
|
2020-04-06 08:53:34 +00:00
|
|
|
|
2022-07-06 19:51:49 +00:00
|
|
|
let cleanup = () => Promise.resolve();
|
|
|
|
let sigint = () => {
|
|
|
|
/** ignore empty */
|
|
|
|
};
|
|
|
|
|
2020-04-06 08:53:34 +00:00
|
|
|
try {
|
|
|
|
const docker = device.getDocker(address);
|
2022-07-06 19:51:49 +00:00
|
|
|
const { containerId, stageImages } = await init.initDevice({
|
2020-04-06 08:53:34 +00:00
|
|
|
address,
|
|
|
|
docker,
|
|
|
|
dockerfile,
|
|
|
|
arch: argv['device-arch'],
|
|
|
|
nocache: argv['nocache'],
|
2024-04-23 22:54:37 +00:00
|
|
|
imageName: argv['image-name'],
|
|
|
|
imageTag: argv['image-tag'],
|
2020-04-06 08:53:34 +00:00
|
|
|
});
|
2021-03-17 18:26:46 +00:00
|
|
|
// Another newline to separate build and livepush output
|
2020-04-07 09:50:59 +00:00
|
|
|
console.log(`Supervisor container: ${containerId}\n`);
|
2020-04-06 08:53:34 +00:00
|
|
|
|
|
|
|
await setupLogs(docker, containerId);
|
2022-07-06 19:51:49 +00:00
|
|
|
cleanup = await startLivepush({
|
2020-04-06 08:53:34 +00:00
|
|
|
dockerfile,
|
|
|
|
containerId,
|
|
|
|
docker,
|
|
|
|
noinit: true,
|
2022-07-06 19:51:49 +00:00
|
|
|
stageImages,
|
|
|
|
});
|
|
|
|
|
|
|
|
await new Promise((_, reject) => {
|
|
|
|
sigint = () => reject(new Error('User interrupt (Ctrl+C) received'));
|
|
|
|
process.on('SIGINT', sigint);
|
2020-04-06 08:53:34 +00:00
|
|
|
});
|
2022-09-20 14:56:22 +00:00
|
|
|
} catch (e: any) {
|
2022-07-06 19:51:49 +00:00
|
|
|
console.error('Error:', e.message);
|
|
|
|
} finally {
|
|
|
|
console.info('Cleaning up. Please wait ...');
|
|
|
|
await cleanup();
|
|
|
|
process.removeListener('SIGINT', sigint);
|
2022-09-20 14:56:22 +00:00
|
|
|
process.exit(0);
|
2020-04-06 08:53:34 +00:00
|
|
|
}
|
|
|
|
})();
|