From b6e39237050a8364ef8b318d05e1d6ba0bc009c9 Mon Sep 17 00:00:00 2001 From: "lucia.guevgeozian" Date: Tue, 16 Oct 2018 19:48:02 -0300 Subject: [PATCH 1/8] adding hooks and config for Caddyfile --- .snapcraft/resources/Caddyfile | 4 +- .snapcraft/resources/initcaddy | 43 +++++++++++- .snapcraft/resources/startRocketChat | 8 +-- .snapcraft/snap/hooks/configure | 57 ++++++++++++++++ .snapcraft/snap/hooks/install | 17 +++++ .snapcraft/snap/snapcraft.yaml | 97 ++++++++++++++++++++++++++++ 6 files changed, 217 insertions(+), 9 deletions(-) create mode 100755 .snapcraft/snap/hooks/configure create mode 100755 .snapcraft/snap/hooks/install create mode 100644 .snapcraft/snap/snapcraft.yaml diff --git a/.snapcraft/resources/Caddyfile b/.snapcraft/resources/Caddyfile index ce07ab1e2ef9..e151a40191eb 100644 --- a/.snapcraft/resources/Caddyfile +++ b/.snapcraft/resources/Caddyfile @@ -1,5 +1,5 @@ -http://:8080 -proxy / localhost:3000 { +_protocol_://_siteurl_ +proxy / localhost:_port_ { websocket transparent } diff --git a/.snapcraft/resources/initcaddy b/.snapcraft/resources/initcaddy index f52cb80de8a2..4078fc992366 100755 --- a/.snapcraft/resources/initcaddy +++ b/.snapcraft/resources/initcaddy @@ -1,3 +1,40 @@ -#!/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="protocol siteurl port" + +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 + +# add or replace an option inside the config file. Create the file if doesn't exist +refresh_opt_in_config() { + 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 +} + +# 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_caddy +update_caddyfile + diff --git a/.snapcraft/resources/startRocketChat b/.snapcraft/resources/startRocketChat index 50d257b4515c..9c2e4b4fb114 100755 --- a/.snapcraft/resources/startRocketChat +++ b/.snapcraft/resources/startRocketChat @@ -15,10 +15,10 @@ function start_rocketchat { export DEPLOY_METHOD=snap 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 ROOT_URL=http://"$(snapctl get siteurl)" + export PORT="$(snapctl get port)" + export MONGO_URL="$(snapctl get mongourl)" + export MONGO_OPLOG_URL="$(snapctl get mongooplogurl)" export Accounts_AvatarStorePath=$SNAP_COMMON/uploads node $SNAP/main.js diff --git a/.snapcraft/snap/hooks/configure b/.snapcraft/snap/hooks/configure new file mode 100755 index 000000000000..94ca808ac7c7 --- /dev/null +++ b/.snapcraft/snap/hooks/configure @@ -0,0 +1,57 @@ +#!/bin/bash + +# Obtain siteurl value +siteurl="$(snapctl get siteurl)" +# Validate it +#siteurl_regex='^https?:\/\/([\da-z\.-]+)?(\.[a-z\.]{2,6})?([\/\w \.-]*)*\/?$' +siteurl_regex='([\da-z\.-]+)?(\.[a-z\.]{2,6})?([\/\w \.-]*)*\/?$' +if ! [[ $siteurl =~ $siteurl_regex ]] ; then + echo "\"$siteurl\" is not a valid url" >&2 + exit 1 +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 mongourl)" +# Validate it +mongourl_regex='^mongodb:\/\/([\da-z\.-]+)?(\.[a-z\.]{2,6})?:(\d{2,5})?\/parties$' +mongourl_regex='mongodb://localhost:27017/parties' +if ! [[ $mongourl =~ $mongourl_regex ]] ; then + echo "\"$mongourl\" is not a valid url" >&2 + exit 1 +fi + + +# Obtain mongooplogurl value +mongooplogurl="$(snapctl get mongooplogurl)" +# Validate it +mongooplogurl_regex='^mongodb:\/\/([\da-z\.-]+)?(\.[a-z\.]{2,6})?:(\d{2,5})?\/local$' +mongooplogurl_regex='mongodb://localhost:27017/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 ]] ; then + snapctl set protocol=https + else snapctl set protocol=http + fi +fi + diff --git a/.snapcraft/snap/hooks/install b/.snapcraft/snap/hooks/install new file mode 100755 index 000000000000..13573ecdd94e --- /dev/null +++ b/.snapcraft/snap/hooks/install @@ -0,0 +1,17 @@ +#!/bin/bash + +# Initialize the SITE_URL (ROOT_URL) to a default +snapctl set siteurl=localhost + +# Initialize the PORT to a default +snapctl set port=3000 + +# Initialize the MONGO_URL to a default +snapctl set mongourl=mongodb://localhost:27017/parties + +# Initialize the MONGO_OPLOG_URL to a default +snapctl set mongooplogurl=mongodb://localhost:27017/local + +# Initialize the protocol to a default +snapctl set https=disable + diff --git a/.snapcraft/snap/snapcraft.yaml b/.snapcraft/snap/snapcraft.yaml new file mode 100644 index 000000000000..d4e590e71d58 --- /dev/null +++ b/.snapcraft/snap/snapcraft.yaml @@ -0,0 +1,97 @@ +# +# Easiest way to work with this file, from an updated Ubuntu 16.04 LTS image +# 1. create a non-root user with sudo priv and perform following steps as non-root +# 2. `sudo apt-get update` +# 3. `sudo apt-get install snapcraft python build-essential` +# 4. `snapcraft stage` +# 5. `snapcraft snap` + +name: rocketchat-server +version: #{RC_VERSION} +summary: Rocket.Chat server +description: Have your own Slack like online chat, built with Meteor. https://rocket.chat/ +confinement: strict +assumes: [snapd2.21] +apps: + rocketchat-server: + command: startRocketChat + daemon: simple + plugs: [network, network-bind] + rocketchat-mongo: + command: startmongo + daemon: simple + plugs: [network, network-bind] + rocketchat-caddy: + command: env LC_ALL=C caddy -conf=$SNAP_DATA/Caddyfile + daemon: simple + plugs: [network, network-bind] + mongo: + command: env LC_ALL=C mongo + plugs: [network] + restoredb: + command: env LC_ALL=C restoredb + plugs: [network] + backupdb: + command: env LC_ALL=C backupdb + plugs: [network] + initcaddy: + command: env LC_ALL=C initcaddy +parts: + node: + plugin: dump + prepare: ./resources/preparenode + build-packages: + # For fibers + - python + - build-essential + - nodejs + rocketchat-server: + build-packages: + - curl + plugin: dump + prepare: ./resources/prepareRocketChat + after: [node] + source: . + stage-packages: + - execstack + - fontconfig-config + stage: + - programs + - main.js + - .node_version.txt + - etc + - usr + mongodb: + build-packages: + - wget + source: ./ + prepare: ./resources/preparemongo + plugin: dump + stage-packages: + - libssl1.0.0 + prime: + - usr + - bin + - lib + scripts: + plugin: dump + source: resources/ + organize: + backupdb: bin/backupdb + restoredb: bin/restoredb + startmongo: bin/startmongo + startRocketChat: bin/startRocketChat + initreplset.js: bin/initreplset.js + Caddyfile: bin/Caddyfile + initcaddy: bin/initcaddy + prime: + - bin + caddy: + prepare: ./resources/preparecaddy + plugin: dump + source: ./ + prime: + - bin + organize: + caddy: bin/caddy + after: [mongodb] From 7e35a1fbfc13d734022ef34fded17828311c7e7c Mon Sep 17 00:00:00 2001 From: "lucia.guevgeozian" Date: Tue, 16 Oct 2018 19:57:26 -0300 Subject: [PATCH 2/8] fixing comments, deleting snapcraft.yaml from rootdir --- .snapcraft/resources/initcaddy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.snapcraft/resources/initcaddy b/.snapcraft/resources/initcaddy index 4078fc992366..559b009480de 100755 --- a/.snapcraft/resources/initcaddy +++ b/.snapcraft/resources/initcaddy @@ -14,7 +14,7 @@ update_caddyfile(){ # Config file path for Caddyfile Caddyfile=$SNAP_DATA/Caddyfile -# add or replace an option inside the config file. Create the file if doesn't exist +# replace an option inside the config file. refresh_opt_in_config() { opt=$1 value="$2" From ecb374d2fdf09dcf35d5b6df7def6d2e571942e1 Mon Sep 17 00:00:00 2001 From: "lucia.guevgeozian" Date: Thu, 18 Oct 2018 18:58:55 -0300 Subject: [PATCH 3/8] fixes for initcaddy --- .snapcraft/resources/initcaddy | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/.snapcraft/resources/initcaddy b/.snapcraft/resources/initcaddy index 559b009480de..96092e4e941a 100755 --- a/.snapcraft/resources/initcaddy +++ b/.snapcraft/resources/initcaddy @@ -3,19 +3,8 @@ # Config options for Caddyfile options="protocol siteurl port" -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 - -# replace an option inside the config file. refresh_opt_in_config() { +# replace an option inside the config file. opt=$1 value="$2" if $(grep -q "_${opt}_" $Caddyfile); then @@ -26,15 +15,23 @@ refresh_opt_in_config() { 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_caddy +create_caddyfile update_caddyfile From f1a9f6ef420e195815157d889240ac37967a3bc4 Mon Sep 17 00:00:00 2001 From: "lucia.guevgeozian" Date: Thu, 18 Oct 2018 19:02:13 -0300 Subject: [PATCH 4/8] remove snapcraft.yaml from root dir --- .snapcraft/snapcraft.yaml | 97 --------------------------------------- 1 file changed, 97 deletions(-) delete mode 100644 .snapcraft/snapcraft.yaml diff --git a/.snapcraft/snapcraft.yaml b/.snapcraft/snapcraft.yaml deleted file mode 100644 index 50354f7afa7f..000000000000 --- a/.snapcraft/snapcraft.yaml +++ /dev/null @@ -1,97 +0,0 @@ -# -# Easiest way to work with this file, from an updated Ubuntu 16.04 LTS image -# 1. create a non-root user with sudo priv and perform following steps as non-root -# 2. `sudo apt-get update` -# 3. `sudo apt-get install snapcraft python build-essential` -# 4. `snapcraft stage` -# 5. `snapcraft snap` - -name: rocketchat-server -version: #{RC_VERSION} -summary: Rocket.Chat server -description: Have your own Slack like online chat, built with Meteor. https://rocket.chat/ -confinement: strict -assumes: [snapd2.21] -apps: - rocketchat-server: - command: startRocketChat - daemon: simple - plugs: [network, network-bind] - rocketchat-mongo: - command: startmongo - daemon: simple - plugs: [network, network-bind] - rocketchat-caddy: - command: env LC_ALL=C caddy -conf=$SNAP_DATA/Caddyfile -host=localhost:8080 - daemon: simple - plugs: [network, network-bind] - mongo: - command: env LC_ALL=C mongo - plugs: [network] - restoredb: - command: env LC_ALL=C restoredb - plugs: [network] - backupdb: - command: env LC_ALL=C backupdb - plugs: [network] - initcaddy: - command: env LC_ALL=C initcaddy -parts: - node: - plugin: dump - prepare: ./resources/preparenode - build-packages: - # For fibers - - python - - build-essential - - nodejs - rocketchat-server: - build-packages: - - curl - plugin: dump - prepare: ./resources/prepareRocketChat - after: [node] - source: . - stage-packages: - - execstack - - fontconfig-config - stage: - - programs - - main.js - - .node_version.txt - - etc - - usr - mongodb: - build-packages: - - wget - source: ./ - prepare: ./resources/preparemongo - plugin: dump - stage-packages: - - libssl1.0.0 - prime: - - usr - - bin - - lib - scripts: - plugin: dump - source: resources/ - organize: - backupdb: bin/backupdb - restoredb: bin/restoredb - startmongo: bin/startmongo - startRocketChat: bin/startRocketChat - initreplset.js: bin/initreplset.js - Caddyfile: bin/Caddyfile - initcaddy: bin/initcaddy - prime: - - bin - caddy: - prepare: ./resources/preparecaddy - plugin: dump - source: ./ - prime: - - bin - organize: - caddy: bin/caddy - after: [mongodb] From 28efeaece5bfdd37f542baca14e2706abfcdfddb Mon Sep 17 00:00:00 2001 From: "lucia.guevgeozian" Date: Tue, 23 Oct 2018 14:37:24 -0300 Subject: [PATCH 5/8] Check dns resolution with https enable, fix regex, add env to change siteUrl at start --- .snapcraft/resources/initcaddy | 4 ++-- .snapcraft/resources/startRocketChat | 3 ++- .snapcraft/snap/hooks/configure | 26 ++++++++++++++++---------- .snapcraft/snap/hooks/install | 2 +- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/.snapcraft/resources/initcaddy b/.snapcraft/resources/initcaddy index 96092e4e941a..ec34a71b7681 100755 --- a/.snapcraft/resources/initcaddy +++ b/.snapcraft/resources/initcaddy @@ -1,14 +1,14 @@ #!/bin/bash # Config options for Caddyfile -options="protocol siteurl port" +options="siteurl 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 + 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" diff --git a/.snapcraft/resources/startRocketChat b/.snapcraft/resources/startRocketChat index 9c2e4b4fb114..8fe6335b1060 100755 --- a/.snapcraft/resources/startRocketChat +++ b/.snapcraft/resources/startRocketChat @@ -15,7 +15,8 @@ function start_rocketchat { export DEPLOY_METHOD=snap export NODE_ENV=production export BABEL_CACHE_DIR=/tmp - export ROOT_URL=http://"$(snapctl get siteurl)" + export ROOT_URL="$(snapctl get siteurl)" + export OVERWRITE_SETTING_Site_Url="$(snapctl get siteurl)" export PORT="$(snapctl get port)" export MONGO_URL="$(snapctl get mongourl)" export MONGO_OPLOG_URL="$(snapctl get mongooplogurl)" diff --git a/.snapcraft/snap/hooks/configure b/.snapcraft/snap/hooks/configure index 94ca808ac7c7..0347aa9b7784 100755 --- a/.snapcraft/snap/hooks/configure +++ b/.snapcraft/snap/hooks/configure @@ -3,8 +3,7 @@ # Obtain siteurl value siteurl="$(snapctl get siteurl)" # Validate it -#siteurl_regex='^https?:\/\/([\da-z\.-]+)?(\.[a-z\.]{2,6})?([\/\w \.-]*)*\/?$' -siteurl_regex='([\da-z\.-]+)?(\.[a-z\.]{2,6})?([\/\w \.-]*)*\/?$' +siteurl_regex='^https?:\/\/([0-9A-Za-z\.-]{1,})?(\.[a-z\.]{2,6})?([\/\da-z\.-]+)?$' if ! [[ $siteurl =~ $siteurl_regex ]] ; then echo "\"$siteurl\" is not a valid url" >&2 exit 1 @@ -22,8 +21,7 @@ fi # Obtain mongourl value mongourl="$(snapctl get mongourl)" # Validate it -mongourl_regex='^mongodb:\/\/([\da-z\.-]+)?(\.[a-z\.]{2,6})?:(\d{2,5})?\/parties$' -mongourl_regex='mongodb://localhost:27017/parties' +mongourl_regex='^mongodb:\/\/([0-9A-Za-z\.-]{1,})?(\.[a-z\.]{2,6})?:([0-9]{2,5})?\/parties$' if ! [[ $mongourl =~ $mongourl_regex ]] ; then echo "\"$mongourl\" is not a valid url" >&2 exit 1 @@ -33,9 +31,7 @@ fi # Obtain mongooplogurl value mongooplogurl="$(snapctl get mongooplogurl)" # Validate it -mongooplogurl_regex='^mongodb:\/\/([\da-z\.-]+)?(\.[a-z\.]{2,6})?:(\d{2,5})?\/local$' -mongooplogurl_regex='mongodb://localhost:27017/local' - +mongooplogurl_regex='^mongodb:\/\/([0-9A-Za-z\.-]+)?(\.[a-z\.]{2,6})?:([0-9]{2,5})?\/local$' if ! [[ $mongooplogurl =~ $mongooplogurl_regex ]] ; then echo "\"$mongooplogurl\" is not a valid url" >&2 exit 1 @@ -49,9 +45,19 @@ if ! [[ $https =~ $https_regex ]]; then echo "\"$https\" should be enable or disable" >&2 exit 1 else - if [[ $https == enable ]] ; then - snapctl set protocol=https - else snapctl set protocol=http + 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 diff --git a/.snapcraft/snap/hooks/install b/.snapcraft/snap/hooks/install index 13573ecdd94e..7a64571056b7 100755 --- a/.snapcraft/snap/hooks/install +++ b/.snapcraft/snap/hooks/install @@ -1,7 +1,7 @@ #!/bin/bash # Initialize the SITE_URL (ROOT_URL) to a default -snapctl set siteurl=localhost +snapctl set siteurl=http://localhost # Initialize the PORT to a default snapctl set port=3000 From 51ba0131fef17dac3097f556ac633f54512d608c Mon Sep 17 00:00:00 2001 From: "lucia.guevgeozian" Date: Thu, 25 Oct 2018 18:16:29 -0300 Subject: [PATCH 6/8] Adding file in to read env vars, change key names, try to add support for path in url in caddy --- .snapcraft/resources/Caddyfile | 4 ++-- .snapcraft/resources/initcaddy | 2 +- .snapcraft/resources/startRocketChat | 13 +++++++++---- .snapcraft/snap/hooks/configure | 29 ++++++++++++++++++++++------ .snapcraft/snap/hooks/install | 7 +++---- 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/.snapcraft/resources/Caddyfile b/.snapcraft/resources/Caddyfile index e151a40191eb..a3b3fb59a825 100644 --- a/.snapcraft/resources/Caddyfile +++ b/.snapcraft/resources/Caddyfile @@ -1,5 +1,5 @@ -_protocol_://_siteurl_ -proxy / localhost:_port_ { +_site_ +proxy /_path_ localhost:_port_ { websocket transparent } diff --git a/.snapcraft/resources/initcaddy b/.snapcraft/resources/initcaddy index ec34a71b7681..1a104fb1b3ae 100755 --- a/.snapcraft/resources/initcaddy +++ b/.snapcraft/resources/initcaddy @@ -1,7 +1,7 @@ #!/bin/bash # Config options for Caddyfile -options="siteurl port" +options="site path port" refresh_opt_in_config() { # replace an option inside the config file. diff --git a/.snapcraft/resources/startRocketChat b/.snapcraft/resources/startRocketChat index 8fe6335b1060..a03990ce6b79 100755 --- a/.snapcraft/resources/startRocketChat +++ b/.snapcraft/resources/startRocketChat @@ -15,13 +15,18 @@ function start_rocketchat { export DEPLOY_METHOD=snap export NODE_ENV=production export BABEL_CACHE_DIR=/tmp - export ROOT_URL="$(snapctl get siteurl)" - export OVERWRITE_SETTING_Site_Url="$(snapctl get siteurl)" + export ROOT_URL=http://localhost export PORT="$(snapctl get port)" - export MONGO_URL="$(snapctl get mongourl)" - export MONGO_OPLOG_URL="$(snapctl get mongooplogurl)" + 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 } diff --git a/.snapcraft/snap/hooks/configure b/.snapcraft/snap/hooks/configure index 0347aa9b7784..6f036f9e68b8 100755 --- a/.snapcraft/snap/hooks/configure +++ b/.snapcraft/snap/hooks/configure @@ -1,12 +1,29 @@ #!/bin/bash # Obtain siteurl value -siteurl="$(snapctl get siteurl)" +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})?([\/\da-z\.-]+)?$' 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 @@ -19,9 +36,9 @@ if ! [[ $port =~ $port_regex ]]; then fi # Obtain mongourl value -mongourl="$(snapctl get mongourl)" +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})?\/parties$' if ! [[ $mongourl =~ $mongourl_regex ]] ; then echo "\"$mongourl\" is not a valid url" >&2 exit 1 @@ -29,9 +46,9 @@ fi # Obtain mongooplogurl value -mongooplogurl="$(snapctl get mongooplogurl)" +mongooplogurl="$(snapctl get mongo-oplog-url)" # Validate it -mongooplogurl_regex='^mongodb:\/\/([0-9A-Za-z\.-]+)?(\.[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 diff --git a/.snapcraft/snap/hooks/install b/.snapcraft/snap/hooks/install index 7a64571056b7..b3c7fa5037b4 100755 --- a/.snapcraft/snap/hooks/install +++ b/.snapcraft/snap/hooks/install @@ -1,17 +1,16 @@ #!/bin/bash # Initialize the SITE_URL (ROOT_URL) to a default -snapctl set siteurl=http://localhost +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 mongourl=mongodb://localhost:27017/parties +snapctl set mongo-url=mongodb://localhost:27017/parties # Initialize the MONGO_OPLOG_URL to a default -snapctl set mongooplogurl=mongodb://localhost:27017/local +snapctl set mongo-oplog-url=mongodb://localhost:27017/local # Initialize the protocol to a default snapctl set https=disable - From 91ff2eec90e5feb290aa27278ca6fb4ea44c27d5 Mon Sep 17 00:00:00 2001 From: "lucia.guevgeozian" Date: Tue, 30 Oct 2018 16:08:25 -0300 Subject: [PATCH 7/8] removing option for url with path --- .snapcraft/resources/Caddyfile | 4 +-- .snapcraft/resources/initcaddy | 3 ++- .snapcraft/snap/hooks/configure | 43 +++++++++++++++++---------------- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/.snapcraft/resources/Caddyfile b/.snapcraft/resources/Caddyfile index a3b3fb59a825..c92feb937041 100644 --- a/.snapcraft/resources/Caddyfile +++ b/.snapcraft/resources/Caddyfile @@ -1,5 +1,5 @@ -_site_ -proxy /_path_ localhost:_port_ { +_site-url_ +proxy / localhost:_port_ { websocket transparent } diff --git a/.snapcraft/resources/initcaddy b/.snapcraft/resources/initcaddy index 1a104fb1b3ae..cdd5ec65a033 100755 --- a/.snapcraft/resources/initcaddy +++ b/.snapcraft/resources/initcaddy @@ -1,7 +1,8 @@ #!/bin/bash # Config options for Caddyfile -options="site path port" +#options="site path port" +options="site-url port" refresh_opt_in_config() { # replace an option inside the config file. diff --git a/.snapcraft/snap/hooks/configure b/.snapcraft/snap/hooks/configure index 6f036f9e68b8..48b40c14de54 100755 --- a/.snapcraft/snap/hooks/configure +++ b/.snapcraft/snap/hooks/configure @@ -3,27 +3,28 @@ # 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})?([\/\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 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 +#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 @@ -62,15 +63,15 @@ 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 ]] && [[ $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 + 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 + 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 From 3b5779c65762092d5b35023e23a6d8faae2adc20 Mon Sep 17 00:00:00 2001 From: "lucia.guevgeozian" Date: Thu, 6 Dec 2018 14:23:25 -0300 Subject: [PATCH 8/8] Changed regex in configure hook, switch to caddy-url and caddy enable, fix hooks --- .snapcraft/resources/Caddyfile | 2 +- .snapcraft/resources/initcaddy | 10 ++++- .snapcraft/resources/startRocketChat | 4 ++ .snapcraft/snap/hooks/configure | 67 ++++++++++++++++++++++------ .snapcraft/snap/hooks/install | 4 +- .snapcraft/snap/snapcraft.yaml | 12 +++++ 6 files changed, 82 insertions(+), 17 deletions(-) diff --git a/.snapcraft/resources/Caddyfile b/.snapcraft/resources/Caddyfile index c92feb937041..23299c1d2b7e 100644 --- a/.snapcraft/resources/Caddyfile +++ b/.snapcraft/resources/Caddyfile @@ -1,4 +1,4 @@ -_site-url_ +_caddy-url_ proxy / localhost:_port_ { websocket transparent diff --git a/.snapcraft/resources/initcaddy b/.snapcraft/resources/initcaddy index cdd5ec65a033..b55d20de02b3 100755 --- a/.snapcraft/resources/initcaddy +++ b/.snapcraft/resources/initcaddy @@ -2,7 +2,7 @@ # Config options for Caddyfile #options="site path port" -options="site-url port" +options="caddy-url port" refresh_opt_in_config() { # replace an option inside the config file. @@ -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= 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" + diff --git a/.snapcraft/resources/startRocketChat b/.snapcraft/resources/startRocketChat index a03990ce6b79..b39e22742a73 100755 --- a/.snapcraft/resources/startRocketChat +++ b/.snapcraft/resources/startRocketChat @@ -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 + fi for filename in $SNAP_COMMON/*.env; do while read env_var; do diff --git a/.snapcraft/snap/hooks/configure b/.snapcraft/snap/hooks/configure index 48b40c14de54..3f2a606e7f11 100755 --- a/.snapcraft/snap/hooks/configure +++ b/.snapcraft/snap/hooks/configure @@ -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://*/} @@ -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 @@ -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 @@ -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=, disabling https ..." + snapctl set https=disable + exit 1 fi fi diff --git a/.snapcraft/snap/hooks/install b/.snapcraft/snap/hooks/install index b3c7fa5037b4..9a78145595ec 100755 --- a/.snapcraft/snap/hooks/install +++ b/.snapcraft/snap/hooks/install @@ -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 diff --git a/.snapcraft/snap/snapcraft.yaml b/.snapcraft/snap/snapcraft.yaml index d4e590e71d58..ae99be479245 100644 --- a/.snapcraft/snap/snapcraft.yaml +++ b/.snapcraft/snap/snapcraft.yaml @@ -36,6 +36,9 @@ apps: plugs: [network] initcaddy: command: env LC_ALL=C initcaddy +hooks: + configure: + plugs: [network] parts: node: plugin: dump @@ -95,3 +98,12 @@ parts: organize: caddy: bin/caddy after: [mongodb] + hooks: + plugin: nil + stage-packages: + - dnsutils + - curl + prime: + - usr + - lib +