From de3b64ce95e2ebbfb2621616d1ac3be8171ce53d Mon Sep 17 00:00:00 2001 From: Evgeny Smyshlyaev Date: Thu, 11 Oct 2018 14:34:23 +0300 Subject: [PATCH] Decouple processes to different docker containers#187(WIP) (#188) analytics and server moved to separate containers, added docker-compose file --- Dockerfile | 29 ------------------------ analytics/.dockerignore | 5 ++++ analytics/Dockerfile | 20 ++++++++++++++++ analytics/config.py | 2 +- docker-compose.yml | 19 ++++++++++++++++ server/.dockerignore | 4 ++++ server/Dockerfile | 26 +++++++++++++++++++++ server/src/config.ts | 1 + server/src/services/analytics_service.ts | 17 ++++++++++---- 9 files changed, 88 insertions(+), 35 deletions(-) delete mode 100644 Dockerfile create mode 100644 analytics/.dockerignore create mode 100644 analytics/Dockerfile create mode 100644 docker-compose.yml create mode 100644 server/.dockerignore create mode 100644 server/Dockerfile diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 4bf865f..0000000 --- a/Dockerfile +++ /dev/null @@ -1,29 +0,0 @@ -FROM python:3.6.6 - -EXPOSE 8000 - -VOLUME [ "/var/www/data" ] - -COPY . /var/www - -WORKDIR /var/www/analytics - -RUN pip install -r requirements.txt - -RUN apt-get update && apt-get install -y \ - apt-utils \ - gnupg \ - curl \ - python \ - make \ - g++ \ - git -RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - - -RUN apt-get update && apt-get install -y nodejs - -WORKDIR /var/www/server - -RUN npm install && npm run build - -CMD ["npm", "start"] diff --git a/analytics/.dockerignore b/analytics/.dockerignore new file mode 100644 index 0000000..ce5fca2 --- /dev/null +++ b/analytics/.dockerignore @@ -0,0 +1,5 @@ +.git +npm-debug +node_modules +__pycache__ +.vscode diff --git a/analytics/Dockerfile b/analytics/Dockerfile new file mode 100644 index 0000000..7b3e23a --- /dev/null +++ b/analytics/Dockerfile @@ -0,0 +1,20 @@ +FROM python:3.6.6 + +WORKDIR /var/www/analytics + +COPY ./requirements.txt /var/www/analytics + +RUN pip install -r requirements.txt \ + && apt-get update && apt-get install -y \ + apt-utils \ + gnupg \ + curl \ + make \ + g++ \ + git + +VOLUME [ "/var/www/data" ] + +COPY . /var/www/analytics/ + +CMD ["python", "server.py"] diff --git a/analytics/config.py b/analytics/config.py index c4b2b2e..d71d955 100644 --- a/analytics/config.py +++ b/analytics/config.py @@ -25,4 +25,4 @@ def get_config_field(field, default_val = None): raise Exception('Please configure {}'.format(field)) ZMQ_DEV_PORT = get_config_field('ZMQ_DEV_PORT', '8002') -ZMQ_CONNECTION_STRING = get_config_field('ZMQ_CONNECTION_STRING', 'tcp://*:%s' % ZMQ_DEV_PORT) +ZMQ_CONNECTION_STRING = get_config_field('ZMQ_CONNECTION_STRING', 'tcp://0.0.0.0:%s' % ZMQ_DEV_PORT) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..1dde626 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,19 @@ +version: '2' +services: + server: + image: hastic/server:latest + build: server + environment: + HASTIC_API_KEY: ${HASTIC_API_KEY} + ZMQ_CONNECTION_STRING: tcp://analytics:8002 + ports: + - 8000:8000 + volumes: + - data-volume:/var/www/data + + analytics: + image: hastic/analytics:latest + build: analytics + +volumes: + data-volume: diff --git a/server/.dockerignore b/server/.dockerignore new file mode 100644 index 0000000..cf23c6e --- /dev/null +++ b/server/.dockerignore @@ -0,0 +1,4 @@ +.git +node_modules +npm-debug +.vscode diff --git a/server/Dockerfile b/server/Dockerfile new file mode 100644 index 0000000..ff47b00 --- /dev/null +++ b/server/Dockerfile @@ -0,0 +1,26 @@ +FROM python:3.6.6 + +RUN apt-get install curl \ + bash \ + gnupg \ + make \ + g++ \ + && curl -sL https://deb.nodesource.com/setup_8.x | bash - \ + && apt-get update \ + && apt-get install nodejs + +VOLUME [ "/var/www/data" ] + +WORKDIR /var/www/server + +COPY package.json /var/www/server + +RUN npm install + +COPY . /var/www/server + +RUN npm run build + +ENV INSIDE_DOCKER true + +CMD ["npm", "start"] diff --git a/server/src/config.ts b/server/src/config.ts index 46349b9..ced4f12 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -20,6 +20,7 @@ export const HASTIC_PORT = getConfigField('HASTIC_PORT', '8000'); export const ZMQ_CONNECTION_STRING = getConfigField('ZMQ_CONNECTION_STRING', null); export const ZMQ_IPC_PATH = getConfigField('ZMQ_IPC_PATH', path.join(os.tmpdir(), 'hastic')); export const ZMQ_DEV_PORT = getConfigField('ZMQ_DEV_PORT', '8002'); +export const ZMQ_HOST = getConfigField('ZMQ_HOST', '127.0.0.1'); export const HASTIC_API_KEY = getConfigField('HASTIC_API_KEY'); export const ANLYTICS_PING_INTERVAL = 500; // ms diff --git a/server/src/services/analytics_service.ts b/server/src/services/analytics_service.ts index 4c7ab22..38c4954 100644 --- a/server/src/services/analytics_service.ts +++ b/server/src/services/analytics_service.ts @@ -18,8 +18,12 @@ export class AnalyticsService { private _ipcPath: string = null; private _analyticsPinger: NodeJS.Timer = null; private _isClosed = false; + private _productionMode = false; + private _inDocker = false; constructor(private _onMessage: (message: AnalyticsMessage) => void) { + this._productionMode = process.env.NODE_ENV !== 'development'; + this._inDocker = process.env.INSIDE_DOCKER !== undefined; this._init(); } @@ -67,10 +71,12 @@ export class AnalyticsService { private async _init() { this._requester = zmq.socket('pair'); - let productionMode = process.env.NODE_ENV !== 'development'; - this._zmqConnectionString = `tcp://127.0.0.1:${config.ZMQ_DEV_PORT}`; // debug mode - if(productionMode) { + this._zmqConnectionString = `tcp://${config.ZMQ_HOST}:${config.ZMQ_DEV_PORT}`; // debug mode + + if(this._inDocker) { + this._zmqConnectionString = config.ZMQ_CONNECTION_STRING; + } else if(this._productionMode && !this._inDocker) { this._zmqConnectionString = config.ZMQ_CONNECTION_STRING; if(this._zmqConnectionString === null) { var createResult = await AnalyticsService.createIPCAddress(); @@ -84,7 +90,7 @@ export class AnalyticsService { this._requester.on("message", this._onAnalyticsMessage.bind(this)); console.log('Ok'); - if(productionMode) { + if(this._productionMode && !this._inDocker) { console.log('Creating analytics process...'); try { var cp = await AnalyticsService._runAnalyticsProcess(this._zmqConnectionString); @@ -165,12 +171,13 @@ export class AnalyticsService { private async _onAnalyticsDown() { console.log('Analytics is down'); - if(process.env.NODE_ENV !== 'development') { + if(this._productionMode && !this._inDocker) { await AnalyticsService._runAnalyticsProcess(this._zmqConnectionString); } } private _onAnalyticsMessage(data: any) { + let text = data.toString(); if(text === 'PONG') { this._pingResponded = true;