Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

Commit

Permalink
Update environment registries (#1562)
Browse files Browse the repository at this point in the history
Update to make environment registries use the file based registry

contributes to #1517

Signed-off-by: Caroline Fletcher <caroline.fletcher@uk.ibm.com>
  • Loading branch information
cazfletch authored and Jakeeyturner committed Oct 17, 2019
1 parent 3e08ea8 commit 3974c14
Show file tree
Hide file tree
Showing 54 changed files with 434 additions and 321 deletions.
3 changes: 2 additions & 1 deletion configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export class SettingConfigurations {
static readonly FABRIC_RUNTIME: string = 'ibm-blockchain-platform.fabric.runtime';
static readonly FABRIC_CLIENT_TIMEOUT: string = 'ibm-blockchain-platform.fabric.client.timeout';
static readonly FABRIC_CHAINCODE_TIMEOUT: string = 'ibm-blockchain-platform.fabric.chaincode.timeout';
static readonly FABRIC_ENVIRONMENTS: string = 'ibm-blockchain-platform.fabric.environments';

// Needed for migration
static readonly OLD_FABRIC_WALLETS: string = 'ibm-blockchain-platform.fabric.wallets';
static readonly OLD_FABRIC_GATEWAYS: string = 'ibm-blockchain-platform.fabric.gateways';
static readonly OLD_ENVIRONMENTS: string = 'ibm-blockchain-platform.fabric.environments';

// EXTENSION CONFIGURATIONS
static readonly EXTENSION_DIRECTORY: string = 'ibm-blockchain-platform.ext.directory';
Expand All @@ -40,4 +40,5 @@ export class SettingConfigurations {
export class FileConfigurations {
static readonly FABRIC_WALLETS: string = 'wallets';
static readonly FABRIC_GATEWAYS: string = 'gateways';
static readonly FABRIC_ENVIRONMENTS: string = 'environments';
}
2 changes: 0 additions & 2 deletions cucumber/helpers/environmentHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { BlockchainEnvironmentExplorerProvider } from '../../extension/explorer/
import { UserInputUtil } from '../../extension/commands/UserInputUtil';
import { FabricEnvironmentRegistryEntry } from '../../extension/registries/FabricEnvironmentRegistryEntry';
import { FabricRuntimeUtil } from '../../extension/fabric/FabricRuntimeUtil';
import { FabricWalletUtil } from '../../extension/fabric/FabricWalletUtil';
import { FabricEnvironmentRegistry } from '../../extension/registries/FabricEnvironmentRegistry';
import { FabricWalletRegistryEntry } from '../../extension/registries/FabricWalletRegistryEntry';
import { FabricNode } from '../../extension/fabric/FabricNode';
Expand Down Expand Up @@ -125,7 +124,6 @@ export class EnvironmentHelper {
registryEntry = new FabricEnvironmentRegistryEntry();
registryEntry.name = FabricRuntimeUtil.LOCAL_FABRIC;
registryEntry.managedRuntime = true;
registryEntry.associatedWallet = FabricWalletUtil.LOCAL_WALLET;
} else {
registryEntry = await FabricEnvironmentRegistry.instance().get(environment);
}
Expand Down
3 changes: 2 additions & 1 deletion cucumber/steps/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { EnvironmentHelper } from '../helpers/environmentHelper';
import { SampleHelper } from '../helpers/sampleHelper';
import { FabricWalletRegistry } from '../../extension/registries/FabricWalletRegistry';
import { FabricGatewayRegistry } from '../../extension/registries/FabricGatewayRegistry';
import { FabricEnvironmentRegistry } from '../../extension/registries/FabricEnvironmentRegistry';

// tslint:disable:no-unused-expression

Expand Down Expand Up @@ -102,13 +103,13 @@ module.exports = function(): any {
await TestUtil.storeRuntimesConfig();
await TestUtil.storeExtensionDirectoryConfig();
await TestUtil.storeRepositoriesConfig();
await TestUtil.storeEnvironmentsConfig();

await vscode.workspace.getConfiguration().update(SettingConfigurations.EXTENSION_DIRECTORY, extDir, vscode.ConfigurationTarget.Global);
await vscode.workspace.getConfiguration().update(SettingConfigurations.EXTENSION_REPOSITORIES, [], vscode.ConfigurationTarget.Global);

await FabricWalletRegistry.instance().clear();
await FabricGatewayRegistry.instance().clear();
await FabricEnvironmentRegistry.instance().clear();

await ExtensionUtil.activateExtension();

Expand Down
13 changes: 2 additions & 11 deletions extension/commands/UserInputUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { FabricGatewayRegistryEntry } from '../registries/FabricGatewayRegistryE
import { MetadataUtil } from '../util/MetadataUtil';
import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter';
import { LogType } from '../logging/OutputAdapter';
import { FabricRuntimeManager } from '../fabric/FabricRuntimeManager';
import { FabricGatewayRegistry } from '../registries/FabricGatewayRegistry';
import { FabricCertificate } from '../fabric/FabricCertificate';
import { FabricWalletRegistryEntry } from '../registries/FabricWalletRegistryEntry';
Expand Down Expand Up @@ -131,17 +130,9 @@ export class UserInputUtil {
placeHolder: prompt
};

const allEnvironments: Array<FabricEnvironmentRegistryEntry> = [];
const environments: FabricEnvironmentRegistryEntry[] = await FabricEnvironmentRegistry.instance().getAll(showLocalFabric);

if (showLocalFabric) {
const runtimeEnvironment: FabricEnvironmentRegistryEntry = await FabricRuntimeManager.instance().getEnvironmentRegistryEntry();
allEnvironments.push(runtimeEnvironment);
}

const environments: FabricEnvironmentRegistryEntry[] = await FabricEnvironmentRegistry.instance().getAll();
allEnvironments.push(...environments);

const environmentsQuickPickItems: Array<IBlockchainQuickPickItem<FabricEnvironmentRegistryEntry>> = allEnvironments.map((environment: FabricEnvironmentRegistryEntry) => {
const environmentsQuickPickItems: Array<IBlockchainQuickPickItem<FabricEnvironmentRegistryEntry>> = environments.map((environment: FabricEnvironmentRegistryEntry) => {
let label: string = environment.name;
if (environment.name === FabricRuntimeUtil.LOCAL_FABRIC) {
label = FabricRuntimeUtil.LOCAL_FABRIC_DISPLAY_NAME;
Expand Down
2 changes: 1 addition & 1 deletion extension/commands/associateIdentityWithNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function associateIdentityWithNode(replace: boolean = false, enviro
let identityName: string;
if (!environmentRegistryEntry || !node) {
// If called from command palette
const environments: Array<FabricEnvironmentRegistryEntry> = await FabricEnvironmentRegistry.instance().getAll();
const environments: Array<FabricEnvironmentRegistryEntry> = await FabricEnvironmentRegistry.instance().getAll(false);
if (environments.length === 0) {
if (!replace) {
outputAdapter.log(LogType.ERROR, `Add an environment to associate identities with nodes. ${FabricRuntimeUtil.LOCAL_FABRIC} cannot be editted.`);
Expand Down
11 changes: 1 addition & 10 deletions extension/commands/deleteEnvironmentCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@
*/
'use strict';
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs-extra';
import { UserInputUtil, IBlockchainQuickPickItem } from './UserInputUtil';
import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter';
import { LogType } from '../logging/OutputAdapter';
import { SettingConfigurations } from '../../configurations';
import { FabricRuntimeUtil } from '../fabric/FabricRuntimeUtil';
import { FabricEnvironmentTreeItem } from '../explorer/runtimeOps/disconnectedTree/FabricEnvironmentTreeItem';
import { FabricEnvironmentRegistryEntry } from '../registries/FabricEnvironmentRegistryEntry';
import { FabricEnvironmentRegistry } from '../registries/FabricEnvironmentRegistry';
import { FabricEnvironmentManager } from '../fabric/FabricEnvironmentManager';
import { ExtensionCommands } from '../../ExtensionCommands';
import { FileSystemUtil } from '../util/FileSystemUtil';

export async function deleteEnvironment(environment: FabricEnvironmentTreeItem | FabricEnvironmentRegistryEntry, force: boolean = false): Promise<void> {
const outputAdapter: VSCodeBlockchainOutputAdapter = VSCodeBlockchainOutputAdapter.instance();
Expand All @@ -37,7 +33,7 @@ export async function deleteEnvironment(environment: FabricEnvironmentTreeItem |
// If called from command palette
// Ask for environment to delete
// First check there is at least one that isn't local_fabric
const environments: Array<FabricEnvironmentRegistryEntry> = await FabricEnvironmentRegistry.instance().getAll();
const environments: Array<FabricEnvironmentRegistryEntry> = await FabricEnvironmentRegistry.instance().getAll(false);
if (environments.length === 0) {
outputAdapter.log(LogType.ERROR, `No environments to delete. ${FabricRuntimeUtil.LOCAL_FABRIC} cannot be deleted.`);
return;
Expand Down Expand Up @@ -67,16 +63,11 @@ export async function deleteEnvironment(environment: FabricEnvironmentTreeItem |
}
}

const extDir: string = vscode.workspace.getConfiguration().get(SettingConfigurations.EXTENSION_DIRECTORY);
const homeExtDir: string = FileSystemUtil.getDirPath(extDir);

const connectedRegistry: FabricEnvironmentRegistryEntry = FabricEnvironmentManager.instance().getEnvironmentRegistryEntry();
for (const _environment of environmentsToDelete) {
if (connectedRegistry && connectedRegistry.name === _environment.name) {
await vscode.commands.executeCommand(ExtensionCommands.DISCONNECT_ENVIRONMENT);
}
const environmentPath: string = path.join(homeExtDir, 'environments', _environment.name);
await fs.remove(environmentPath);

await FabricEnvironmentRegistry.instance().delete(_environment.name);
}
Expand Down
2 changes: 1 addition & 1 deletion extension/commands/deleteNodeCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function deleteNode(nodeTreeItem: NodeTreeItem): Promise<void> {
// If called from command palette
// Ask for environment to delete
// First check there is at least one that isn't local_fabric
const environments: Array<FabricEnvironmentRegistryEntry> = await FabricEnvironmentRegistry.instance().getAll();
const environments: Array<FabricEnvironmentRegistryEntry> = await FabricEnvironmentRegistry.instance().getAll(false);
if (environments.length === 0) {
outputAdapter.log(LogType.ERROR, `No environments to choose from. Nodes from ${FabricRuntimeUtil.LOCAL_FABRIC} cannot be deleted.`);
return;
Expand Down
2 changes: 1 addition & 1 deletion extension/commands/dissociateWalletCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export async function dissociateWallet(associatedGatewayTreeItem: GatewayAssocia
const fabricGatewayRegistry: FabricGatewayRegistry = FabricGatewayRegistry.instance();
gateway.associatedWallet = '';
await fabricGatewayRegistry.update(gateway);
outputAdapter.log(LogType.SUCCESS, `Successfully dissociated wallet from "${gateway.name}" gateway`);

outputAdapter.log(LogType.SUCCESS, `Successfully dissociated wallet from "${gateway.name}" gateway`);
} catch (error) {
outputAdapter.log(LogType.ERROR, `Unable to dissociate wallet: ${error.message}`, `Unable to dissociate wallet: ${error.toString()}`);
}
Expand Down
5 changes: 3 additions & 2 deletions extension/debug/FabricDebugConfigurationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { FabricEnvironmentManager } from '../fabric/FabricEnvironmentManager';
import { FabricEnvironmentRegistryEntry } from '../registries/FabricEnvironmentRegistryEntry';
import { GlobalState, ExtensionData } from '../util/GlobalState';
import { FabricChaincode } from '../fabric/FabricChaincode';
import { FabricEnvironmentRegistry } from '../registries/FabricEnvironmentRegistry';

export abstract class FabricDebugConfigurationProvider implements vscode.DebugConfigurationProvider {

Expand All @@ -49,13 +50,13 @@ export abstract class FabricDebugConfigurationProvider implements vscode.DebugCo
let environmentRegistryEntry: FabricEnvironmentRegistryEntry = FabricEnvironmentManager.instance().getEnvironmentRegistryEntry();
if (!environmentRegistryEntry.managedRuntime) {
await vscode.commands.executeCommand(ExtensionCommands.DISCONNECT_ENVIRONMENT);
environmentRegistryEntry = FabricRuntimeManager.instance().getEnvironmentRegistryEntry();
environmentRegistryEntry = await FabricEnvironmentRegistry.instance().get(FabricRuntimeUtil.LOCAL_FABRIC);
await vscode.commands.executeCommand(ExtensionCommands.CONNECT_TO_ENVIRONMENT, environmentRegistryEntry);
connection = await FabricEnvironmentManager.instance().getConnection();

}
} else {
const environmentRegistryEntry: FabricEnvironmentRegistryEntry = FabricRuntimeManager.instance().getEnvironmentRegistryEntry();
const environmentRegistryEntry: FabricEnvironmentRegistryEntry = await FabricEnvironmentRegistry.instance().get(FabricRuntimeUtil.LOCAL_FABRIC);
await vscode.commands.executeCommand(ExtensionCommands.CONNECT_TO_ENVIRONMENT, environmentRegistryEntry);
connection = await FabricEnvironmentManager.instance().getConnection();
}
Expand Down
50 changes: 25 additions & 25 deletions extension/explorer/environmentExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,35 +187,35 @@ export class BlockchainEnvironmentExplorerProvider implements BlockchainExplorer

const tree: BlockchainTreeItem[] = [];

const runtime: FabricRuntime = FabricRuntimeManager.instance().getRuntime();

try {
const fabricEnvironmentRegistryEntry: FabricEnvironmentRegistryEntry = FabricRuntimeManager.instance().getEnvironmentRegistryEntry();

const treeItem: RuntimeTreeItem = await RuntimeTreeItem.newRuntimeTreeItem(this,
runtime.getName(),
fabricEnvironmentRegistryEntry,
{
command: ExtensionCommands.CONNECT_TO_ENVIRONMENT,
title: '',
arguments: [fabricEnvironmentRegistryEntry]
}
);
tree.push(treeItem);

const environmentEntries: FabricEnvironmentRegistryEntry[] = await FabricEnvironmentRegistry.instance().getAll();
for (const environmentEntry of environmentEntries) {
const environmentTreeItem: FabricEnvironmentTreeItem = new FabricEnvironmentTreeItem(this,
environmentEntry.name,
environmentEntry,
{
command: ExtensionCommands.CONNECT_TO_ENVIRONMENT,
title: '',
arguments: [environmentEntry]
}
);
if (environmentEntry.name === FabricRuntimeUtil.LOCAL_FABRIC) {
const runtime: FabricRuntime = FabricRuntimeManager.instance().getRuntime();
const treeItem: RuntimeTreeItem = await RuntimeTreeItem.newRuntimeTreeItem(this,
runtime.getName(),
environmentEntry,
{
command: ExtensionCommands.CONNECT_TO_ENVIRONMENT,
title: '',
arguments: [environmentEntry]
}
);
tree.push(treeItem);

tree.push(environmentTreeItem);
} else {
const environmentTreeItem: FabricEnvironmentTreeItem = new FabricEnvironmentTreeItem(this,
environmentEntry.name,
environmentEntry,
{
command: ExtensionCommands.CONNECT_TO_ENVIRONMENT,
title: '',
arguments: [environmentEntry]
}
);

tree.push(environmentTreeItem);
}
}
} catch (error) {
outputAdapter.log(LogType.ERROR, `Error populating Fabric Environment Panel: ${error.message}`, `Error populating Fabric Environment Panel: ${error.toString()}`);
Expand Down
3 changes: 2 additions & 1 deletion extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>

// Remove managedWallet boolean from wallets in user settings
// Ensure wallets are stored correctly
outputAdapter.log(LogType.INFO, undefined, 'Tidying wallet and gateway settings');
outputAdapter.log(LogType.INFO, undefined, 'Tidying wallet, gateway and environment settings');
await FabricWalletUtil.tidyWalletSettings();
// Ensure gateways are stored correctly
await FabricGatewayHelper.migrateGateways();
await ExtensionUtil.migrateEnvironments();
}

await GlobalState.update(newExtensionData);
Expand Down
4 changes: 2 additions & 2 deletions extension/fabric/FabricEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import * as fs from 'fs-extra';
import * as vscode from 'vscode';
import { EventEmitter } from 'events';
import { FabricNode, FabricNodeType } from './FabricNode';
import { SettingConfigurations } from '../../configurations';
import { SettingConfigurations, FileConfigurations } from '../../configurations';
import { FileSystemUtil } from '../util/FileSystemUtil';

export class FabricEnvironment extends EventEmitter {
Expand All @@ -30,7 +30,7 @@ export class FabricEnvironment extends EventEmitter {
const extDir: string = vscode.workspace.getConfiguration().get(SettingConfigurations.EXTENSION_DIRECTORY);
const resolvedExtDir: string = FileSystemUtil.getDirPath(extDir);
this.name = name;
this.path = path.join(resolvedExtDir, 'environments', name);
this.path = path.join(resolvedExtDir, FileConfigurations.FABRIC_ENVIRONMENTS, name);
}

public getName(): string {
Expand Down
15 changes: 11 additions & 4 deletions extension/fabric/FabricRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import { FabricWalletRegistry } from '../registries/FabricWalletRegistry';
import { FabricGatewayRegistryEntry } from '../registries/FabricGatewayRegistryEntry';
import { FabricWalletUtil } from './FabricWalletUtil';
import { FabricGatewayRegistry } from '../registries/FabricGatewayRegistry';
import { FabricEnvironmentRegistry } from '../registries/FabricEnvironmentRegistry';
import { FabricEnvironmentRegistryEntry } from '../registries/FabricEnvironmentRegistryEntry';

export enum FabricRuntimeState {
STARTING = 'starting',
Expand Down Expand Up @@ -76,8 +78,14 @@ export class FabricRuntime extends FabricEnvironment {
public async create(): Promise<void> {

// Delete any existing runtime directory, and then recreate it.
await fs.remove(this.path);
await fs.ensureDir(this.path);
await FabricEnvironmentRegistry.instance().delete(this.name, true);

const registryEntry: FabricEnvironmentRegistryEntry = new FabricEnvironmentRegistryEntry({
name: this.name,
managedRuntime: true
});

await FabricEnvironmentRegistry.instance().add(registryEntry);

// Use Yeoman to generate a new network configuration.
await YeomanUtil.run('fabric:network', {
Expand All @@ -91,7 +99,6 @@ export class FabricRuntime extends FabricEnvironment {
couchDB: this.ports.couchDB,
logspout: this.ports.logs
});

}

public async importWalletsAndIdentities(): Promise<void> {
Expand Down Expand Up @@ -292,7 +299,7 @@ export class FabricRuntime extends FabricEnvironment {
}

public async isCreated(): Promise<boolean> {
return await fs.pathExists(this.path);
return FabricEnvironmentRegistry.instance().exists(this.name);
}

public async isGenerated(): Promise<boolean> {
Expand Down
11 changes: 0 additions & 11 deletions extension/fabric/FabricRuntimeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import * as vscode from 'vscode';
import { FabricRuntime } from './FabricRuntime';
import { FabricRuntimePorts } from './FabricRuntimePorts';
import { FabricWalletUtil } from './FabricWalletUtil';
import * as semver from 'semver';
import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter';
import { CommandUtil } from '../util/CommandUtil';
Expand Down Expand Up @@ -68,7 +67,6 @@ export class FabricRuntimeManager {
// Check to see if the runtime has been created.
const created: boolean = await this.runtime.isCreated();
if (!created) {

// Nope - create it now.
await this.runtime.create();

Expand All @@ -93,15 +91,6 @@ export class FabricRuntimeManager {
});
}

public getEnvironmentRegistryEntry(): FabricEnvironmentRegistryEntry {
const runtime: FabricRuntime = this.getRuntime();
return new FabricEnvironmentRegistryEntry({
name: runtime.getName(),
managedRuntime: true,
associatedWallet: FabricWalletUtil.LOCAL_WALLET
});
}

public async migrate(oldVersion: string): Promise<void> {
const runtimeSetting: any = await this.migrateRuntimesConfiguration();
await this.migrateRuntimeConfiguration(runtimeSetting);
Expand Down
Loading

0 comments on commit 3974c14

Please sign in to comment.