From 6eba3ddac22bd7ee9c9f9e07b1e4208447a60ea3 Mon Sep 17 00:00:00 2001 From: KOMATSU Seiji Date: Thu, 23 Feb 2017 04:07:58 +0900 Subject: [PATCH 1/5] Fix sharedocsEnv template - Changed LOGIN_PROVIDER to app - Changed UPLOAD_DESTINATION to s3 --- sharedocsEnv-template | 15 ++++++++++----- sharedocsEnv-template.bat | 12 ++++++++++-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/sharedocsEnv-template b/sharedocsEnv-template index 355d677..6b52669 100644 --- a/sharedocsEnv-template +++ b/sharedocsEnv-template @@ -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 @@ -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 @@ -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" @@ -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 \ No newline at end of file +#export MARKDOWN_HELP_PAGE_ID=1 diff --git a/sharedocsEnv-template.bat b/sharedocsEnv-template.bat index f274bc4..7cbcb13 100644 --- a/sharedocsEnv-template.bat +++ b/sharedocsEnv-template.bat @@ -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 @@ -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 @@ -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 \ No newline at end of file +rem SET MARKDOWN_HELP_PAGE_ID=1 From f2b4495a4694c470c7250afffc7585e1342a8589 Mon Sep 17 00:00:00 2001 From: KOMATSU Seiji Date: Thu, 23 Feb 2017 00:11:18 +0900 Subject: [PATCH 2/5] Improved sharedocs script --- README.md | 38 +++++++---- sharedocs | 196 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 213 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..794bab9 100755 --- a/sharedocs +++ b/sharedocs @@ -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 <&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} + + # 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 + From 5af21b17d7c7f38b26528b0689068927180bbf35 Mon Sep 17 00:00:00 2001 From: KOMATSU Seiji Date: Thu, 23 Feb 2017 04:58:40 +0900 Subject: [PATCH 3/5] Rename current docker-compose.yml to docker-compose-dev.yml --- docker-compose.yml => docker-compose-dev.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename docker-compose.yml => docker-compose-dev.yml (100%) diff --git a/docker-compose.yml b/docker-compose-dev.yml similarity index 100% rename from docker-compose.yml rename to docker-compose-dev.yml From 6886bbac5e14fd7f500393d214e7cfaab6fce8e8 Mon Sep 17 00:00:00 2001 From: KOMATSU Seiji Date: Thu, 23 Feb 2017 05:00:21 +0900 Subject: [PATCH 4/5] Provide an official docker container image Implemented `./sharedocs package:docker` to build an official docker container image, and implemented `./sharedocs publish:docker` to push an official docker container image to Dockerhub. --- .gitignore | 3 ++- build.sbt | 9 +++++++ docker-compose.yml | 37 +++++++++++++++++++++++++++ project/plugins.sbt | 4 +++ sharedocs | 45 ++++++++++++++++++++++++++++++++- sharedocs_standalone_docker.sbt | 30 ++++++++++++++++++++++ 6 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 docker-compose.yml create mode 100644 sharedocs_standalone_docker.sbt diff --git a/.gitignore b/.gitignore index 8b9600b..52c8ac7 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ task/target/ task/src/main/resources/ build/target/ standalone-build/ +standalone-docker-build/ *.ser *.class @@ -72,4 +73,4 @@ sharedocsEnv.bat terraform.tfvars *.tfstate -*.tfstate.backup \ No newline at end of file +*.tfstate.backup diff --git a/build.sbt b/build.sbt index b3b95be..eb4d47d 100644 --- a/build.sbt +++ b/build.sbt @@ -146,3 +146,12 @@ lazy val standaloneBuild = (project in file("standalone-build")).settings(packag ideaIgnoreModule := true, ivyXML := ) +lazy val standaloneDockerBuild = (project in file("standalone-docker-build")) + .enablePlugins(DockerPlugin) + .settings(packagingBaseSettings) + .settings( + name := appName + "-standalone", + libraryDependencies += "org.skinny-framework" %% "skinny-standalone" % skinnyVersion, + ideaIgnoreModule := true, + ivyXML := +) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..1ede5d3 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,37 @@ +version: '3' +services: + db: + build: + context: . + dockerfile: Dockerfile-db + volumes: + - sharedocs-db-data:/var/lib/postgresql/data + environment: + POSTGRES_USER: sharedocs + POSTGRES_PASSWORD: sharedocs + POSTGRES_DB: sharedocs + POSTGRES_INITDB_ARGS: "-E UTF8 -T template0 --lc-collate=ja_JP.UTF-8 --lc-ctype=ja_JP.UTF-8" + app: + build: + context: . + dockerfile: Dockerfile-app + ports: + - 8080:8080 + links: + - db + volumes: + - sharedocs-app-upload:/var/sharedocs/upload + environment: + DATABASE_HOST: db:5432 + DATABASE_DBNAME: sharedocs + DATABASE_USER: sharedocs + DATABASE_PASSWORD: sharedocs + SITE_NAME: Sharedocs:local + SITE_TITLE: Sharedocs + LOGIN_PROVIDOR: app + UPLOAD_DESTINATION: local + LOCAL_UPLOAD_BASE_DIR: /var/sharedocs/upload + LOCAL_UPLOAD_BASE_URL: /static/uploads +volumes: + sharedocs-db-data: + sharedocs-app-upload: diff --git a/project/plugins.sbt b/project/plugins.sbt index 7fb575c..b971aba 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -39,3 +39,7 @@ addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.5.0") // check the latest version of dependencies // addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.1.10") + +// -------- +addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.0") +addSbtPlugin("se.marcuslonnberg" % "sbt-docker" % "1.4.0") diff --git a/sharedocs b/sharedocs index 794bab9..2b2f000 100755 --- a/sharedocs +++ b/sharedocs @@ -7,6 +7,7 @@ DEFAULT_DATABASE_HOST=localhost:5432 DEFAULT_DATABASE_USER=sharedocs DEFAULT_DATABASE_PASSWORD=sharedocs DEFAULT_DATABASE_DBNAME=sharedocs +SBT=${MYDIR}/sbt usage() { cat <&2 @@ -14,6 +15,8 @@ Usage: $0 help: Show help of sharedocs commands $0 help-skinny: Show help of skinny commands $0 setup: Setup to build + $0 package:docker: Build docker image + $0 publish:docker: Publish docker image $0 : Run skinny command (for more details, run $0 help-skinny) EOT @@ -127,10 +130,17 @@ migrate_db() { ok "Migrating database succeeded" else ng "Migrating database failed" - exit 16 + exit 15 fi } +setup_standalone_docker_build() { + rm -rf standalone-docker-build + mkdir standalone-docker-build + cp -pr src standalone-docker-build/src + cp -p sharedocs_standalone_docker.sbt standalone-docker-build/ +} + setup() { npm_install npm_run_webpack @@ -169,6 +179,33 @@ setup() { info "" } +package_docker() { + npm_install + npm_run_webpack + + info "Building sharedocs-standalone docker image" + setup_standalone_docker_build + ${SBT} standaloneDockerBuild/docker + if [ $? -eq 0 ]; then + ok "Building sharedocs-standalone docker image succeeded" + else + ng "Building sharedocs-standalone docker image failed" + exit 16 + fi +} + +publish_docker() { + info "Pushing sharedocs-standalone docker image" + ${SBT} standaloneDockerBuild/dockerPush + if [ $? -eq 0 ]; then + ok "Pushing sharedocs-standalone docker image succeeded" + else + ng "Pushing sharedocs-standalone docker image failed" + exit 17 + fi +} + + cd ${MYDIR} command="$1" @@ -182,6 +219,12 @@ case "$command" in help-skinny) ./skinny help ;; + package:docker) + package_docker + ;; + publish:docker) + publish_docker + ;; *) if [ $# -eq 0 ]; then usage diff --git a/sharedocs_standalone_docker.sbt b/sharedocs_standalone_docker.sbt new file mode 100644 index 0000000..ab1dc8d --- /dev/null +++ b/sharedocs_standalone_docker.sbt @@ -0,0 +1,30 @@ +mainClass in assembly := Some("skinny.standalone.JettyLauncher") +_root_.sbt.Keys.test in assembly := {} +resourceGenerators in Compile <+= (resourceManaged, baseDirectory) map { (managedBase, base) => + val webappBase = base / "src" / "main" / "webapp" + for ( (from, to) <- webappBase ** "*" `pair` rebase(webappBase, managedBase / "main/") ) + yield { + Sync.copy(from, to) + to + } +} + +dockerfile in docker := { + val artifact: File = assembly.value + val artifactTargetPath = s"/app/${artifact.name}" + + new Dockerfile { + from("openjdk:8-jre-alpine") + add(artifact, artifactTargetPath) + entryPoint("java", "-jar", "-Dskinny.env=production", artifactTargetPath) + } +} + +imageNames in docker := Seq( + ImageName(s"sharedocs/${name.value}:latest"), + ImageName( + namespace = Some("sharedocs"), + repository = name.value, + tag = Some("v" + version.value) + ) +) From 986af2410b32ca5c7ba2f7cdadb6ec8ad06b1433 Mon Sep 17 00:00:00 2001 From: KOMATSU Seiji Date: Thu, 23 Feb 2017 05:08:47 +0900 Subject: [PATCH 5/5] Enable docker container build/push on TravisCI --- .travis.yml | 12 +++++++++--- deployment/docker/deploy.sh | 7 +++++++ 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100755 deployment/docker/deploy.sh diff --git a/.travis.yml b/.travis.yml index 84549d1..267e9cb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,11 @@ language: scala -sudo: false +sudo: required +services: + - docker scala: -- 2.12.1 + - 2.12.1 jdk: -- oraclejdk8 + - oraclejdk8 cache: directories: - $HOME/.ivy2/cache @@ -13,3 +15,7 @@ script: - ./sharedocs test - find $HOME/.sbt -name "*.lock" | xargs rm -f - find $HOME/.ivy2 -name "ivydata-*.properties" | xargs rm -f +after_success: + - echo $TRAVIS_PULL_REQUEST + - echo $TRAVIS_BRANCH + - ./deployment/docker/deploy.sh diff --git a/deployment/docker/deploy.sh b/deployment/docker/deploy.sh new file mode 100755 index 0000000..82489db --- /dev/null +++ b/deployment/docker/deploy.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +if [ "$TRAVIS_PULL_REQUEST" = "false" ] && [ "$TRAVIS_BRANCH" = "master" ]; then + docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD" && + ./sharedocs package:docker && + ./sharedocs publish:docker +fi