json解析参考









conf.json

{
"Dev": [
{
"Ip": "x.x.x.x",
"Dst": "/path/to/dst/",
"User": "****",
"Password": "*********",
"RsyncArgs":"-rv --exclude-from .gitignore --exclude .git --exclude migrations"
}
],
"Test": [
{
"Ip": "x.x.x.x",
"Dst": "/path/to/dst/",
"User": "****",
"Password": "*********",
"RsyncArgs":"-rv --exclude-from .gitignore --exclude .git --exclude migrations --exclude deploy_api/settings.py"
}
],
"Uat": [
{
"Ip": "x.x.x.x",
"Dst": "/path/to/dst/",
"User": "****",
"Password": "*********",
"RsyncArgs":"-rv --exclude-from .gitignore --exclude .git --exclude migrations --exclude deploy_api/settings.py"
},
{
"Ip": "x.x.x.x",
"Dst": "/path/to/dst/",
"User": "****",
"Password": "*********",
"RsyncArgs":"-rv --exclude-from .gitignore --exclude .git --exclude migrations --exclude deploy_api/settings.py"
}
]
}




deploy.sh


#!/usr/bin/env bash
function createExcludeArgs(){
EXCELUDE=".gitignore .git migrations deploy_api/settings.py" #忽略文件
local args=" "
for e in ${EXCELUDE};do
args=$args"--exclude ${e} "
done
echo $args
}

function DeployDev(){
Dev=$(cat $config|jq ".Dev")
DevNum=$(echo ${Dev}|jq ".|length")
((DevNum--))
for n in $(seq 0 $DevNum);do
Ip=$(echo $Dev|jq -r ".[$n].Ip")
Password=$(echo $Dev|jq -r ".[$n].Password")
Dst=$(echo $Dev|jq -r ".[$n].Dst")
User=$(echo $Dev|jq -r ".[$n].User")
RsyncArgs=$(echo $Dev|jq -r ".[$n].RsyncArgs")
sshpass -p "${Password}" rsync $RsyncArgs ${ABS_PATH} ${User}@${Ip}:${Dst}
done
}
function DeployUat(){
Uat=$(cat $config|jq ".Uat")
UatNum=$(echo ${Uat}|jq ".|length")
((UatNum--))
for n in $(seq 0 $UatNum);do
Ip=$(echo $Uat|jq -r ".[$n].Ip")
Password=$(echo $Uat|jq -r ".[$n].Password")
Dst=$(echo $Uat|jq -r ".[$n].Dst")
User=$(echo $Uat|jq -r ".[$n].User")
sshpass -p "${Password}" rsync $RsyncArgs ${ABS_PATH} ${User}@${Ip}:${Dst}
done
}
function DeployTest(){
Test=$(cat $config|jq ".Test")
TestNum=$(echo ${Test}|jq ".|length")
((TestNum--))
for n in $(seq 0 $TestNum);do
Ip=$(echo $Test|jq -r ".[$n].Ip")
Password=$(echo $Test|jq -r ".[$n].Password")
Dst=$(echo $Test|jq -r ".[$n].Dst")
User=$(echo $Test|jq -r ".[$n].User")
sshpass -p "${Password}" rsync $RsyncArgs ${ABS_PATH} ${User}@${Ip}:${Dst}
done
}

function Usage(){
cat << eof
Usage: $0 "uat|test|dev"
conf = conf.json

eof
}

function main(){
set -e
#set -x
. /etc/profile
cd `dirname $0`
export ABS_PATH="`pwd`/"
export config="conf.json"
if [[ ! -f $config ]];then
Usage
fi

# Args=$(createExcludeArgs)
if [[ $# -ne 1 ]];then
Usage
fi
case $1 in
"uat")
DeployUat ;;
"test")
DeployTest ;;
"dev")
DeployDev ;;
"*")
Usage;;
esac

}
main $@



这是一个简单的使用rsync 发布django项目的脚本,conf.json 和 deploy.sh 放到项目根目录