forked from usefathom/fathom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·258 lines (212 loc) · 7.77 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/bash
#
# Fathom Installer script
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Defaults
SERVER_PORT="$(shuf -i9000-9100 -n1)" # Port for Fathom server
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "Please run the Fathom installer as root (to install the necessary NGINX & systemd files)"
exit
fi
echo "Welcome to the Fathom quick installer. Press CTRL-C at any time to abort."
function download_fathom() {
# Download latest version of the Fathom application
echo "Downloading latest Fathom"
wget -O fathom https://usesfathom.com/downloads/fathom-latest
# Move Fathom to $PATH so we can run the command from anywhere
echo "Moving Fathom to /usr/local/bin/fathom"
chmod +x fathom
mv fathom /usr/local/bin/fathom
FATHOM_PATH=$(command -v fathom)
echo "Fathom installed to $FATHOM_PATH"
echo ""
}
function new_site_dir() {
read -p "Where would you like to store your new Fathom instance? (default: new-fathom): " SITE_DIR
if [[ "$SITE_DIR" == "" ]]; then
SITE_DIR="new-fathom"
fi;
SITE_DIR_ABS="$PWD/$SITE_DIR"
if [ -d "$SITE_DIR" ]; then
read -p "Warning: $SITE_DIR_ABS already exists. Are you sure? (y/N): " CONTINUE
if [ "$CONTINUE" != "y" ]; then exit 0; fi
fi;
if [ ! -e "$SITE_DIR" ]; then
echo "Creating directory $SITE_DIR_ABS"
mkdir -p "$SITE_DIR"
chmod 755 "$SITE_DIR"
fi;
cd "$SITE_DIR"
echo ""
}
function setup_config() {
# Ask for configuration values
echo "Choose database engine:"
echo " 1) SQLite (default)"
if [ "$(command -v mysql)" ]; then
echo " 2) MySQL"
fi;
read DATABASE_CHOICE
if [ "$DATABASE_CHOICE" == "1" ] || [ "$DATABASE_CHOICE" == "" ]; then
DATABASE="sqlite3"
fi;
if [ "$DATABASE_CHOICE" == "2" ]; then
DATABASE="mysql"
fi;
# set filename if using sqlite3
if [ "$DATABASE" == "sqlite3" ]; then
DATABASE_NAME="$SITE_DIR_ABS/fathom.db"
fi;
# or ask for credentials if using postgres or mysql
if [ "$DATABASE" == "mysql" ] || [ "$DATABASE" == "postgres" ]; then
echo "Enter your $DATABASE credentials: "
read -p " Database user: " DATABASE_USER
read -p " Database password: " DATABASE_PASSWORD
read -p " Database name (default: fathom): " DATABASE_NAME
if [ "$DATABASE_NAME" == "" ]; then
DATABASE_NAME="fathom"
fi;
echo "Creating $DATABASE database $DATABASE_NAME"
mysql --user="$DATABASE_USER" --password="$DATABASE_PASSWORD" --execute="CREATE DATABASE $DATABASE_NAME;"
# TODO: Add Postgres support
fi;
# Create configuration file
TEMPLATE=$(cat <<-END
FATHOM_DEBUG=false
FATHOM_SERVER_ADDR=":$SERVER_PORT"
FATHOM_DATABASE_DRIVER="$DATABASE"
FATHOM_DATABASE_NAME="$DATABASE_NAME"
FATHOM_DATABASE_USER="$DATABASE_USER"
FATHOM_DATABASE_PASSWORD="$DATABASE_PASSWORD"
FATHOM_DATABASE_HOST=""
FATHOM_SECRET="abcdefghijklmnopqrstuvwxyz1234567890"
END
)
echo "$TEMPLATE" > ".env"
echo "Created configuration file: $SITE_DIR_ABS/.env"
echo "You can now run Fathom using \`fathom --config=$SITE_DIR_ABS/.env server\`"
echo ""
}
function new_fathom_user() {
echo "Create user account: "
read -p " Email address: " USER_EMAIL
read -p " Password: " USER_PASSWORD
echo "Running command: fathom --config=\"$SITE_DIR_ABS/.env\" register --email=\"$USER_EMAIL\" --password=\"$USER_PASSWORD\""
fathom --config="$SITE_DIR_ABS/.env" register --email="$USER_EMAIL" --password="$USER_PASSWORD"
echo ""
}
function new_nginx_server() {
echo "Set your domain name, eg fathom.yoursite.com. Make sure you have a DNS record pointing the domain to this server."
read -p "Domain name: " SERVER_NAME
if [ "$SERVER_NAME" == "" ]; then exit 0; fi
# Make sure we're not overwriting existing server blocks
if [ -e "/etc/nginx/sites-available/$SERVER_NAME" ]; then
read -p "Warning: /etc/nginx/sites-available/$SERVER_NAME already exists. Are you sure? (y/N): " CONTINUE
if [ "$CONTINUE" != "y" ]; then exit 0; fi
fi;
TEMPLATE=$(cat <<-END
server {
server_name $SERVER_NAME;
location / {
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$remote_addr;
proxy_set_header Host \$host;
proxy_pass http://127.0.0.1:$SERVER_PORT;
}
}
END
)
echo "Creating file /etc/nginx/sites-available/$SERVER_NAME"
echo "$TEMPLATE" > "/etc/nginx/sites-available/$SERVER_NAME"
ln -s "/etc/nginx/sites-available/$SERVER_NAME" "/etc/nginx/sites-enabled/$SERVER_NAME" || true
echo "Testing updated NGINX configuration"
nginx -t
echo "Reloading NGINX"
service nginx reload
echo ""
}
function new_systemctl_service() {
echo ""
# Use server name or ask for new service name
SERVICE_NAME="$SERVER_NAME"
if [ "$SERVICE_NAME" == "" ]; then
echo "Choose a name for your service. We recommend something descriptive like the domain to your Fathom instance."
read -p "Service name: " SERVICE_NAME
fi;
# Make sure we're not overwriting existing service files
if [ -e "/etc/systemd/system/$SERVICE_NAME.service" ]; then
read -p "Warning: /etc/systemd/system/$SERVICE_NAME.service already exists. Are you sure? (y/N): " CONTINUE
if [ "$CONTINUE" != "y" ]; then exit 0; fi
fi;
TEMPLATE=$(cat <<-END
[Unit]
Description=Fathom service for $SERVICE_NAME
Requires=network.target
After=network.target
[Service]
Type=simple
User=$USER
Restart=always
WorkingDirectory=$SITE_DIR_ABS
ExecStart=$FATHOM_PATH --config=$SITE_DIR_ABS/.env server
SyslogIdentifier=$SERVICE_NAME
[Install]
WantedBy=multi-user.target
END
)
echo "Creating file /etc/systemd/system/$SERVICE_NAME.service"
echo "$TEMPLATE" > "/etc/systemd/system/$SERVICE_NAME.service"
echo "Reloading systemd service files"
systemctl daemon-reload
echo "Enabling $SERVICE_NAME at system boot"
systemctl enable "$SERVICE_NAME"
echo "Starting service $SERVICE_NAME"
systemctl start "$SERVICE_NAME"
echo "Success! You can manually start the service using \`systemctl start $SERVICE_NAME\`"
echo ""
}
function new_cerbot_cert() {
certbot --nginx -d "$SERVER_NAME"
echo ""
}
# Download Fathom if command does not exist
FATHOM_PATH="$(command -v fathom)"
if [ "$FATHOM_PATH" != "" ]; then
echo "Fathom detected in $FATHOM_PATH"
echo "Installed: $(fathom --version)"
read -p "Download latest Fathom anyway? (Y/n): " CONTINUE
fi
if [ "$FATHOM_PATH" == "" ] || [ "$CONTINUE" != "n" ]; then
download_fathom
fi;
new_site_dir
setup_config
new_fathom_user
# Ask to setup new NGINX server block
if [ "$(command -v nginx)" ]; then
read -p "NGINX detected. Create a new server block? (Y/n): " CONTINUE
if [ "$CONTINUE" != "n" ]; then
new_nginx_server
fi;
fi;
# Ask to setup new Systemctl service file
if [ "$(command -v systemctl)" ]; then
read -p "Systemctl detected. Create a new service? (Y/n): " CONTINUE
if [ "$CONTINUE" != "n" ]; then
new_systemctl_service
fi
fi;
# Ask to request new LetsEncrypt certificate
if [ "$SERVER_NAME" != "" ] && [ "$(command -v certbot)" ]; then
read -p "LetsEncrypt detected. Request SSL certificate for $SERVER_NAME? (Y/n): " CONTINUE
if [ "$CONTINUE" != "n" ]; then
new_cerbot_cert
fi
fi;
# Try to be helpful
if [ "$SERVER_NAME" != "" ] && [ "$SERVICE_NAME" != "" ]; then
echo "Success! You should now see Fathom running on $SERVER_NAME"
exit 0
fi;