start to add mongo configuration

This commit is contained in:
Cassie Tarakajian 2018-06-25 16:13:31 -07:00
parent 1c135635ca
commit f06198a4e1
9 changed files with 222 additions and 29 deletions

View file

@ -1,5 +1,10 @@
API_URL=/api API_URL=/api
MONGO_URL=mongodb://localhost:27017/p5js-web-editor MONGO_URL=mongodb://localhost:27017/p5js-web-editor
MONGO_HOSTNAME=localhost
MONGO_PORT=27017
MONGO_NAME=p5js-web-editor
MONGO_ROOT_USERNAME=processing_user
MONGO_ROOT_PASSWORD=<change-this-to-a-secure-password>
PORT=8000 PORT=8000
SESSION_SECRET=whatever_you_want_this_to_be_it_only_matters_for_production SESSION_SECRET=whatever_you_want_this_to_be_it_only_matters_for_production
AWS_ACCESS_KEY=<your-aws-access-key> AWS_ACCESS_KEY=<your-aws-access-key>

View file

@ -2,6 +2,9 @@ sudo: required
language: node_js language: node_js
node_js: node_js:
- "8.11.1" - "8.11.1"
python:
- "3.4"
- "pypy-5.3.1"
services: services:
- docker - docker
@ -18,7 +21,24 @@ script:
- docker-compose exec -T app npm run test --verbose - docker-compose exec -T app npm run test --verbose
after_success: after_success:
# eventually wrap this in a if branch is master # only run this if on the master branch eventually, not while testing tho
- sudo apt-get install -y python3.4
- sudo apt-get install --upgrade -y python-pip
- sudo apt-get install jq
- sudo pip install --user virtualenv
# Create a virtual environment for AWS CLI
- virtualenv my_py3 --python=/usr/bin/python3.4
- source my_py3/bin/activate
- pip install --upgrade awscli
- pip install --upgrade awsebcli
# Set AWS information
- aws configure set aws_access_key_id $AWS_ACCESS_KEY
- aws configure set aws_secret_access_key $AWS_SECRET_KEY
- aws configure set default.region $AWS_REGION
- aws configure set metadata_service_timeout 1200
- aws configure set metadata_service_num_attempts 3
- aws configure list
# build images
- docker-compose stop - docker-compose stop
- docker-compose build --no-cache - docker-compose build --no-cache
- docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD" - docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"

View file

@ -1,33 +1,38 @@
version: '3.4' version: '3.4'
services: services:
mongo: mongo:
image: mongo:3.4.7 build: ./mongo
volumes: volumes:
- dbdata:/data/db - dbdata:/data/db
expose: expose:
- "27017" - "27017"
nginx: # restart: always
build: ./nginx env_file:
ports: - .env
- '80:80' # nginx:
app: # build: ./nginx
build: # ports:
context: . # - '80:80'
dockerfile: Dockerfile # app:
target: production # build:
# context: .
# dockerfile: Dockerfile
# target: production
# env_file: # env_file:
# - .env # - .env
environment: # environment:
- MONGO_URL=mongodb://mongo:27017/p5js-web-editor # - MONGO_URL=mongodb://mongo:27017/p5js-web-editor
# - API_URL=https://localhost/api # - MONGO_HOSTNAME=mongo
# - PORT=80 # # - API_URL=https://localhost/api
volumes: # # - PORT=80
- .:/opt/node/app # volumes:
- /opt/node/app/node_modules # - .:/opt/node/app
ports: # - /opt/node/app/node_modules
- '8000:8000' # ports:
# - '443:443' # - '8000:8000'
depends_on: # # - '443:443'
- mongo # # - '80:80'
# depends_on:
# - mongo
volumes: volumes:
dbdata: dbdata:

13
mongo/Dockerfile Normal file
View file

@ -0,0 +1,13 @@
# Use an official mongo runtime as a parent image
FROM mongo:3.4
# Expose the mongo port
EXPOSE 27017
# Copy the mongod.conf file to env
# COPY mongod.conf /env/
# Copy the entrypoint file as well
COPY ./docker-entrypoint.sh .
CMD ["/bin/bash", "docker-entrypoint.sh"]

View file

@ -0,0 +1,42 @@
#!/bin/bash
set -euo pipefail
function waitForMongo {
port=$1
n=0
until [ $n -ge 20 ]
do
mongo admin --quiet --port $port --eval "db" && break
n=$[$n+1]
sleep 2
done
}
if ! [[ -a /data/db/mydb-initialized ]]; then
mongod & MONGO_PID=$!
waitForMongo 27017
mongo admin --port 27017 --eval "db.system.version.insert({ '_id' : 'authSchema', 'currentVersion' : 3 })"
mongod --shutdown
mongod & MONGO_PID=$!
waitForMongo 27017
mongo admin --port 27017 --eval "db.createUser({ user: '${MONGO_INITDB_ROOT_USERNAME}', pwd: '${MONGO_INITDB_ROOT_PASSWORD}', roles: [ 'root' ]})"
mongo admin -u ${MONGO_INITDB_ROOT_USERNAME} -p ${MONGO_INITDB_ROOT_PASSWORD} --port 27017 --eval "db.createUser({ user: '${MONGO_RW_USERNAME}', pwd: '${MONGO_RW_PASSWORD}', roles: [ {role: 'readWrite', db: 'isf-website'}, {role: 'readWrite', db: 'sessions'} ]})"
mongod --shutdown
mongod -f /env/mongod.conf --auth & MONGO_PID=$!
waitForMongo 27017
mongo admin -u ${MONGO_INITDB_ROOT_USERNAME} -p ${MONGO_INITDB_ROOT_PASSWORD} --port 27017 --eval "db.runCommand({ replSetInitiate: '{}' })"
mongo admin -u ${MONGO_INITDB_ROOT_USERNAME} -p ${MONGO_INITDB_ROOT_PASSWORD} --port 27017 --eval "db.setSlaveOk()"
mongod --shutdown
touch /data/db/mydb-initialized
fi
mongod -f /env/mongod.conf --auth & MONGO_PID=$!
waitForMongo 27017
trap 'echo "KILLING"; kill $MONGO_PID; wait $MONGO_PID' SIGINT SIGTERM EXIT
wait $MONGO_PID

73
server/Dockerrun.aws.json Normal file
View file

@ -0,0 +1,73 @@
{
"AWSEBDockerrunVersion": 2,
"authentication": {
"bucket": "",
"key": ".dockercfg"
},
"volumes": [
{
"name": "storage",
"host": {
"sourcePath": "/var/data"
}
},
{
"name": "app",
"host": {
"sourcePath": "/var/app/current/app"
}
}
],
"containerDefinitions": [
{
"name": "mongo",
"image": "mongo:3.4",
"essential": true,
"memory": 512,
"portMappings": [
{
"hostPort": 27017,
"containerPort": 27017
}
],
"mountPoints": [
{
"sourceVolume": "storage",
"containerPath": "/var/lib/mysql"
}
],
"environment": [
{
"name": "MYSQL_ROOT_PASSWORD",
"value": "password"
},
{
"name": "MYSQL_DATABASE",
"value": "my_db"
}
]
},
{
"name": "app",
"image": ":",
"essential": true,
"memory": 256,
"portMappings": [
{
"hostPort": 80,
"containerPort": 80
}
],
"links": [
"db"
],
"mountPoints": [
{
"sourceVolume": "app",
"containerPath": "/var/www/html",
"readOnly": true
}
]
}
]
}

View file

@ -3,6 +3,7 @@ import Q from 'q';
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import objectID from 'bson-objectid'; import objectID from 'bson-objectid';
import shortid from 'shortid'; import shortid from 'shortid';
import { URL } from 'url';
import eachSeries from 'async/eachSeries'; import eachSeries from 'async/eachSeries';
import User from '../models/user'; import User from '../models/user';
@ -65,7 +66,17 @@ canvas {
const headers = { 'User-Agent': 'p5js-web-editor/0.0.1' }; const headers = { 'User-Agent': 'p5js-web-editor/0.0.1' };
mongoose.connect(process.env.MONGO_URL); let mongoConnectionString;
if (process.env.NODE_ENV === 'production') {
const { MONGO_RW_USERNAME, MONGO_RW_PASSWORD, MONGO_HOSTNAME, MONGO_PORT, MONGO_NAME } = process.env;
const muo = new URL(`mongodb://${MONGO_HOSTNAME}:${MONGO_PORT}/${MONGO_NAME}`);
muo.username = MONGO_RW_USERNAME;
muo.password = MONGO_RW_PASSWORD;
mongoConnectionString = muo.href + "?authSource=admin";
} else {
mongoConnectionString = process.env.MONGO_URL;
}
mongoose.connect(mongoConnectionString);
mongoose.connection.on('error', () => { mongoose.connection.on('error', () => {
console.error('MongoDB Connection Error. Please make sure that MongoDB is running.'); console.error('MongoDB Connection Error. Please make sure that MongoDB is running.');
process.exit(1); process.exit(1);

View file

@ -4,6 +4,7 @@ import mongoose from 'mongoose';
import objectID from 'bson-objectid'; import objectID from 'bson-objectid';
import shortid from 'shortid'; import shortid from 'shortid';
import eachSeries from 'async/eachSeries'; import eachSeries from 'async/eachSeries';
import { URL } from 'url';
import User from '../models/user'; import User from '../models/user';
import Project from '../models/project'; import Project from '../models/project';
@ -35,7 +36,18 @@ const clientSecret = process.env.GITHUB_SECRET;
const headers = { 'User-Agent': 'p5js-web-editor/0.0.1' }; const headers = { 'User-Agent': 'p5js-web-editor/0.0.1' };
mongoose.connect(process.env.MONGO_URL); let mongoConnectionString;
if (process.env.NODE_ENV === 'production') {
const { MONGO_RW_USERNAME, MONGO_RW_PASSWORD, MONGO_HOSTNAME, MONGO_PORT, MONGO_NAME } = process.env;
const muo = new URL(`mongodb://${MONGO_HOSTNAME}:${MONGO_PORT}/${MONGO_NAME}`);
muo.username = MONGO_RW_USERNAME;
muo.password = MONGO_RW_PASSWORD;
mongoConnectionString = muo.href + "?authSource=admin";
} else {
mongoConnectionString = process.env.MONGO_URL;
}
mongoose.connect(mongoConnectionString);
mongoose.connection.on('error', () => { mongoose.connection.on('error', () => {
console.error('MongoDB Connection Error. Please make sure that MongoDB is running.'); console.error('MongoDB Connection Error. Please make sure that MongoDB is running.');
process.exit(1); process.exit(1);

View file

@ -7,6 +7,7 @@ import session from 'express-session';
import connectMongo from 'connect-mongo'; import connectMongo from 'connect-mongo';
import passport from 'passport'; import passport from 'passport';
import path from 'path'; import path from 'path';
import { URL } from 'url';
// Webpack Requirements // Webpack Requirements
import webpack from 'webpack'; import webpack from 'webpack';
@ -44,6 +45,17 @@ if (process.env.NODE_ENV === 'development') {
corsOriginsWhitelist.push(/localhost/); corsOriginsWhitelist.push(/localhost/);
} }
let mongoConnectionString;
if (process.env.NODE_ENV === 'production') {
const { MONGO_RW_USERNAME, MONGO_RW_PASSWORD, MONGO_HOSTNAME, MONGO_PORT, MONGO_NAME } = process.env;
const muo = new URL(`mongodb://${MONGO_HOSTNAME}:${MONGO_PORT}/${MONGO_NAME}`);
muo.username = MONGO_RW_USERNAME;
muo.password = MONGO_RW_PASSWORD;
mongoConnectionString = muo.href + "?authSource=admin";
} else {
mongoConnectionString = process.env.MONGO_URL;
}
// Enable Cross-Origin Resource Sharing (CORS) for all origins // Enable Cross-Origin Resource Sharing (CORS) for all origins
const corsMiddleware = cors({ const corsMiddleware = cors({
credentials: true, credentials: true,
@ -72,7 +84,7 @@ app.use(session({
secure: false, secure: false,
}, },
store: new MongoStore({ store: new MongoStore({
url: process.env.MONGO_URL, url: mongoConnectionString,
autoReconnect: true autoReconnect: true
}) })
})); }));
@ -105,7 +117,7 @@ require('./config/passport');
// const passportConfig = require('./config/passport'); // const passportConfig = require('./config/passport');
// Connect to MongoDB // Connect to MongoDB
mongoose.connect(process.env.MONGO_URL); mongoose.connect(mongoConnectionString);
mongoose.connection.on('error', () => { mongoose.connection.on('error', () => {
console.error('MongoDB Connection Error. Please make sure that MongoDB is running.'); console.error('MongoDB Connection Error. Please make sure that MongoDB is running.');
process.exit(1); process.exit(1);