forked from c9s/bbgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.sh
executable file
·136 lines (105 loc) · 3.48 KB
/
deploy.sh
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
set -e
target=$1
# bin_type is the binary type that you want to build bbgo
# use "bbgo" for full-features binary (including web application)
# use "bbgo-slim" for slim version binary (without web application)
bin_type=bbgo-slim
# host_bin_dir is the directory that binary file will be uploaded to.
# default to $HOME/bin
host_bin_dir=bin
host=bbgo
host_user=root
host_home=/root
host_systemd_service_dir=/etc/systemd/system
host_os=linux
host_arch=amd64
# setup_host_systemd_service: should we create a new systemd service file if it does not exist?
# change this to "yes" to enable the automatic setup.
# if setup_host_systemd_service is enabled, the script will create a systemd service file from a template
# and then upload the systemd service file to $host_systemd_service_dir,
# root permission might be needed, you can change the host user to root temporarily while setting up the environment.
setup_host_systemd_service=no
if [[ -n $SETUP_SYSTEMD ]] ; then
setup_host_systemd_service=yes
fi
# use the git describe as the binary version, you may override this with something else.
tag=$(git describe --tags)
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
function remote_test() {
ssh $host "(test $* && echo yes)"
}
function remote_run() {
ssh $host "$*"
}
function remote_eval() {
ssh $host "echo $*"
}
function warn() {
echo "${YELLOW}$*${NC}"
}
function error() {
echo "${RED}$*${NC}"
}
function info() {
echo "${GREEN}$@${NC}"
}
if [[ -n $BBGO_HOST ]]; then
host=$BBGO_HOST
else
warn "env var BBGO_HOST is not set, using \"bbgo\" host alias as the default host, you can add \"bbgo\" to your ~/.ssh/config file"
host=bbgo
fi
if [[ -z $target ]]; then
echo "Usage: $0 [target]"
echo "target name is required"
exit 1
fi
# initialize the remote environment
# create the directory for placing binaries
ssh $host "mkdir -p \$HOME/$host_bin_dir && mkdir -p \$HOME/$target"
if [[ $(remote_test "-e $host_systemd_service_dir/$target.service") != "yes" ]]; then
if [[ "$setup_host_systemd_service" == "no" ]]; then
error "The systemd $target.service on host $host is not configured, can not deploy"
exit 1
fi
warn "$host_systemd_service_dir/$target.service does not exist, setting up..."
if [[ -z $host_home ]]; then
host_home=$(remote_eval "\$HOME")
fi
cat <<END >".systemd.$target.service"
[Unit]
After=network-online.target
Wants=network-online.target
[Install]
WantedBy=multi-user.target
[Service]
WorkingDirectory=$host_home/$target
KillMode=process
ExecStart=$host_home/$target/bbgo run
User=$host_user
Restart=always
RestartSec=30
END
info "uploading systemd service file..."
scp ".systemd.$target.service" "$host:$host_systemd_service_dir/$target.service"
info "reloading systemd daemon..."
remote_run "sudo systemctl daemon-reload && systemctl enable $target"
fi
info "building binary: $bin_type-$host_os-$host_arch..."
make $bin_type-$host_os-$host_arch
# copy the binary to the server
info "deploying..."
info "copying binary to host $host..."
if [[ $(remote_test "-e $host_bin_dir/bbgo-$tag") != "yes" ]] ; then
scp build/bbgo/$bin_type-$host_os-$host_arch $host:$host_bin_dir/bbgo-$tag
else
info "binary $host_bin_dir/bbgo-$tag already exists, we will use the existing one"
fi
# link binary and restart the systemd service
info "linking binary and restarting..."
ssh $host "(cd $target && ln -sf \$HOME/$host_bin_dir/bbgo-$tag bbgo && sudo systemctl restart $target.service)"
info "deployed successfully!"