Skip to content

Commit 0566c67

Browse files
Vitalii BulyzhynVitalii Bulyzhyn
Vitalii Bulyzhyn
authored and
Vitalii Bulyzhyn
committed
Add command to create docker-compose file for local developemtn
1 parent c523f02 commit 0566c67

14 files changed

+1186
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
/node_modules
1+
/node_modules
2+
docker-compose-dev.yaml

bin/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env node
22
const yargs = require("yargs")
33
const create_action = require('./utils/action')
4+
const create_dev_container = require('./utils/container')
45

56
const usage = "\nDiffgram CLI:";
67
const options = yargs
@@ -11,5 +12,6 @@ const options = yargs
1112
describe: 'Name of your new Diffgram action'
1213
})
1314
}, create_action)
15+
.command('compose', 'Creates a docker-compose file for development environment', create_dev_container)
1416
.help(true)
1517
.argv;

bin/snippets/docker-compose/base.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version: "3.9"
2+
services:
3+
REPLACE
4+
volumes:
5+
static: {}

bin/snippets/docker-compose/db.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
db:
2+
image: ${POSTGRES_IMAGE}
3+
hostname: db
4+
restart: always
5+
healthcheck:
6+
test: [ "CMD-SHELL", "pg_isready", "-d", "db_prod" ]
7+
interval: 30s
8+
timeout: 60s
9+
retries: 5
10+
start_period: 80s
11+
environment:
12+
- POSTGRES_HOST_AUTH_METHOD=trust
13+
- POSTGRES_DB=diffgram
14+
volumes:
15+
# Default INTERNAL_POSTGRES_DIR=/var/lib/postgresql/data:Z
16+
- ${POSTGRES_DATA_DIR:-./postgres-data}:${INTERNAL_POSTGRES_DIR:-/var/lib/postgresql/data}
17+
ports:
18+
- 5432:5432
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
db_migration:
2+
image: gcr.io/diffgram-open-core/default:${DIFFGRAM_VERSION_TAG}
3+
env_file:
4+
- .env
5+
environment:
6+
PYTHONPATH: "/app/"
7+
command: ["/app/db-init.sh", "${DATABASE_HOST}"]
8+
depends_on:
9+
db:
10+
condition: service_healthy
11+
rabbitmq:
12+
condition: service_healthy
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
default:
2+
image: gcr.io/diffgram-open-core/default:${DIFFGRAM_VERSION_TAG}
3+
healthcheck:
4+
test: curl --fail http://default:8080 || exit 1
5+
interval: 30s
6+
timeout: 30s
7+
retries: 5
8+
start_period: 80s
9+
ports:
10+
- "0.0.0.0:8080:8080"
11+
expose: [8080]
12+
depends_on:
13+
db:
14+
condition: service_healthy
15+
rabbitmq:
16+
condition: service_healthy
17+
db_migration:
18+
condition: service_completed_successfully
19+
volumes:
20+
- ${GCP_SERVICE_ACCOUNT_FILE_PATH}:/gcp/gcp-service-account.json
21+
env_file:
22+
- .env
23+
environment:
24+
PYTHONPATH: "/app/"
25+
DIFFGRAM_SERVICE_NAME: default
26+
command: ["python3", "-u", "/app/main.py"]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
dispatcher:
2+
image: gcr.io/diffgram-open-core/local_dispatcher:latest
3+
ports:
4+
- "0.0.0.0:8085:8085"
5+
expose: [8085]
6+
depends_on:
7+
- frontend
8+
- default
9+
- walrus
10+
environment:
11+
SAME_HOST: ${SAME_HOST}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
eventhandlers:
2+
image: gcr.io/diffgram-open-core/eventhandlers:${DIFFGRAM_VERSION_TAG}
3+
ports:
4+
- "0.0.0.0:8086:8086"
5+
expose: [8086]
6+
depends_on:
7+
db:
8+
condition: service_healthy
9+
default:
10+
condition: service_started
11+
rabbitmq:
12+
condition: service_healthy
13+
volumes:
14+
- ${GCP_SERVICE_ACCOUNT_FILE_PATH}:/gcp/gcp-service-account.json
15+
env_file:
16+
- .env
17+
environment:
18+
PYTHONPATH: "/app/"
19+
DIFFGRAM_SERVICE_NAME: eventhandlers
20+
entrypoint: ["python3", "-u", "/app/main.py"]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
frontend:
2+
image: gcr.io/diffgram-open-core/frontend:${DIFFGRAM_VERSION_TAG}
3+
ports:
4+
- "0.0.0.0:8081:80"
5+
expose: [80]
6+
depends_on:
7+
- default
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
rabbitmq:
2+
image: rabbitmq:3.10-rc-management-alpine
3+
env_file:
4+
- .env
5+
healthcheck:
6+
test: rabbitmq-diagnostics -q ping
7+
interval: 30s
8+
timeout: 30s
9+
retries: 3
10+
container_name: 'rabbitmq'
11+
expose: [5672, 15672]
12+
ports:
13+
- 5672:5672
14+
- 15672:15672
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
walrus:
2+
image: gcr.io/diffgram-open-core/walrus:${DIFFGRAM_VERSION_TAG}
3+
ports:
4+
- "0.0.0.0:8082:8082"
5+
expose: [8082]
6+
depends_on:
7+
db:
8+
condition: service_healthy
9+
default:
10+
condition: service_started
11+
rabbitmq:
12+
condition: service_healthy
13+
volumes:
14+
- ${GCP_SERVICE_ACCOUNT_FILE_PATH}:/gcp/gcp-service-account.json
15+
env_file:
16+
- .env
17+
environment:
18+
PYTHONPATH: "/app/"
19+
DIFFGRAM_SERVICE_NAME: walrus
20+
entrypoint: ["python3", "-u", "/app/main.py"]

bin/utils/container.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const Prompt = require('prompt-checkbox');
2+
const fs = require("fs");
3+
const readModuleFile = require('../tools/readModuleFile')
4+
5+
const replacer = new RegExp('REPLACE', 'g')
6+
7+
const templateMap = {
8+
'Postgres database (db)': {
9+
position: 7,
10+
path: "../snippets/docker-compose/db.txt"
11+
},
12+
'RabbitMQ (rabbitmq)' : {
13+
position: 8,
14+
path: "../snippets/docker-compose/rabbit.txt"
15+
},
16+
'DB migrations (db_migration)': {
17+
position: 6,
18+
path: "../snippets/docker-compose/db_migrations.txt"
19+
},
20+
'Default (default)': {
21+
position: 5,
22+
path: "../snippets/docker-compose/default.txt"
23+
},
24+
'Walrus (walrus)': {
25+
position: 4,
26+
path: "../snippets/docker-compose/walrus.txt"
27+
},
28+
'Event handlers (eventhandlers)': {
29+
position: 3,
30+
path: "../snippets/docker-compose/eventhandlers.txt"
31+
},
32+
'Dispatcher (dispatcher)': {
33+
position: 1,
34+
path: "../snippets/docker-compose/dispatch.txt"
35+
},
36+
'Frontend (frontend)': {
37+
position: 2,
38+
path: "../snippets/docker-compose/frontend.txt"
39+
},
40+
}
41+
42+
const prompt = new Prompt({
43+
name: 'services',
44+
message: 'What services do you want to run through the container?',
45+
choices: [ ...Object.keys(templateMap) ]
46+
});
47+
48+
function create_dev_container () {
49+
const file_to_create = `${process.cwd()}/docker-compose-dev.yaml`
50+
51+
const baseComposeFileBuffer = readModuleFile("../snippets/docker-compose/base.txt");
52+
const baseTemplate = baseComposeFileBuffer.toString()
53+
54+
prompt.run()
55+
.then(function(answers) {
56+
if (answers.length === 0) {
57+
console.log("Nothing was selected")
58+
return
59+
}
60+
let servicesTemplate = ''
61+
answers
62+
.sort(function(a, b) {
63+
return templateMap[a].position - templateMap[b].position;
64+
})
65+
.map(answer => {
66+
const buffer = readModuleFile(templateMap[answer].path)
67+
servicesTemplate += buffer.toString()
68+
servicesTemplate += '\n'
69+
})
70+
const composeReady = baseTemplate.replace(replacer, servicesTemplate)
71+
fs.writeFileSync(file_to_create, composeReady);
72+
console.log('Now you can run your dev container with "docker-compose -f docker-compose-dev.yaml up"')
73+
})
74+
}
75+
76+
module.exports = create_dev_container;

0 commit comments

Comments
 (0)