Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve sharedocs script #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -80,15 +92,15 @@ Setup PostgreSQL server on your machine and create database.
| AWS_S3_BASE_DIR | (if UPLOAD_DESTINATION==s3)<br />base pash to upload image file | "images/" |
| AWS_S3_BASE_URL | (if UPLOAD_DESTINATION==s3)<br />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
Expand Down
196 changes: 188 additions & 8 deletions sharedocs
Original file line number Diff line number Diff line change
@@ -1,13 +1,193 @@
#!/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 <<EOT >&2
Usage:
$0 help: Show help of sharedocs commands
$0 help-skinny: Show help of skinny commands
$0 setup: Setup to build
$0 <skinny command>: 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 $@
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}

# TODO: This is workaround to allow any domains
sed -e 's/^\(export LOGIN_PERMITTED_EMAIL_DOMAINS\)/#\1/' -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

15 changes: 10 additions & 5 deletions sharedocsEnv-template
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/bin/bash
### -------------------------------------------------------------------------------------
### Sharedocs configuration variables
###
### For more details, see: https://github.com/atware/sharedocs/blob/master/README.md
### -------------------------------------------------------------------------------------

export SITE_NAME=Sharedocs:local
export SITE_TITLE=Sharedocs
Expand All @@ -10,10 +14,12 @@ export DATABASE_PASSWORD="********"

export GOOGLE_ANALYTICS_KEY="********"

export LOGIN_PROVIDOR=google
export LOGIN_PROVIDOR=app
export LOGIN_PERMITTED_EMAIL_DOMAINS=example.com

export SKINNY_OAUTH2_CLIENT_ID_GOOGLE="*************************"
export SKINNY_OAUTH2_CLIENT_SECRET_GOOGLE="***********************"

export LDAP_TYPE="plain"
export LDAP_HOST="***"
export LDAP_PORT=389
Expand All @@ -24,8 +30,7 @@ export LDAP_USER_NAME_ATTRIBUTE=uid
export LDAP_MAIL_ADDRESS_ATTRIBUTE=mail
export LDAP_KEY_STORE=

export UPLOAD_DESTINATION=s3

export UPLOAD_DESTINATION=local
export LOCAL_UPLOAD_BASE_DIR="/tmp"
export LOCAL_UPLOAD_BASE_URL="/static/uploads"

Expand All @@ -39,4 +44,4 @@ export EXTERNAL_INTEGRATION_SERVICE=logger

export OGP_ALLOW_UA_PREFIXES="UA prefix to allow to access"

#export MARKDOWN_HELP_PAGE_ID=1
#export MARKDOWN_HELP_PAGE_ID=1
12 changes: 10 additions & 2 deletions sharedocsEnv-template.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
@ECHO OFF

rem -------------------------------------------------------------------------------------
rem Sharedocs configuration variables
rem
rem For more details, see: https://github.com/atware/sharedocs/blob/master/README.md
rem -------------------------------------------------------------------------------------

SET SITE_NAME=Sharedocs:local
SET SITE_TITLE=Sharedocs

Expand All @@ -10,10 +16,12 @@ SET DATABASE_PASSWORD=********

SET GOOGLE_ANALYTICS_KEY=********

SET LOGIN_PROVIDOR=google
SET LOGIN_PROVIDOR=app
SET LOGIN_PERMITTED_EMAIL_DOMAINS=example.com

SET SKINNY_OAUTH2_CLIENT_ID_GOOGLE=*************************
SET SKINNY_OAUTH2_CLIENT_SECRET_GOOGLE=***********************

SET LDAP_TYPE=plain
SET LDAP_HOST=***
SET LDAP_PORT=389
Expand All @@ -39,4 +47,4 @@ SET EXTERNAL_INTEGRATION_SERVICE=logger

SET OGP_ALLOW_UA_PREFIXES="UA prefix to allow to access"

rem SET MARKDOWN_HELP_PAGE_ID=1
rem SET MARKDOWN_HELP_PAGE_ID=1