Skip to content

Commit

Permalink
dev environment files added to dev/, updated documentation to reflect…
Browse files Browse the repository at this point in the history
… new Docker option, add env variables for use with 'pg' module, see #167
  • Loading branch information
mbarlow12 committed Sep 29, 2018
1 parent 1ef0dc8 commit 69300aa
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 16 deletions.
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Rosetta is run as user "phet-admin". It requires the certain fields filled out i

The Rosetta uses syslog to save output to the winston log. You can view the logs by typing `sudo journalctl -u rosetta` or to tail the logs type `sudo journalctl -f -u rosetta`

### Database setup
### Local Development Setup (in progress)

Rosetta requires a postgres database setup for the saving strings functionality.

Expand All @@ -56,6 +56,43 @@ To setup the database locally:
`psql rosetta -f init.sql`
4. If default connection string does not work, you will need to add a different pgConnectionString in build-local.json (see https://github.com/brianc/node-postgres for more details)

The `pg` module uses environment variables to establish it's connection parameters. The defaults are:
```
PGHOST='localhost'
PGUSER=process.env.USER
PGDATABASE=process.env.USER
PGPASSWORD=null
PGPORT=5432
```

If those don't work for your setup, you can set them in your `build-local.json` using the keys referenced in the next section.

#### Docker

Another option is to use [Docker](https://docs.docker.com/). Follow the [installation instructions](https://docs.docker.com/install/) to get access to the `docker` command and execute the following:

`docker pull postgres`

If you have the latest version of the repo and this is your first time running the container, you can simply execute `start_pg_docker.sh` script within the `docker` directory at the repo's root. The script will start the container with the following attributes:
- container name: `rosettadb`
- db network address: `0.0.0.0:5432` or `localhost:5432`
- db username: `phet`
- db password: `phet`
- db name: `rosetta`

Rosetta pulls the development connection parameters from your `build-local.json`, so the following will need to be added there:
```
{
"rosettaDevDbUser": "phet",
"rosettaDevDbPass": "phetpass",
"rosettaDevDbHost": "0.0.0.0",
"rosettaDevDbPort": "5432",
"rosettaDevDbName": "rosetta"
}```
All postgres specific attributes (`username`, `password`, `database name`) are set within the `pg.env` environment file. Feel free to update them in `pg.env`, but be sure to also update them in `build-local.json`.
To stop the container, run `docker stop rosettadb`. After stopping, the container is still built and can be restarted with the command `docker restart rosettadb`. The data is persisted in `/docker/data`.
To set up the database on the server:
The database is already configured on the server. See init.sql for the changes that have been made to the website database.
9 changes: 9 additions & 0 deletions dev/.bash_history
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cd root/
ls -la
man psql
psql --help
psql --username=phet -f init.sql
psql --username=phet -d rosetta -f init.sql
psql
psql -d rosetta --username=phet
exit
4 changes: 4 additions & 0 deletions dev/.psql_history
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
\h
\?
\d
\q
4 changes: 4 additions & 0 deletions dev/docker.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POSTGRES_USER=phet
POSTGRES_PASSWORD=phetpass
POSTGRES_DB=rosetta
PGPASSWORD=phet1
File renamed without changes.
7 changes: 7 additions & 0 deletions dev/start_pg_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

docker run -d -p 5432:5432 --rm --name=rosettadb \
--env-file docker.env \
-v dbdata:/var/lib/postgres/data \
-v $(pwd):/root \
postgres
32 changes: 32 additions & 0 deletions js/configurePreferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

const assert = require( 'assert' );
const fs = require( 'fs' );
const { Client } = require( 'pg' ); //eslint-disable-line

module.exports = function() {

Expand Down Expand Up @@ -42,6 +43,37 @@ module.exports = function() {
preferences.productionServerURL = preferences.productionServerURL || 'https://phet.colorado.edu';
preferences.productionServerName = preferences.productionServerName || 'phet-server.int.colorado.edu';

// if we're in a development environment
if ( process.env.ENV === 'dev' ) {
// assert appropriate params are set in build-local.json
assert( preferences.rosettaDevDbHost, `rosettaDevDbHost is missing from ${PREFERENCES_FILE}` );
assert( preferences.rosettaDevDbPort, `rosettaDevDbPort is missing from ${PREFERENCES_FILE}` );
assert( preferences.rosettaDevDbName, `rosettaDevDbName is missing from ${PREFERENCES_FILE}` );
assert( preferences.rosettaDevDbUser, `rosettaDevDbUser is missing from ${PREFERENCES_FILE}` );
assert( preferences.rosettaDevDbPass, `rosettaDevDbPass is missing from ${PREFERENCES_FILE}` );

// 'pg' module uses env variables to establish the db connection
process.env = {
...process.env,
PGUSER: preferences.rosettaDevDbUser,
PGHOST: preferences.rosettaDevDbHost,
PGPASSWORD: preferences.rosettaDevDbPass,
PGDATABASE: preferences.rosettaDevDbName,
PGPORT: preferences.rosettaDevDbPort
}

// assert that the server is running and that there is a successful database connection
const client = new Client();
client.connect().catch( () => {
assert( false, 'There was an error connecting to the dev server' );
} )
.then( () => client.query('SELECT NOW()') )
.catch( () => {
assert( false, 'There was an error connection to the database' );
} )
.then( () => { client.end(); } );
}

/*
* Add "babelBranch": "tests" in build-local.json for rosetta testing, so that commits will change the 'tests' branch
* of babel instead of master. Make sure to checkout the tests branch in babel on the server as well.
Expand Down
10 changes: 3 additions & 7 deletions js/configureStartup.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const childProcess = require( 'child_process' ); // eslint-disable-line require-
const configurePreferences = require( './configurePreferences' );
const dateformat = require( 'dateformat' );
const parseArgs = require( 'minimist' ); // eslint-disable-line require-statement-match
const query = require( 'pg-query' ); // eslint-disable-line require-statement-match
const winston = require( 'winston' );


Expand Down Expand Up @@ -56,12 +55,9 @@ module.exports = function() {
}
} );

// configure postgres connection
if ( global.preferences.pgConnectionString ) {
query.connectionParameters = global.preferences.pgConnectionString;
}
else {
query.connectionParameters = 'postgresql://localhost/rosetta';
if ( process.env.ENV === 'dev' ) {
debugger;
process.env = { ...process.env, ...global.preferences.devDbOptions };
}

// Handle command line input
Expand Down
22 changes: 17 additions & 5 deletions js/routeHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
// node modules
const https = require( 'https' );
const nodeFetch = require( 'node-fetch' ); // eslint-disable-line
const query = require( 'pg-query' ); // eslint-disable-line
const { Client } = require( 'pg' ); // eslint-disable-line
// const query = require( 'pg-query' ); // eslint-disable-line
const winston = require( 'winston' );
const _ = require( 'underscore' ); // eslint-disable-line

Expand Down Expand Up @@ -235,6 +236,8 @@ module.exports.chooseSimulationAndLanguage = async function( req, res ) {
*/
module.exports.renderTranslationPage = async function( req, res ) {

const client = new Client();

let simName = req.params.simName;
let targetLocale = req.params.targetLocale;
let activeSimsPath = '/phetsims/perennial/master/data/active-sims';
Expand Down Expand Up @@ -308,7 +311,7 @@ module.exports.renderTranslationPage = async function( req, res ) {
} );

// wait until all string files have been retrieved before moving to the next step
return Promise.all( stringPromises ).then( results => {
return Promise.all( stringPromises ).then( async results => {
winston.log( 'info', 'finished called in renderTranslationPage' );

let currentSimStringsArray = [];
Expand All @@ -332,11 +335,14 @@ module.exports.renderTranslationPage = async function( req, res ) {
let savedStringsQuery = 'SELECT * from saved_translations where user_id = $1 AND locale = $2 AND (' + repositories + ')';
winston.log( 'info', 'running query: ' + savedStringsQuery );

await client.connect();

// query postgres to see if there are any saved strings for this user
query( savedStringsQuery, [ userId, targetLocale ], async function( err, rows ) {
client.query( savedStringsQuery, [ userId, targetLocale ], async function( err, rows ) {
winston.log( 'info', 'query returned' );
if ( err ) {
winston.log( 'error', 'query failed, error = ' + err );
return;
}

// load saved strings from database to saveStrings object if there are any
Expand All @@ -348,6 +354,8 @@ module.exports.renderTranslationPage = async function( req, res ) {
}
}

client.end();

let simTitle; // sim title gets filled in here (e.g. Area Builder instead of area-builder)
let otherSims = []; // other sim dependencies get filled in here (e.g. beers-law-lab when translating concentration)

Expand Down Expand Up @@ -588,9 +596,12 @@ module.exports.saveStrings = function( req, res ) {
const ts = new Date();

( function( key, stringValue ) {
const client = new Client();

if ( key && stringValue && stringValue.length > 0 ) {
query( 'SELECT upsert_saved_translations' +
'($1::bigint, $2::varchar(255), $3::varchar(255), $4::varchar(8), $5::varchar(255), $6::timestamp)', [ userId, key, repo, targetLocale, stringValue, ts ],
client.query( 'SELECT upsert_saved_translations' +
'($1::bigint, $2::varchar(255), $3::varchar(255), $4::varchar(8), $5::varchar(255), $6::timestamp)',
[ userId, key, repo, targetLocale, stringValue, ts ],
function( err, rows, result ) {
if ( err ) {
winston.log(
Expand All @@ -605,6 +616,7 @@ module.exports.saveStrings = function( req, res ) {
else {
finished();
}

} )( key, stringValue );
}
else {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
"body-parser": "^1.18.2",
"cookie-parser": "^1.4.3",
"dateformat": "^3.0.2",
"emailjs": "^1.0.12",
"emailjs": "^2.2.0",
"express": "^4.16.2",
"express-dot": "^0.1.0",
"express-session": "^1.15.6",
"minimist": "^1.2.0",
"node-fetch": "^1.7.3",
"octonode": "^0.9.1",
"passwd-user": "^2.1.0",
"pg": "^4.5.7",
"pg": "^7.4.0",
"pg-query": "^0.11.0",
"promise-queue": "^2.2.3",
"request": "^2.83.0",
Expand All @@ -29,7 +29,8 @@
"grunt": "~1.0.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "ENV=dev node js/rosetta.js"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 69300aa

Please sign in to comment.