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
4 changes: 2 additions & 2 deletions .snapcraft/resources/Caddyfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
http://:8080
proxy / localhost:3000 {
_site-url_
proxy / localhost:_port_ {
websocket
transparent
}
41 changes: 38 additions & 3 deletions .snapcraft/resources/initcaddy
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
#!/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="site-url port"

refresh_opt_in_config() {
geekgonecrazy marked this conversation as resolved.
Show resolved Hide resolved
# 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
}

create_caddyfile
update_caddyfile

12 changes: 9 additions & 3 deletions .snapcraft/resources/startRocketChat
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ function start_rocketchat {
export NODE_ENV=production
export BABEL_CACHE_DIR=/tmp
export ROOT_URL=http://localhost
export PORT=3000
export MONGO_URL=mongodb://localhost:27017/parties
export MONGO_OPLOG_URL=mongodb://localhost:27017/local
export PORT="$(snapctl get port)"
export MONGO_URL="$(snapctl get mongo-url)"
export MONGO_OPLOG_URL="$(snapctl get mongo-oplog-url)"
export Accounts_AvatarStorePath=$SNAP_COMMON/uploads

for filename in $SNAP_COMMON/*.env; do
while read env_var; do
export "$env_var"
done < $filename
done

node $SNAP/main.js
}

Expand Down
81 changes: 81 additions & 0 deletions .snapcraft/snap/hooks/configure
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/bash

# Obtain siteurl value
siteurl="$(snapctl get site-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})?$'
geekgonecrazy marked this conversation as resolved.
Show resolved Hide resolved
if ! [[ $siteurl =~ $siteurl_regex ]] ; then
echo "\"$siteurl\" is not a valid url" >&2
exit 1
#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 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})?\/parties$'
geekgonecrazy marked this conversation as resolved.
Show resolved Hide resolved
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 ]] && [[ $siteurl =~ ^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://"}
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
fi
fi
fi

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

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

# 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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ apps:
daemon: simple
plugs: [network, network-bind]
rocketchat-caddy:
command: env LC_ALL=C caddy -conf=$SNAP_DATA/Caddyfile -host=localhost:8080
command: env LC_ALL=C caddy -conf=$SNAP_DATA/Caddyfile
daemon: simple
plugs: [network, network-bind]
mongo:
Expand Down