Skip to content

Commit

Permalink
docs(examples): fix supply chain app container image hyperledger-cact…
Browse files Browse the repository at this point in the history
…i#1312

WORK IN PROGRESS
===============

Fixes hyperledger-cacti#1312

Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
  • Loading branch information
petermetz committed Sep 9, 2021
1 parent 5f45813 commit 8de3a86
Show file tree
Hide file tree
Showing 25 changed files with 532 additions and 109 deletions.
22 changes: 22 additions & 0 deletions .vscode/template.launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@
"console": "integratedTerminal",
"sourceMaps": true,
"cwd": "${workspaceRoot}"
},
{
"name": "Example: Supply Chain App",
"type": "node",
"request": "launch",
"protocol": "inspector",
"env": {
"TS_NODE_PROJECT": "${workspaceFolder}/tsconfig.json"
},
"args": [
"../cactus-example-supply-chain-backend/src/main/typescript/supply-chain-app-cli.ts",
"dotenv_config_path=process.env"
],
"runtimeArgs": [
"-r",
"ts-node/register",
"-r",
"dotenv/config"
],
"console": "integratedTerminal",
"sourceMaps": true,
"cwd": "${workspaceFolder}/examples/supply-chain-app/"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,9 @@ export class SupplyChainAppDummyInfrastructure {
public async start(): Promise<void> {
try {
this.log.info(`Starting dummy infrastructure...`);
await Promise.all([
this.besu.start(),
this.quorum.start(),
this.fabric.start(),
]);
await this.fabric.start();
await this.besu.start();
await this.quorum.start();
this.log.info(`Started dummy infrastructure OK`);
} catch (ex) {
this.log.error(`Starting of dummy infrastructure crashed: `, ex);
Expand All @@ -172,11 +170,11 @@ export class SupplyChainAppDummyInfrastructure {

await this.keychain.set(
BookshelfRepositoryJSON.contractName,
BookshelfRepositoryJSON.contractName,
JSON.stringify(BookshelfRepositoryJSON),
);
await this.keychain.set(
BambooHarvestRepositoryJSON.contractName,
BambooHarvestRepositoryJSON.contractName,
JSON.stringify(BambooHarvestRepositoryJSON),
);
{
this._quorumAccount = await this.quorum.createEthTestAccount(2000000);
Expand Down
65 changes: 7 additions & 58 deletions examples/supply-chain-app/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,55 +1,3 @@
FROM node:16.3.0-buster-slim as builder

RUN apt-get update
RUN apt-get -y upgrade

# Generic dependencies that are usually needed by other software
RUN apt-get install -y build-essential libssl-dev libffi-dev python3-dev

# Need git because some of our npm depedencies might be coming
# straight from github instead of being an npm package on npmjs.com.
RUN apt-get install -y git

# Need OpenJDK for the OpenAPI generator tool
ENV DEBIAN_FRONTEND=noninteractive

WORKDIR /opt
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
ca-certificates \
curl \
&& curl \
-L \
-o openjdk.tar.gz \
https://download.java.net/java/GA/jdk11/13/GPL/openjdk-11.0.1_linux-x64_bin.tar.gz \
&& mkdir jdk \
&& tar zxf openjdk.tar.gz -C jdk --strip-components=1 \
&& rm -rf openjdk.tar.gz \
&& ln -sf /opt/jdk/bin/* /usr/local/bin/ \
&& java --version \
&& javac --version \
&& jlink --version

# Some install scripts of the npm package fabric-network need python/pip
RUN apt-get install -y python3-pip

WORKDIR /
RUN mkdir /app/
WORKDIR /app/
COPY ./ ./
RUN npm ci
RUN ./node_modules/.bin/lerna clean --yes
RUN ./node_modules/.bin/lerna bootstrap
RUN npm rebuild
RUN npm run build:dev:backend
RUN npm run webpack:dev:web
RUN npm run build:dev:frontend -- --scope='@hyperledger/cactus-example-supply-chain-frontend'

RUN rm -rf ./packages/cactus-test-plugin*
RUN rm -rf ./packages/cactus-test-cmd*
RUN rm -rf ./packages/cactus-test-api*
RUN rm -rf ./node_modules/

FROM cruizba/ubuntu-dind:19.03.11 as runner

USER root
Expand All @@ -76,22 +24,23 @@ RUN useradd -m ${APP_USER}
RUN usermod -a -G ${APP_USER} ${APP_USER}
RUN mkdir -p ${APP}

COPY --chown=$APP_USER:$APP_USER --from=builder /app/ ${APP}

RUN mkdir -p "${APP}/log/"
RUN chown -R $APP_USER:$APP_USER "${APP}/log/"
RUN chown -R $APP_USER:$APP_USER "${APP}/"

# TODO: Can we hack it together so that the whole thing works rootless?
USER ${APP_USER}
WORKDIR ${APP}

SHELL ["/bin/bash", "--login", "-i", "-c"]

# Installing Node Version Manager (nvm)
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
RUN source ~/.bashrc && nvm install 16.3.0
RUN source ~/.bashrc && \
nvm install 16.8.0 && \
npm install -g yarn && \
yarn add @hyperledger/cactus-example-supply-chain-backend@0.9.1-docs-1312.94850924.6+94850924 --ignore-engines --production

SHELL ["/bin/bash", "--login", "-c"]

WORKDIR ${APP}

COPY --chown=${APP_USER}:${APP_USER} ./examples/supply-chain-app/healthcheck.sh /

Expand Down
2 changes: 1 addition & 1 deletion examples/supply-chain-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.2.0",
"description": "An example project demonstrating how Cactus can be used for cross-chain dapp development.",
"scripts": {
"start": "node -r dotenv/config ./node_modules/.bin/cactus-example-supply-chain-backend dotenv_config_path=process.env",
"start": "node -r dotenv/config node_modules/@hyperledger/cactus-example-supply-chain-backend/dist/lib/main/typescript/supply-chain-app-cli.js dotenv_config_path=process.env",
"start-direct": "node ./node_modules/@hyperledger/cactus-example-supply-chain-backend/dist/lib/main/typescript/supply-chain-app-cli.js"
},
"private": true,
Expand Down
4 changes: 3 additions & 1 deletion examples/supply-chain-app/process.env
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ API_TLS_CLIENT_CA_PEM=-
API_TLS_KEY_PEM=-
API_HOST=0.0.0.0
API_PORT=4000
LOG_LEVEL=DEBUG
GRPC_TLS_ENABLED=false
LOG_LEVEL=TRACE
AUTHORIZATION_PROTOCOL=NONE
AUTHORIZATION_CONFIG_JSON="{}"
4 changes: 3 additions & 1 deletion examples/supply-chain-app/supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ loglevel = info
command=/usr/local/bin/dockerd
autostart=true
autorestart=true
startretries=20
stderr_logfile=/usr/src/app/log/dockerd.err.log
stdout_logfile=/usr/src/app/log/dockerd.out.log

[program:supply-chain-app]
command=/home/appuser/.nvm/versions/node/v16.3.0/bin/node /usr/src/app/examples/cactus-example-supply-chain-backend/dist/lib/main/typescript/supply-chain-app-cli.js
command=/home/appuser/.nvm/versions/node/v16.8.0/bin/node /usr/src/app/node_modules/@hyperledger/cactus-example-supply-chain-backend/dist/lib/main/typescript/supply-chain-app-cli.js
autostart=true
autorestart=unexpected
startretries=20
exitcodes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"custom-checks": "TS_NODE_PROJECT=./tools/tsconfig.json node --trace-deprecation --experimental-modules --abort-on-uncaught-exception --loader ts-node/esm --experimental-specifier-resolution=node ./tools/custom-checks/run-custom-checks.ts",
"generate-api-server-config": "node ./tools/generate-api-server-config.js",
"start:api-server": "node ./packages/cactus-cmd-api-server/dist/lib/main/typescript/cmd/cactus-api.js --config-file=.config.json",
"start:example-supply-chain": "cd ./examples/supply-chain-app/ && npm i --no-package-lock && npm run start",
"start:example-supply-chain": "yarn build:dev && cd ./examples/supply-chain-app/ && yarn --no-lockfile && yarn start",
"start:example-carbon-accounting": "CONFIG_FILE=examples/cactus-example-carbon-accounting-backend/example-config.json node examples/cactus-example-carbon-accounting-backend/dist/lib/main/typescript/carbon-accounting-app-cli.js",
"purge-build-cache": "del-cli .build-cache/*",
"clean": "npm run purge-build-cache && del-cli \"./{packages,examples,extensions}/cactus-*/{dist,.nyc_output,src/main/proto/generated/*,src/main/typescript/generated/proto/protoc-gen-ts/*,src/main/typescript/generated/openapi/typescript-axios/*}\"",
Expand Down
1 change: 1 addition & 0 deletions packages/cactus-cmd-api-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"express-jwt": "6.0.0",
"express-openapi-validator": "3.10.0",
"fs-extra": "10.0.0",
"google-protobuf": "3.18.0-rc.2",
"jose": "1.28.1",
"lmify": "0.3.0",
"node-forge": "0.10.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/cactus-test-tooling/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"is-port-reachable": "3.0.0",
"joi": "17.4.2",
"keycloak-admin": "1.14.21",
"lodash": "4.17.21",
"node-ssh": "12.0.0",
"p-retry": "4.6.1",
"run-time-error": "1.4.0",
Expand All @@ -88,6 +89,7 @@
"@types/dockerode": "3.2.7",
"@types/esm": "3.2.0",
"@types/fs-extra": "9.0.12",
"@types/lodash": "4.14.172",
"@types/node-ssh": "7.0.1",
"@types/ssh2": "0.5.47",
"@types/ssh2-streams": "0.1.9",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class BesuMpTestLedger {
return container;
}
if (!omitPull) {
await Containers.pullImage(this.imageFqn);
await Containers.pullImage(this.imageFqn, {}, this.options.logLevel);
}

const dockerEnvVars: string[] = new Array(...this.envVars).map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class CactusKeychainVaultServer {
}
const docker = new Docker();

await Containers.pullImage(this.imageFqn);
await Containers.pullImage(this.imageFqn, {}, this.options.logLevel);

return new Promise<Container>((resolve, reject) => {
const eventEmitter: EventEmitter = docker.run(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from "path";
import { Duplex } from "stream";
import { Duplex, Stream } from "stream";
import { IncomingMessage } from "http";
import throttle from "lodash/throttle";
import { Container, ContainerInfo } from "dockerode";
import Dockerode from "dockerode";
import execa from "execa";
Expand All @@ -16,6 +17,7 @@ import {
Strings,
ILoggerOptions,
} from "@hyperledger/cactus-common";
import { IDockerPullProgress } from "./i-docker-pull-progress";

export interface IPruneDockerResourcesRequest {
logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -156,7 +158,7 @@ export class Containers {

const pack = tar.pack({ autoDestroy: true });

pack.entry({ name: opts.dstFileName }, fileAsString, (err: any) => {
pack.entry({ name: opts.dstFileName }, fileAsString, (err: unknown) => {
if (err) {
reject(err);
} else {
Expand All @@ -167,11 +169,11 @@ export class Containers {
});

return new Promise((resolve, reject) => {
const handler = (err: any, data: any) => {
const handler = (err: unknown, data: unknown) => {
if (err) {
reject(err);
} else {
resolve(data);
resolve(data as IncomingMessage);
}
};

Expand Down Expand Up @@ -210,12 +212,12 @@ export class Containers {
Checks.truthy(container, "Containers#pullFile() container");
Checks.truthy(filePath, "Containers#pullFile() filePath");

const response: any = await container.getArchive({ path: filePath });
const response = await container.getArchive({ path: filePath });
const extract: tar.Extract = tar.extract({ autoDestroy: true });

return new Promise((resolve, reject) => {
let fileContents = "";
extract.on("entry", async (header: any, stream, next) => {
extract.on("entry", async (header: unknown, stream, next) => {
stream.on("error", (err: Error) => {
reject(err);
});
Expand Down Expand Up @@ -247,12 +249,12 @@ export class Containers {
Checks.truthy(container, `${fnTag} container`);
Checks.truthy(filePath, `${fnTag} filePath`);

const response: any = await container.getArchive({ path: filePath });
const response = await container.getArchive({ path: filePath });
const extract: tar.Extract = tar.extract({ autoDestroy: true });

return new Promise((resolve, reject) => {
let buffer: Buffer;
extract.on("entry", async (header: any, stream, next) => {
extract.on("entry", async (header: unknown, stream, next) => {
stream.on("error", (err: Error) => {
reject(err);
});
Expand Down Expand Up @@ -404,9 +406,9 @@ export class Containers {
}
public static pullImage(
imageFqn: string,
options: any = {},
options: Record<string, unknown> = {},
logLevel?: LogLevelDesc,
): Promise<any[]> {
): Promise<unknown[]> {
const defaultLoggerOptions: ILoggerOptions = {
label: "containers#pullImage()",
level: logLevel || "INFO",
Expand All @@ -424,9 +426,9 @@ export class Containers {

public static tryPullImage(
imageFqn: string,
options: any = {},
options: Record<string, unknown> = {},
logLevel?: LogLevelDesc,
): Promise<any[]> {
): Promise<unknown[]> {
return new Promise((resolve, reject) => {
const loggerOptions: ILoggerOptions = {
label: "containers#tryPullImage()",
Expand All @@ -436,15 +438,19 @@ export class Containers {

const docker = new Dockerode();

const pullStreamStartedHandler = (pullError: any, stream: any) => {
const progressPrinter = throttle((msg: IDockerPullProgress): void => {
log.debug(JSON.stringify(msg.progress || msg.status));
}, 1000);

const pullStreamStartedHandler = (pullError: unknown, stream: Stream) => {
if (pullError) {
log.error(`Could not even start ${imageFqn} pull:`, pullError);
reject(pullError);
} else {
log.debug(`Started ${imageFqn} pull progress stream OK`);
docker.modem.followProgress(
stream,
(progressError: any, output: any[]) => {
(progressError: unknown, output: unknown[]) => {
if (progressError) {
log.error(`Failed to finish ${imageFqn} pull:`, progressError);
reject(progressError);
Expand All @@ -453,6 +459,7 @@ export class Containers {
resolve(output);
}
},
(msg: IDockerPullProgress): void => progressPrinter(msg),
);
}
};
Expand All @@ -461,11 +468,11 @@ export class Containers {
});
}

public static stop(container: Container): Promise<any> {
public static stop(container: Container): Promise<unknown> {
const fnTag = "Containers#stop()";
return new Promise((resolve, reject) => {
if (container) {
container.stop({}, (err: any, result: any) => {
container.stop({}, (err: unknown, result: unknown) => {
if (err) {
reject(err);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface IDockerPullProgressDetail {
readonly current: number;
readonly total: number;
}

export interface IDockerPullProgress {
readonly status: "Downloading";
readonly progressDetail: IDockerPullProgressDetail;
readonly progress: string;
readonly id: string;
}
Loading

0 comments on commit 8de3a86

Please sign in to comment.