Commit f9970b0d authored by Fabio Montefuscolo's avatar Fabio Montefuscolo
Browse files

Dockerfile and entrypoint

parents
FROM debian:jessie
MAINTAINER <contato@hacklab.com.br>
ENV DEBIAN_FRONTEND noninteractive
ENV PXC_HOME="/var/cache/mysql"
ENV PXC_DATA="/var/lib/mysql"
ENV PXC_RUN="/var/run/mysqld"
ENV MYSQL_ROOT_PASSWORD="rootpass"
ENV MYSQL_ALLOW_EMPTY_PASSWORD="no"
ENV MYSQL_DATABASE="database"
ENV MYSQL_USER="pxcuser"
ENV MYSQL_PASSWORD="pxcpass"
ENV XTRABACKUP_PASSWORD="password"
ENV MYSQL_LOG_FILE=/var/log/mysqld.log
RUN groupadd -r mysql && useradd -r -m -d "${PXC_HOME}" -g mysql mysql \
&& apt-get update \
&& apt-get install -y curl jq \
&& curl -sLo pxc.deb https://repo.percona.com/apt/percona-release_0.1-5.jessie_all.deb \
&& dpkg -i pxc.deb \
&& apt-get update \
&& apt-get install -y percona-xtradb-cluster-57 \
&& rm -Rf "${PXC_DATA}" "${PXC_RUN}" \
&& mkdir -m 755 -p "${PXC_DATA}" "${PXC_RUN}" \
&& chown -R mysql: "${PXC_DATA}" "${PXC_RUN}" \
&& rm pxc.deb \
&& rm /etc/mysql/my.cnf.old
RUN { \
echo "[mysqld]"; \
echo "server-id=1"; \
echo "datadir=${PXC_DATA}"; \
echo "log-error=${MYSQL_LOG_FILE}"; \
echo "socket=${PXC_RUN}/mysqld.sock"; \
echo "pid-file=${PXC_RUN}/mysqld.pid"; \
echo "log-bin"; \
echo "log_slave_updates"; \
echo "default_storage_engine=InnoDB"; \
echo "binlog_format=ROW"; \
echo "innodb_flush_log_at_trx_commit = 0"; \
echo "innodb_flush_method = O_DIRECT"; \
echo "innodb_file_per_table = 1"; \
echo "innodb_autoinc_lock_mode=2"; \
echo "bind_address = 0.0.0.0"; \
echo "wsrep_slave_threads=2"; \
echo "wsrep_cluster_address=gcomm://"; \
echo "wsrep_provider=/usr/lib/galera3/libgalera_smm.so"; \
echo "wsrep_cluster_name=Theistareykjarbunga"; \
echo "wsrep_sst_method=xtrabackup-v2"; \
echo "wsrep_sst_auth=\"root:\""; \
} > /etc/mysql/percona-xtradb-cluster.conf.d/mysqld.cnf
VOLUME ["${PXC_DATA}","${PXC_RUN}"]
EXPOSE 3306 4567 4568
COPY entrypoint /entrypoint
ENTRYPOINT ["/entrypoint/main.sh"]
CMD ["mysqld", "--user=mysql", "--wsrep_sst_method=xtrabackup-v2"]
#!/bin/bash
bootstrap_sql_timezone () {
mysql_tzinfo_to_sql /usr/share/zoneinfo 2>&- \
| sed 's/Local time zone must be set--see zic manual page/FCTY/'
}
bootstrap_sql_root_user () {
cat <<-EOSQL
-- What's done in this file shouldn't be replicated
-- or products like mysql-fabric won't work
SET @@SESSION.SQL_LOG_BIN=0;
CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ;
ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}';
CREATE USER 'xtrabackup'@'localhost' IDENTIFIED BY '${XTRABACKUP_PASSWORD}';
GRANT RELOAD,PROCESS,LOCK TABLES,REPLICATION CLIENT ON *.* TO 'xtrabackup'@'localhost';
GRANT REPLICATION CLIENT ON *.* TO monitor@'%' IDENTIFIED BY 'monitor';
GRANT PROCESS ON *.* TO monitor@localhost IDENTIFIED BY 'monitor';
DROP DATABASE IF EXISTS test ;
FLUSH PRIVILEGES ;
EOSQL
}
bootstrap_sql_database () {
printf 'CREATE DATABASE IF NOT EXISTS `%s`;' "$MYSQL_DATABASE"
echo ''
}
bootstrap_sql_user () {
printf "CREATE USER '%s'@'%%' IDENTIFIED BY '%s';" "$MYSQL_USER" "$MYSQL_PASSWORD"
echo ''
}
bootstrap_sql_grant_user () {
printf "GRANT ALL ON \`%s\`.* TO '%s'@'%%';" "$MYSQL_DATABASE" "$MYSQL_USER"
echo 'FLUSH PRIVILEGES;'
}
bootstrap_database () {
sql_file="${PXC_HOME}/bootstrap.sql"
bootstrap_sql_timezone >> "$sql_file"
bootstrap_sql_root_user >> "$sql_file"
if [ -n "$MYSQL_DATABASE" ];
then
bootstrap_sql_database >> "$sql_file"
fi
if [ -n "$MYSQL_USER" ] && [ -n "$MYSQL_PASSWORD" ];
then
bootstrap_sql_user >> $sql_file
if [ -n "$MYSQL_DATABASE" ];
then
bootstrap_sql_grant_user >> "$sql_file"
fi
fi
mysql_install_db \
--insecure \
--user=mysql \
--basedir=/usr \
--datadir="${PXC_DATA}" \
--extra-sql-file="$sql_file"
rm "$sql_file"
}
#!/bin/bash
BASE_URL="http://$ETCD_HOST/v2/keys/pxc-cluster"
MY_IP=$(hostname -i | awk '{ print $1 }')
TTL=60
register_me_on_queue () {
curl -sS "${BASE_URL}/queue/$CLUSTER_NAME" -XPOST -d value=$MY_IP -d ttl=$TTL \
| tee /dev/stderr
| jq '.'
}
register_me_on_quorum () {
curl -sS "${BASE_URL}/$CLUSTER_NAME/$MY_IP/ipaddr" -XPUT -d value="$MY_IP" -d ttl=$TTL
curl -sS "${BASE_URL}/$CLUSTER_NAME/$MY_IP/hostname" -XPUT -d value="$HOSTNAME" -d ttl=$TTL
curl -sS "${BASE_URL}/$CLUSTER_NAME/$MY_IP" -XPUT -d ttl=30 -d dir=true -d prevExist=true
}
get_queue_addresses () {
curl -sS "${BASE_URL}/queue/$CLUSTER_NAME" \
| jq ".node.nodes[] | select(.value != \"$MY_IP\") .value" \
| tee /dev/stderr
}
get_quorum_addresses () {
curl "${BASE_URL}/$CLUSTER_NAME/?quorum=true"
| jq -r '.node.nodes[]?.key' \
| grep -o '[0-9\.]*$'
}
get_cluster_addresses () {
get_queue_addresses;
get_quorum_addresses;
}
#!/bin/bash
if [ -z "${CLUSTER_NAME}" ];
then
echo 'Set environment variable CLUSTER_NAME' > /dev/stderr
exit 1
fi
if [ -z "$(ls -A $PXC_DATA)" ];
then
mkdir -m 755 -p "${PXC_DATA}"
chown -R mysql: "${PXC_DATA}"
source /entrypoint/bootstrap.sh;
bootstrap_database
fi
exec "$@"
#!/bin/bash
MY_IP=$(hostname -i | awk '{ print $1 }')
if [ -z "${CLUSTER_NAME}" ];
then
echo 'Set environment variable CLUSTER_NAME' > /dev/stderr
exit 1
fi
if [ -z "$(ls -A $PXC_DATA)" ];
then
mkdir -m 755 -p "${PXC_DATA}"
chown -R mysql: "${PXC_DATA}"
source /entrypoint/bootstrap.sh;
bootstrap_database
fi
if [ -z "$CLUSTER_JOIN" ];
then
if [ -n "$ETCD_HOST" ];
then
source /entrypoint/discovery_etcd.sh
register_me_on_queue;
fi
CLUSTER_JOIN=$(get_cluster_addresses | sed -Eze 's/^\s*//;s/\s*$//;s/\s+/,/g')
export CLUSTER_JOIN
fi
if [ "$(basename $1)" = "mysqld" ];
then
rm "${MYSQL_LOG_FILE}"
mkfifo -m 777 "${MYSQL_LOG_FILE}"
cat "${MYSQL_LOG_FILE}" &
exec "$@" \
--wsrep_cluster_name=$CLUSTER_NAME \
--wsrep_cluster_address="gcomm://$CLUSTER_JOIN" \
--wsrep_sst_auth="xtrabackup:$XTRABACKUP_PASSWORD" \
--wsrep_node_address="$MY_IP"
else
exec "$@"
fi
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment