#!/usr/bin/env bash

userdir=`who|awk '{print $1}'`
cd /home/$userdir

apt update
apt upgrade -y

#下载新go包
wget https://dl.google.com/go/go1.18.3.linux-amd64.tar.gz
echo "=========下载到的go软件包版本========"
ls go1.*

sudo tar -zxvf go1.18.3.linux-amd64.tar.gz  -C  /usr/local
cd /root
sudo mkdir -p go/{src,pkg,bin}
sudo chmod u+x go/{src,pkg,bin}

echo 'export PATH=$PATH:/usr/local/go/bin' >> /home/$userdir/.profile
echo 'export GOROOT=/usr/local/go' >> /home/$userdir/.profile
echo 'export GOPATH=$HOME/go' >> /home/$userdir/.profile
echo 'export PATH=$PATH:$HOME/go/bin' >> /home/$userdir/.profile
source /home/$userdir/.profile

echo "=========看到go的版本与go配置信息========"
go version
go env
sleep 3

echo 'export PATH=$PATH:/usr/local/go/bin' >> /root/.bashrc
echo 'export GOROOT=/usr/local/go' >> /root/.bashrc
echo 'export GOPATH=$HOME/go' >> /root/.bashrc
echo 'export PATH=$PATH:$HOME/go/bin' >> /root/.bashrc
source /root/.bashrc

echo "=========Go环境部署与配置完成。========"
sleep 3
echo "=========安装工具包。========"
sudo apt update && apt upgrade -y
sudo apt-get install git
sudo apt install gcc
sudo apt install make

echo "=========下载go-ipfs源码。========"

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
go get -u github.com/ipfs/go-ipfs

cd  /root/go/pkg/mod/github.com/ipfs/go-ipfs@v0.12.0/
chmod 777 bin/check_go_version 
chmod 777 plugin/loader/preload.sh
make install
ipfs version
echo "=========如出ipfs version 则部署成功========"



<<AAA
docker --version >/dev/null 2>&1
if [ $? -eq 0 ]
then
    echo "已安装docker"
else
    echo "正在安装docker-----------"
    apt install docker.io -y >/dev/null 2>&1
	echo "=======安装完成=========="
    sleep 5
fi

cur_dir=$(cd "$(dirname "$0")";pwd)
cd $cur_dir
IMAGE_NAME='mysql:8.0.13'
DOCKER_NAME='master'
MYSQL_PASSWORD='123456'
PORT='4306'
script='user_mysql.sh'
confdir='/usr/local/master/conf'
datadir='/usr/local/master/data'

#创建映射目录
[ ! -d $confdir ] && mkdir -p $confdir
[ ! -d $datadir ] && mkdir -p $datadir
[ ! -f $confdir/my.cnf ] && touch $confdir/my.cnf

#创建MySQL容器
create_mysql () {
	docker pull $IMAGE_NAME >/dev/null 2>&1
	docker run --name $DOCKER_NAME -p $PORT:3306 --restart always -v $confdir:/etc/mysql/conf.d/ -v $datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=$MYSQL_PASSWORD -d $IMAGE_NAME
}

#制作创建MySQL用户脚本
create_script () {
	echo '#!/usr/bin/env bash' >${script}
    echo "mysql -uroot -p$MYSQL_PASSWORD <<EOF" >>${script}
    echo "create user '$DOCKER_NAME'@'%' identified with mysql_native_password by '$MYSQL_PASSWORD';" >>${script}
    echo "grant replication slave,replication client on *.* TO '$DOCKER_NAME'@'%';" >>${script}
    echo 'flush privileges;' >>${script}
    echo 'EOF' >>${script}
}

#配置MySQL数据库
config_databases () {
	create_script
	chmod +x ${script}
	docker cp ${script} $DOCKER_NAME:/
	echo "正在复制脚本----------------------"
	sleep 15
	docker exec -it $DOCKER_NAME bash -c " ./${script}; "
}
	
#主函数
config_mysql () {
	cat >$confdir/my.cnf <<EOF
[mysqld]
# master01主服务器01唯一ID
server-id=$PORT

# 启用二进制日志
log-bin=mysql-bin

#从库的中继日志，主库日志写到中继日志，中继日志再重做到从库
# relay-log=myslql-relay-bin

# binlog保留时间7天
expire_logs_days=7

#指定不同步库
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
EOF
}

#重启docker容器使主函数生效
docker restart $DOCKER_NAME

main () {
	echo "=========正在创建MySQL容器========"
	create_mysql
	echo "========正在配置MySQL数据库======="
	config_databases
	echo "========正在导入数据库配置========"
	config_mysql
	echo "=================================="
	echo -e "\033[40;36m success \033[0m"
	echo "=================================="
}
main $@
AAA

exit 2
