Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/credebl/platform into qa
Browse files Browse the repository at this point in the history
  • Loading branch information
KulkarniShashank committed Feb 28, 2024
2 parents 0395c95 + bf5a752 commit 8bdf391
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 13 deletions.
40 changes: 30 additions & 10 deletions Dockerfiles/Dockerfile.agent-provisioning
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,23 @@ FROM node:18-alpine as build
# RUN apk add openssh-client
# RUN apk update
# RUN apk add aws-cli
RUN npm install -g pnpm --ignore-scripts \
&& apk update \
&& apk add openssh-client \
&& apk add aws-cli
RUN set -eux \
&& apk --no-cache add \
openssh-client \
aws-cli \
docker \
docker-compose \
&& npm install -g pnpm --ignore-scripts \
&& export PATH=$PATH:/usr/lib/node_modules/pnpm/bin \
&& rm -rf /var/cache/apk/*

RUN docker --version && \
docker-compose --version

ARG ROOT_PATH
ENV ROOT_PATH ${ROOT_PATH}

RUN echo "ROOT_PATH is set to: $ROOT_PATH"

# Set the working directory
WORKDIR /app
Expand Down Expand Up @@ -37,10 +50,15 @@ FROM node:18-alpine as prod
# RUN apk add openssh-client
# RUN apk update
# RUN apk add aws-cli
RUN npm install -g pnpm --ignore-scripts \
&& apk update \
&& apk add openssh-client \
&& apk add aws-cli
RUN set -eux \
&& apk --no-cache add \
openssh-client \
aws-cli \
docker \
docker-compose \
&& npm install -g pnpm --ignore-scripts \
&& export PATH=$PATH:/usr/lib/node_modules/pnpm/bin \
&& rm -rf /var/cache/apk/*

WORKDIR /app

Expand All @@ -49,6 +67,7 @@ RUN mkdir -p ./agent-provisioning/AFJ/agent-config
RUN mkdir -p ./agent-provisioning/AFJ/port-file
RUN mkdir -p ./agent-provisioning/AFJ/token


# Copy the compiled code
COPY --from=build /app/dist/apps/agent-provisioning/ ./dist/apps/agent-provisioning/
COPY --from=build /app/node_modules ./node_modules
Expand All @@ -58,15 +77,16 @@ COPY --from=build /app/apps/agent-provisioning/AFJ/port-file ./agent-provisionin
# Set permissions
RUN chmod +x /app/agent-provisioning/AFJ/scripts/start_agent.sh
RUN chmod +x /app/agent-provisioning/AFJ/scripts/start_agent_ecs.sh
RUN chmod +x /app/agent-provisioning/AFJ/scripts/docker_start_agent.sh
RUN chmod 777 /app/agent-provisioning/AFJ/endpoints
RUN chmod 777 /app/agent-provisioning/AFJ/agent-config
RUN chmod 777 /app/agent-provisioning/AFJ/token

# Copy the libs folder
COPY libs/ ./libs/

# Set the command to run the microservice
CMD ["sh", "-c", "cd libs/prisma-service && npx prisma migrate deploy && npx prisma generate && cd ../.. && node dist/apps/agent-provisioning/main.js"]

CMD ["sh", "-c", "cd libs/prisma-service && npx prisma migrate deploy && npx prisma generate && cd ../.. && node dist/apps/agent-provisioning/main.js $ROOT_PATH"]

# docker build -t agent-provisioning-service -f Dockerfiles/Dockerfile.agent-provisioning .
# docker run -d --env-file .env --name agent-provisioning-service docker.io/library/agent-provisioning-service
16 changes: 16 additions & 0 deletions Dockerfiles/Dockerfile.agent-service
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Stage 1: Build the application
FROM node:18-alpine as build

RUN npm install -g pnpm --ignore-scripts \
&& apk update \
&& apk add openssh-client \
&& apk add aws-cli \
&& apk add docker \
&& apk add docker-compose

RUN npm install -g pnpm
# Set the working directory
WORKDIR /app
Expand All @@ -19,6 +27,14 @@ RUN pnpm run build agent-service

# Stage 2: Create the final image
FROM node:18-alpine

RUN npm install -g pnpm --ignore-scripts \
&& apk update \
&& apk add openssh-client \
&& apk add aws-cli \
&& apk add docker \
&& apk add docker-compose

RUN npm install -g pnpm
# Set the working directory
WORKDIR /app
Expand Down
259 changes: 259 additions & 0 deletions apps/agent-provisioning/AFJ/scripts/docker_start_agent.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
#!/bin/sh

START_TIME=$(date +%s)

AGENCY=$1
EXTERNAL_IP=$2
WALLET_NAME=$3
WALLET_PASSWORD=$4
RANDOM_SEED=$5
WEBHOOK_HOST=$6
WALLET_STORAGE_HOST=$7
WALLET_STORAGE_PORT=$8
WALLET_STORAGE_USER=$9
WALLET_STORAGE_PASSWORD=${10}
CONTAINER_NAME=${11}
PROTOCOL=${12}
TENANT=${13}
AFJ_VERSION=${14}
INDY_LEDGER=${15}

echo "AGENCY: $AGENCY"
echo "EXTERNAL_IP: $EXTERNAL_IP"
echo "WALLET_NAME: $WALLET_NAME"
echo "WALLET_PASSWORD: $WALLET_PASSWORD"
echo "RANDOM_SEED: $RANDOM_SEED"
echo "WEBHOOK_HOST: $WEBHOOK_HOST"
echo "WALLET_STORAGE_HOST: $WALLET_STORAGE_HOST"
echo "WALLET_STORAGE_PORT: $WALLET_STORAGE_PORT"
echo "WALLET_STORAGE_USER: $WALLET_STORAGE_USER"
echo "WALLET_STORAGE_PASSWORD: $WALLET_STORAGE_PASSWORD"
echo "CONTAINER_NAME: $CONTAINER_NAME"
echo "PROTOCOL: $PROTOCOL"
echo "TENANT: $TENANT"
echo "AFJ_VERSION: $AFJ_VERSION"
echo "INDY_LEDGER: $INDY_LEDGER"

ADMIN_PORT_FILE="$PWD/agent-provisioning/AFJ/port-file/last-admin-port.txt"
INBOUND_PORT_FILE="$PWD/agent-provisioning/AFJ/port-file/last-inbound-port.txt"
ADMIN_PORT=8001
INBOUND_PORT=9001

increment_port() {
local port="$1"
local lower_limit="$2"

while [ "$port" -le "$lower_limit" ]; do
port=$((port + 1)) # Increment the port using arithmetic expansion
done

echo "$port"
}

# Check if admin port file exists and if not, create and initialize it
if [ ! -e "$ADMIN_PORT_FILE" ]; then
echo "$ADMIN_PORT" > "$ADMIN_PORT_FILE"
fi

# Read the last used admin port number from the file
last_used_admin_port=$(cat "$ADMIN_PORT_FILE")
echo "Last used admin port: $last_used_admin_port"

# Increment the admin port number starting from the last used port
last_used_admin_port=$(increment_port "$last_used_admin_port" "$last_used_admin_port")

# Save the updated admin port number back to the file and update the global variable
echo "$last_used_admin_port" > "$ADMIN_PORT_FILE"
ADMIN_PORT="$last_used_admin_port"

# Check if inbound port file exists and if not, create and initialize it
if [ ! -e "$INBOUND_PORT_FILE" ]; then
echo "$INBOUND_PORT" > "$INBOUND_PORT_FILE"
fi

# Read the last used inbound port number from the file
last_used_inbound_port=$(cat "$INBOUND_PORT_FILE")
echo "Last used inbound port: $last_used_inbound_port"

# Increment the inbound port number starting from the last used port
last_used_inbound_port=$(increment_port "$last_used_inbound_port" "$last_used_inbound_port")

# Save the updated inbound port number back to the file and update the global variable
echo "$last_used_inbound_port" > "$INBOUND_PORT_FILE"
INBOUND_PORT="$last_used_inbound_port"

echo "Last used admin port: $ADMIN_PORT"
echo "Last used inbound port: $INBOUND_PORT"

echo "AGENT SPIN-UP STARTED"

if [ -d "${PWD}/agent-provisioning/AFJ/endpoints" ]; then
echo "Endpoints directory exists."
else
echo "Error: Endpoints directory does not exists."
mkdir ${PWD}/agent-provisioning/AFJ/endpoints
fi

if [ -d "${PWD}/agent-provisioning/AFJ/agent-config" ]; then
echo "Endpoints directory exists."
else
echo "Error: Endpoints directory does not exists."
mkdir ${PWD}/agent-provisioning/AFJ/agent-config
fi

AGENT_ENDPOINT="${PROTOCOL}://${EXTERNAL_IP}:${INBOUND_PORT}"

echo "-----$AGENT_ENDPOINT----"
CONFIG_FILE="${PWD}/agent-provisioning/AFJ/agent-config/${AGENCY}_${CONTAINER_NAME}.json"

# Check if the file exists
if [ -f "$CONFIG_FILE" ]; then
# If it exists, remove the file
rm "$CONFIG_FILE"
fi


cat <<EOF >${CONFIG_FILE}
{
"label": "${AGENCY}_${CONTAINER_NAME}",
"walletId": "$WALLET_NAME",
"walletKey": "$WALLET_PASSWORD",
"walletType": "postgres",
"walletUrl": "$WALLET_STORAGE_HOST:$WALLET_STORAGE_PORT",
"walletAccount": "$WALLET_STORAGE_USER",
"walletPassword": "$WALLET_STORAGE_PASSWORD",
"walletAdminAccount": "$WALLET_STORAGE_USER",
"walletAdminPassword": "$WALLET_STORAGE_PASSWORD",
"walletScheme": "DatabasePerWallet",
"indyLedger": $INDY_LEDGER,
"endpoint": [
"$AGENT_ENDPOINT"
],
"autoAcceptConnections": true,
"autoAcceptCredentials": "contentApproved",
"autoAcceptProofs": "contentApproved",
"logLevel": 5,
"inboundTransport": [
{
"transport": "$PROTOCOL",
"port": "$INBOUND_PORT"
}
],
"outboundTransport": [
"$PROTOCOL"
],
"webhookUrl": "$WEBHOOK_HOST/wh/$AGENCY",
"adminPort": "$ADMIN_PORT",
"tenancy": $TENANT
}
EOF

FILE_NAME="docker-compose_${AGENCY}_${CONTAINER_NAME}.yaml"

DOCKER_COMPOSE="${PWD}/agent-provisioning/AFJ/${FILE_NAME}"

# Check if the file exists
if [ -f "$DOCKER_COMPOSE" ]; then
# If it exists, remove the file
rm "$DOCKER_COMPOSE"
fi
echo ${PWD}
cat <<EOF >${DOCKER_COMPOSE}
version: '3'
services:
agent:
image: $AFJ_VERSION
container_name: ${AGENCY}_${CONTAINER_NAME}
restart: always
environment:
AFJ_REST_LOG_LEVEL: 1
ROOT_PATH: ${ROOT_PATH}
ports:
- ${INBOUND_PORT}:${INBOUND_PORT}
- ${ADMIN_PORT}:${ADMIN_PORT}
volumes:
- ${ROOT_PATH}:/agent-config
command: --auto-accept-connections --config /agent-config/${AGENCY}_${CONTAINER_NAME}.json
volumes:
pgdata:
agent-indy_client:
agent-tmp:
EOF

if [ $? -eq 0 ]; then
cd agent-provisioning/AFJ
echo "docker-compose generated successfully!"
echo "================="
echo "spinning up the container"
echo "================="
echo "container-name::::::${CONTAINER_NAME}"
echo "file-name::::::$FILE_NAME"

docker compose -f $FILE_NAME up -d
if [ $? -eq 0 ]; then

n=0
until [ "$n" -ge 6 ]; do
if netstat -tln | grep ${ADMIN_PORT} >/dev/null; then

AGENTURL="http://${EXTERNAL_IP}:${ADMIN_PORT}/agent"
agentResponse=$(curl -s -o /dev/null -w "%{http_code}" $AGENTURL)

if [ "$agentResponse" = "200" ]; then
echo "Agent is running" && break
else
echo "Agent is not running"
n=$((n + 1))
sleep 10
fi
else
echo "No response from agent"
n=$((n + 1))
sleep 10
fi
done

echo "Creating agent config"
# Capture the logs from the container
container_logs=$(docker logs $(docker ps -q --filter "name=${AGENCY}_${CONTAINER_NAME}"))

# Extract the token from the logs using sed
token=$(echo "$container_logs" | sed -nE 's/.*API Toekn: ([^ ]+).*/\1/p')

# Print the extracted token
echo "Token: $token"

ENDPOINT="${PWD}/endpoints/${AGENCY}_${CONTAINER_NAME}.json"

# Check if the file exists
if [ -f "$ENDPOINT" ]; then
# If it exists, remove the file
rm "$ENDPOINT"
fi
cat <<EOF >${ENDPOINT}
{
"CONTROLLER_ENDPOINT":"${EXTERNAL_IP}:${ADMIN_PORT}"
}
EOF

cat <<EOF >${PWD}/token/${AGENCY}_${CONTAINER_NAME}.json
{
"token" : "$token"
}
EOF
echo "Agent config created"
else
echo "==============="
echo "ERROR : Failed to spin up the agent!"
echo "===============" && exit 125
fi
else
echo "ERROR : Failed to execute!" && exit 125
fi

echo "Total time elapsed: $(date -ud "@$(($(date +%s) - $START_TIME))" +%T) (HH:MM:SS)"
2 changes: 1 addition & 1 deletion apps/agent-provisioning/AFJ/scripts/start_agent.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh

START_TIME=$(date +%s)

Expand Down
4 changes: 3 additions & 1 deletion apps/api-gateway/src/agent-service/dto/create-tenant.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ export class CreateTenantDto {
})
seed: string;

@ApiProperty({ example: [1] })
@ApiProperty({ example: ["b942473d-6fdd-4a38-b76e-a3314fca66b6"] })
@ApiPropertyOptional()
@IsOptional()
@IsArray({ message: 'ledgerId must be an array' })
@IsNotEmpty({ message: 'please provide valid ledgerId' })
@IsString({ each: true, message: 'Each ledgerId must be a string' })
@MaxLength(36, { each: true, message: 'ledgerId must be at most 36 characters.' })
ledgerId?: string[];

@ApiProperty({ example: 'XzFjo1RTZ2h9UVFCnPUyaQ' })
Expand Down
Loading

0 comments on commit 8bdf391

Please sign in to comment.