Skip to content

Commit

Permalink
Generate local Fabric with correct port configuration - code changes …
Browse files Browse the repository at this point in the history
…(contributes to IBM-Blockchain#776)

Signed-off-by: Simon Stone <sstone1@uk.ibm.com>
  • Loading branch information
Simon Stone committed Apr 24, 2019
1 parent f67eea6 commit 9533379
Show file tree
Hide file tree
Showing 24 changed files with 449 additions and 469 deletions.
35 changes: 17 additions & 18 deletions client/src/commands/connectCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ export async function connect(gatewayRegistryEntry: FabricGatewayRegistryEntry,
gatewayRegistryEntry = chosenEntry.data;
}

if (gatewayRegistryEntry.managedRuntime) {

const runtimeManager: FabricRuntimeManager = FabricRuntimeManager.instance();
const runtime: FabricRuntime = runtimeManager.getRuntime();
const running: boolean = await runtime.isRunning();
if (!running) {
await vscode.commands.executeCommand(ExtensionCommands.START_FABRIC);
if (!(await runtimeManager.getRuntime().isRunning())) {
// Start local_fabric failed so return
return;
}
}

runtimeData = 'managed runtime';
}

let wallet: IFabricWallet;
let walletPath: string;
let walletName: string;
Expand Down Expand Up @@ -108,27 +124,10 @@ export async function connect(gatewayRegistryEntry: FabricGatewayRegistryEntry,
identityName = identityNames[0];
}

let connection: IFabricClientConnection;
if (gatewayRegistryEntry.managedRuntime) {

const runtimeManager: FabricRuntimeManager = FabricRuntimeManager.instance();
const runtime: FabricRuntime = runtimeManager.getRuntime();
const running: boolean = await runtime.isRunning();
if (!running) {
await vscode.commands.executeCommand(ExtensionCommands.START_FABRIC);
if (!(await runtimeManager.getRuntime().isRunning())) {
// Start local_fabric failed so return
return;
}
}

runtimeData = 'managed runtime';
}

const connectionData: { connectionProfilePath: string } = {
connectionProfilePath: gatewayRegistryEntry.connectionProfilePath
};
connection = FabricConnectionFactory.createFabricClientConnection(connectionData);
const connection: IFabricClientConnection = FabricConnectionFactory.createFabricClientConnection(connectionData);

try {
await connection.connect(wallet, identityName);
Expand Down
20 changes: 4 additions & 16 deletions client/src/commands/createNewIdentityCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import { LogType } from '../logging/OutputAdapter';
import { UserInputUtil } from './UserInputUtil';
import { FabricRuntimeManager } from '../fabric/FabricRuntimeManager';
import { IFabricWallet } from '../fabric/IFabricWallet';
import { FabricRuntime } from '../fabric/FabricRuntime';
import { FabricConnectionFactory } from '../fabric/FabricConnectionFactory';
import { IFabricRuntimeConnection } from '../fabric/IFabricRuntimeConnection';

export async function createNewIdentity(certificateAuthorityTreeItem?: CertificateAuthorityTreeItem): Promise<void> {
Expand Down Expand Up @@ -56,40 +54,30 @@ export async function createNewIdentity(certificateAuthorityTreeItem?: Certifica
return;
}

let connection: IFabricRuntimeConnection;

try {
const mspid: string = 'Org1MSP';
const affiliation: string = 'org1.department1'; // Currently works for org1.department1, org1.department2
// check to see if identity of same name exists
const wallet: IFabricWallet = FabricRuntimeManager.instance().gatewayWallet;
const connection: IFabricRuntimeConnection = await FabricRuntimeManager.instance().getConnection();
const wallet: IFabricWallet = await connection.getWallet(certificateAuthorityName);
const identityExists: boolean = await wallet.exists(identityName);
if (identityExists) {
outputAdapter.log(LogType.ERROR, `An identity called ${identityName} already exists in the runtime wallet`, `An identity called ${identityName} already exists in the runtime wallet`);
return;
}

const runtime: FabricRuntime = FabricRuntimeManager.instance().getRuntime();
connection = FabricConnectionFactory.createFabricRuntimeConnection(runtime);
// Connect and then register the user
await connection.connect();
// Register the user
const secret: string = await connection.register(certificateAuthorityName, identityName, affiliation);

// Enroll the user
const details: { certificate: string, privateKey: string } = await connection.enroll(certificateAuthorityName, identityName, secret);

// Import the new identity to the gateway wallet (no -ops in the name)
// Import the new identity to the wallet
await wallet.importIdentity(details.certificate, details.privateKey, identityName, mspid);

await vscode.commands.executeCommand(ExtensionCommands.REFRESH_WALLETS);
outputAdapter.log(LogType.SUCCESS, 'Successfully added identity', `Successfully added ${identityName} to runtime gateway`);

connection.disconnect();
return;
} catch (error) {
outputAdapter.log(LogType.ERROR, `Issue creating new identity: ${error.message}`, `Issue creating new identity: ${error.toString()}`);

connection.disconnect();
return;
}
}
15 changes: 2 additions & 13 deletions client/src/commands/createSmartContractProjectCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutput
import { CommandUtil } from '../util/CommandUtil';
import * as path from 'path';
import { UserInputUtil, LanguageQuickPickItem } from './UserInputUtil';
import * as yeoman from 'yeoman-environment';
import { YeomanAdapter } from '../util/YeomanAdapter';
import * as util from 'util';
import { ExtensionUtil } from '../util/ExtensionUtil';
import { LogType } from '../logging/OutputAdapter';
import * as GeneratorFabric from 'generator-fabric';
import * as GeneratorFabricPackageJSON from 'generator-fabric/package.json';
import { YeomanUtil } from '../util/YeomanUtil';

const localize: nls.LocalizeFunc = nls.loadMessageBundle();

Expand Down Expand Up @@ -79,15 +76,7 @@ export async function createSmartContractProject(): Promise<void> {
}

try {
// tslint:disable-next-line
let env = yeoman.createEnv([], {}, new YeomanAdapter());
env.registerStub(GeneratorFabric, 'fabric');

env.lookup = util.promisify(env.lookup);
env.run = util.promisify(env.run);
await env.lookup();

// tslint:disable-next-line
const packageJson: any = ExtensionUtil.getPackageJSON();
const runOptions: any = {
'destination': folderPath,
Expand All @@ -106,7 +95,7 @@ export async function createSmartContractProject(): Promise<void> {
cancellable: false
}, async (progress: vscode.Progress<{message: string}>): Promise<void> => {
progress.report({message: 'Generating smart contract project'});
await env.run(generator, runOptions);
await YeomanUtil.run(generator, runOptions);
});

outputAdapter.log(LogType.SUCCESS, 'Successfully generated smart contract project');
Expand Down
15 changes: 11 additions & 4 deletions client/src/commands/exportConnectionProfileCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@
import * as vscode from 'vscode';
import { IBlockchainQuickPickItem, UserInputUtil } from './UserInputUtil';
import { FabricRuntimeManager } from '../fabric/FabricRuntimeManager';
import { FabricRuntime } from '../fabric/FabricRuntime';
import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter';
import * as path from 'path';
import { LogType } from '../logging/OutputAdapter';
import { FabricGatewayRegistryEntry } from '../fabric/FabricGatewayRegistryEntry';
import * as fs from 'fs-extra';
import { FabricRuntimeUtil } from '../fabric/FabricRuntimeUtil';

export async function exportConnectionProfile(): Promise<void> {
const outputAdapter: VSCodeBlockchainOutputAdapter = VSCodeBlockchainOutputAdapter.instance();

const fabricRuntime: FabricRuntime = FabricRuntimeManager.instance().getRuntime();
// Assume there's only one registry entry for now.
const runtimeGatewayRegistryEntries: FabricGatewayRegistryEntry[] = await FabricRuntimeManager.instance().getGatewayRegistryEntries();
const runtimeGatewayRegistryEntry: FabricGatewayRegistryEntry = runtimeGatewayRegistryEntries[0];

let dir: string;
const workspaceFolders: Array<vscode.WorkspaceFolder> = UserInputUtil.getWorkspaceFolders();
Expand All @@ -44,10 +48,13 @@ export async function exportConnectionProfile(): Promise<void> {
}

try {
await fabricRuntime.exportConnectionProfile(VSCodeBlockchainOutputAdapter.instance(), dir);
dir = path.resolve(dir, FabricRuntimeUtil.LOCAL_FABRIC);
await fs.ensureDir(dir);
const targetConnectionProfilePath: string = path.resolve(dir, 'connection.json');
await fs.copyFile(runtimeGatewayRegistryEntry.connectionProfilePath, targetConnectionProfilePath);
} catch (error) {
outputAdapter.log(LogType.ERROR, 'Issue exporting connection profile, see output channel for more information');
return;
}
outputAdapter.log(LogType.SUCCESS, `Successfully exported connection profile to ${path.join(dir, fabricRuntime.getName())}`);
outputAdapter.log(LogType.SUCCESS, `Successfully exported connection profile to ${dir}`);
}
2 changes: 1 addition & 1 deletion client/src/commands/openFabricRuntimeTerminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function openFabricRuntimeTerminal(): Promise<void> {

const name: string = `Fabric runtime - ${runtime.getName()}`;
const shellPath: string = 'docker';
const peerContainerName: string = runtime.getPeerContainerName();
const peerContainerName: string = await runtime.getPeerContainerName();
const shellArgs: string[] = [
'exec',
'-e',
Expand Down
8 changes: 5 additions & 3 deletions client/src/commands/startFabricRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ export async function startFabricRuntime(): Promise<void> {
cancellable: false
}, async (progress: vscode.Progress<{ message: string }>) => {
progress.report({ message: `Starting Fabric runtime ${runtime.getName()}` });
const generated: boolean = await runtime.isGenerated();
if (!generated) {
await runtime.generate(outputAdapter);
}
await runtime.start(outputAdapter);

// ensure the wallets are populated properly before the gateway view uses it
await FabricRuntimeManager.instance().getConnection();
await runtime.importWalletsAndIdentities();

await vscode.commands.executeCommand(ExtensionCommands.REFRESH_LOCAL_OPS);
await vscode.commands.executeCommand(ExtensionCommands.REFRESH_GATEWAYS);
Expand Down
1 change: 1 addition & 0 deletions client/src/commands/teardownFabricRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export async function teardownFabricRuntime(): Promise<void> {
await vscode.commands.executeCommand(ExtensionCommands.DISCONNECT);
}
await runtime.teardown(outputAdapter);
await runtime.deleteWalletsAndIdentities();
await vscode.commands.executeCommand(ExtensionCommands.REFRESH_LOCAL_OPS);
await vscode.commands.executeCommand(ExtensionCommands.REFRESH_GATEWAYS);
await vscode.commands.executeCommand(ExtensionCommands.REFRESH_WALLETS);
Expand Down
2 changes: 1 addition & 1 deletion client/src/debug/FabricDebugConfigurationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export abstract class FabricDebugConfigurationProvider implements vscode.DebugCo
protected abstract async getChaincodeName(folder: vscode.WorkspaceFolder | undefined): Promise<string>;

protected async getChaincodeAddress(): Promise<string> {
return this.runtime.getChaincodeAddress();
return this.runtime.getPeerChaincodeURL();
}

protected abstract async resolveDebugConfigurationInner(folder: vscode.WorkspaceFolder | undefined, config: vscode.DebugConfiguration, token?: vscode.CancellationToken): Promise<vscode.DebugConfiguration>;
Expand Down
74 changes: 0 additions & 74 deletions client/src/docker/Docker.ts

This file was deleted.

18 changes: 1 addition & 17 deletions client/src/explorer/model/LocalGatewayTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export class LocalGatewayTreeItem extends BlockchainTreeItem {
constructor(provider: BlockchainExplorerProvider, public readonly label: string, public gateway: FabricGatewayRegistryEntry, public readonly collapsableState: vscode.TreeItemCollapsibleState, public readonly command?: vscode.Command) {
super(provider, label, collapsableState);
const runtimeManager: FabricRuntimeManager = FabricRuntimeManager.instance();
this.name = gateway.name;
this.runtime = runtimeManager.getRuntime();
this.name = this.runtime.getName();
this.runtime.on('busy', () => {
this.safelyUpdateProperties();
});
Expand All @@ -60,12 +60,10 @@ ${FabricWalletUtil.LOCAL_WALLET}`;

private async updateProperties(): Promise<void> {
const busy: boolean = this.runtime.isBusy();
const created: boolean = await this.runtime.isCreated();
const running: boolean = await this.runtime.isRunning();
const developmentMode: boolean = this.runtime.isDevelopmentMode();
let newLabel: string = this.name + ' ';
let newCommand: vscode.Command = this.command;
let newContextLabel: string;
if (busy) {
// Busy!
this.enableBusyTicker();
Expand All @@ -74,7 +72,6 @@ ${FabricWalletUtil.LOCAL_WALLET}`;
this.tooltip = `${this.label}
${this.tooltip}`;
newCommand = null;
newContextLabel = 'blockchain-local-gateway-item-busy';
} else if (running) {
// Running!
this.disableBusyTicker();
Expand All @@ -84,27 +81,18 @@ ${this.tooltip}`;
newLabel += '●';
this.tooltip = `Local Fabric is running
${this.tooltip}`;

newContextLabel = 'blockchain-local-gateway-item-started';
} else {
// Not running!
this.disableBusyTicker();
newLabel += '○';
this.tooltip = `Local Fabric is not running
${this.tooltip}`;

if (created) {
newContextLabel = 'blockchain-local-gateway-item-stopped';
} else {
newContextLabel = 'blockchain-local-gateway-item-removed';
}
}
if (developmentMode) {
newLabel += ' ∞';
}
this.setLabel(newLabel);
this.setCommand(newCommand);
this.setContextValue(newContextLabel);
this.refresh();
}

Expand All @@ -118,10 +106,6 @@ ${this.tooltip}`;
(this as any).command = command;
}

private setContextValue(contextValue: string): void {
this.contextValue = contextValue;
}

private enableBusyTicker(): void {
if (!this.busyTicker) {
this.busyTicker = setInterval(() => {
Expand Down
Loading

0 comments on commit 9533379

Please sign in to comment.