Skip to content

Commit 44f9348

Browse files
authored
Merge pull request #19 from topcoder-platform/feature/docker-setup-improve
Improve local setup
2 parents a3a1ab3 + c70bd7d commit 44f9348

File tree

6 files changed

+165
-64
lines changed

6 files changed

+165
-64
lines changed

README.md

Lines changed: 124 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,122 @@
77
- ElasticSearch
88
- Docker, Docker Compose
99

10+
## Local setup
11+
12+
1. Install node dependencies:
13+
14+
```bash
15+
npm install
16+
```
17+
18+
2. Run docker compose with dependant services:
19+
20+
```bash
21+
cd local/
22+
docker-compose up
23+
```
24+
25+
<details><summary>Click to see details</summary>
26+
27+
This docker-compose run all the dependencies which are necessary for `project-processor-es` to work.
28+
29+
| Service | Name | Port |
30+
|----------|:-----:|:----:|
31+
| Elasticsearch | esearch | 9200 |
32+
| Zookeeper | zookeeper | 2181 |
33+
| Kafka | kafka | 9092 |
34+
35+
`docker-compose` automatically creates Kafka topics which are used by `project-processor-es` listed in `local/kafka-client/topics.txt`.
36+
37+
</details>
38+
39+
40+
3. Set environment variables for M2M authentication: `AUTH0_CLIENT_ID`, `AUTH0_CLIENT_SECRET`, `AUTH0_URL`, `AUTH0_AUDIENCE`, `AUTH0_PROXY_SERVER_URL`:
41+
42+
```bash
43+
export AUTH0_CLIENT_ID=<insert required value here>
44+
export AUTH0_CLIENT_SECRET=<insert required value here>
45+
export AUTH0_URL=<insert required value here>
46+
export AUTH0_AUDIENCE=<insert required value here>
47+
export AUTH0_PROXY_SERVER_URL=<insert required value here>
48+
```
49+
50+
4. Initialize Elasticsearch indexes:
51+
52+
```bash
53+
npm run sync:es
54+
```
55+
56+
5. Start processor app:
57+
58+
```bash
59+
npm start
60+
```
61+
62+
## Commands
63+
64+
### Lint & Tests commands
65+
66+
| Command | Description |
67+
|----------|--------------|
68+
| `npm run lint` | Run lint check. |
69+
| `npm run lin:fix` | Run lint check with automatic fixing of errors and warnings where possible. |
70+
| `npm run test` | Run integration tests. |
71+
| `npm run test:cov` | Run integration tests with coverage report. |
72+
73+
### View data in Elasticsearch indexes
74+
75+
You may run the next command to output documents in the Elasticsearch indexes for debugging purposes.
76+
77+
```bash
78+
npm run view-data <INDEX_NAME> <DOCUMENT_ID>
79+
```
80+
81+
##### Examples
82+
83+
- `npm run view-data projects 1` view document with id `1` in `projects` index
84+
- `npm run view-data timelines 1` view document with id `1` in `timelines` index
85+
- `npm run view-data metadata 1` view document with id `1` in `timelines` index *(this index has only one document and all the data is stored inside one document which might be very big)*.
86+
87+
### Kafka commands
88+
89+
If you've used `docker-compose` with the file `local/docker-compose.yml` during local setup to spawn kafka & zookeeper, you can use the following commands to manipulate kafka topics and messages:
90+
(Replace `TOPIC_NAME` with the name of the desired topic)
91+
92+
#### Create Topic
93+
94+
```bash
95+
docker exec project-processor-es-kafka /opt/kafka/bin/kafka-topics.sh --create --zookeeper zookeeper:2181 --partitions 1 --replication-factor 1 --topic TOPIC_NAME
96+
```
97+
98+
#### List Topics
99+
100+
```bash
101+
docker exec project-processor-es-kafka /opt/kafka/bin/kafka-topics.sh --list --zookeeper zookeeper:2181
102+
```
103+
104+
#### Watch Topic
105+
106+
```bash
107+
docker exec project-processor-es-kafka /opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic TOPIC_NAME
108+
```
109+
110+
#### Post Message to Topic (from stdin)
111+
112+
```bash
113+
docker exec -it project-processor-es-kafka /opt/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic TOPIC_NAME
114+
```
115+
116+
- Enter or copy/paste the message into the console after starting this command.
117+
118+
#### Post Message to Topic (from file)
119+
120+
```bash
121+
docker exec -i project-processor-es-kafka /opt/kafka/bin/kafka-console-producer.sh --topic project.action.create --broker-list localhost:9092 < test_message.json
122+
```
123+
124+
- All example for messages are in: `./test/data`.
125+
10126
## Configuration
11127

12128
Configuration for the processor is at `config/default.js`.
@@ -40,56 +156,6 @@ Also note that there is a `/health` endpoint that checks for the health of the a
40156

41157
Config for tests are at `config/test.js`, it overrides some default config.
42158

43-
## Local Kafka setup
44-
- Call extracted directory kafka_2.11-0.11.0.1 : `path_to_kafka`
45-
- Call our project root directory : `our_project_root_directory`
46-
- `http://kafka.apache.org/quickstart` contains details to setup and manage Kafka server,
47-
below provides details to setup Kafka server in Mac, Windows will use bat commands in bin/windows instead
48-
- Download kafka at `https://www.apache.org/dyn/closer.cgi?path=/kafka/1.1.0/kafka_2.11-1.1.0.tgz`
49-
- Extract out the doanlowded tgz file
50-
- Go to extracted directory kafka_2.11-0.11.0.1
51-
- Start ZooKeeper server:
52-
`bin/zookeeper-server-start.sh config/zookeeper.properties`
53-
- Use another terminal, go to same directory, start the Kafka server:
54-
`bin/kafka-server-start.sh config/server.properties`
55-
- Note that the zookeeper server is at localhost:2181, and Kafka server is at localhost:9092
56-
- Use another terminal, go to same directory, create some topics:
57-
`bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic project.action.create`
58-
`bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic project.action.update`
59-
`bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic project.action.delete`
60-
- Verify that the topics are created:
61-
`bin/kafka-topics.sh --list --zookeeper localhost:2181`,
62-
it should list out the created topics
63-
- run the producer and then write some message into the console to send to the `project.action.create` topic:
64-
`bin/kafka-console-producer.sh --broker-list localhost:9092 --topic project.action.create`
65-
in the console, write message, one message per line:
66-
`{"topic":"project.action.create","originator":"project-api","timestamp":"2019-06-20T13:43:25.817Z","mime-type":"application/json","payload":{"resource":"project","createdAt":"2019-06-20T13:43:23.554Z","updatedAt":"2019-06-20T13:43:23.555Z","terms":[],"id":1,"name":"test project","description":"Hello I am a test project","type":"app","createdBy":40051333,"updatedBy":40051333,"projectEligibility":[],"bookmarks":[],"external":null,"status":"draft","lastActivityAt":"2019-06-20T13:43:23.514Z","lastActivityUserId":"40051333","members":[{"createdAt":"2019-06-20T13:43:23.555Z","updatedAt":"2019-06-20T13:43:23.625Z","id":2,"isPrimary":true,"role":"manager","userId":40051333,"updatedBy":40051333,"createdBy":40051333,"projectId":2,"deletedAt":null,"deletedBy":null}],"version":"v2","directProjectId":null,"billingAccountId":null,"estimatedPrice":null,"actualPrice":null,"details":null,"cancelReason":null,"templateId":null,"deletedBy":null,"attachments":null,"phases":null,"projectUrl":"https://connect.topcoder-dev.com/projects/2"}}`
67-
- Optionally, use another terminal, go to same directory, start a consumer to view the messages:
68-
`bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic project.action.create --from-beginning`
69-
- If the kafka don't allow to input long message you can use this script to write message from file:
70-
`path_to_kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic project.action.create < our_project_root_directory/test/data/project/project.action.create.json`
71-
- Writing/reading messages to/from other topics are similar. All example for messages are in:
72-
`our_project_root_directory/test/data`
73-
74-
## Local Elasticsearch setup
75-
76-
- In the `docker-es` folder, run `docker-compose up`
77-
78-
## Local deployment
79-
- Install dependencies `npm i`
80-
- Run code lint check `npm run lint`, running `npm run lint:fix` can fix some lint errors if any
81-
- Initialize Elasticsearch, create configured Elasticsearch index if not present: `npm run sync:es`
82-
- Start processor app `npm start`
83-
84-
Note that you need to set AUTH0 related environment variables belows before you can start the processor.
85-
86-
- AUTH0_URL
87-
- AUTH0_AUDIENCE
88-
- TOKEN_CACHE_TIME
89-
- AUTH0_CLIENT_ID
90-
- AUTH0_CLIENT_SECRET
91-
- AUTH0_PROXY_SERVER_URL
92-
93159
## Local Deployment with Docker
94160

95161
To run the Challenge ES Processor using docker, follow the below steps
@@ -135,12 +201,10 @@ npm run test:cov
135201
```
136202

137203
## Verification
138-
139-
- Call extracted directory kafka_2.11-0.11.0.1 : `path_to_kafka`
140-
- Call our project root directory : `our_project_root_directory`
141-
- Start kafka server, start elasticsearch, initialize Elasticsearch, start processor app
204+
- Start Docker services, initialize Elasticsearch, start processor app
205+
- Navigate to the repository root directory.
142206
- Send message:
143-
`path_to_kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic project.action.create < our_project_root_directory/test/data/project/project.action.create.json`
207+
`docker exec -i project-processor-es-kafka /opt/kafka/bin/kafka-console-producer.sh --topic project.action.create --broker-list localhost:9092 < ./test/data/project/project.action.create.json`
144208
- run command `npm run view-data projects 1` to view the created data, you will see the data are properly created:
145209

146210
```bash
@@ -193,7 +257,8 @@ info: {
193257

194258

195259
- Run the producer and then write some invalid message into the console to send to the `project.action.create` topic:
196-
`bin/kafka-console-producer.sh --broker-list localhost:9092 --topic project.action.create`
260+
261+
`docker exec -it project-processor-es-kafka /opt/kafka/bin/kafka-console-producer.sh --topic project.action.create --broker-list localhost:9092`
197262
in the console, write message, one message per line:
198263
`{ "topic": "project.action.create", "originator": "project-api", "timestamp": "2019-02-16T00:00:00", "mime-type": "application/json", "payload": { "id": "invalid", "typeId": "8e17090c-465b-4c17-b6d9-dfa16300b0ff", "track": "Code", "name": "test", "description": "desc", "timelineTemplateId": "8e17090c-465b-4c17-b6d9-dfa16300b0aa", "phases": [{ "id": "8e17090c-465b-4c17-b6d9-dfa16300b012", "name": "review", "isActive": true, "duration": 10000 }], "prizeSets": [{ "type": "prize", "prizes": [{ "type": "winning prize", "value": 500 }] }], "reviewType": "code review", "tags": ["code"], "projectId": 123, "forumId": 456, "status": "Active", "created": "2019-02-16T00:00:00", "createdBy": "admin" } }`
199264

@@ -203,7 +268,8 @@ info: {
203268
- Then in the app console, you will see error messages
204269

205270
- Sent message to update data:
206-
`path_to_kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic project.action.update < our_project_root_directory/test/data/project/project.action.update.json`
271+
272+
`docker exec -i project-processor-es-kafka /opt/kafka/bin/kafka-console-producer.sh --topic project.action.update --broker-list localhost:9092 < ./test/data/project/project.action.update.json`
207273
- Run command `npm run view-data projects 1` to view the updated data, you will see the data are properly updated:
208274

209275
```bash
@@ -258,7 +324,7 @@ info: {
258324

259325

260326
- Run the producer and then write some invalid message into the console to send to the `project.action.create` topic:
261-
`bin/kafka-console-producer.sh --broker-list localhost:9092 --topic project.action.create`
327+
`docker exec -it project-processor-es-kafka /opt/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic project.action.create`
262328
in the console, write message, one message per line:
263329
`{ "topic": "project.action.update", "originator": "project-api", "timestamp": "2019-02-17T01:00:00", "mime-type": "application/json", "payload": { "id": "173803d3-019e-4033-b1cf-d7205c7f774c", "typeId": "123", "track": "Code", "name": "test3", "description": "desc3", "timelineTemplateId": "8e17090c-465b-4c17-b6d9-dfa16300b0dd", "groups": ["group2", "group3"], "updated": "2019-02-17T01:00:00", "updatedBy": "admin" } }`
264330

docker-es/docker-compose.yml

Lines changed: 0 additions & 6 deletions
This file was deleted.

local/docker-compose.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: "2"
2+
services:
3+
esearch:
4+
image: "elasticsearch:2.3"
5+
ports:
6+
- "9200:9200"
7+
zookeeper:
8+
image: wurstmeister/zookeeper
9+
ports:
10+
- "2181:2181"
11+
environment:
12+
zk_id: "1"
13+
kafka:
14+
image: wurstmeister/kafka
15+
container_name: project-processor-es-kafka
16+
depends_on:
17+
- zookeeper
18+
ports:
19+
- "9092:9092"
20+
environment:
21+
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
22+
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
23+
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
24+
kafka-client:
25+
build: ./kafka-client
26+
depends_on:
27+
- kafka
28+
- zookeeper

local/kafka-client/Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
From wurstmeister/kafka
2+
WORKDIR /app/
3+
COPY topics.txt .
4+
COPY create-topics.sh .
5+
ENTRYPOINT ["/app/create-topics.sh"]

local/kafka-client/create-topics.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
while read topic; do
4+
/opt/kafka/bin/kafka-topics.sh --create --zookeeper zookeeper:2181 --partitions 1 --replication-factor 1 --topic $topic
5+
done < topics.txt

local/kafka-client/topics.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
project.action.create
2+
project.action.delete
3+
project.action.update

0 commit comments

Comments
 (0)