From 54d729796f6187fb14c33d74fcc66f41d564a3a0 Mon Sep 17 00:00:00 2001 From: Matheus Nogueira Date: Wed, 18 Nov 2020 00:59:56 -0300 Subject: [PATCH 1/6] Setup docker-compose to run Khan locally --- docker-compose.yml | 97 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..fe60171b --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,97 @@ +version: "3.8" + +services: + khan: + build: + context: . + dockerfile: Dockerfile + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + elasticsearch: + condition: service_healthy + links: + - postgres + - redis + - elasticsearch + ports: + - "80:80" + networks: + - khan + environment: + - KHAN_POSTGRES_HOST=postgres + - KHAN_POSTGRES_USER=postgres + - KHAN_POSTGRES_PASSWORD=123456 + - KHAN_ELASTICSEARCH_HOST=elasticsearch + + migrate: + build: + context: . + dockerfile: Dockerfile + command: "make migrate" + depends_on: + postgres: + condition: service_healthy + links: + - postgres + networks: + - khan + environment: + - KHAN_POSTGRES_HOST=postgres + - KHAN_POSTGRES_USER=postgres + - KHAN_POSTGRES_PASSWORD=123456 + + postgres: + image: postgres:9.6 + restart: always + environment: + - POSTGRES_PASSWORD=123456 + - POSTGRES_USER=postgres + - POSTGRES_DB=khan + ports: + - "5432:5432" + volumes: + - ./docker-data/postgres:/var/lib/postgresql/data + networks: + - khan + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + interval: 5s + timeout: 5s + retries: 5 + + redis: + image: redis + restart: always + ports: + - "6379:6379" + networks: + - khan + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 1s + timeout: 3s + retries: 30 + + elasticsearch: + image: elasticsearch:5.6.12 + ports: + - '9200:9200' + - '9300:9300' + networks: + - khan + volumes: + - ./docker-data/elasticsearch:/usr/share/elasticsearch/data + environment: + - xpack.security.enabled=false + - discovery.type=single-node + healthcheck: + test: ["CMD-SHELL", "curl --silent --fail localhost:9200/_cluster/health || exit 1"] + interval: 30s + timeout: 30s + retries: 3 + +networks: + khan: \ No newline at end of file From 3c0406dd496b56a5cc6014bf998034dd2dee84bc Mon Sep 17 00:00:00 2001 From: Matheus Nogueira Date: Wed, 18 Nov 2020 01:11:10 -0300 Subject: [PATCH 2/6] Document the usage of docker-compose --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index d1598f90..da7ad6bc 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,21 @@ To run a new khan instance, run: $ make run-docker +### Running with docker-compose + +We already provide a docker-compose.yml as well with all dependencies configured for you to run. +Before you can run the project, you need to execute the migrations in the database. You can achieve that executing: + +```sh + $ docker-compose up migrate +``` + +After executing the migration, you can execute the project by running: + +```sh + $ docker-compose up khan +``` + ### Tests Running tests can be done with `make test`, while creating the test database can be accomplished with `make drop-test` and `make db-test`. From 64d2840a9fa2d3fb2d1168335f299c179004a7b5 Mon Sep 17 00:00:00 2001 From: Matheus Nogueira Date: Wed, 18 Nov 2020 01:23:50 -0300 Subject: [PATCH 3/6] Update CI Go version to 1.12 strings.ReplaceAll was removed from go 1.11 and it's making the CI pipeline to break --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 516b508c..5daf11e6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: go go: - - 1.11 + - 1.12 sudo: false From 2d7830ff7698ac64ea3080b655186123f86028f5 Mon Sep 17 00:00:00 2001 From: Matheus Nogueira Date: Wed, 18 Nov 2020 01:45:04 -0300 Subject: [PATCH 4/6] Bump version to 4.3.13 --- util/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/version.go b/util/version.go index fd05c996..c5a529fa 100644 --- a/util/version.go +++ b/util/version.go @@ -8,4 +8,4 @@ package util // VERSION identifies Khan's current version -var VERSION = "4.3.12" +var VERSION = "4.3.13" From 554191f59a0ddb1c4668800edbcc9e048bb0499c Mon Sep 17 00:00:00 2001 From: Matheus Nogueira Date: Wed, 18 Nov 2020 12:56:18 -0300 Subject: [PATCH 5/6] Make khan service execute the migrations before executing the API --- .dockerignore | 1 + Dockerfile | 10 +++++++--- Makefile | 4 +++- README.md | 11 +++-------- docker-compose.yml | 28 ++++++---------------------- docker/start-khan.sh | 6 ++++++ 6 files changed, 26 insertions(+), 34 deletions(-) create mode 100644 docker/start-khan.sh diff --git a/.dockerignore b/.dockerignore index 94143827..58d58a3b 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1,2 @@ Dockerfile +docker-compose.yml \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index f58d47b9..65f5114e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,7 @@ FROM golang:1.11-alpine MAINTAINER TFG Co +WORKDIR /go/src/github.com/topfreegames/khan EXPOSE 80 @@ -12,9 +13,10 @@ RUN go get -u github.com/golang/dep/... RUN go get -u github.com/topfreegames/goose/cmd/goose ADD loadtest/words /usr/share/dict/words -ADD . /go/src/github.com/topfreegames/khan +ADD Gopkg.* ./ +RUN dep ensure --vendor-only -WORKDIR /go/src/github.com/topfreegames/khan +ADD . . RUN dep ensure RUN go install github.com/topfreegames/khan @@ -44,4 +46,6 @@ ENV KHAN_BASICAUTH_PASSWORD "" ENV KHAN_RUN_WORKER "" -CMD /bin/bash -c 'if [ "$KHAN_RUN_WORKER" != "true" ]; then /go/bin/khan start --bind 0.0.0.0 --port 80 --fast --config /go/src/github.com/topfreegames/khan/config/default.yaml; else /go/bin/khan worker --config /go/src/github.com/topfreegames/khan/config/default.yaml; fi' +RUN chmod +x ./docker/start-khan.sh + +CMD [ "./docker/start-khan.sh" ] diff --git a/Makefile b/Makefile index 1d7ab724..5f069e39 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,6 @@ setup-ci: @go get github.com/topfreegames/goose/cmd/goose @go get github.com/mattn/goveralls @go get github.com/onsi/ginkgo/ginkgo - @dep ensure build: @go build -o ./bin/khan main.go @@ -88,6 +87,9 @@ run-fast: build-docker: @docker build -t khan . +start-on-docker: migrate + @bash ./docker/start-khan.sh + build-dev-docker: @cp ./config/default.yaml ./dev @cp ./bin/khan-linux-x86_64 ./dev diff --git a/README.md b/README.md index da7ad6bc..ae4d649f 100644 --- a/README.md +++ b/README.md @@ -56,18 +56,13 @@ To run a new khan instance, run: ### Running with docker-compose -We already provide a docker-compose.yml as well with all dependencies configured for you to run. -Before you can run the project, you need to execute the migrations in the database. You can achieve that executing: +We already provide a docker-compose.yml as well with all dependencies configured for you to run. To run Khan and all its dependencies, run: ```sh - $ docker-compose up migrate + $ docker-compose up ``` -After executing the migration, you can execute the project by running: - -```sh - $ docker-compose up khan -``` +**Note** If you are running it on MacOS, you will need to update the amount of RAM docker has access to. Docker, by default, can use 2GB of RAM, however, Khan uses an instance of ElasticSearch and it needs at least 2GB of RAM to work properly. So, if you are experiencing problems while connecting to the elastic search, this might be the root cause of the problem. ### Tests diff --git a/docker-compose.yml b/docker-compose.yml index fe60171b..b6eb966b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,6 +5,7 @@ services: build: context: . dockerfile: Dockerfile + command: "make start-on-docker" depends_on: postgres: condition: service_healthy @@ -19,29 +20,12 @@ services: ports: - "80:80" networks: - - khan + - wildlife-services environment: - KHAN_POSTGRES_HOST=postgres - KHAN_POSTGRES_USER=postgres - KHAN_POSTGRES_PASSWORD=123456 - KHAN_ELASTICSEARCH_HOST=elasticsearch - - migrate: - build: - context: . - dockerfile: Dockerfile - command: "make migrate" - depends_on: - postgres: - condition: service_healthy - links: - - postgres - networks: - - khan - environment: - - KHAN_POSTGRES_HOST=postgres - - KHAN_POSTGRES_USER=postgres - - KHAN_POSTGRES_PASSWORD=123456 postgres: image: postgres:9.6 @@ -55,7 +39,7 @@ services: volumes: - ./docker-data/postgres:/var/lib/postgresql/data networks: - - khan + - wildlife-services healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 5s @@ -68,7 +52,7 @@ services: ports: - "6379:6379" networks: - - khan + - wildlife-services healthcheck: test: ["CMD", "redis-cli", "ping"] interval: 1s @@ -81,7 +65,7 @@ services: - '9200:9200' - '9300:9300' networks: - - khan + - wildlife-services volumes: - ./docker-data/elasticsearch:/usr/share/elasticsearch/data environment: @@ -94,4 +78,4 @@ services: retries: 3 networks: - khan: \ No newline at end of file + wildlife-services: \ No newline at end of file diff --git a/docker/start-khan.sh b/docker/start-khan.sh new file mode 100644 index 00000000..88581e75 --- /dev/null +++ b/docker/start-khan.sh @@ -0,0 +1,6 @@ +#!/bin/bash +if [ "$KHAN_RUN_WORKER" != "true" ]; then + /go/bin/khan start --bind 0.0.0.0 --port 80 --fast --config /go/src/github.com/topfreegames/khan/config/default.yaml +else + /go/bin/khan worker --config /go/src/github.com/topfreegames/khan/config/default.yaml +fi \ No newline at end of file From a05b19b4692237f91032b59f8653b8c3eb30142c Mon Sep 17 00:00:00 2001 From: Matheus Nogueira Date: Wed, 18 Nov 2020 13:49:53 -0300 Subject: [PATCH 6/6] Add worker to docker-compose --- Dockerfile | 2 +- docker-compose.yml | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 65f5114e..ca67accd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ WORKDIR /go/src/github.com/topfreegames/khan EXPOSE 80 RUN apk update -RUN apk add git make g++ apache2-utils +RUN apk add git make g++ apache2-utils curl RUN apk add --update bash RUN go get -u github.com/golang/dep/... diff --git a/docker-compose.yml b/docker-compose.yml index b6eb966b..8a1ae69b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -25,8 +25,35 @@ services: - KHAN_POSTGRES_HOST=postgres - KHAN_POSTGRES_USER=postgres - KHAN_POSTGRES_PASSWORD=123456 + - KHAN_REDIS_HOST=redis - KHAN_ELASTICSEARCH_HOST=elasticsearch - + healthcheck: + test: ["CMD-SHELL", "curl --silent --fail http://localhost/healthcheck || exit 1"] + interval: 30s + timeout: 30s + retries: 3 + + khan-worker: + build: + context: . + dockerfile: Dockerfile + depends_on: + khan: + condition: service_healthy + links: + - khan + ports: + - "8080:8080" + networks: + - wildlife-services + environment: + - KHAN_RUN_WORKER=true + - KHAN_POSTGRES_HOST=postgres + - KHAN_POSTGRES_USER=postgres + - KHAN_POSTGRES_PASSWORD=123456 + - KHAN_REDIS_HOST=redis + - KHAN_ELASTICSEARCH_HOST=elasticsearch + postgres: image: postgres:9.6 restart: always