diff --git a/README.md b/README.md index 3692af9..729fe08 100644 --- a/README.md +++ b/README.md @@ -23,15 +23,12 @@ BASE_URL=http://127.0.0.1:8000 ./run.sh Then point your browser at the URL below to install license and configuation for each applicaiton: -| JIRA | BASE_URL/jira | -| Stash | BASE_URL/stash | -| Fisheye | BASE_URL/fisheye | -| Bamboo | BASE_URL/bamboo | +| Application | URL | +|---------------|---------------------| +| JIRA | BASE_URL/jira | +| Stash | BASE_URL/stash | +| Fisheye | BASE_URL/fisheye | +| Bamboo | BASE_URL/bamboo | -### TO DO -1. add initdb to one line provision. -2. format this document properly -3. add docker status check so that docker stop/rm does not throw error messages - diff --git a/atl-postgres/Dockerfile b/atl-postgres/Dockerfile new file mode 100644 index 0000000..d1b0108 --- /dev/null +++ b/atl-postgres/Dockerfile @@ -0,0 +1,7 @@ +FROM postgres:9.3 +MAINTAINER Li Lin + +RUN mkdir -p /docker-entrypoint-initdb.d +ADD atl-dbinit.sh /docker-entrypoint-initdb.d + + diff --git a/atl-postgres/atl-dbinit.sh b/atl-postgres/atl-dbinit.sh new file mode 100755 index 0000000..01f0ee2 --- /dev/null +++ b/atl-postgres/atl-dbinit.sh @@ -0,0 +1,14 @@ +DB_USER=$postgres + +create_db_if_not_exist() { + + ( psql -l -U $DB_USER | grep $1 ) || \ + echo " CREATE DATABASE $1 WITH ENCODING='UTF8' TEMPLATE=template0; " | psql -U $DB_USER +} + +create_db_if_not_exist jira +create_db_if_not_exist stash +create_db_if_not_exist fisheye +create_db_if_not_exist bamboo + + diff --git a/atl-postgres/build.sh b/atl-postgres/build.sh new file mode 100644 index 0000000..b05719b --- /dev/null +++ b/atl-postgres/build.sh @@ -0,0 +1,2 @@ +TAG="sloppycoder/atl-postgres" +docker build --rm --tag ${TAG}:latest . diff --git a/build.sh b/build.sh index 3d66e0b..148e64c 100755 --- a/build.sh +++ b/build.sh @@ -18,6 +18,11 @@ cd atl-bamboo ./build.sh cd .. +cd atl-postgres +./build.sh +cd .. + + cd atl-web ./build.sh cd .. diff --git a/dbinit.sh b/dbinit.sh deleted file mode 100755 index 251f69b..0000000 --- a/dbinit.sh +++ /dev/null @@ -1,11 +0,0 @@ -echo " -DROP DATABASE jira; -DROP DATABASE stash; -DROP DATABASE fisheye; -DROP DATABASE bamboo; -CREATE DATABASE jira WITH ENCODING='UTF8' TEMPLATE=template0; -CREATE DATABASE stash WITH ENCODING='UTF8' TEMPLATE=template0; -CREATE DATABASE fisheye WITH ENCODING='UTF8' TEMPLATE=template0; -CREATE DATABASE bamboo WITH ENCODING='UTF8' TEMPLATE=template0; " \ - | PGPASSWORD="password" psql -h db -p 5432 -d postgres -U postgres -w - diff --git a/run.sh b/run.sh index 8fd7cc4..3a08f72 100755 --- a/run.sh +++ b/run.sh @@ -1,5 +1,7 @@ #! /bin/bash +DOCKER_HUB_USER=sloppycoder + usage() { echo " @@ -17,13 +19,11 @@ echo " run.sh web # restart web server - run.sh db # start db server - - run.sh dbshell # launch a shell into db server + run.sh db # stop and remove db container, then start a new one - run.sh initdb # drop and recreate database used by applications + run.sh dbsh # launch a shell into db server - run.sh data # start an interactive container for examine the shared data volumes + run.sh datash # start an interactive container for examine the shared data volumes run.sh backup # backup all data inside data container into /atldata.tar.gz @@ -33,6 +33,24 @@ echo " } + +is_container_running() { + + if [ "`docker inspect -f '{{ .State.Running }}' $1 `" = "true" ]; then + return 1 + else + return 0 + fi +} + + +stop_and_rm_container() { + + is_container_running $1 && docker stop $1 + (docker ps -a | grep $1) || docker rm $1 +} + + start_app() { if [ "$2" = "-i" ]; then @@ -41,61 +59,49 @@ start_app() { RUN_MODE="-d" fi + # start data container if it does not exist + (docker ps -a | grep atldata) && start_data + docker run $RUN_MODE --name $1 --link postgres:db \ --volumes-from="atldata" -e BASE_URL=$BASE_URL \ - sloppycoder/atl-$1 $3 + ${DOCKER_HUB_USER}/atl-$1 $3 } + start_db() { - docker stop postgres - docker rm postgres + stop_and_rm_container postgres docker run -d --name postgres -e POSTGRES_PASSWORD=password \ - --volumes-from="atldata" postgres:9.3 + --volumes-from="atldata" ${DOCKER_HUB_USER}/atl-postgres } -init_db() { - - cat dbinit.sh | docker run --rm -i --link postgres:db postgres:9.3 bash - -} start_data() { - - docker run --name atldata \ - -v /opt/atlassian-home \ - -v /var/lib/postgresql/data \ - centos:7 /bin/bash + (docker ps -a | grep atldata) && \ + docker run --name atldata \ + -v /opt/atlassian-home \ + -v /var/lib/postgresql/data \ + centos:7 echo "data container atldata started" } -start_web() { - - docker stop atlweb - docker rm atlweb - - if [ "`docker inspect -f '{{ .State.Running }}' jira`" = "true" ]; then - LINKS="$LINKS --link jira:jira" - fi - if [ "`docker inspect -f '{{ .State.Running }}' fisheye`" = "true" ]; then - LINKS="$LINKS --link fisheye:fisheye" - fi - - if [ "`docker inspect -f '{{ .State.Running }}' stash`" = "true" ]; then - LINKS="$LINKS --link stash:stash" - fi +start_web() { - if [ "`docker inspect -f '{{ .State.Running }}' bamboo`" = "true" ]; then - LINKS="$LINKS --link bamboo:bamboo" - fi + is_container_running jira && LINKS="$LINKS --link jira:jira" + is_container_running stash && LINKS="$LINKS --link stash:stash" + is_container_running fisheye && LINKS="$LINKS --link fisheye:fisheye" + is_container_running bamboo && LINKS="$LINKS --link bamboo:bamboo" echo starting httpd with links $LINKS + stop_and_rm_container atlweb docker run -d --name atlweb -p 80:80 -p 443:443 \ $LINKS \ - sloppycoder/atl-web + ${DOCKER_HUB_USER}/atl-web test -z "$LINKS" && echo httpd started but no application running. } + ACTION=$1 test -z "$ACTION" && ACTION=all @@ -104,9 +110,15 @@ case "$ACTION" in all) echo + echo use + echo run.sh -h + echo + echo to see other options available in this command + echo echo echo starting all docker containers for Atlassian Jira, Stash, Fisheye and Bamboo - echo all images will be downloaded from docker hub for the first time, this will take a while + echo all images will be downloaded from docker hub for the first time, this will take a while. + echo echo hit ctrl-C to abort ... echo sleep 2 @@ -127,47 +139,63 @@ case "$ACTION" in ;; jira|stash|fisheye|bamboo) - docker stop $1 - docker rm $1 + + stop_and_rm_container $1 start_app $1 $2 $3 $4 - if [ -z "$2" ]; then - start_web - fi + test -z "$2" && start_web ;; web) + start_web + ;; db) + start_db + ;; initdb) + init_db + ;; - dbshell) + dbsh) + docker exec -it postgres /bin/bash + ;; - data) + datash) + docker run -it --rm --volumes-from="atldata" centos:7 /bin/bash + ;; backup) + BACKUP_DIR=$(readlink -f $2) - docker run -it --rm --volumes-from atldata -v $BACKUP_DIR:/backup sloppycoder/java-base \ + echo backing up all data in atldata to $BACKUP_DIR + + docker run -it --rm --volumes-from atldata -v $BACKUP_DIR:/backup ${DOCKER_HUB_USER}/java-base \ tar czvf /backup/atldata.tar.gz \ /opt/atlassian-home /var/lib/postgresql/data + ;; clean) + docker rmi $(docker images | grep "^" | awk ' {print $3} ') + ;; *) + usage + ;; esac