#!/bin/bash

# 配置主机名
host_name=$1
hostnamectl set-hostname $host_name
mkdir -p /etc/xyapp/
echo ${host_name}  > /etc/xyapp/hostname
mkdir -p /etc/xyvod
echo "XYBM_${host_name}" | sed s/'-'/'_'/g > /etc/xyvod/miner_sn

# 基础包
apt -y install libncurses5 lshw
ln -s /usr/bin/python3 /usr/bin/python

# 配置开机启动
cat > /etc/systemd/system/rc-local.service <<EOF
[Unit]
Description=/etc/rc.local
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
EOF

cat > /etc/rc.local <<EOF
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# bash /root/bindip.sh
# exit 0
EOF

chmod +x /etc/rc.local
systemctl enable rc-local >/dev/null 2>&1
systemctl start rc-local.service >/dev/null 2>&1

# 清理数据盘
sys_disk=$(mount | grep -w '/' | awk '{print$1}' | awk -F '/' '{print$NF}' | sed s/[0-9]//g)
data_disk=$(lsblk  | grep -E "^s|^n" | awk '{print$1}' | grep -Ev "${sys_disk}|sr0")
for i in  ${data_disk};do
    dd if=/dev/zero of=/dev/${i} bs=1M count=1000
done


#安装dc插件
touch  /var/spool/cron/crontabs/root
chattr -i /var/spool/cron/crontabs/root
wget -O /tmp/install_datacollect.sh  http://thunder:xunlei@apt.sandai.net/basic_setup/dkj/bare_metal/comm/install_datacollect.sh >/dev/null 2>&1  && sh /tmp/install_datacollect.sh

#安装nc插件
wget --no-check-certificate -O - https://hub-data.onething.net/node_exporter/install_node_exporter.sh | bash /dev/stdin latest
cat > /usr/local/check_ne_alive.sh<<EOF
#!/bin/sh
if [ \$(ps auxf | grep [n]ode_exporter -c) -eq 0 ];then
    /usr/local/node_exporter/bin/control start
fi
EOF
chmod +x /usr/local/check_ne_alive.sh
chattr -i /var/spool/cron/crontabs/root
echo '*/5 * * * * /usr/local/check_ne_alive.sh > /dev/null 2>&1 &' >> /var/spool/cron/crontabs/root

# 安装flume插件
wget -O - https://hub-data.onething.net/flume-agent/install-flume-agent.sh | bash /dev/stdin latest
wget  http://thunder:xunlei@apt.sandai.net/basic_setup/dkj/inst_base_monitor.sh  -O  /tmp/inst_base_monitor.sh >/dev/null 2>&1  && bash /tmp/inst_base_monitor.sh
