-
Notifications
You must be signed in to change notification settings - Fork 171
/
install.sh
206 lines (160 loc) · 4.79 KB
/
install.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/bash
set -e
DOMAIN_NAME=$1
APP_URL="https://github.com/postalsys/emailengine/releases/latest/download/emailengine.tar.gz"
show_info () {
echo "Usage: $0 <domain-name>"
echo "Where"
echo " <domain-name> is the domain name for EmailEngine, eg. \"example.com\""
}
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
show_info
exit 1
fi
if [ "$DOMAIN_NAME" = "help" ]; then
show_info
exit
fi
if [ "$DOMAIN_NAME" = "-h" ]; then
show_info
exit
fi
echo
echo "NB! This install script works on public-facing servers, do not run it on local instances."
echo "The installer tries to provision an HTTPS certificate, and the process will fail if the server
is inaccessible from the public web.
"
read -n 1 -s -r -p "Press any key to continue..."
echo "
"
if ! [ -x `command -v curl` ]; then
apt-get update
apt-get install curl -q -y
echo ""
fi
if [[ -z $DOMAIN_NAME ]]; then
echo "Enter the domain name for your new EmailEngine installation."
echo "(ex. example.com or test.example.com)"
echo "Leave empty to autogenerate a domain name."
while [ -z "$DOMAIN_NAME" ]
do
#echo -en "\n"
read -p "Domain/Subdomain name: " DOMAIN_NAME
if [ -z "$DOMAIN_NAME" ]
then
DOMAIN_NAME=$(curl --silent --fail -XPOST "https://api.nodemailer.com/autoassign" -H "Content-Type: application/json" -d '{
"prefix": "engine",
"version": "1",
"requestor": "install"
}')
fi
done
fi
echo "Using the domain name \"${DOMAIN_NAME}\" for this installation."
# Prepare Caddy
apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
# Install Redis and Caddy
apt-get update
apt-get install redis-server caddy wget -q -y
# Just in case the installation does not start Caddy already
systemctl enable caddy
systemctl start caddy
# Download and extract EmailEngine executable
TMPDIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'ee')
cd $TMPDIR
if ! [ -x `command -v wget` ]; then
if ! [ -x `command -v curl` ]; then
echo "Can not download application"
exit 1
else
curl "$APP_URL" -L -o emailengine.tar.gz
fi
else
# use wget do download EmailEngine
wget "$APP_URL"
fi
tar xzf emailengine.tar.gz
rm -rf emailengine.tar.gz
mv emailengine /opt
chmod +x /opt/emailengine
rm -rf $TMPDIR
# Create unit file for EmailEngine
echo "[Unit]
Description=EmailEngine
After=redis-server
[Service]
# Configure environment variables
Environment=\"EENGINE_REDIS=redis://127.0.0.1:6379/8\"
Environment=\"EENGINE_PORT=3000\"
Environment=\"EENGINE_API_PROXY=true\"
# Triggers install script specific upgrade instructions
Environment=\"EENGINE_INSTALL_SCRIPT=true\"
# Folder where EmailEngine executable is located
WorkingDirectory=/opt
# EmailEngine does not require any special privileges
User=www-data
Group=www-data
# Run the EmailEngine executable
ExecStart=/opt/emailengine
Type=simple
Restart=always
SyslogIdentifier=emailengine
[Install]
WantedBy=multi-user.target" > /etc/systemd/system/emailengine.service
systemctl daemon-reload
systemctl enable emailengine
systemctl restart emailengine
# Create Caddyfile
echo "
:80 {
redir https://${DOMAIN_NAME}{uri}
}
${DOMAIN_NAME} {
reverse_proxy localhost:3000
}" > /etc/caddy/Caddyfile
# Create upgrade script
cat > '/opt/upgrade-emailengine.sh' <<'EOL'
#!/bin/bash
set -e
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
OLD_VERSION=`/opt/emailengine -v`
TMPDIR=$(mktemp -d -t ci-XXXXXXXXXX)
cd "$TMPDIR"
wget https://github.com/postalsys/emailengine/releases/latest/download/emailengine.tar.gz
tar xzf emailengine.tar.gz
rm -rf emailengine.tar.gz
NEW_VERSION=`./emailengine -v`
if [ "$OLD_VERSION" = "$NEW_VERSION" ]; then
rm -rf "$TMPDIR"
echo "Nothing to upgrade, already running $NEW_VERSION"
else
mv emailengine /opt
rm -rf "$TMPDIR"
chmod +x /opt/emailengine
systemctl restart emailengine
echo "Upgraded EmailEngine"
echo " - was: $OLD_VERSION"
echo " - now: $NEW_VERSION"
fi
EOL
chmod +x /opt/upgrade-emailengine.sh
systemctl reload caddy
printf "Waiting for the web server to start up.."
until $(curl --output /dev/null --silent --fail https://${DOMAIN_NAME}/); do
printf '.'
sleep 2
done
echo "."
echo ""
echo "Installation complete."
echo "Access your new EmailEngine installation in a browser at https://${DOMAIN_NAME}/"
echo ""
echo "To upgrade EmailEngine in the future, run the following command:"
echo " /opt/upgrade-emailengine.sh"
echo ""