adminset分析
从server的install脚本分析
1、首先初始化环境目录
main_dir="/var/opt/adminset" #所有目录的基础
adminset_dir="$main_dir/main" #主要文件目录
data_dir="$main_dir/data" #数据目录
config_dir="$main_dir/config" #配置文件目录
logs_dir="$main_dir/logs" #日志文件路径
cd "$( dirname "$0" )"
cd .. && cd .. #9-10在命令行状态下单纯执行 $ cd `dirname $0` 是毫无意义的。因为他返回当前路径的"."。
这个命令写在脚本文件里才有作用,他返回这个脚本文件放置的目录,并可以根据这个目录来定位所要运行程序的相对位置(绝对位置除外)。
cur_dir=$(pwd) #获取当前目录
mkdir -p $adminset_dir
mkdir -p $data_dir/scripts
mkdir -p $data_dir/files
mkdir -p $data_dir/ansible/playbook
mkdir -p $data_dir/ansible/roles
mkdir -p $config_dir
mkdir -p $config_dir/webssh
mkdir -p $logs_dir
mkdir -p $logs_dir/execlog
mkdir -p $main_dir/pid #13-22创建相应的目录
# 关闭selinux
se_status=$(getenforce)
if [ $se_status != Enforcing ]
then
echo "selinux is diabled, install progress is running"
sleep 1
else
echo "Please attention, Your system selinux is enforcing"
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux
fi #先通过getenforce获取selinux的状态并把它赋值给一个变量,判断这个变量,如果变量的值不等于Enforcing,证明关闭的,
然后程序在1s后继续,如果selinux是开启的,首先setenforce设置关闭,在利用sed将配置文件中的修改
# 安装依赖
echo "####install depandencies####"
yum install -y epel-release
yum install -y gcc expect ansible python-pip python-devel smartmontools dmidecode libselinux-python git rsync dos2unix
yum install -y openssl openssl-devel openldap-devel #可以查看相应的依赖的相关作用是什么
# 分发代码
if [ ! $cur_dir ] || [ ! $adminset_dir ]
then
echo "install directory info error, please check your system environment program exit"
exit 1
else
rsync --delete --progress -ra --exclude '.git' $cur_dir/ $adminset_dir
fi #判断目标目录是否存在,如果不存在提示error,如果存在则同步相关文件
和文件夹,并排除某些文件和文件夹,需要研究rsync命令的细节?
scp $adminset_dir/install/server/ansible/ansible.cfg /etc/ansible/ansible.cfg #将ansible的配置文件拷贝到指定的目录下面
#安装数据库,mariadb的作用是什么?
echo "####install database####"
echo "installing a new mariadb...."
yum install -y mariadb-server mariadb-devel #安装mariadb
/bin/systemctl start mariadb #启动mariadb
mysql -e "CREATE DATABASE if not exists adminset DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;" #利用mysql创建数据库,为什么安装的是mariadb,
却要用mysql启动呢?
/bin/systemctl enable mariadb.service #这个是设置开机自启动mariadb数据库
# 安装mongodb,这个作用是什么?
echo "####install mongodb####"
echo "installing a new Mongodb...."
yum install -y mongodb mongodb-server
/bin/systemctl enable mongod.service
/bin/systemctl start mongod.service #安装mongodb,设置启动,设置开机自启动
# 安装主程序
mkdir -p ~/.pip
cat <<EOF > ~/.pip/pip.conf
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF #创建pip的目录,像配置文件中写入阿里云镜像
pip install -U pip
pip install --ignore-installed enum34==1.1.6
pip install --ignore-installed ipaddress==1.0.18
pip install kombu==4.2.1
pip install celery==4.2.1
pip install billiard==3.5.0.3
pip install pytz==2017.3
pip install setuptools==39.2.0
cd $adminset_dir/vendor/django-celery-results-master
python setup.py build
python setup.py install #通过pip安装相应的组件,需要分析每个组件的作用?
cd $adminset_dir
pip install -r requirements.txt #安装requirements.txt 中的组件,里面的组件的作用
python manage.py makemigrations
python manage.py migrate #python中的数据转移
#python manage.py createsuperuser
source /etc/profile
/usr/bin/mysql -e "insert into adminset.accounts_userinfo (password,username,email,is_active,is_superuser) values ('pbkdf2_sha256\$24000\$2odRjOCV1G1V\$SGJCqWf0Eqej6bjjxusAojWtZkz99vEJlDbQHUlavT4=','admin','admin@126.com',1,1);" #导入相应数据
scp $adminset_dir/install/server/adminset.service /usr/lib/systemd/system #将主服务拷贝到相应目录下
/bin/systemctl enable adminset.service #设置开机自启动
# install webssh 这个的作用是什么?
cd $adminset_dir/vendor/webssh/
/usr/bin/env python setup.py install
scp /var/opt/adminset/main/install/server/webssh/webssh.service /usr/lib/systemd/system/webssh.service
/bin/systemctl enable webssh.service #进入相应目录执行install脚本,将程序拷贝到相应目录下面,设置开机自启动
#安装redis
echo "####install redis####"
yum install redis -y
/bin/systemctl start redis
/bin/systemctl enable redis.service #安装redis并启动,设置开机自启动
# 安装celery 这个作用是什么?
echo "####install celery####"
mkdir -p $config_dir/celery
scp $adminset_dir/install/server/celery/beat.conf $config_dir/celery/beat.conf
scp $adminset_dir/install/server/celery/celery.service /usr/lib/systemd/system
scp $adminset_dir/install/server/celery/start_celery.sh $config_dir/celery/start_celery.sh
scp $adminset_dir/install/server/celery/beat.service /usr/lib/systemd/system
chmod +x $config_dir/celery/start_celery.sh
/bin/systemctl daemon-reload
/bin/systemctl enable celery.service
/bin/systemctl enable beat.service
/bin/systemctl start celery.service
/bin/systemctl start beat.service #安装celery,拷贝到相应目录下并启动,设置开机自启
# 安装nginx
echo "####install nginx####"
yum install nginx -y
scp $adminset_dir/install/server/nginx/adminset.conf /etc/nginx/conf.d
scp $adminset_dir/install/server/nginx/nginx.conf /etc/nginx
/bin/systemctl start nginx.service
/bin/systemctl enable nginx #安装nginx将对应文件拷贝到对应的目录下面并启动,设置开机自启
# create ssh config
echo "create ssh-key, you could choose no if you had have ssh key"
if [ ! -e ~/.ssh/id_rsa.pub ]
then
ssh-keygen -q -N "" -t rsa -f /root/.ssh/id_rsa
else
echo "you had already have a ssh rsa file."
fi
scp $adminset_dir/install/server/ssh/config ~/.ssh/config #生成ssh-key这个是后面设置ssh免密登录使用的
# 完成安装,安装完成后设置相应服务启动
echo "#######Waiting Starting Service##############"
/bin/systemctl daemon-reload
/bin/systemctl restart mariadb
/bin/systemctl restart celery
/bin/systemctl restart beat
/bin/systemctl restart mongod
/bin/systemctl restart webssh
/bin/systemctl restart nginx
/bin/systemctl restart sshd
/bin/systemctl restart adminset
echo "please access website http://server_ip"
echo "you have installed adminset successfully!!!"
echo "################################################"