-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NEW] Config hooks for snap (#12351)
* adding hooks and config for Caddyfile * fixing comments, deleting snapcraft.yaml from rootdir * fixes for initcaddy * remove snapcraft.yaml from root dir * Check dns resolution with https enable, fix regex, add env to change siteUrl at start * Adding file in to read env vars, change key names, try to add support for path in url in caddy * removing option for url with path * Changed regex in configure hook, switch to caddy-url and caddy enable, fix hooks
- Loading branch information
1 parent
4361c63
commit b154482
Showing
6 changed files
with
212 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
http://:8080 | ||
proxy / localhost:3000 { | ||
_caddy-url_ | ||
proxy / localhost:_port_ { | ||
websocket | ||
transparent | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,46 @@ | ||
#!/bin/sh | ||
cp $SNAP/bin/Caddyfile $SNAP_DATA/Caddyfile | ||
echo "Replace $SNAP_DATA/Caddyfile with your own to customize reverse proxy" | ||
#!/bin/bash | ||
|
||
# Config options for Caddyfile | ||
#options="site path port" | ||
options="caddy-url port" | ||
|
||
refresh_opt_in_config() { | ||
# replace an option inside the config file. | ||
opt=$1 | ||
value="$2" | ||
if $(grep -q "_${opt}_" $Caddyfile); then | ||
sed "s,_${opt}_,$value," $Caddyfile 2>/dev/null > ${Caddyfile}.new | ||
mv -f ${Caddyfile}.new $Caddyfile 2>/dev/null | ||
else | ||
echo "Fail to update $opt in Caddyfile" | ||
fi | ||
} | ||
|
||
create_caddyfile(){ | ||
# Copy template to config Caddyfile | ||
cp $SNAP/bin/Caddyfile $SNAP_DATA/Caddyfile | ||
} | ||
|
||
update_caddyfile(){ | ||
# Config file path for Caddyfile | ||
Caddyfile=$SNAP_DATA/Caddyfile | ||
|
||
# Iterate through the config options array | ||
for opt in $options | ||
do | ||
# Use snapctl to get the value registered by the snap set command | ||
refresh_opt_in_config $opt $(snapctl get $opt) | ||
done | ||
} | ||
|
||
caddy="$(snapctl get caddy)" | ||
if [[ $caddy == "disable" ]]; then | ||
echo "Caddy is not enabled, please set caddy-url=<your_url> and caddy=enable" | ||
exit 1 | ||
fi | ||
|
||
create_caddyfile | ||
update_caddyfile | ||
|
||
echo "Your URL was successfully configured - Please restart rocketchat and caddy services to apply configuration changes" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#!/bin/bash | ||
|
||
export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH" | ||
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu" | ||
export LD_LIBRARY_PATH=$SNAP_LIBRARY_PATH:$LD_LIBRARY_PATH | ||
|
||
# Obtain caddyurl value | ||
caddyurl="$(snapctl get caddy-url)" | ||
# Validate it | ||
#caddyurl_regex='^https?:\/\/([0-9A-Za-z\.-]+){1,}(\.[a-z\.]{2,6})?([\/\da-z\.-]+)?$' #(supporting path) | ||
caddyurl_regex='^https?:\/\/([0-9A-Za-z\.-]+){1,}(\.[a-z\.]{2,6})?$' | ||
if [ -n "$caddyurl" ]; then | ||
if ! [[ $caddyurl =~ $caddyurl_regex ]]; then | ||
echo "\"$caddyurl\" is not a valid url" >&2 | ||
exit 1 | ||
fi | ||
#else | ||
# if [[ $siteurl =~ ^http: ]] && [[ $siteurl =~ ^http:\/\/([0-9A-Za-z\.-]+){1,}?(\.[a-z\.]{2,6})?\/([\/\da-z\.-]+){1,}$ ]]; then | ||
# path=${siteurl#http://*/} | ||
# site=${siteurl#"http://"} | ||
# site=${site%%/*} | ||
# site=http://$site | ||
# elif [[ $siteurl =~ ^https: ]] && [[ $siteurl =~ ^https:\/\/([0-9A-Za-z\.-]+){1,}?(\.[a-z\.]{2,6})?\/([\/\da-z\.-]+){1,}$ ]]; then | ||
# path=${siteurl#https://*/} | ||
# site=${siteurl#"https://"} | ||
# site=${site%%/*} | ||
# site=https://$site | ||
# else | ||
# path="" | ||
# site=$siteurl | ||
# fi | ||
# snapctl set path=$path | ||
# snapctl set site=$site | ||
fi | ||
|
||
# Obtain caddy value | ||
caddy="$(snapctl get caddy)" | ||
# Validate it | ||
caddy_regex='((enable)|(disable))' | ||
if ! [[ $caddy =~ $caddy_regex ]]; then | ||
echo "\"$caddy_regex\" is not a valid, set to enable or disable" >&2 | ||
exit 1 | ||
else | ||
if [[ $caddy == enable ]]; then | ||
caddyurl="$(snapctl get caddy-url)" | ||
if [ -z "$caddyurl" ]; then | ||
echo "You tried to enable caddy but caddy-url is not set yet, please set up caddy-url first and then enable caddy again" >&2 | ||
snapctl set caddy=disable | ||
exit 1 | ||
else | ||
snapctl set siteurl=$caddyurl | ||
fi | ||
else | ||
siteurl="$(snapctl get siteurl)" | ||
if [ -n "$siteurl" ]; then | ||
snapctl set siteurl= | ||
fi | ||
fi | ||
fi | ||
|
||
# Obtain port value | ||
port="$(snapctl get port)" | ||
# Validate it | ||
port_regex='^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$' | ||
if ! [[ $port =~ $port_regex ]]; then | ||
echo "\"$port\" is not a valid port" >&2 | ||
exit 1 | ||
fi | ||
|
||
# Obtain mongourl value | ||
mongourl="$(snapctl get mongo-url)" | ||
# Validate it | ||
mongourl_regex='^mongodb:\/\/([0-9A-Za-z\.\-\_]+){1,}(([a-z\.\-\_]+){2,6})?(:[0-9]{2,5})?\/([0-9A-Za-z\_\-]+)$' | ||
if ! [[ $mongourl =~ $mongourl_regex ]] ; then | ||
echo "\"$mongourl\" is not a valid url" >&2 | ||
exit 1 | ||
fi | ||
|
||
# Obtain mongooplogurl value | ||
mongooplogurl="$(snapctl get mongo-oplog-url)" | ||
# Validate it | ||
mongooplogurl_regex='^mongodb:\/\/([0-9A-Za-z\.\-\_]+){1,}(([a-z\.\-\_]+){2,6})?(:[0-9]{2,5})?\/local$' | ||
if ! [[ $mongooplogurl =~ $mongooplogurl_regex ]] ; then | ||
echo "\"$mongooplogurl\" is not a valid url" >&2 | ||
exit 1 | ||
fi | ||
|
||
# Obtain site protocol | ||
https="$(snapctl get https)" | ||
# Validate it | ||
https_regex='((enable)|(disable))' | ||
if ! [[ $https =~ $https_regex ]]; then | ||
echo "\"$https\" should be enable or disable" >&2 | ||
exit 1 | ||
else | ||
if [[ $https == enable ]] && [[ $caddyurl =~ ^http: ]]; then | ||
echo "Error: You enabled https but your site URL starts with http, disabling https ..." | ||
snapctl set https=disable | ||
exit 1 | ||
elif [[ $https == enable ]] && [[ $caddyurl =~ ^https: ]]; then | ||
domain=${caddyurl#"https://"} | ||
domain=${domain%%/*} | ||
pubip=$(dig $domain |grep -A1 ";; ANSWER SECTION:" |tail -1 | awk '{print $5}') | ||
if [ -z "$pubip" ]; then | ||
echo "Error: Can't resove DNS query for $domain, check your DNS configuration, disabling https ..." | ||
snapctl set https=disable | ||
exit 1 | ||
else | ||
ip=$(curl ipinfo.io/ip 2>/dev/null) | ||
if [[ $ip != $pubip ]]; then | ||
echo "Error: Your public IP doesn't match the one resolved for caddy-url, disabling https ..." | ||
snapctl set https=disable | ||
exit 1 | ||
fi | ||
fi | ||
elif [[ $https == enable ]] && [ -z "$caddyurl" ]; then | ||
echo "Error: You enabled https but your site URL is empty, please set caddy-url=<your_site_url>, disabling https ..." | ||
snapctl set https=disable | ||
exit 1 | ||
fi | ||
fi | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/bin/bash | ||
|
||
# Initialize the CADDY_URL to a default | ||
snapctl set caddy=disable | ||
|
||
# Initialize the PORT to a default | ||
snapctl set port=3000 | ||
|
||
# Initialize the MONGO_URL to a default | ||
snapctl set mongo-url=mongodb://localhost:27017/parties | ||
|
||
# Initialize the MONGO_OPLOG_URL to a default | ||
snapctl set mongo-oplog-url=mongodb://localhost:27017/local | ||
|
||
# Initialize the protocol to a default | ||
snapctl set https=disable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters