Skip to content
This repository has been archived by the owner on Jun 3, 2023. It is now read-only.

Commit

Permalink
added postgres container to encapsulate db initialization. enhanced r…
Browse files Browse the repository at this point in the history
…un.sh to do proper checking of container status to avoid docker stop and docker rm error messages
  • Loading branch information
Li Lin committed Jan 29, 2015
1 parent af5c9af commit 3cb7005
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 66 deletions.
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


7 changes: 7 additions & 0 deletions atl-postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM postgres:9.3
MAINTAINER Li Lin <[email protected]>

RUN mkdir -p /docker-entrypoint-initdb.d
ADD atl-dbinit.sh /docker-entrypoint-initdb.d


14 changes: 14 additions & 0 deletions atl-postgres/atl-dbinit.sh
Original file line number Diff line number Diff line change
@@ -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


2 changes: 2 additions & 0 deletions atl-postgres/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TAG="sloppycoder/atl-postgres"
docker build --rm --tag ${TAG}:latest .
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ cd atl-bamboo
./build.sh
cd ..

cd atl-postgres
./build.sh
cd ..


cd atl-web
./build.sh
cd ..
Expand Down
11 changes: 0 additions & 11 deletions dbinit.sh

This file was deleted.

120 changes: 74 additions & 46 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#! /bin/bash

DOCKER_HUB_USER=sloppycoder

usage() {

echo "
Expand All @@ -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 <dir> # backup all data inside data container into <dir>/atldata.tar.gz
Expand All @@ -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
Expand All @@ -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
Expand 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
Expand All @@ -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 "^<none>" | awk ' {print $3} ')

;;

*)

usage

;;

esac
Expand Down

0 comments on commit 3cb7005

Please sign in to comment.