2019-03-04 11:23:12 +08:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
set -o nounset
|
|
|
|
set -o errexit
|
|
|
|
|
|
|
|
function usage() {
|
|
|
|
cat <<EOF
|
|
|
|
Usage:
|
|
|
|
$0 start <kubeasz_ver> To start a kubeasz(tagged) container
|
|
|
|
$0 clean To stop and remove all running containers
|
|
|
|
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
function start_kubeasz_docker() {
|
|
|
|
# kubeasz docker version
|
2019-03-05 23:10:18 +08:00
|
|
|
KUBEASZ_DOCKER_VER=jmgao1983/kubeasz:$1
|
2019-03-17 17:58:39 +08:00
|
|
|
echo "[INFO] VERSION $KUBEASZ_DOCKER_VER"
|
2019-03-04 11:23:12 +08:00
|
|
|
|
|
|
|
# init host dir
|
|
|
|
if [ -d "/etc/ansible/roles/" ];then
|
2019-03-17 17:58:39 +08:00
|
|
|
echo "[WARN] aleady Initialized."
|
2019-03-04 11:23:12 +08:00
|
|
|
else
|
|
|
|
rm -rf /etc/ansible
|
2019-03-17 17:58:39 +08:00
|
|
|
echo "[INFO] run a temporary container"
|
2019-03-04 11:23:12 +08:00
|
|
|
docker run -d --name temp_easz $KUBEASZ_DOCKER_VER
|
2019-03-17 17:58:39 +08:00
|
|
|
echo "[INFO] init host kubeasz directory"
|
2019-03-04 11:23:12 +08:00
|
|
|
docker cp temp_easz:/etc/ansible /etc/ansible
|
2019-03-17 17:58:39 +08:00
|
|
|
echo "[INFO] stop&remove temporary container"
|
2019-03-04 11:23:12 +08:00
|
|
|
docker stop temp_easz
|
|
|
|
docker rm temp_easz
|
|
|
|
fi
|
|
|
|
|
|
|
|
# get host's IP
|
2019-03-17 17:58:39 +08:00
|
|
|
host_if=$(ip route|grep default|cut -d' ' -f5)
|
|
|
|
host_ip=$(ip a|grep $host_if|awk 'NR==2{print $2}'|cut -d'/' -f1)
|
|
|
|
echo "[INFO] get host IP: $host_ip"
|
2019-03-04 11:23:12 +08:00
|
|
|
|
|
|
|
# run kubeasz docker container
|
2019-03-17 17:58:39 +08:00
|
|
|
echo "[INFO] run kubeasz in a container"
|
2019-03-04 11:23:12 +08:00
|
|
|
docker run --detach \
|
|
|
|
--name kubeasz \
|
|
|
|
--restart always \
|
2019-03-17 17:58:39 +08:00
|
|
|
--env HOST_IP=$host_ip \
|
2019-03-04 11:23:12 +08:00
|
|
|
--volume /etc/ansible:/etc/ansible \
|
|
|
|
--volume /root/.kube:/root/.kube \
|
|
|
|
--volume /root/.ssh/id_rsa:/root/.ssh/id_rsa:ro \
|
|
|
|
--volume /root/.ssh/id_rsa.pub:/root/.ssh/id_rsa.pub:ro \
|
|
|
|
--volume /root/.ssh/known_hosts:/root/.ssh/known_hosts:ro \
|
|
|
|
$KUBEASZ_DOCKER_VER
|
|
|
|
}
|
|
|
|
|
|
|
|
function clean_container() {
|
2019-03-17 17:58:39 +08:00
|
|
|
echo "[INFO] clean all running containers"
|
2019-03-04 11:23:12 +08:00
|
|
|
docker ps -a|awk 'NR>1{print $1}'|xargs docker rm -f
|
|
|
|
}
|
|
|
|
|
|
|
|
[ "$#" -gt 0 ] || { usage >&2; exit 2; }
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
|
|
|
|
(start)
|
|
|
|
[ "$#" -gt 1 ] || { usage >&2; exit 2; }
|
|
|
|
start_kubeasz_docker $2
|
|
|
|
;;
|
|
|
|
(clean)
|
|
|
|
clean_container
|
|
|
|
;;
|
|
|
|
(*)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
esac
|