-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
deploy.sh
executable file
·128 lines (113 loc) · 2.72 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
#!/bin/bash
init="no"
host="$1"
if [ "$host" == "--init" ]; then
init="yes"
host="$2"
fi
config=""
if [ "$init" == "yes" ]; then
read -p "? server configuration (JSON): " v
if [ "$v" != "" ]; then
config="$v"
fi
fi
if [ "$host" == "" ]; then
read -p "? deploy to (domain or IP): " v
if [ "$v" != "" ]; then
host="$v"
fi
fi
if [ "$host" == "" ]; then
echo "missing host"
exit
fi
read -p "? host os (default is \"linux\"): " goos
read -p "? host architecture (default is \"amd64\"): " goarch
if [ "$goos" == "" ]; then
goos="linux"
fi
if [ "$goarch" == "" ]; then
goarch="amd64"
fi
user="root"
read -p "? login user (default is \"root\"): " v
if [ "$v" != "" ]; then
user="$v"
fi
sshPort="22"
read -p "? ssh port (default is 22): " v
if [ "$v" != "" ]; then
sshPort="$v"
fi
echo "--- building(${goos}_$goarch)..."
export GOOS=$goos
export GOARCH=$goarch
go build -o esmd $(dirname $0)/../main.go
if [ "$?" != "0" ]; then
exit
fi
echo "--- uploading..."
tar -czf esmd.tar.gz esmd
scp -P $sshPort esmd.tar.gz $user@$host:/tmp/esmd.tar.gz
if [ "$?" != "0" ]; then
rm -f esmd
rm -f esmd.tar.gz
exit
fi
echo "--- installing..."
ssh -p $sshPort ${user}@${host} << EOF
glv=\$(git lfs version)
if [ "\$?" != "0" ]; then
apt update
apt install -y git git-lfs
git lfs install
fi
echo \$glv
if [ "$init" == "yes" ]; then
servicefn=/etc/systemd/system/esmd.service
if [ -f \$servicefn ]; then
rm -f servicefn
fi
echo "[Unit]" >> \$servicefn
echo "Description=esm.sh service" >> \$servicefn
echo "After=network.target" >> \$servicefn
echo "StartLimitIntervalSec=0" >> \$servicefn
echo "[Service]" >> \$servicefn
echo "Type=simple" >> \$servicefn
if [ "$config" != "" ]; then
mkdir -p /etc/esmd
rm -f /etc/esmd/config.json
echo "$config" >> /etc/esmd/config.json
echo "ExecStart=/usr/local/bin/esmd --config=/etc/esmd/config.json" >> \$servicefn
else
echo "ExecStart=/usr/local/bin/esmd" >> \$servicefn
fi
echo "USER=\${USER}" >> \$servicefn
echo "Restart=always" >> \$servicefn
echo "RestartSec=5" >> \$servicefn
echo "Environment=\"USER=\${USER}\"" >> \$servicefn
echo "Environment=\"HOME=\${HOME}\"" >> \$servicefn
echo "[Install]" >> \$servicefn
echo "WantedBy=default.target" >> \$servicefn
else
systemctl stop esmd.service
echo "Stopped esmd.service."
fi
cd /tmp
tar -xzf esmd.tar.gz
if [ "\$?" != "0" ]; then
exit 1
fi
rm -f esmd.tar.gz
chmod +x esmd
mv -f esmd /usr/local/bin/esmd
if [ "$init" == "yes" ]; then
systemctl daemon-reload
systemctl enable esmd.service
fi
systemctl start esmd.service
echo "Started esmd.service."
EOF
rm -f esmd
rm -f esmd.tar.gz