From 54a5c6a46e9398ad7c690ab6c93eb6cb2c4c9aba Mon Sep 17 00:00:00 2001 From: geek Date: Wed, 21 Jun 2017 20:05:45 -0500 Subject: [PATCH] feat(cp-frontend): use nginx instead of serve --- local-compose.yml | 2 + packages/cp-frontend/Dockerfile | 16 +-- packages/cp-frontend/etc/containerpilot.json5 | 28 +++-- packages/cp-frontend/etc/nginx.conf.tmpl | 101 ++++++++++++++++++ packages/cp-frontend/src/state/store.js | 2 +- packages/portal-api/bootstrap-data.js | 12 ++- packages/portal-api/etc/containerpilot.json5 | 8 +- packages/portal-api/package.json | 3 + packages/portal-api/server.js | 27 +++-- setup.sh | 5 + 10 files changed, 178 insertions(+), 26 deletions(-) create mode 100644 packages/cp-frontend/etc/nginx.conf.tmpl diff --git a/local-compose.yml b/local-compose.yml index 1eaeada2..a53b0b12 100644 --- a/local-compose.yml +++ b/local-compose.yml @@ -95,6 +95,8 @@ api: links: - consul:consul - rethinkdb:rethinkdb + env_file: + - _env environment: - CONSUL=consul - PORT=3000 diff --git a/packages/cp-frontend/Dockerfile b/packages/cp-frontend/Dockerfile index 7369daa8..5cd184de 100644 --- a/packages/cp-frontend/Dockerfile +++ b/packages/cp-frontend/Dockerfile @@ -1,15 +1,19 @@ FROM quay.io/yldio/alpine-node-containerpilot:latest +RUN apk add --update nginx + ENV CONTAINERPILOT /etc/containerpilot.json5 -RUN npm install -g npm@^4 \ - && npm config set loglevel info \ - && yarn add lerna@^2.0.0-rc.5 serve \ - && ./node_modules/.bin/lerna clean --yes --scope joyent-cp-frontend --include-filtered-dependencies \ - && ./node_modules/.bin/lerna bootstrap --scope joyent-cp-frontend --include-filtered-dependencies +RUN npm install -g npm@^4 +RUN npm config set loglevel info \ + && yarn add lerna@^2.0.0-rc.5 + +RUN ./node_modules/.bin/lerna clean --yes --scope joyent-cp-frontend --include-filtered-dependencies \ + && ./node_modules/.bin/lerna bootstrap --scope joyent-cp-frontend --include-filtered-dependencies COPY packages/cp-frontend/etc/containerpilot.json5 ${CONTAINERPILOT} +COPY packages/cp-frontend/etc/nginx.conf.tmpl /etc/nginx/nginx.conf.tmpl + WORKDIR /opt/app/packages/cp-frontend -EXPOSE 3069 CMD ["/bin/containerpilot"] diff --git a/packages/cp-frontend/etc/containerpilot.json5 b/packages/cp-frontend/etc/containerpilot.json5 index ff1310e7..e39a0684 100644 --- a/packages/cp-frontend/etc/containerpilot.json5 +++ b/packages/cp-frontend/etc/containerpilot.json5 @@ -2,26 +2,28 @@ consul: 'localhost:8500', jobs: [ { - name: 'build', - exec: 'yarn run build' + name: 'config-nginx', + exec: 'containerpilot -config /etc/nginx/nginx.conf.tmpl -template -out /etc/nginx/nginx.conf' }, { name: 'cp-frontend', port: {{.PORT}}, - exec: '../../node_modules/.bin/serve --single build --port {{.PORT}}', + exec: 'nginx', + interfaces: ["eth0", "eth1"], + restarts: 'unlimited', when: { - source: 'build', + source: 'config-nginx', once: 'exitSuccess' }, health: { exec: '/usr/bin/curl -o /dev/null --fail -s http://localhost:{{.PORT}}', - interval: 2, - ttl: 5 + interval: 5, + ttl: 25 }, tags: [ 'traefik.backend=cp-frontend', 'traefik.frontend.rule=PathPrefix:/', - 'traefik.frontend.entryPoints=http' + 'traefik.frontend.entryPoints={{ .ENTRYPOINTS | default "http,ws,wss" }}' ] }, { @@ -36,6 +38,18 @@ '-retry-interval', '10s'], restarts: 'unlimited' }, + { + name: 'is-built', + exec: '[ -d /opt/app/packages/cp-frontend/build/static ]' + }, + { + name: 'build', + exec: 'yarn run build', + when: { + source: 'is-built', + once: 'exitFailed' + } + }, { name: 'sensor_memory_usage', exec: '/bin/sensors memory', diff --git a/packages/cp-frontend/etc/nginx.conf.tmpl b/packages/cp-frontend/etc/nginx.conf.tmpl new file mode 100644 index 00000000..0878dd6c --- /dev/null +++ b/packages/cp-frontend/etc/nginx.conf.tmpl @@ -0,0 +1,101 @@ +# /etc/nginx/nginx.conf + +user nginx; +worker_processes 1; +daemon off; + +# Enables the use of JIT for regular expressions to speed-up their processing. +pcre_jit on; + +# Configures default error logger. +error_log /var/log/nginx/error.log warn; + +pid /var/run/nginx.pid; + +# Includes files with directives to load dynamic modules. +include /etc/nginx/modules/*.conf; + + +events { + # The maximum number of simultaneous connections that can be opened by + # a worker process. + worker_connections 1024; +} + +http { + index index.html index.htm; + server { + server_name _; + listen {{ .PORT | default "80" }} default_server; + listen [::]:{{ .PORT | default "80" }} default_server; + root /opt/app/packages/cp-frontend/build; + location / { + try_files $uri /index.html; + } + } + # Includes mapping of file name extensions to MIME types of responses + # and defines the default type. + include /etc/nginx/mime.types; + default_type application/octet-stream; + + # Name servers used to resolve names of upstream servers into addresses. + # It's also needed when using tcpsocket and udpsocket in Lua modules. + #resolver 208.67.222.222 208.67.220.220; + + # Don't tell nginx version to clients. + server_tokens off; + + # Specifies the maximum accepted body size of a client request, as + # indicated by the request header Content-Length. If the stated content + # length is greater than this size, then the client receives the HTTP + # error code 413. Set to 0 to disable. + client_max_body_size 1m; + + # Timeout for keep-alive connections. Server will close connections after + # this time. + keepalive_timeout 65; + + # Sendfile copies data between one FD and other from within the kernel, + # which is more efficient than read() + write(). + sendfile on; + + # Don't buffer data-sends (disable Nagle algorithm). + # Good for sending frequent small bursts of data in real time. + tcp_nodelay on; + + # Causes nginx to attempt to send its HTTP response head in one packet, + # instead of using partial frames. + #tcp_nopush on; + + # Path of the file with Diffie-Hellman parameters for EDH ciphers. + #ssl_dhparam /etc/ssl/nginx/dh2048.pem; + + # Specifies that our cipher suits should be preferred over client ciphers. + ssl_prefer_server_ciphers on; + + # Enables a shared SSL cache with size that can hold around 8000 sessions. + ssl_session_cache shared:SSL:2m; + + + # Enable gzipping of responses. + #gzip on; + + # Set the Vary HTTP header as defined in the RFC 2616. + gzip_vary on; + + # Enable checking the existence of precompressed files. + #gzip_static on; + + + # Specifies the main log format. + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + # Sets the path, format, and configuration for a buffered log write. + access_log /var/log/nginx/access.log main; + + + # Includes virtual hosts configs. + # include /etc/nginx/conf.d/*.conf; +} diff --git a/packages/cp-frontend/src/state/store.js b/packages/cp-frontend/src/state/store.js index 0c61b1b5..fef45b27 100644 --- a/packages/cp-frontend/src/state/store.js +++ b/packages/cp-frontend/src/state/store.js @@ -31,7 +31,7 @@ export const client = new ApolloClient({ return `${o.__typename}:${id}`; }, networkInterface: createNetworkInterface({ - uri: `http://${GQL_HOSTNAME}:${GQL_PORT}/graphql` + uri: `http://${GQL_HOSTNAME}:${GQL_PORT}/api/graphql` }) }); diff --git a/packages/portal-api/bootstrap-data.js b/packages/portal-api/bootstrap-data.js index d5577e07..32419b93 100644 --- a/packages/portal-api/bootstrap-data.js +++ b/packages/portal-api/bootstrap-data.js @@ -17,11 +17,14 @@ const bootstrap = function () { } }); + const region = process.env.TRITON_DC || 'us-sw-1'; + const login = process.env.TRITON_USER || 'nikola'; + data.connect(() => { - data.createDatacenter({ region: 'us-sw-1', name: 'us-sw-1' }, (err, datacenter) => { + data.createDatacenter({ region, name: region }, (err, datacenter) => { ifError(err); - data.createUser({ firstName: 'Nikola', lastName: 'Tesla', email: 'nikola@tesla.com', login: 'nikola' }, (err, user) => { + data.createUser({ firstName: 'Nikola', lastName: 'Tesla', email: 'nikola@tesla.com', login }, (err, user) => { ifError(err); data.createPortal({ @@ -36,7 +39,7 @@ const bootstrap = function () { }); }); }; - +/* const main = function () { const dropData = new Data({ db: { @@ -50,4 +53,5 @@ const main = function () { }); }); }; -main(); +*/ +bootstrap(); diff --git a/packages/portal-api/etc/containerpilot.json5 b/packages/portal-api/etc/containerpilot.json5 index 30c489e9..b989de6e 100644 --- a/packages/portal-api/etc/containerpilot.json5 +++ b/packages/portal-api/etc/containerpilot.json5 @@ -16,9 +16,13 @@ }, tags: [ 'traefik.backend=api', - 'traefik.frontend.rule=PathPrefix:/graphql,/graphiql', + 'traefik.frontend.rule=PathPrefixStrip:/api', 'traefik.frontend.entryPoints=http' - ] + ], + when: { + source: 'bootstrap', + once: 'exitSuccess' + } }, { name: 'consul-agent', diff --git a/packages/portal-api/package.json b/packages/portal-api/package.json index 08f11011..981ab02e 100644 --- a/packages/portal-api/package.json +++ b/packages/portal-api/package.json @@ -28,6 +28,9 @@ "dependencies": { "boom": "^5.1.0", "brule": "^2.0.0", + "good": "^7.2.0", + "good-console": "^6.4.0", + "good-squeeze": "^5.0.2", "graphi": "^2.2.1", "hoek": "^4.1.1", "joi": "^10.6.0", diff --git a/packages/portal-api/server.js b/packages/portal-api/server.js index 6b10c3fa..135a8664 100644 --- a/packages/portal-api/server.js +++ b/packages/portal-api/server.js @@ -1,6 +1,7 @@ 'use strict'; const Brule = require('brule'); +const Good = require('good'); const Hapi = require('hapi'); const HapiSwagger = require('hapi-swagger'); const Inert = require('inert'); @@ -10,12 +11,7 @@ const Pack = require('./package'); const Portal = require('./lib'); const server = new Hapi.Server(); -server.connection({ - port: 3000, - routes: { - cors: true - } -}); +server.connection({ port: 3000 }); const swaggerOptions = { info: { @@ -32,10 +28,29 @@ const portalOptions = { } }; +const goodOptions = { + ops: { + interval: 1000 + }, + reporters: { + consoleReporter: [{ + module: 'good-squeeze', + name: 'Squeeze', + args: [{ log: '*', response: '*', error: '*' }] + }, { + module: 'good-console' + }, 'stdout'] + } +}; + server.register([ Brule, Inert, Vision, + { + register: Good, + options: goodOptions + }, { register: Portal, options: portalOptions diff --git a/setup.sh b/setup.sh index 3d8ee105..ff96ea4d 100755 --- a/setup.sh +++ b/setup.sh @@ -24,10 +24,15 @@ check() { exit 1 } + TRITON_USER=$(triton profile get | awk -F": " '/account:/{print $2}') + TRITON_DC=$(triton profile get | awk -F"/" '/url:/{print $3}' | awk -F'.' '{print $1}') + echo '# docker-compose-client for Triton' > _env TRITON_CREDS_PATH=/root/.triton echo TRITON_CREDS_PATH=${TRITON_CREDS_PATH} >> _env echo DOCKER_CERT_PATH=${TRITON_CREDS_PATH} >> _env + echo TRITON_USER=${TRITON_USER} >> _env + echo TRITON_DC=${TRITON_DC} >> _env echo TRITON_CA=$(cat "${DOCKER_CERT_PATH}"/ca.pem | tr '\n' '#') >> _env echo TRITON_CA_PATH=${TRITON_CREDS_PATH}/ca.pem >> _env echo TRITON_KEY=$(cat "${DOCKER_CERT_PATH}"/key.pem | tr '\n' '#') >> _env