-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add commands for exporting and importing app/fleet releases.
Change-type: minor Signed-off-by: Carlo Miguel F. Cruz <[email protected]>
- Loading branch information
Showing
12 changed files
with
339 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* @license | ||
* Copyright 2016-2024 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 { commitOrIdArg } from '.'; | ||
import { Flags } from '@oclif/core'; | ||
import Command from '../../command'; | ||
import * as cf from '../../utils/common-flags'; | ||
import { getBalenaSdk, stripIndent } from '../../utils/lazy'; | ||
import { create } from '@balena/release-bundle'; | ||
import * as fs from 'fs/promises'; | ||
import * as semver from 'balena-semver'; | ||
import { ExpectedError } from '../../errors'; | ||
|
||
export default class ReleaseExportCmd extends Command { | ||
public static description = stripIndent` | ||
Saves a release into a file. | ||
Saving a release to a file allows you to import an exact | ||
copy of the original release into another application. | ||
Only successful releases can be saved into a file. | ||
`; | ||
public static examples = [ | ||
'$ balena release export a777f7345fe3d655c1c981aa642e5555 -o ../path/to/release.tar', | ||
'$ balena release export 1234567 -o ../path/to/release.tar', | ||
'$ balena release export myOrg/myFleet:1.2.3 -o ../path/to/release.tar', | ||
]; | ||
|
||
public static usage = 'release export <commitOrId>'; | ||
|
||
public static flags = { | ||
output: Flags.string({ | ||
description: 'output path', | ||
char: 'o', | ||
required: true, | ||
}), | ||
help: cf.help, | ||
}; | ||
|
||
public static args = { | ||
commitOrId: commitOrIdArg({ | ||
description: 'commit, ID, or version of the release to export', | ||
required: true, | ||
}), | ||
}; | ||
|
||
public static authenticated = true; | ||
|
||
public async run() { | ||
const { args: params, flags: options } = await this.parse(ReleaseExportCmd); | ||
|
||
const balena = getBalenaSdk(); | ||
|
||
let release: balenaSdk.Release; | ||
if ( | ||
typeof params.commitOrId === 'string' && | ||
params.commitOrId.includes(':') | ||
) { | ||
const fleet = params.commitOrId.split(':')[0]; | ||
const parsedVersion = semver.parse(params.commitOrId.split(':')[1]); | ||
if (parsedVersion == null) { | ||
throw new ExpectedError( | ||
`Release ${params.commitOrId} could not be exported; version must be valid SemVer.`, | ||
); | ||
} else { | ||
const rawVersion = | ||
parsedVersion.build.length === 0 | ||
? parsedVersion.version | ||
: `${parsedVersion.version}+${parsedVersion.build[0]}`; | ||
release = await balena.models.release.get( | ||
{ application: fleet, rawVersion }, | ||
{ $select: ['id'] }, | ||
); | ||
} | ||
} else { | ||
release = await balena.models.release.get(params.commitOrId, { | ||
$select: ['id'], | ||
}); | ||
} | ||
|
||
try { | ||
const releaseBundle = await create({ | ||
sdk: balena, | ||
releaseId: release.id, | ||
}); | ||
await fs.writeFile(options.output, releaseBundle); | ||
console.log( | ||
`Release ${params.commitOrId} has been exported to ${options.output}.`, | ||
); | ||
} catch (error) { | ||
throw new ExpectedError( | ||
`Release ${params.commitOrId} could not be exported: ${error.message}`, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* @license | ||
* Copyright 2016-2024 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 { Flags, Args } from '@oclif/core'; | ||
import Command from '../../command'; | ||
import * as cf from '../../utils/common-flags'; | ||
import { getBalenaSdk, stripIndent } from '../../utils/lazy'; | ||
import { apply } from '@balena/release-bundle'; | ||
import { createReadStream } from 'fs'; | ||
import { ExpectedError } from '../../errors'; | ||
|
||
export default class ReleaseImportCmd extends Command { | ||
public static description = stripIndent` | ||
Imports a release from a file to an application or fleet. | ||
To save a release into a file, use 'balena release export'. | ||
Use the --override-version option to specify the version | ||
of the imported release, overriding the one saved in the file. | ||
`; | ||
public static examples = [ | ||
'$ balena release import ../path/to/release.tar -f 1234567', | ||
'$ balena release import ../path/to/release.tar -f myFleet', | ||
'$ balena release import ../path/to/release.tar -f myOrg/myFleet', | ||
'$ balena release import ../path/to/release.tar -f myOrg/myFleet -V 1.2.3', | ||
]; | ||
|
||
public static usage = 'release import <file>'; | ||
|
||
public static flags = { | ||
fleet: { ...cf.fleet, exclusive: ['device'] }, | ||
'override-version': Flags.string({ | ||
description: | ||
'Imports this release with the specified version overriding the version in the file.', | ||
char: 'V', | ||
required: false, | ||
}), | ||
help: cf.help, | ||
}; | ||
|
||
public static args = { | ||
bundle: Args.string({ | ||
required: true, | ||
description: 'path to a file, e.g. "release.tar"', | ||
}), | ||
}; | ||
|
||
public static authenticated = true; | ||
|
||
public async run() { | ||
const { args: params, flags: options } = await this.parse(ReleaseImportCmd); | ||
|
||
const balena = getBalenaSdk(); | ||
|
||
const bundle = createReadStream(params.bundle); | ||
|
||
try { | ||
if ( | ||
typeof options.fleet !== 'number' && | ||
typeof options.fleet !== 'string' | ||
) { | ||
throw new ExpectedError('Fleet must be a number or slug.'); | ||
} | ||
|
||
// TODO: validate if the path to the release bundle exists | ||
|
||
const application = await balena.models.application.get(options.fleet, { | ||
$select: ['id'], | ||
}); | ||
await apply({ | ||
sdk: balena, | ||
application: application.id, | ||
stream: bundle, | ||
version: options['override-version'], | ||
}); | ||
console.log( | ||
`Release bundle ${params.bundle} has been imported to ${options.fleet}.`, | ||
); | ||
} catch (error) { | ||
throw new ExpectedError( | ||
`Could not import release bundle ${params.bundle} to fleet ${options.fleet}. ${error.message}`, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.