Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Config hooks for snap #12351

Merged
merged 9 commits into from
Dec 6, 2018
2 changes: 1 addition & 1 deletion .snapcraft/resources/Caddyfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
_site-url_
_caddy-url_
proxy / localhost:_port_ {
websocket
transparent
Expand Down
10 changes: 9 additions & 1 deletion .snapcraft/resources/initcaddy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Config options for Caddyfile
#options="site path port"
options="site-url port"
options="caddy-url port"

refresh_opt_in_config() {
geekgonecrazy marked this conversation as resolved.
Show resolved Hide resolved
# replace an option inside the config file.
Expand Down Expand Up @@ -33,6 +33,14 @@ for opt in $options
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"

4 changes: 4 additions & 0 deletions .snapcraft/resources/startRocketChat
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ function start_rocketchat {
export MONGO_URL="$(snapctl get mongo-url)"
export MONGO_OPLOG_URL="$(snapctl get mongo-oplog-url)"
export Accounts_AvatarStorePath=$SNAP_COMMON/uploads
siteurl="$(snapctl get siteurl)"
if [ -n "$siteurl" ]; then
export OVERWRITE_SETTING_Site_Url=$siteurl

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to see this expanded to allow configuration of any rocketchat setting from snap.
Something similar to that performed by the ansible rocketchat role here: RocketChat/Rocket.Chat.Ansible@81060e9
(See also RocketChat/Rocket.Chat.Ansible#67)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be right after that, can drop any file with env variables in $SNAP_COMMON directory and they will be exported

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nickbroon the problem we run into is snap settings are very picky. So we can't even use the exact same syntax. If we could we could easily let any setting be set. But we would either have to have a predefined list or a very reliable way of converting between one format and another.

the environment variable file in the root is the current way we're going to recommend doing this.

fi

for filename in $SNAP_COMMON/*.env; do
while read env_var; do
Expand Down
67 changes: 54 additions & 13 deletions .snapcraft/snap/hooks/configure
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#!/bin/bash

# Obtain siteurl value
siteurl="$(snapctl get site-url)"
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
#siteurl_regex='^https?:\/\/([0-9A-Za-z\.-]+){1,}(\.[a-z\.]{2,6})?([\/\da-z\.-]+)?$'
siteurl_regex='^https?:\/\/([0-9A-Za-z\.-]+){1,}(\.[a-z\.]{2,6})?$'
if ! [[ $siteurl =~ $siteurl_regex ]] ; then
echo "\"$siteurl\" is not a valid url" >&2
#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://*/}
Expand All @@ -27,6 +33,31 @@ if ! [[ $siteurl =~ $siteurl_regex ]] ; then
# 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
Expand All @@ -39,17 +70,16 @@ 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})?\/parties$'
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$'
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
Expand All @@ -63,19 +93,30 @@ if ! [[ $https =~ $https_regex ]]; then
echo "\"$https\" should be enable or disable" >&2
exit 1
else
if [[ $https == enable ]] && [[ $siteurl =~ ^http: ]]; then
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 ]] && [[ $siteurl =~ ^https: ]]; then
domain=${siteurl#"https://"}
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
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

4 changes: 2 additions & 2 deletions .snapcraft/snap/hooks/install
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# Initialize the SITE_URL (ROOT_URL) to a default
snapctl set site-url=http://localhost
# Initialize the CADDY_URL to a default
snapctl set caddy=disable

# Initialize the PORT to a default
snapctl set port=3000
Expand Down
12 changes: 12 additions & 0 deletions .snapcraft/snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ apps:
plugs: [network]
initcaddy:
command: env LC_ALL=C initcaddy
hooks:
configure:
plugs: [network]
parts:
node:
plugin: dump
Expand Down Expand Up @@ -95,3 +98,12 @@ parts:
organize:
caddy: bin/caddy
after: [mongodb]
hooks:
plugin: nil
stage-packages:
- dnsutils
- curl
prime:
- usr
- lib