码迷,mamicode.com
首页 > 其他好文 > 详细

自动部署脚本-bash

时间:2019-12-01 13:47:34      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:rap   $?   red   ksh   etc   nod   star   准备   oca   

from here

!/bin/bash

Check if user is root

if [ $(id -u) != "0" ]; then
Echo_Red "Error: You must be root to run this script"
exit 1
fi

. common.sh
. basic_soft.sh
. config_soft.sh

paths config

current_path=pwd
config_path=$current_path/config
include_path=$current_path/include
package_path=$current_path/package
env_path=/etc/profile.d/env.sh

安装准备工作

所有的ssh-keygen命令的输入,都直接回车

Prepare_Install

安装 Nginx

Insert_Nginx

安装 PHP

Insert_PHP

安装数据库

Insert_MariaDB

安装 Redis

Insert_Redis

-------------- 以下属于 mynote 应用紧密相关,经常变动部分

cd ~/init
. app.sh
. config.sh

for file in $include_path/*.sh
do
. $file
done

Prepare_Deploy

安装 Gogs

Insert_Gogs

Deploy_Mynote

Deploy_Front

Deploy_Addi

Deploy_Fastadmin

Deploy_Phpadmin

最后再进行必要的手动操作

Deploy_End

最后安装、启动es,因为需要手动回车

Insert_ES

Echo_Blue "安装完成!enjoy uninote :)"


!/bin/bash

mynote 等应用部署前的准备工作

Prepare_Deploy()
{
# 写入git server的key,避免询问
echo git.uninote.com.cn,47.110.125.97 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEApV+odcYgu2NvjNnHURxjx3as7/cBjS7u9rGuOy1ZQIGz/9NJJFJ70YeRok9oc5ANINtpZcvKTG0UFWUofXX0nmZpDa78+SgDMR5aHxSyiH6ABxqdhOzpzqqNJHGdXtqe1fwTN17Ub3rbT5pVjnJmET//IpIOgv/8XuJe9IJiodAzLc9A+ijecLyJt5rBGy0tyiArQ8YGqv9LXvisQjZf3Al9hcu2kpI50ry6EysIguFIcilzZ3KS6Php9gFMtqH5QZwRoRVL435TfGKShmYVx22IFyd7TAH0epav6tswUB8IP/3GrRV+gn7psmyfvtIpKzIwc5I0LiYS96c1fEd5AQ== >> ~/.ssh/known_hosts

cd $current_path
mkdir /home/www/.ssh
/bin/cp config/ssh-www/id_rsa /home/www/.ssh/
/bin/cp config/ssh-www/id_rsa.pub /home/www/.ssh/
chown -R www:www /home/www/.ssh
chmod 600 /home/www/.ssh/id_rsa

# git config for article update
git config --system core.quotepath false

}

启动 Gogs

Start_Gogs()
{
# 因为权限问题,就重定向到当前目录下
su - git -c ‘
cd /home/git/gogs
nohup ./gogs web >& gogslog &

}

安装 Gogs

Insert_Gogs()
{
# copy 内置的 www‘s pub key 到 git‘s authorized_key
cd $current_path
/bin/cp config/ssh-git/authorized_keys /home/git/.ssh/
chmod 600 /home/git/.ssh/authorized_keys
chown git:git /home/git/.ssh/authorized_keys

cd $package_path
tar -zxf gogs_0.11.86_linux_amd64.tar.gz -C /home/git/
mkdir -p /home/git/gogs/custom/conf
mkdir -p /home/git/gogs-repositories
echo "[repository]" >> app.ini
echo "ROOT = /home/git/gogs-repositories" >> app.ini
chown git:git -R /home/git/gogs-repositories
chown git:git -R /home/git/gogs
cp /home/git/gogs/scripts/init/centos/gogs /etc/init.d/gogs
chmod +x /etc/init.d/gogs

mysql -u root -p$db_passwd <<ENDTAG

create database gogs;
ENDTAG

Start_Gogs

等待 gogs 启动,重试10次(10秒)

try_count=0
while (($try_count < 10)); do :
curl "http://127.0.0.1:3000/install" --data "db_type=MySQL&db_host=127.0.0.1%3A3306&db_user=root&db_passwd=$db_passwd&db_name=gogs&ssl_mode=disable&db_path=data%2Fgogs.db&app_name=Gogs&repo_root_path=%2Fhome%2Fgit%2Fgogs-repositories&run_user=git&domain=localhost&ssh_port=22&http_port=3000&app_url=http%3A%2F%2Flocalhost%3A3000%2F&log_root_path=%2Fhome%2Fgit%2Fgogs%2Flog&smtp_host=&smtp_from=&smtp_user=&smtp_passwd=&enable_captcha=on&admin_name=&admin_passwd=&admin_confirm_passwd=&admin_email=" --compressed --insecure
a=$?
if [ "$a" == "0" ]
then
echo "Gogs installed!"
break;
fi
((try_count++))
sleep 1
done

if (($try_count >= 10))
then
echo "Gogs install error!"
fi
}

安装es

Insert_ES()
{
cd $package_path
rpm -i jdk-8u181-linux-x64.rpm

# 添加 es 账号
useradd es

# 修改用户资源限制
echo "

es soft nofile 65536
es hard nofile 131072
es soft nproc 2048
es hard nproc 4096
" >> /etc/security/limits.conf

# 修改用户资源限制2
echo "es     soft    nproc     4096" >> /etc/security/limits.d/90-nproc.conf
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
# 生效配置
sysctl -p

# 解压es并需改配置
tar xf elasticsearch-6.7.1.tar.gz -C /home/es
cd /home/es
chown -R es:es elasticsearch-6.7.1
cd elasticsearch-6.7.1
echo "

bootstrap.memory_lock: false
bootstrap.system_call_filter: false
network.host: 0.0.0.0
http.port: 9200
" >> config/elasticsearch.yml

# 安装es的插件
cd $package_path
mkdir /home/es/elasticsearch-6.7.1/plugins/ik
mkdir /home/es/elasticsearch-6.7.1/plugins/pinyin
unzip elasticsearch-analysis-ik-6.7.1.zip -d /home/es/elasticsearch-6.7.1/plugins/ik
unzip elasticsearch-analysis-pinyin-6.7.1.zip -d /home/es/elasticsearch-6.7.1/plugins/pinyin
cd /home/es
chown -R es:es elasticsearch-6.7.1

# 启动es
# 因为权限问题,就重定向到当前目录下
su - es -c 'cd elasticsearch-6.7.1 && nohup bin/elasticsearch >& eslog &'

}

Deploy_Mynote()
{
echo ‘root:$apr1$VEnSRa9p$OTFyusoZY9NOD85k2.XAs0‘ > /usr/local/nginx/passwd.db

mkdir -p /usr/local/nginx/conf/vhost
cat $config_path/mynote.conf > /usr/local/nginx/conf/vhost/mynote.conf
cat $config_path/enable-php.conf > /usr/local/nginx/conf/enable-php.conf
cat $config_path/nuxt.conf > /usr/local/nginx/conf/vhost/nuxt.conf
sed -i "s/__front_host/$front_host/" /usr/local/nginx/conf/vhost/nuxt.conf

mkdir -p /home/www/wwwlogs
chown -R www:www /home/www/wwwlogs

cd /home/www
# git clone git@git.uninote.com.cn:eson/mynote.git
unzip -q $package_path/mynote.git.zip -d ./mynote
unzip -q $package_path/mynote_composer.zip -d ./mynote/vendor
cd mynote
git remote set-url origin git@git.uninote.com.cn:eson/mynote.git
git fetch origin
git reset --hard $mynote_commit
# 等待所有文件就绪了再chown
cd /home/www
chown -R www:www mynote

tmp_path=/tmp/mynote_init.sql
cat /home/www/mynote/sql/*.sql > $tmp_path
sed -i "s/CREATE DATABASE*/-- /" $tmp_path
mysql -u root -p$db_passwd <<ENDTAG

CREATE DATABASE mynote DEFAULT charset=utf8;
use mynote
source $tmp_path
ENDTAG

echo "

<?php

return [
‘dsn‘ => ‘mysql:host=127.0.0.1;dbname=mynote‘,
‘password‘ => ‘$db_passwd‘];
" > /home/www/mynote/basic/config/local_db.php

cp $config_path/local_params.php /home/www/mynote/basic/config/local_params.php
chown www:www /home/www/mynote/basic/config/local_params.php

cd /home/www
cp $package_path/composer.phar /bin/composer
source $env_path
# composer config -g repo.packagist composer https://packagist.phpcomposer.com
# su - www -c '
#     cd /home/www/mynote/
#     composer install
# '

nginx -s reload

}

Deploy_Front()
{
# node 安装
# 需要先安装 xz,再解压:
yum install xz -y
tar -xf $package_path/node-v10.16.3-linux-x64.tar.xz
# 安装到 /usr/node10
mv node-v10.16.3-linux-x64 /usr/node10
# 写入 profile.d,打开 shell 自动执行:
echo ‘export PATH=/usr/node10/bin:$PATH‘ >> $env_path
source $env_path

cd /home/www
unzip -q $package_path/mynote-front-dist.git.zip -d ./front
cd front
git remote set-url origin git@git.uninote.com.cn:cyb/mynote-front-dist.git
git fetch origin
git reset --hard $mynote_front_dist_commit

# pm2 install
tar -xf $package_path/pm2.tar.gz
mv pm2 /usr/node10/lib/node_modules/
cd /usr/node10/bin
ln -s ../lib/node_modules/pm2/bin/pm2 pm2

# start front using pm2
cd /home/www/front
pm2 start node -- node_modules/nuxt/bin/nuxt.js start

# for 头像 & 编辑器图片
ln -s /home/www/mynote/basic/web/docs/__pic /home/www/front/static/__pic
ln -s /home/www/mynote/basic/web/upload_pic /home/www/front/static/upload_pic
# favicon,如果不配置,则拷贝默认版本
/bin/cp ~/init/config/favicon/$deployment/favicon.ico /home/www/front/static/

}

需要先部署 mynote

Deploy_Addi()
{
cd /home/www
# git clone git@git.uninote.com.cn:collin/mynote-env-init-v2.git
unzip -q $package_path/additional_item.git.zip -d ./additional_item
cd additional_item
git remote set-url origin git@git.uninote.com.cn:eson/additional_item.git
git fetch origin
git reset --hard $addi_commit

# configs
cat $config_path/additional_item_82.conf > /usr/local/nginx/conf/vhost/additional_item_82.conf
cat $config_path/additional_item_config.php > /home/www/additional_item/config_my.php
sed -i "s/__db_passwd/$db_passwd/" /home/www/additional_item/config_my.php

# 等待所有文件就绪了再chown
cd /home/www
chown -R www:www additional_item

tmp_path=/tmp/additional_item_init.sql
cat /home/www/additional_item/log_storage/sql/mynote*.sql > $tmp_path
mysql -u root -p$db_passwd <<ENDTAG

use mynote
source $tmp_path
ENDTAG

nginx -s reload

}

Deploy_Fastadmin() {
cat $config_path/fastadmin.conf > /usr/local/nginx/conf/vhost/fastadmin.conf
# 切换执行用户为www
cd /home/www
# git clone git@git.uninote.com.cn:eson/fastadmin.git
unzip -q $package_path/fastadmin.git.zip -d ./fastadmin
cd fastadmin
git fetch origin
git reset --hard $fastadmin_commit
# 初始化,会生成database.php等文件
cd /home/www/fastadmin
./init.sh
# 等待所有文件就绪了再chown
cd /home/www
chown -R www:www fastadmin

nginx -s reload

curl "http://127.0.0.1:81/install.php" --data "mysqlHost=127.0.0.1&mysqlDatabase=mynote&mysqlUsername=root&mysqlPassword=$db_passwd&mysqlPrefix=fa_&mysqlHostport=3306&adminUsername=admin&adminEmail=admin%40admin.com&adminPassword=123123&adminPasswordConfirmation=123123" --compressed --insecure


cd /home/www/fastadmin
./curd.sh

cd /home/www
chown -R www:www fastadmin

}

Deploy_Phpadmin() {
cd /home/www
unzip -q $package_path/phpMyAdmin-4.8.3-all-languages.zip
mv phpMyAdmin-4.8.3-all-languages phpMyAdmin
chown -R www:www phpMyAdmin
cat $config_path/phpmyadmin.conf > /usr/local/nginx/conf/vhost/phpmyadmin.conf
nginx -s reload
}

必要的手动操作

Deploy_End() {

# Gogs host
sed -i "s/__gogs_host/$gogs_host/" /home/www/mynote/basic/config/local_params.php
sed -i "s/__es_host/$es_host/" /home/www/mynote/basic/config/local_params.php
sed -i "s/__es_index/$es_index/" /home/www/mynote/basic/config/local_params.php
# 同时也是api的host
echo "

<?php

return [
‘api_url‘ => ‘http://$gogs_host/api‘,
‘pic_base‘ => ‘http://$front_host‘,
‘pic_host‘ => ‘http://$front_host‘,];
" > /home/www/mynote/basic/config/local_api.php
chown www:www /home/www/mynote/basic/config/local_api.php

# 用cp的绝对路径,避免询问是否覆盖
/bin/cp $config_path/app.ini /home/git/gogs/custom/conf/
sed -i "s/__gogs_host/$gogs_host/" /home/git/gogs/custom/conf/app.ini
sed -i "s/__db_passwd/$db_passwd/" /home/git/gogs/custom/conf/app.ini
# 初始化gogs数据库
mysql -uroot -p$db_passwd <<EOF

use gogs;
source $config_path/gogs_init.sql
EOF
# 重启gogs
kill -9 ps -ef |grep gogs | awk ‘{print $2,$3}‘
Start_Gogs

# 初始化www用户的known_hosts
# 因为无法提前知道127.0.0.1的公钥,只能用这种方式生成
# 无法用这种方式,因为ssh要求true terminal
# su - www -c 'git clone git@127.0.0.1:none_exist.git'

if [ cat ~/.ssh/known_hosts |grep 127.0.0.1|wc -l = 0 ]; then
Echo_Green "输入 yes,再多次回车即可"
ssh 127.0.0.1
else
echo "known_hosts already handled."
fi
/bin/cp ~/.ssh/known_hosts /home/www/.ssh/
chown www:www /home/www/.ssh/known_hosts

# 自启动的 workman 有各种诡异问题,不再自启动
# 启动workman,需要放到最后,这个会导致无法输入(workman 里面使用了 su - www -c 'cd $docs && ./update.sh 2>&1' )
# su - www -c '
# cd /home/www/mynote/art_workman
# php start.php start -d
# '

}


自动部署脚本-bash

标签:rap   $?   red   ksh   etc   nod   star   准备   oca   

原文地址:https://www.cnblogs.com/yizijianxin/p/11965503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!