Skip to content
This repository has been archived by the owner on May 18, 2018. It is now read-only.

Commit

Permalink
add postgresql-docker
Browse files Browse the repository at this point in the history
  • Loading branch information
HsuTing committed Jul 6, 2017
1 parent cf29e61 commit 314d4ca
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 30 deletions.
38 changes: 8 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,14 @@ Then generate your new project:
yo cat
```

## Subgenerator
- Those of subgenerator will be controled by `plugins` in `.yo-rc.json`.
- `babel`: use to set setting of `babel`.
- This will be different when plugins have `react` or `graphql`.
- `graphql`: add main `schema`.
- `eslint`: use to add setting of `eslint`.
- `npm`: use to add `.npmignore`.
- `react`: use to install plugins of `react`.
- `readme`: use to add default `README.md`.
- `server`: use to add server.
- This server use `koa` and add some middleware of `koa`.
- If `graphql` is include in plugins, it will also make a `graphql` server.
- `template`: use to add default template.
- This is a template of `nunjucks`.
- This will add favions for all platform.
- `webpack`: use to add setting of `webpack`.
- This will be deifferent when plugins have `graphql`.
- This use `webpack 2`.
- `add`: use to add new files.
- `component`: add default component and js of `react`.
- `reducer`: add default reducer and action of `redux`.
- `store`: add default store of `redux`.
- `router`: add default router of `react-router`.
- `schema`: add default schema of `graphql`.
- `relay`: add default component, route and js of `react-relay`.
- `sqlite3`: add bin of `sqlite3` to build default data.
- `fb-bot`: template of `fb bot` with `cat-middleware`.
- `line-bot`: template of `line bot` with `cat-middleware`.
- `test`: use to add test framework.
- This use `istanbul` and `mocha`.
## Subgenerators
We does not recommend using those subgenerators because the most of those subgenerators use the `plugins` in `.yo-rc.json`. Use those subgenerators with `yo cat:subgenerators`.

- `cat:postgresql-docker`: Add a bin file to use `docker` with `postgresql`.
- `cat:add`: You can use to add some template file to your project.
- `options`
- `item`: Choose the template which will be added to project.
- `name`: This is the name of the output file.

## Getting To Know Yeoman

Expand Down
25 changes: 25 additions & 0 deletions generators/postgresql-docker/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const Base = require('./../base');

module.exports = class extends Base {
initializing() {
this.addDependencies([
'cat-utils',
'shelljs'
]);
}

writing() {
this.writePkgScripts({
'postgresql-copy': 'postgresql-copy',
docker: 'export DOCKER_ENV=production && node ./bin/docker.js',
'test-docker': 'node ./bin/docker.js'
});

this.writeFiles({
'docker.js': 'bin/docker.js',
Dockerfile: 'Dockerfile'
});
}
}
34 changes: 34 additions & 0 deletions generators/postgresql-docker/templates/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FROM node:latest

# upgrade system
RUN apt-get -y update
RUN apt-get -y upgrade
RUN apt-get -y dist-upgrade
RUN apt-get -y autoremove

# copy file
RUN mkdir /root/accounting-system
WORKDIR /root/accounting-system

COPY .babelrc \
yarn.lock \
package.json \
webpack.config.js ./
COPY bin ./bin
COPY public/favicon ./public/favicon
COPY src ./src
COPY views ./views

# start project
RUN yarn install
RUN yarn prod

# set server
ENV PORT=8000
ENV NODE_ENV="production"
ENV PGUSER="hsuting"
ENV PGPASSWORD="hsuting"
ENV PGDATABASE="db"
ENV PGHOST="db"
ENV PGPORT=5432
EXPOSE 8000
97 changes: 97 additions & 0 deletions generators/postgresql-docker/templates/docker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env node
'use strict';

const fs = require('fs');
const path = require('path');
const process = require('process');
const shell = require('shelljs');

const pkg = require(path.resolve(process.cwd(), './package.json'));

if(!shell.which('docker')) {
shell.echo('Sorry, this script requires docker.');
shell.exit(1);
}

const ENV = process.env.DOCKER_ENV === 'production';
const server_image_name = `${ENV ? '' : 'test-'}${pkg.name}`;
const server_name = ENV ? 'server' : 'test-server';
const db_name = ENV ? `db-${pkg.version}` : 'test-db';
const run = ENV ? 'run': 'run --rm';

const build = [
// build db container
`docker ${run} --name ${db_name} -e POSTGRES_USER=hsuting -e POSTGRES_PASSWORD=hsuting -e POSTGRES_DB=db -d postgres`,
// build server image
`docker build -t="${server_image_name}" .`,
// build server container
`docker ${run} --name ${server_name} --link=${db_name}:db -d -p 8000:8000 ${server_image_name} /bin/sh -c "yarn start"`,
// create tables
`docker run --rm --name build --link=${db_name}:db ${server_image_name} /bin/sh -c "node ./bin/db.js"`
];

const rebuild = [
`docker container restart ${db_name}`,
// build server image
`docker build -t="${server_image_name}" .`,
// build server container
`docker ${run} --name ${server_name} --link=${db_name}:db -d -p 8000:8000 ${server_image_name} /bin/sh -c "yarn start"`,
];

const update = ({pre_version}) => {
if(!pre_version)
throw new Error(`Error: update: previous version can not be undefined, use like "update:0.1.0"`);

if(!fs.existsSync('./../backup')) {
if(shell.mkdir('-p', path.resolve(__dirname, './../backup')).code !== 0) {
console.log(`Error: "make backup folder" failed`);
}
}

if(!ENV)
throw new Error(`Error: update: "DOCKER_ENV" is needed to be "production"`);

return [
`docker export db-${pre_version} > ./backup/db-${pre_version}.tar`,
`docker run --rm --name update --link=db-${pre_version}:db --link=${db_name}:new_db ${server_image_name} /bin/sh -c "yarn postgresql-copy"`
];
};

const start = [
`docker container restart ${db_name} ${server_name}`
];

const stop = [
`docker container stop ${db_name} ${server_name}`
];

process.argv.slice(2).forEach(argv => {
const command_name = argv.split(':')[0];
const commands = {
build,
rebuild,
update,
start,
stop
}[command_name];
const options = {};

if(!commands) {
console.log(`Error: ${argv}: command not found`);
shell.exit(1);
}

if(command_name === 'update')
options.pre_version = argv.split(':')[1];

(
commands instanceof Array ?
commands :
commands(options)
).forEach(command => {
if(shell.exec(command).code !== 0) {
console.log(`Error: "${command}" failed`);
shell.exit(1);
}
});
});

0 comments on commit 314d4ca

Please sign in to comment.