This repository was archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
FAB-30 Added deploy snapshot command to export agent snapshot and generate manifest file #239
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,48 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| /* | ||
| * Copyright 2018 Cognitive Scale, Inc. All Rights Reserved. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| const chalk = require('chalk'); | ||
| const program = require('../src/commander'); | ||
|
|
||
| const { withCompatibilityCheck } = require('../src/compatibility'); | ||
|
|
||
| const { | ||
| DeploySnapshotCommand | ||
| } = require('../src/commands/deploy'); | ||
|
|
||
| program.description('Export Cortex artifacts for deployment'); | ||
|
|
||
| // Export Agent Snapshot. Provide snapshotIds in quotes separated by space like "snapshotId1 snapshotId2". This is to support multiple agents in one deployment manifest file. | ||
| program | ||
| .command('snapshots <snapshotIds>') | ||
| .description('Export Agent(s) snapshots(s) for deployment. Provide snapshotIds separated by space, as <"snapshotId1 snapshotId2 ...">') | ||
| .option('--no-compat', 'Ignore API compatibility checks') | ||
| .option('--color [on/off]', 'Turn on/off colors for JSON output.', 'on') | ||
| .option('--profile [profile]', 'The profile to use') | ||
| .option('-y, --yaml', 'Use YAML for snapshot export format') | ||
| .option('-f, --force', 'Force delete existing exported files') | ||
| .action(withCompatibilityCheck((skillDefinition, options) => { | ||
| try { | ||
| new DeploySnapshotCommand(program).execute(skillDefinition, options); | ||
| } | ||
| catch (err) { | ||
| console.error(chalk.red(err.message)); | ||
| } | ||
| })); | ||
|
|
||
| program.parse(process.argv); | ||
This file contains hidden or 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 hidden or 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,103 @@ | ||
| /* | ||
| * Copyright 2018 Cognitive Scale, Inc. All Rights Reserved. | ||
| * | ||
| * 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. | ||
| */ | ||
| const debug = require('debug')('cortex:cli'); | ||
| const {loadProfile} = require('../config'); | ||
| const path = require('path'); | ||
| const Agents = require('../client/agents'); | ||
| const {printSuccess, printError, filterObject, cleanInternalFields, jsonToYaml, writeToFile, fileExists, deleteFile} = require('./utils'); | ||
|
|
||
|
|
||
| /** | ||
| * Cortex deploy command is to export Cortex artifacts (agent, snapshot, skill, action etc) for CI/CD deployment. This command: | ||
| * 1. Exports cortex artifacts | ||
| * 2. generates manifest files to drive artifacts deployment | ||
| * Files are exported/generated in pre-determined layout to support CI/CD deployment | ||
| * .fabric | ||
| * snapshots | ||
| * snapshot1.yaml | ||
| * snapshot2.yaml | ||
| * agents | ||
| * ... | ||
| * skills | ||
| * ... | ||
| * actions | ||
| * ... | ||
| * fabric.yaml (manifest file) | ||
| * | ||
| * Currently only exporting agent snapshots. | ||
| * | ||
| * @type {DeploySnapshotCommand} | ||
| */ | ||
| module.exports.DeploySnapshotCommand = class { | ||
| constructor(program) { | ||
| this.program = program; | ||
| } | ||
|
|
||
| execute(snapshotIds, options) { | ||
| const exportPath = '.fabric'; | ||
| const manifestFile = 'fabric.yaml'; | ||
|
|
||
| if (fileExists(exportPath) || fileExists(manifestFile)) { | ||
| if (options.force) { | ||
| deleteFile(exportPath); | ||
| deleteFile(manifestFile); | ||
| printSuccess(`Deleted ${exportPath} and ${manifestFile}`); | ||
| } else { | ||
| printError(`Aborting, because export path ${exportPath} or manifest file ${manifestFile} exists. Use -f option to force delete.`); | ||
| } | ||
| } | ||
| const profile = loadProfile(options.profile); | ||
| const envName = options.environmentName; | ||
| debug('%s.exportDeploymentSnapshot(%s)', profile.name, snapshotIds); | ||
|
|
||
| const agents = new Agents(profile.url); | ||
| const promises = []; | ||
| snapshotIds.split(' ').forEach(function (snapshotId) { | ||
| promises.push(agents.describeAgentSnapshot(profile.token, snapshotId, envName).then((response) => { | ||
| if (response.success) { | ||
| let result = filterObject(response.result, options); | ||
| result = cleanInternalFields(result); | ||
| //TODO add validation for not exporting tip snapshots, as they don't have all dependencies. Should we enforce? | ||
| let filename = snapshotId + ".json"; | ||
| if (options.yaml) { | ||
| result = jsonToYaml(result); | ||
| filename = snapshotId + ".yaml"; | ||
| } | ||
| const filepath = path.join(exportPath, 'snapshots', filename); | ||
| writeToFile(result, filepath); | ||
| printSuccess(`Successfully exported agent snapshot ${filepath}`); | ||
| return filepath; | ||
| } else { | ||
| printError(`Failed to export agent snapshot ${snapshotId}: ${response.message}`, options); | ||
| } | ||
| }).catch((err) => { | ||
| printError(`Failed to export agent snapshot ${snapshotId}: ${err.status} ${err.message}`, options); | ||
| })); | ||
| }); | ||
|
|
||
| Promise.all(promises).then(result => { | ||
| const manifest = { | ||
| "version": 1, | ||
| "kind": "deployment-manifest", | ||
| "cortex": { | ||
| "snapshots": result | ||
| } | ||
| }; | ||
| writeToFile(jsonToYaml(manifest), manifestFile); | ||
| printSuccess(`Successfully generated manifest file ${manifestFile}`); | ||
| }); | ||
| } | ||
| }; |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does
fabricaccept both yaml or JSON ?Just make yaml the default if that is what
fabricaccepts.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,
fabricaccepts Agent Snapshot in both YAML and JSON (determines format based on file extension). Should I make YAML default and add-j/--jsonflag for JSON?-yis aligned with other commands.