1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# bin/setup_remote.sh
user=mangosteen
root=/home/$user/deploys/$version
container_name=mangosteen-prod-1
nginx_container_name=mangosteen-nginx-1
db_container_name=db-for-mangosteen
function set_env {
name=$1
hint=$2
[[ ! -z "${!name}" ]] && return
while [ -z "${!name}" ]; do
[[ ! -z "$hint" ]] && echo "> 请输入 $name: $hint" || echo "> 请输入 $name:"
read $name
done
sed -i "1s/^/export $name=${!name}\n/" ~/.bashrc
echo "${name} 已保存至 ~/.bashrc"
}
function title {
echo
echo "###############################################################################"
echo "## $1"
echo "###############################################################################"
echo
}
title '设置远程机器的环境变量'
set_env DB_HOST
set_env DB_PASSWORD
set_env RAILS_MASTER_KEY '请将 config/credentials/production.key' 的内容复制到这里
title '创建数据库'
if [ "$(docker ps -aq -f name=^${DB_HOST}$)" ]; then
echo '已存在数据库'
else
docker run -d --name $DB_HOST \
--network=network1 \
-e POSTGRES_USER=mangosteen \
-e POSTGRES_DB=mangosteen_production \
-e POSTGRES_PASSWORD=$DB_PASSWORD \
-e PGDATA=/var/lib/postgresql/data/pgdata \
-v mangosteen-data:/var/lib/postgresql/data \
postgres:14
echo '创建成功'
fi
title 'app: docker build'
docker build $root -t mangosteen:$version
if [ "$(docker ps -aq -f name=^mangosteen-prod-1$)" ]; then
title 'app: docker rm'
docker rm -f $container_name
fi
title 'app: docker run'
docker run -d -p 3000:3000 \
--network=network1 \
--name=$container_name \
-e DB_HOST=$DB_HOST \
-e DB_PASSWORD=$DB_PASSWORD \
-e RAILS_MASTER_KEY=$RAILS_MASTER_KEY \
mangosteen:$version
echo
echo "是否要更新数据库?[y/N]"
read ans
case $ans in
y|Y|1 ) echo "yes"; title '更新数据库'; docker exec $container_name bin/rails db:create db:migrate ;;
n|N|2 ) echo "no" ;;
"" ) echo "no" ;;
esac
if [ "$(docker ps -aq -f name=^${nginx_container_name}$)" ]; then
title 'doc: docker rm'
docker rm -f $nginx_container_name
fi
title 'doc: docker run'
docker run -d -p 8080:80 \
--network=network1 \
--name=$nginx_container_name \
-v /home/$user/deploys/$version/api:/usr/share/nginx/html:ro \
nginx:latest
title '全部执行完毕'
|