Skip to content

Commit 3f6bbac

Browse files
author
Bill Maxwell
committed
Add MySQL as the default option
Handle configuring/starting/seeding mysql as the default option. In this pass, we are handling Links, Runtime Variables and with in the container. A future iteration will include service discovery.
1 parent 020f0de commit 3f6bbac

21 files changed

+440
-9
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
dist
22
.vagrant
33
.idea
4+
*.py[c-o]
5+
__pycache__
6+
.tox
7+
server/target
8+
MANIFEST

.wrap-docker-args

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--privileged

Dockerfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM rancher/dind:v0.1.0
2+
COPY ./scripts/bootstrap /scripts/bootstrap
3+
RUN /scripts/bootstrap
4+
WORKDIR /source

scripts/bootstrap

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
apt-get update -q
4+
apt-get install -y python-dev python-pip python-tox libyaml-dev
5+
6+
pip install -U pip tox virtualenv
7+
/usr/local/bin/pip install docker-compose==1.1.0

scripts/test

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/bin/bash
2+
3+
teardown()
4+
{
5+
rm -rf .tox
6+
docker-compose -f fig-test-env.yml stop
7+
}
8+
9+
wait_for_env()
10+
{
11+
local url=$1
12+
echo "Checking for $url availability"
13+
for ((i=0;i<180;i++))
14+
do
15+
if [[ "$(curl -s ${url}/ping)" = "pong" ]]; then
16+
if [[ "$i" = "1" ]]; then
17+
echo "Environment at ${url} did not come up"
18+
fi
19+
break
20+
else
21+
sleep 1
22+
fi
23+
done
24+
}
25+
26+
get_port()
27+
{
28+
local id=$1
29+
echo $(docker inspect -f '{{ (index (index .NetworkSettings.Ports "8080/tcp") 0).HostPort }}' \
30+
$id)
31+
}
32+
33+
get_url()
34+
{
35+
local port=$1
36+
echo "http://${DOCKER_IP}:${port}"
37+
}
38+
39+
setup()
40+
{
41+
local server=$1
42+
local url=$2
43+
ID=$(fig_get_id $server)
44+
PORT=$(get_port $ID)
45+
export $url=$(get_url $PORT)
46+
eval url=\$$url
47+
wait_for_env $url
48+
}
49+
50+
if [ -x /usr/local/bin/wrapdocker ]; then
51+
wrapdocker > /tmp/docker.log 2>&1
52+
WRAPPED="true"
53+
fi
54+
55+
if [ "$(uname -s)" == "Linux" ]; then
56+
export DOCKER_IP=$(ip addr show eth0 |grep inet|grep -v inet6|cut -d' ' -f6|cut -d'/' -f1)
57+
fi
58+
59+
if [ $(command -v boot2docker) ]; then
60+
export DOCKER_IP=$(boot2docker ip)
61+
fi
62+
63+
cd $(dirname $0)/..
64+
pushd ./server > /dev/null
65+
./build-image.sh
66+
popd > /dev/null
67+
68+
# Just doing server for now.
69+
pushd ./tests/server > /dev/null
70+
71+
export CATTLE_DB_CATTLE_MYSQL_HOST=$DOCKER_IP
72+
docker-compose -f fig-test-env.yml build
73+
docker-compose -f fig-test-env.yml up -d
74+
75+
fig_get_id()
76+
{
77+
echo $(docker-compose -f fig-test-env.yml ps -q $1)
78+
}
79+
80+
trap teardown EXIT
81+
82+
for db_case in "h2dbcattle" "mysqllinkscattle" "mysqlmanualcattle" "localmysqlcattle"; do
83+
echo $db_case
84+
85+
case $db_case in
86+
h2dbcattle)
87+
setup $db_case "CATTLE_H2DB_TEST_URL"
88+
;;
89+
mysqllinkscattle)
90+
setup $db_case "CATTLE_MYSQL_LINK_TEST_URL"
91+
;;
92+
localmysqlcattle)
93+
setup $db_case "CATTLE_MYSQL_LOCAL_TEST_URL"
94+
;;
95+
mysqlmanualcattle)
96+
setup $db_case "CATTLE_MYSQL_MANUAL_TEST_URL"
97+
;;
98+
*)
99+
echo "No tests setup for $db_case"
100+
;;
101+
esac
102+
done
103+
104+
popd > /dev/null
105+
if [[ "$WRAPPED" == "true" ]]; then
106+
cp -r ./tests /scratch
107+
pushd /scratch > /dev/null
108+
fi
109+
pushd tests/server >/dev/null
110+
rm -rf .tox
111+
tox

server/Dockerfile

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
FROM ubuntu:14.04.1
2-
RUN apt-get update && apt-get install -y --no-install-recommends openjdk-7-jre-headless
3-
RUN apt-get update && apt-get install -y curl
2+
3+
ENV DEBIAN_FRONTEND noninteractive
4+
RUN apt-get update && apt-get install -y --no-install-recommends openjdk-7-jre-headless \
5+
curl \
6+
mysql-server
7+
48
ENV CATTLE_HOME /var/lib/cattle
59
ENV CATTLE_API_UI_INDEX http://cdn.rancher.io/ui/0.8.19/static/index.html
10+
ENV CATTLE_DB_CATTLE_DATABASE mysql
611
ADD artifacts /usr/share/cattle
12+
13+
ADD service /service
14+
ENV S6_SERVICE_DIR /service
15+
16+
COPY target/*static.tar.gz /s6-statics/
17+
RUN cd / && for i in $(ls /s6-statics/*static.tar.gz);do tar -zxvf $i;done
18+
719
VOLUME ["/var/lib/cattle"]
20+
VOLUME ["/var/lib/mysql"]
21+
822
EXPOSE 8080
9-
ENV RANCHER_SERVER_IMAGE v0.9.1
10-
CMD ["/usr/share/cattle/cattle.sh"]
23+
ENV RANCHER_SERVER_IMAGE v0.10.0-rc1
24+
25+
EXPOSE 3306
1126
ADD https://github.com/rancherio/cattle/releases/download/v0.16.0/cattle.jar /usr/share/cattle/
27+
CMD ["/usr/bin/s6-svscan", "/service"]

server/artifacts/cattle.sh

+52-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ cd /var/lib/cattle
55

66
JAR=/usr/share/cattle/cattle.jar
77
DEBUG_JAR=/var/lib/cattle/lib/cattle-debug.jar
8+
export S6_SERVICE_DIR=${S6_SERVICE_DIR:-$S6_SERVICE_DIR}
89

910
if [ "$URL" != "" ]
1011
then
@@ -34,12 +35,59 @@ setup_gelf()
3435
fi
3536
}
3637

38+
start_local_mysql()
39+
{
40+
s6-svc -u ${S6_SERVICE_DIR}/mysql
41+
42+
set +e
43+
for ((i=0;i<60;i++))
44+
do
45+
if mysqladmin status 2> /dev/null; then
46+
break
47+
else
48+
if [ "$i" -eq "59" ]; then
49+
echo "Could not start MySQL..." 1>&2
50+
exit 1
51+
fi
52+
sleep 1
53+
fi
54+
done
55+
set -e
56+
}
57+
58+
setup_local_db()
59+
{
60+
local db_user=$CATTLE_DB_CATTLE_USERNAME
61+
local db_pass=$CATTLE_DB_CATTLE_PASSWORD
62+
local db_name=$CATTLE_DB_CATTLE_MYSQL_NAME
63+
64+
echo "Setting up database"
65+
mysql -uroot<< EOF
66+
CREATE DATABASE IF NOT EXISTS ${db_name} COLLATE = 'utf8_general_ci' CHARACTER SET = 'utf8';
67+
GRANT ALL ON ${db_name}.* TO "${db_user}"@'%' IDENTIFIED BY "${db_pass}";
68+
GRANT ALL ON ${db_name}.* TO "${db_user}"@'localhost' IDENTIFIED BY "${db_pass}";
69+
EOF
70+
}
71+
3772
setup_mysql()
3873
{
39-
export CATTLE_DB_CATTLE_MYSQL_HOST=${CATTLE_DB_CATTLE_MYSQL_HOST:-$MYSQL_PORT_3306_TCP_ADDR}
40-
export CATTLE_DB_CATTLE_MYSQL_PORT=${CATTLE_DB_CATTLE_MYSQL_PORT:-$MYSQL_PORT_3306_TCP_PORT}
41-
if [ -n "$CATTLE_DB_CATTLE_MYSQL_HOST" ]; then
42-
export CATTLE_DB_CATTLE_DATABASE=${CATTLE_DB_CATTLE_DATABASE:-mysql}
74+
# Set in the Dockerfile by default... overriden by runtime.
75+
if [ ${CATTLE_DB_CATTLE_DATABASE} == "mysql" ]; then
76+
export CATTLE_DB_CATTLE_MYSQL_HOST=${CATTLE_DB_CATTLE_MYSQL_HOST:-$MYSQL_PORT_3306_TCP_ADDR}
77+
export CATTLE_DB_CATTLE_MYSQL_PORT=${CATTLE_DB_CATTLE_MYSQL_PORT:-$MYSQL_PORT_3306_TCP_PORT}
78+
export CATTLE_DB_CATTLE_USERNAME=${CATTLE_DB_CATTLE_USERNAME:-cattle}
79+
export CATTLE_DB_CATTLE_PASSWORD=${CATTLE_DB_CATTLE_PASSWORD:-cattle}
80+
export CATTLE_DB_CATTLE_MYSQL_NAME=${CATTLE_DB_CATTLE_MYSQL_NAME:-cattle}
81+
82+
if [ -z "$CATTLE_DB_CATTLE_MYSQL_HOST" ]; then
83+
export CATTLE_DB_CATTLE_MYSQL_HOST="localhost"
84+
start_local_mysql
85+
setup_local_db
86+
fi
87+
88+
if [ -z "$CATTLE_DB_CATTLE_MYSQL_PORT" ]; then
89+
CATTLE_DB_CATTLE_MYSQL_PORT=3306
90+
fi
4391
fi
4492
}
4593

server/build-image.sh

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#!/bin/bash
22

3+
4+
cd $(dirname $0)
5+
6+
mkdir target
7+
docker run -it -v $(pwd)/target:/output rancher/s6-builder:v0.1.0 /opt/build.sh
8+
39
TAG=${TAG:-dev}
410
IMAGE=rancher/server:${TAG}
511

6-
cd $(dirname $0)
712
docker build -t ${IMAGE} .

server/service/cattle/finish

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
s6-svcscanctl -t /service

server/service/cattle/run

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
exec /usr/share/cattle/cattle.sh

server/service/mysql/down

Whitespace-only changes.

server/service/mysql/finish

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
s6-svcscanctl -t /service

server/service/mysql/run

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
exec 2>&1
4+
exec /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/log/mysql/error.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306

tests/server/README.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Rancher tests

tests/server/fig-test-env.yml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
h2dbcattle:
2+
image: rancher/server:dev
3+
ports:
4+
- "8080:8080"
5+
environment:
6+
CATTLE_DB_CATTLE_DATABASE: h2
7+
mysqllink:
8+
image: mysql:latest
9+
environment:
10+
MYSQL_ROOT_PASSWORD: password
11+
MYSQL_USER: cattle
12+
MYSQL_PASSWORD: cattle
13+
MYSQL_DATABASE: cattle
14+
mysqllinkscattle:
15+
image: rancher/server:dev
16+
ports:
17+
- "8081:8080"
18+
links:
19+
- mysqllink:mysql
20+
localmysqlcattle:
21+
image: rancher/server:dev
22+
ports:
23+
- "8082:8080"
24+
mysqlmanual:
25+
image: mysql:latest
26+
ports:
27+
- "13306:3306"
28+
environment:
29+
MYSQL_ROOT_PASSWORD: 'password'
30+
MYSQL_USER: 'cattle1'
31+
MYSQL_PASSWORD: 'cattle1'
32+
MYSQL_DATABASE: 'cattle1'
33+
mysqlmanualcattle:
34+
image: rancher/server:dev
35+
environment:
36+
CATTLE_DB_CATTLE_MYSQL_HOST:
37+
CATTLE_DB_CATTLE_MYSQL_PORT: '13306'
38+
CATTLE_DB_CATTLE_USERNAME: 'cattle1'
39+
CATTLE_DB_CATTLE_PASSWORD: 'cattle1'
40+
CATTLE_DB_CATTLE_MYSQL_NAME: 'cattle1'
41+
ports:
42+
- "8083:8080"

tests/server/pytest.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
norecursedirs = .git .tox server agent docs server

tests/server/ranchertests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)