From c091c213d6ff37a0338e329e610e30eea22a92bd Mon Sep 17 00:00:00 2001 From: KOMATSU Seiji Date: Thu, 23 Feb 2017 00:11:18 +0900 Subject: [PATCH] Improved sharedocs script --- README.md | 38 +++++++---- sharedocs | 193 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 210 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 3000323..c246933 100644 --- a/README.md +++ b/README.md @@ -20,32 +20,44 @@ or ## Setup for Development Environment -### Setup PostgreSQL +### Setup in one go + +#### mac/*unix + + $ ./sharedocs setup + +#### Windows + +*Currently not supported* + +### Setup in steps + +#### Setup PostgreSQL Setup PostgreSQL server on your machine and create database. $ createuser -P sharedocs $ createdb -E UTF8 -T template0 --lc-collate=ja_JP.UTF-8 --lc-ctype=ja_JP.UTF-8 sharedocs -### Prepare Tables +#### Prepare Tables -#### mac/*nix +##### mac/*nix ./sharedocs db:migrate -#### Windows +##### Windows - ./sharedocs.bat db:migrate + sharedocs.bat db:migrate -### Set Environment Variables to sharedocsEnv script +#### Set Environment Variables to sharedocsEnv script -#### mac/*nix +##### mac/*nix cp sharedocsEnv-template sharedocsEnv -#### Windows +##### Windows - cp sharedocsEnv-template.bat sharedocsEnv.bat + copy sharedocsEnv-template.bat sharedocsEnv.bat ##### Environment Variables @@ -80,15 +92,15 @@ Setup PostgreSQL server on your machine and create database. | AWS_S3_BASE_DIR | (if UPLOAD_DESTINATION==s3)
base pash to upload image file | "images/" | | AWS_S3_BASE_URL | (if UPLOAD_DESTINATION==s3)
base url to access uploaded image file | "https://xxxxxxxx.s3.amazonaws.com/" | -### Run Application +#### Run Application -#### mac/*nix +##### mac/*nix ./sharedocs run -#### Windows +##### Windows - ./sharedocs.bat run + sharedocs.bat run ## Additional setup for Development Environment diff --git a/sharedocs b/sharedocs index 04c068f..3fed3f3 100755 --- a/sharedocs +++ b/sharedocs @@ -1,13 +1,190 @@ #!/usr/bin/env bash -cd $(dirname $0) -source ./sharedocsEnv +MYDIR=$(dirname "$0") +ENV_FILE=${MYDIR}/sharedocsEnv +ENV_FILE_TEMPLATE=${MYDIR}/sharedocsEnv-template +DEFAULT_DATABASE_HOST=localhost:5432 +DEFAULT_DATABASE_USER=sharedocs +DEFAULT_DATABASE_PASSWORD=sharedocs +DEFAULT_DATABASE_DBNAME=sharedocs -command="$1" -if [ "$command" == "run" -o "$command" == "s" -o "$command" == "server" -o "$command" == "debug" -o "$command" == "d" ]; then - if [ ! -e src/main/webapp/assets/dist/version.txt ]; then - npm run webpack +usage() { + cat <&2 +Usage: + $0 help: Show help of sharedocs commands + $0 help-skinny: Show help of skinny commands + $0 setup: Setup to build + $0 : Run skinny command (for more details, run $0 help-skinny) +EOT + + exit 9 +} + +info() { + echo -e "\e[1m[INFO]\e[0m $*" >&2 +} + +warn() { + echo -e "\e[1m[\e[33mWARN\e[39m]\e[0m $*" >&2 +} + + +ok() { + echo -e "\e[1m[\e[32m OK \e[39m]\e[0m $*" >&2 +} + +ng() { + echo -e "\e[1m[\e[31m NG \e[39m]\e[0m $*" >&2 +} + +npm_install() { + npm install + if [ $? -eq 0 ]; then + ok "npm install succeeded" + else + ng "npm install failed" + exit 11 + fi +} + +npm_run_webpack() { + npm run webpack + if [ $? -eq 0 ]; then + ok "npm run webpack succeeded" + else + ng "npm run webpack failed" + exit 12 + fi +} + +create_db_user() { + info "Creating db user for Sharedocs." + + read -p "Database Host (default=$DEFAULT_DATABASE_HOST): " DATABASE_HOST + if [ -z "$DATABASE_HOST" ]; then + DATABASE_HOST=$DEFAULT_DATABASE_HOST + fi + export DATABASE_HOST + + read -p "Username (default=$DEFAULT_DATABASE_USER): " DATABASE_USER + if [ -z "$DATABASE_USER" ]; then + DATABASE_USER=$DEFAULT_DATABASE_USER + fi + export DATABASE_USER + + read -p "Password (default=$DEFAULT_DATABASE_PASSWORD): " DATABASE_PASSWORD + if [ -z "$DATABASE_PASSWORD" ]; then + DATABASE_PASSWORD=$DEFAULT_DATABASE_PASSWORD + fi + export DATABASE_PASSWORD + + info "Checking db user \`${DATABASE_USER}' exists" + result=$(psql postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='${DATABASE_USER}'") + if [ "$result" = "1" ]; then + ok "DB user \`${DATABASE_USER}' already exists." + return 0 fi -fi -./skinny $@ \ No newline at end of file + info "Creating db user \`${DATABASE_USER}'" + psql postgres -tAc "create user ${DATABASE_USER} with password '${DATABASE_PASSWORD}';" + if [ $? -eq 0 ]; then + ok "Creating user \`${DATABASE_USER}' succeeded" + else + ng "Creating user \`${DATABASE_USER}' failed" + exit 13 + fi +} + +create_db() { + info "Creating database for Sharedocs." + read -p "Database Name (default=$DEFAULT_DATABASE_DBNAME): " DATABASE_DBNAME + if [ -z "$DATABASE_DBNAME" ]; then + DATABASE_DBNAME=$DEFAULT_DATABASE_DBNAME + fi + export DATABASE_DBNAME + + info "Checking db \`${DATABASE_DBNAME}' exists" + result=$(psql postgres -tAc "SELECT 1 FROM pg_database WHERE datname='${DATABASE_DBNAME}'") + if [ "$result" = "1" ]; then + ok "Database \`${DATABASE_DBNAME}' already exists." + return 0 + fi + + info "Creating database \`${DATABASE_DBNAME}'" + createdb -E UTF8 -T template0 -O ${DATABASE_USER} --lc-collate=ja_JP.UTF-8 --lc-ctype=ja_JP.UTF-8 ${DATABASE_DBNAME} + if [ $? -eq 0 ]; then + ok "Creating database \`${DATABASE_DBNAME}' succeeded" + else + ng "Creating user \`${DATABASE_DBNAME}' failed" + exit 14 + fi +} + +migrate_db() { + info "Migrating database" + ./skinny db:migrate + if [ $? -eq 0 ]; then + ok "Migrating database succeeded" + else + ng "Migrating database failed" + exit 16 + fi +} + +setup() { + npm_install + npm_run_webpack + create_db_user + create_db + migrate_db + + if [ ! -f "${ENV_FILE}" ]; then + warn "${ENV_FILE} does not exist." + warn "Copying from ${ENV_FILE_TEMPLATE} to ${ENV_FILE}." + cp -p ${ENV_FILE_TEMPLATE} ${ENV_FILE} + fi + + sed -e 's/\(DATABASE_HOST="\).*\("\)/\1'"${DATABASE_HOST}"'\2/' -i '' ${ENV_FILE} + sed -e 's/\(DATABASE_DBNAME="\).*\("\)/\1'"${DATABASE_DBNAME}"'\2/' -i '' ${ENV_FILE} + sed -e 's/\(DATABASE_USER="\).*\("\)/\1'"${DATABASE_USER}"'\2/' -i '' ${ENV_FILE} + sed -e 's/\(DATABASE_PASSWORD="\).*\("\)/\1'"${DATABASE_PASSWORD}"'\2/' -i '' ${ENV_FILE} + + info "" + info "***** Getting Started to Run Sharedocs *****" + info "" + info "To run Sharedocs, at least the following variables must be configured in ${ENV_FILE}." + info " DATABASE_HOST" + info " DATABASE_DBNAME" + info " DATABASE_USER" + info " DATABASE_PASSWORD" + info " LOGIN_PERMITTED_EMAIL_DOMAINS" + info "" + info "If you have configured variables, you're now ready to run Sharedocs!" + info "Run the following command:" + info "" + info " ./sharedocs run" + info "" +} + +cd ${MYDIR} + +command="$1" +case "$command" in + setup) + setup + ;; + help|--help|-h) + usage + ;; + help-skinny) + ./skinny help + ;; + *) + if [ $# -eq 0 ]; then + usage + fi + [ -f ${ENV_FILE} ] && . ${ENV_FILE} + ./skinny $@ + ;; +esac +