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 {
_protocol_://_siteurl_
proxy / localhost:_port_ {
websocket
transparent
}
43 changes: 40 additions & 3 deletions .snapcraft/resources/initcaddy
Original file line number Diff line number Diff line change
@@ -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

# replace an option inside the config file.
refresh_opt_in_config() {
geekgonecrazy marked this conversation as resolved.
Show resolved Hide resolved
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

8 changes: 4 additions & 4 deletions .snapcraft/resources/startRocketChat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions .snapcraft/snap/hooks/configure
Original file line number Diff line number Diff line change
@@ -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

17 changes: 17 additions & 0 deletions .snapcraft/snap/hooks/install
Original file line number Diff line number Diff line change
@@ -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

97 changes: 97 additions & 0 deletions .snapcraft/snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -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]