Skip to content

Commit

Permalink
Merge pull request #23 from developmentseed/feature/22-setup-script
Browse files Browse the repository at this point in the history
Setup script Fix #22
  • Loading branch information
olafveerman authored Dec 21, 2018
2 parents 6a8e371 + ec3b982 commit 302df3e
Show file tree
Hide file tree
Showing 6 changed files with 408 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
yarn.lock
node_modules
.env
cloudformation.yml
cloudformation.yml
setup/config.js
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Follow these steps before deploying the first stack:
* the key pair under `default.ecs.keyPairName`
* Set the environment variables in `.kes/.env`

This configuration is shared between different stacks. If you run multiple stack, you only have to do this once.
This configuration is shared between different stacks. If you run multiple stacks, you only have to do this once.

## Deploying a stack
To deploy a new stack:
Expand All @@ -45,11 +45,13 @@ The new stack will inherit all configuration from the default stack. It's possib
stackName: ram-another-instance
```
* run kes deploy
`./node_modules/.bin/kes cf deploy --deployment ram-my-deployment --region us-east-1 --kes-class .kes/kes.js`.
* setup initial DB structure
When deploying a stack for the first time, find the postgres connection string in the RDS interface. Add this to `local.js` of the `[ram-backend](https://github.com/WorldBank-Transport/ram-backend)` and run `yarn setup --db`
`$ ./node_modules/.bin/kes cf deploy --deployment ram-my-deployment --region us-east-1`

Go to ECS console to find out the IP of the `frontend` machine. RAM will be accessible through that IP.
Once the deploy finishes (which may take a while), Kes prints information about the stack. This includes IP addresses and the database connection string. Use this connection string to set up the database structure:

```
$ yarn setup --db postgres://your:own@connection:1234/string
```

## Deleting a stack
Deleting a stack will take all resources down, but keep the S3 bucket that was created by the stack for file storage. It can be manually deleted.
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "RAM deployment with the CloudFormation",
"main": "index.js",
"scripts": {
"setup": "DEBUG=true node setup/",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
Expand All @@ -17,6 +18,8 @@
},
"homepage": "https://github.com/developmentseed/ram-deployment#readme",
"dependencies": {
"kes": "^2.2.6"
"kes": "^2.2.7",
"knex": "^0.16.3",
"pg": "^7.7.1"
}
}
46 changes: 46 additions & 0 deletions setup/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';
const knex = require('knex');
const setup = require('./structure');

const arg = (a) => process.argv.indexOf(a) !== -1;

const db = knex({
client: 'pg',
connection: process.argv[2]
});

async function checkDangerousDbOp () {
const exists = await db.schema.hasTable('scenarios');
if (exists && !arg('--force-override')) {
console.log('ERROR: Database is not empty.');
console.log('Use --force-override if you want to delete everything.');
process.exit(1);
}
}

async function main () {
try {
if (arg('--help') || arg('-h') || process.argv.length < 3 || process.argv[2] === '--force-override') {
console.log('Set up the RAM database with:')
console.log('');
console.log(' yarn setup [postgres_connection_string]');
console.log('');
console.log('Options:');
console.log(' --force-override', ' Use to override safe data check.');
console.log(' WARNING: All data will be lost');
console.log('');
process.exit(0);
}

await checkDangerousDbOp();
await setup.setupStructure(db);

console.log('done');
process.exit(0);
} catch (error) {
console.log(error);
process.exit(1);
}
}

main();
Loading

0 comments on commit 302df3e

Please sign in to comment.