test/lib/matrix.sh
2022-12-13 16:56:20 +00:00

222 lines
6.7 KiB
Bash

#!/bin/bash
#
# Federated Computer Matrix / Element Service
PATH=$HOME/.docker/cli-plugins:/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
config_matrix() {
echo -ne "\n* Configuring /federated/apps/matrix container.."
spin &
SPINPID=$!
if [ ! -d "/federated/apps/matrix" ]; then
mkdir -p /federated/apps/matrix/data/matrix apps/matrix/data/element &> /dev/null
cp -rf /federated/apps/dns/data/etc/letsencrypt/archive/$DOMAIN/*.pem apps/matrix/data/matrix
chmod 644 /federated/apps/matrix/data/matrix/*.pem
fi
DOMAIN_ARRAY=(${DOMAIN//./ })
DOMAIN_FIRST=${DOMAIN_ARRAY[0]}
DOMAIN_LAST=${DOMAIN_ARRAY[1]}
cat > /federated/apps/matrix/docker-compose.yml <<EOF
version: '3.7'
services:
element:
image: vectorim/element-web:latest
container_name: element
hostname: element.$DOMAIN
domainname: $DOMAIN
restart: always
volumes:
- ./data/element/element-config.json:/app/config.json
networks:
federated:
ipv4_address: 172.99.0.31
environment:
- VIRTUAL_PROTO=http
- VIRTUAL_PORT=80
- VIRTUAL_HOST=element.$DOMAIN
synapse:
image: matrixdotorg/synapse:latest
container_name: matrix
hostname: matrix.$DOMAIN
domainname: $DOMAIN
restart: always
networks:
federated:
ipv4_address: 172.99.0.32
volumes:
- ./data/matrix:/data
environment:
- VIRTUAL_PROTO=http
- VIRTUAL_PORT=8008
- VIRTUAL_HOST=matrix.$DOMAIN
networks:
federated:
external: true
EOF
cat > /federated/apps/matrix/data/element/element-config.json <<EOF
{
"default_server_config": {
"m.homeserver": {
"base_url": "https://matrix.$DOMAIN",
"server_name": "matrix.$DOMAIN"
},
"m.identity_server": {
"base_url": "https://vector.im"
}
},
"jitsi": {
"preferredDomain": "jitsi.$DOMAIN"
},
"brand": "Element",
"integrations_ui_url": "https://scalar.vector.im/",
"integrations_rest_url": "https://scalar.vector.im/api",
"integrations_widgets_urls": [
"https://scalar.vector.im/_matrix/integrations/v1",
"https://scalar.vector.im/api",
"https://scalar-staging.vector.im/_matrix/integrations/v1",
"https://scalar-staging.vector.im/api",
"https://scalar-staging.riot.im/scalar/api"
],
"hosting_signup_link": "https://element.io/matrix-services?utm_source=element-web&utm_medium=web",
"bug_report_endpoint_url": "https://element.io/bugreports/submit",
"uisi_autorageshake_app": "element-auto-uisi",
"showLabsSettings": true,
"roomDirectory": {
"servers": [
"matrix.org",
"gitter.im",
"libera.chat"
]
},
"enable_presence_by_hs_url": {
"https://matrix.org": false,
"https://matrix-client.matrix.org": false
},
"terms_and_conditions_links": [
{
"url": "https://element.io/privacy",
"text": "Privacy Policy"
},
{
"url": "https://element.io/cookie-policy",
"text": "Cookie Policy"
}
],
"hostSignup": {
"brand": "Element Home",
"cookiePolicyUrl": "https://element.io/cookie-policy",
"domains": [
"matrix.org"
],
"privacyPolicyUrl": "https://element.io/privacy",
"termsOfServiceUrl": "https://element.io/terms-of-service",
"url": "https://ems.element.io/element-home/in-app-loader"
},
"sentry": {
"dsn": "https://029a0eb289f942508ae0fb17935bd8c5@sentry.matrix.org/6",
"environment": "develop"
},
"posthog": {
"projectApiKey": "phc_Jzsm6DTm6V2705zeU5dcNvQDlonOR68XvX2sh1sEOHO",
"apiHost": "https://posthog.element.io"
},
"privacy_policy_url": "https://element.io/cookie-policy",
"features": {
"feature_spotlight": true,
"feature_video_rooms": true
},
"element_call": {
"url": "https://element-call.netlify.app"
},
"map_style_url": "https://api.maptiler.com/maps/streets/style.json?key=fU3vlMsMn4Jb6dnEIFsx"
}
EOF
# Generate the matrix homeserver.yaml file
docker run -it --rm -v "`pwd`//federated/apps/matrix/data/matrix:/data" -e SYNAPSE_SERVER_NAME=matrix.$DOMAIN -e SYNAPSE_REPORT_STATS=yes matrixdotorg/synapse:latest generate &> /dev/null
[ $? -ne 0 ] && fail "Couldn't run docker matrixdotorg/synapse:latest generate"
# Take out default Sqlite database config
sed -i 's!database: /data/homeserver.db!!g' /federated/apps/matrix/data/matrix/homeserver.yaml
sed -i 's!database:!!g' /federated/apps/matrix/data/matrix/homeserver.yaml
sed -i 's!name: sqlite3!!g' /federated/apps/matrix/data/matrix/homeserver.yaml
sed -i 's!args:!!g' /federated/apps/matrix/data/matrix/homeserver.yaml
# Insert our Postgres and LDAP config
cat >> /federated/apps/matrix/data/matrix/homeserver.yaml <<EOF
database:
name: psycopg2
args:
user: matrix
password: d3r3k123
host: postgresql.$DOMAIN
database: matrix
cp_min: 5
cp_max: 10
modules:
- module: "ldap_auth_provider.LdapAuthProviderModule"
config:
enabled: true
uri: "ldaps://ldap.$DOMAIN:636"
start_tls: true
base: "dc=$DOMAIN_FIRST,dc=$DOMAIN_LAST"
attributes:
uid: "cn"
mail: "mail"
name: "givenName"
bind_dn: cn=admin,dc=$DOMAIN_FIRST,dc=$DOMAIN_LAST
bind_password: $ADMINPASS
tls_options:
validate: true
local_certificate_file: /data/fullchain1.pem
local_private_key_file: /data/privkey1.pem
EOF
kill -9 $SPINPID &> /dev/null
echo -ne "done."
}
start_matrix() {
# Start /federated/apps/matrix with output to /dev/null
echo -ne "\n* Starting /federated/apps/matrix service.."
spin &
SPINPID=$!
if [ $DEBUG ]; then
# Start /federated/apps/matrix with output to console for debug
docker-compose -f /federated/apps/matrix/docker-compose.yml -p matrix up
[ $? -eq 0 ] && echo -ne "done.\n" || fail "There was a problem starting service /federated/apps/matrix"
else
docker-compose -f /federated/apps/matrix/docker-compose.yml -p matrix up -d &> /dev/null
# Keep trying matrix port 8008 to make sure it's up
# before we proceed
RETRY="30"
while [ $RETRY -gt 0 ]; do
nc -z 172.99.0.32 8008 &> /dev/null
if [ $? -eq 0 ]; then
break
else
if [ "$RETRY" == 1 ]; then
docker-compose -f /federated/apps/matrix/docker-compose.yml -p matrix down &> /dev/null
kill -9 $SPINPID &> /dev/null
fail "There was a problem starting service /federated/apps/matrix\nCheck the output of 'docker logs matrix' or turn on\ndebug with -d"
fi
((RETRY--))
sleep 7
fi
done
fi
kill -9 $SPINPID &> /dev/null
echo -ne "done."
}