Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: setting up a production instance #7529

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ figwheel_server.log
andb-*.json

node_modules/

data/
resources/public/css/
!resources/public/css/carousel.css

Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,6 @@ data/cards-pt.edn
data/cards-ru.edn
data/cards-zh-hans.edn
data/cards-zh-hant.edn

# generated files
docker-compose.prod.yml
35 changes: 35 additions & 0 deletions docker/prod/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
FROM clojure:latest AS BUILD

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

RUN chmod -R 777 /root/

# Install npm+dependencies
ENV NODE_VERSION=19.6.0
RUN apt update && apt install -y curl
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
ENV NVM_DIR=/root/.nvm
RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"

COPY . /usr/src/app

RUN npm ci
RUN npm run release

RUN lein uberjar

FROM openjdk:11-slim

WORKDIR /opt/netrunner
RUN apt update && apt install -y zip && rm -rf /var/lib/apt/lists/*

COPY ./docker/prod/run-with-config .
COPY --from=BUILD /usr/src/app/target/netrunner-standalone.jar .

EXPOSE 2042

CMD ["./run-with-config", "netrunner-standalone.jar", "prod.edn"]
36 changes: 36 additions & 0 deletions docker/prod/docker-compose.yml.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
services:

endpoint:
image: nginx:alpine
ports:
- {{ port-endpoint }}:8000
volumes:
- ./docker/prod/nginx.conf:/etc/nginx/nginx.conf:ro
- {{ folder-resources }}:/usr/share/netrunner
depends_on:
- server
links:
- server

server:
image: {{ image-name }}
build:
context: .
dockerfile: ./docker/prod/Dockerfile
volumes:
- {{ config-file }}:/opt/netrunner/prod.edn
depends_on:
- database
links:
- database:mongo

database:
image: {{ image-name-mongodb }}
container_name: mongodb
restart: unless-stopped
{{#expose-mongodb}}
ports:
- 27017-27019:27017-27019
{{/expose-mongodb}}
volumes:
- {{ folder-mongodb }}:/data/db
36 changes: 36 additions & 0 deletions docker/prod/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
events {
}

http {
server {
listen 8000;

location /img/ {
root /usr/share/netrunner;
autoindex on;
autoindex_exact_size off;
autoindex_format html;
autoindex_localtime on;
}

location / {
proxy_pass http://server:1042;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}

location /api/remote-relay {
proxy_pass http://server:1042;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}

location /chsk {
proxy_pass http://server:1042;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}
40 changes: 40 additions & 0 deletions docker/prod/prod.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{:server/mode "prod"
:mongodb/connection {:address "mongo"
:port 27017
:name "netrunner"
; Monger requires a db name in the URI string, be sure to specify
:connection-string #or [#env MONGO_CONNECTION_URI nil]}
:web/app-state nil
:web/server {:port 1042
:app #ig/ref :web/app}
:web/auth {:expiration 60
:secret "HBPOSDc8ugXl5ikqzgJqYFjmp4MBQHrqQaiYLEhJYLqLV4leirkV1G0AiQTwmOmz"
:cookie {:http-only true
:same-site :lax
:secure true
; 60 days
:max-age 5184000}}
:web/lobby {:interval 1000
:mongo #ig/ref :mongodb/connection
:time-inactive 600}
:web/chat {:max-length 144
;; sliding window size in seconds
:rate-window 60
;; number of messages allowed in the sliding window
:rate-cnt 10}
:web/email {:host nil
:user nil
:pass nil
:ssl nil}
:web/app {:server-mode #ig/ref :server/mode
:mongodb/connection #ig/ref :mongodb/connection
:web/auth #ig/ref :web/auth
:web/chat #ig/ref :web/chat
:web/email #ig/ref :web/email}
:web/banned-msg {:initial "Account Banned"
:mongo #ig/ref :mongodb/connection}
:frontend/version {:initial "1"
:mongo #ig/ref :mongodb/connection}
:sente/router nil
:game/quotes nil
:jinteki/cards {:mongo #ig/ref :mongodb/connection}}
17 changes: 17 additions & 0 deletions docker/prod/run-with-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

JAR_FILE=$1
CONFIG_FILE=$2

# Echo a message to indicate the start of the process
echo "Adding configuration file $CONFIG_FILE to JAR $JAR_FILE..."

# Add the configuration file to the root of the JAR
zip -j $JAR_FILE $CONFIG_FILE

# Echo a message to indicate that the configuration file has been added
echo "...configuration file added successfully!"

# Run the JAR file
echo "Running jar file..."
java -jar $JAR_FILE
6 changes: 4 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
[integrant "0.8.0"]
[cljc.java-time "0.1.18"]
[time-literals "0.1.5"]
[metosin/reitit "0.5.18"]]
[metosin/reitit "0.5.18"]
[de.ubercode.clostache/clostache "1.4.0"]]

:test-selectors {:default (fn [m] (not (:kaocha/pending m)))}

Expand Down Expand Up @@ -105,4 +106,5 @@
"create-sample-data" ["run" "-m" "tasks.db/create-sample-data"]
"get-game-stats" ["run" "-m" "tasks.game-stats/all-games"]
"get-user-stats" ["run" "-m" "tasks.user-stats/all-users"]
"get-background-stats" ["run" "-m" "tasks.user-stats/all-backgrounds"]})
"get-background-stats" ["run" "-m" "tasks.user-stats/all-backgrounds"]
"generate-docker" ["run" "-m" "tasks.generate-docker/command"]})
50 changes: 50 additions & 0 deletions src/clj/tasks/generate_docker.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
(ns tasks.generate-docker
"Generate docker-compose file"
(:require
[clojure.tools.cli :refer [parse-opts]]
[clojure.string :as str]
[clostache.parser :as cp]))

(defn usage
[options-summary]
(->> [""
"Usage: lein generate-docker [options]"
""
"Options:"
options-summary]
(str/join \newline)))

(def cli-options
[["-t" "--template PATH" "Path to docker-compose template" :default "docker/prod/docker-compose.yml.tpl"]
["-o" "--output PATH" "Path to generated docker-compose file" :default "docker-compose.prod.yml"]
["-i" "--image IMAGE-NAME" "Image name is required" "Netrunner Docker image name" :missing "Image name is required"]
["-p" "--port PORT" "Port exposing Netrunner" :default "1042"]
["-r" "--folder-resources FOLDER-RESOURCES" "Path to the public resources" :default "./resources/public/"]
["-f" "--config CONFIG-FILE" "Path to the configuration file" :default "./docker/prod/prod.edn"]
["-m" "--image-mongodb IMAGE-NAME-MONGODB" "Image name of MongoDB" :default "mongo"]
["-d" "--folder-mongodb FOLDER-MONGODB" "Folder of the MongoDB database" :default "./data/"]
["-c" "--close-mongodb" "Disable MongoDB connectivity outside of Docker internal network"
:id :expose-mongodb :default true :parse-fn not]])

(defn exit [status msg]
(binding [*out* *err*]
(println msg))
(System/exit status))

(defn command
[& args]
(let [{:keys [options arguments errors summary]} (parse-opts args cli-options)]
(if (or errors
(not-empty arguments))
(exit 1 (str/join \newline (conj errors (usage summary))))
(let [tpl (slurp (:template options))]
(spit (:output options) (cp/render tpl {
:image-name (:image options)
:config-file (:config options)
:port-endpoint (:port options)
:folder-resources (:folder-resources options)
:image-name-mongodb (:image-mongodb options)
:folder-mongodb (:folder-mongodb options)
:expose-mongodb (:expose-mongodb options)}))))))


Loading