2020-10-14 10:02:02 +00:00
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
import type { flags } from '@oclif/command';
|
|
|
|
import type { Output as ParserOutput } from '@oclif/parser';
|
|
|
|
|
2020-10-14 10:02:02 +00:00
|
|
|
import Command from '../../command';
|
|
|
|
import * as cf from '../../utils/common-flags';
|
2020-12-10 12:30:17 +00:00
|
|
|
import * as ca from '../../utils/common-args';
|
2020-10-14 10:02:02 +00:00
|
|
|
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
|
2021-07-15 13:41:38 +00:00
|
|
|
import {
|
|
|
|
applicationIdInfo,
|
|
|
|
appToFleetCmdMsg,
|
|
|
|
warnify,
|
|
|
|
} from '../../utils/messages';
|
2020-10-14 10:02:02 +00:00
|
|
|
|
|
|
|
interface FlagsDef {
|
|
|
|
help: void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ArgsDef {
|
2021-07-15 13:41:38 +00:00
|
|
|
fleet: string;
|
2020-10-14 10:02:02 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
export class FleetPurgeCmd extends Command {
|
2020-10-14 10:02:02 +00:00
|
|
|
public static description = stripIndent`
|
2021-07-15 13:41:38 +00:00
|
|
|
Purge data from a fleet.
|
2020-10-14 10:02:02 +00:00
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
Purge data from all devices belonging to a fleet.
|
|
|
|
This will clear the fleet's '/data' directory.
|
2020-12-10 12:30:17 +00:00
|
|
|
|
|
|
|
${applicationIdInfo.split('\n').join('\n\t\t')}
|
|
|
|
`;
|
|
|
|
|
|
|
|
public static examples = [
|
2021-07-15 13:41:38 +00:00
|
|
|
'$ balena fleet purge MyFleet',
|
|
|
|
'$ balena fleet purge myorg/myfleet',
|
2020-10-14 10:02:02 +00:00
|
|
|
];
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
public static args = [ca.fleetRequired];
|
2020-12-10 12:30:17 +00:00
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
public static usage = 'fleet purge <fleet>';
|
2020-10-14 10:02:02 +00:00
|
|
|
|
|
|
|
public static flags: flags.Input<FlagsDef> = {
|
|
|
|
help: cf.help,
|
|
|
|
};
|
|
|
|
|
|
|
|
public static authenticated = true;
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
public async run(parserOutput?: ParserOutput<FlagsDef, ArgsDef>) {
|
|
|
|
const { args: params } =
|
|
|
|
parserOutput || this.parse<FlagsDef, ArgsDef>(FleetPurgeCmd);
|
2020-12-10 12:30:17 +00:00
|
|
|
|
|
|
|
const { getApplication } = await import('../../utils/sdk');
|
2020-10-14 10:02:02 +00:00
|
|
|
|
|
|
|
const balena = getBalenaSdk();
|
|
|
|
|
|
|
|
// balena.models.application.purge only accepts a numeric id
|
2020-12-10 12:30:17 +00:00
|
|
|
// so we must first fetch the app to get it's id,
|
2021-07-15 13:41:38 +00:00
|
|
|
const application = await getApplication(balena, params.fleet);
|
2020-10-14 10:02:02 +00:00
|
|
|
|
|
|
|
try {
|
2020-12-10 12:30:17 +00:00
|
|
|
await balena.models.application.purge(application.id);
|
2020-10-14 10:02:02 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (e.message.toLowerCase().includes('no online device(s) found')) {
|
|
|
|
// application.purge throws an error if no devices are online
|
|
|
|
// ignore in this case.
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-15 13:41:38 +00:00
|
|
|
|
|
|
|
export default class AppPurgeCmd extends FleetPurgeCmd {
|
|
|
|
public static description = stripIndent`
|
|
|
|
DEPRECATED alias for the 'fleet purge' command
|
|
|
|
|
|
|
|
${appToFleetCmdMsg
|
|
|
|
.split('\n')
|
|
|
|
.map((l) => `\t\t${l}`)
|
|
|
|
.join('\n')}
|
|
|
|
|
|
|
|
For command usage, see 'balena help fleet purge'
|
|
|
|
`;
|
|
|
|
public static examples = [];
|
|
|
|
public static usage = 'app purge <fleet>';
|
|
|
|
public static args = FleetPurgeCmd.args;
|
|
|
|
public static flags = FleetPurgeCmd.flags;
|
|
|
|
public static authenticated = FleetPurgeCmd.authenticated;
|
|
|
|
public static primary = FleetPurgeCmd.primary;
|
|
|
|
|
|
|
|
public async run() {
|
|
|
|
// call this.parse() before deprecation message to parse '-h'
|
|
|
|
const parserOutput = this.parse<FlagsDef, ArgsDef>(AppPurgeCmd);
|
|
|
|
if (process.stderr.isTTY) {
|
|
|
|
console.error(warnify(appToFleetCmdMsg));
|
|
|
|
}
|
|
|
|
await super.run(parserOutput);
|
|
|
|
}
|
|
|
|
}
|