|  | @@ -0,0 +1,394 @@
 | 
	
		
			
			|  | 1 | +#!/bin/bash
 | 
	
		
			
			|  | 2 | +#
 | 
	
		
			
			|  | 3 | +# .---.                  .              .
 | 
	
		
			
			|  | 4 | +# |                      |              |
 | 
	
		
			
			|  | 5 | +# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
 | 
	
		
			
			|  | 6 | +# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
 | 
	
		
			
			|  | 7 | +# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
 | 
	
		
			
			|  | 8 | +#
 | 
	
		
			
			|  | 9 | +#                    Freedom in the Cloud
 | 
	
		
			
			|  | 10 | +#
 | 
	
		
			
			|  | 11 | +# matrix server
 | 
	
		
			
			|  | 12 | +#
 | 
	
		
			
			|  | 13 | +# https://raw.githubusercontent.com/silvio/docker-matrix
 | 
	
		
			
			|  | 14 | +#
 | 
	
		
			
			|  | 15 | +# License
 | 
	
		
			
			|  | 16 | +# =======
 | 
	
		
			
			|  | 17 | +#
 | 
	
		
			
			|  | 18 | +# Copyright (C) 2016 Bob Mottram <bob@freedombone.net>
 | 
	
		
			
			|  | 19 | +#
 | 
	
		
			
			|  | 20 | +# This program is free software: you can redistribute it and/or modify
 | 
	
		
			
			|  | 21 | +# it under the terms of the GNU Affero General Public License as published by
 | 
	
		
			
			|  | 22 | +# the Free Software Foundation, either version 3 of the License, or
 | 
	
		
			
			|  | 23 | +# (at your option) any later version.
 | 
	
		
			
			|  | 24 | +#
 | 
	
		
			
			|  | 25 | +# This program is distributed in the hope that it will be useful,
 | 
	
		
			
			|  | 26 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
	
		
			
			|  | 27 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
	
		
			
			|  | 28 | +# GNU Affero General Public License for more details.
 | 
	
		
			
			|  | 29 | +#
 | 
	
		
			
			|  | 30 | +# You should have received a copy of the GNU Affero General Public License
 | 
	
		
			
			|  | 31 | +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
	
		
			
			|  | 32 | +
 | 
	
		
			
			|  | 33 | +VARIANTS=''
 | 
	
		
			
			|  | 34 | +
 | 
	
		
			
			|  | 35 | +IN_DEFAULT_INSTALL=0
 | 
	
		
			
			|  | 36 | +SHOW_ON_ABOUT=1
 | 
	
		
			
			|  | 37 | +
 | 
	
		
			
			|  | 38 | +MATRIX_DATA_DIR='/var/lib/matrix'
 | 
	
		
			
			|  | 39 | +MATRIX_TURN_PORT=3478
 | 
	
		
			
			|  | 40 | +MATRIX_PORT=8448
 | 
	
		
			
			|  | 41 | +MATRIX_REPO="https://github.com/matrix-org/synapse"
 | 
	
		
			
			|  | 42 | +MATRIX_COMMIT='f5a4001bb116c468cc5e8e0ae04a1c570e2cb171'
 | 
	
		
			
			|  | 43 | +
 | 
	
		
			
			|  | 44 | +matrix_variables=(ONION_ONLY
 | 
	
		
			
			|  | 45 | +                  MY_USERNAME
 | 
	
		
			
			|  | 46 | +                  MATRIX_PASSWORD
 | 
	
		
			
			|  | 47 | +                  DEFAULT_DOMAIN_NAME)
 | 
	
		
			
			|  | 48 | +
 | 
	
		
			
			|  | 49 | +function matrix_generate_turn_key {
 | 
	
		
			
			|  | 50 | +    local turnkey="${1}"
 | 
	
		
			
			|  | 51 | +    local filepath="${2}"
 | 
	
		
			
			|  | 52 | +
 | 
	
		
			
			|  | 53 | +    echo "lt-cred-mech" > "${filepath}"
 | 
	
		
			
			|  | 54 | +    echo "use-auth-secret" >> "${filepath}"
 | 
	
		
			
			|  | 55 | +    echo "static-auth-secret=${turnkey}" >> "${filepath}"
 | 
	
		
			
			|  | 56 | +    echo "realm=turn.${DEFAULT_DOMAIN_NAME}" >> "${filepath}"
 | 
	
		
			
			|  | 57 | +    echo "cert=${MATRIX_DATA_DIR}/${DEFAULT_DOMAIN_NAME}.tls.crt" >> "${filepath}"
 | 
	
		
			
			|  | 58 | +    echo "pkey=${MATRIX_DATA_DIR}/${DEFAULT_DOMAIN_NAME}.tls.key" >> "${filepath}"
 | 
	
		
			
			|  | 59 | +}
 | 
	
		
			
			|  | 60 | +
 | 
	
		
			
			|  | 61 | +function matrix_generate_synapse_file {
 | 
	
		
			
			|  | 62 | +    local filepath="${1}"
 | 
	
		
			
			|  | 63 | +
 | 
	
		
			
			|  | 64 | +    cd /etc/matrix
 | 
	
		
			
			|  | 65 | +    python -m synapse.app.homeserver \
 | 
	
		
			
			|  | 66 | +           --config-path "${filepath}" \
 | 
	
		
			
			|  | 67 | +           --generate-config \
 | 
	
		
			
			|  | 68 | +           --report-stats ${REPORT_STATS} \
 | 
	
		
			
			|  | 69 | +           --server-name ${DEFAULT_DOMAIN_NAME}
 | 
	
		
			
			|  | 70 | +}
 | 
	
		
			
			|  | 71 | +
 | 
	
		
			
			|  | 72 | +function matrix_configure_homeserver_yaml {
 | 
	
		
			
			|  | 73 | +    local turnkey="${1}"
 | 
	
		
			
			|  | 74 | +    local filepath="${2}"
 | 
	
		
			
			|  | 75 | +
 | 
	
		
			
			|  | 76 | +    local ymltemp="$(mktemp)"
 | 
	
		
			
			|  | 77 | +
 | 
	
		
			
			|  | 78 | +    awk -v TURNURIES="turn_uris: [\"turn:${DEFAULT_DOMAIN_NAME}:${MATRIX_TURN_PORT}?transport=udp\", \"turn:${DEFAULT_DOMAIN_NAME}:${MATRIX_TURN_PORT}?transport=tcp\"]" \
 | 
	
		
			
			|  | 79 | +        -v TURNSHAREDSECRET="turn_shared_secret: \"${turnkey}\"" \
 | 
	
		
			
			|  | 80 | +        -v PIDFILE="pid_file: ${MATRIX_DATA_DIR}/homeserver.pid" \
 | 
	
		
			
			|  | 81 | +        -v DATABASE="database: \"${MATRIX_DATA_DIR}/homeserver.db\"" \
 | 
	
		
			
			|  | 82 | +        -v LOGFILE="log_file: \"${MATRIX_DATA_DIR}/homeserver.log\"" \
 | 
	
		
			
			|  | 83 | +        -v MEDIASTORE="media_store_path: \"${MATRIX_DATA_DIR}/media_store\"" \
 | 
	
		
			
			|  | 84 | +        '{
 | 
	
		
			
			|  | 85 | +        sub(/turn_shared_secret: "YOUR_SHARED_SECRET"/, TURNSHAREDSECRET);
 | 
	
		
			
			|  | 86 | +        sub(/turn_uris: \[\]/, TURNURIES);
 | 
	
		
			
			|  | 87 | +        sub(/pid_file: \/homeserver.pid/, PIDFILE);
 | 
	
		
			
			|  | 88 | +        sub(${MATRIX_DATA_DIR}base: "\/homeserver.db"/, DATABASE);
 | 
	
		
			
			|  | 89 | +        sub(/log_file: "\/homeserver.log"/, LOGFILE);
 | 
	
		
			
			|  | 90 | +        sub(/media_store_path: "\/media_store"/, MEDIASTORE);
 | 
	
		
			
			|  | 91 | +        print;
 | 
	
		
			
			|  | 92 | +        }' "${filepath}" > "${ymltemp}"
 | 
	
		
			
			|  | 93 | +
 | 
	
		
			
			|  | 94 | +    mv ${ymltemp} "${filepath}"
 | 
	
		
			
			|  | 95 | +}
 | 
	
		
			
			|  | 96 | +
 | 
	
		
			
			|  | 97 | +function matrix_start {
 | 
	
		
			
			|  | 98 | +    if [ -f ${MATRIX_DATA_DIR}/turnserver.conf ]; then
 | 
	
		
			
			|  | 99 | +        echo "-=> start turn"
 | 
	
		
			
			|  | 100 | +        /usr/bin/turnserver --daemon -c ${MATRIX_DATA_DIR}/turnserver.conf
 | 
	
		
			
			|  | 101 | +    fi
 | 
	
		
			
			|  | 102 | +
 | 
	
		
			
			|  | 103 | +    echo "-=> start riot.im client"
 | 
	
		
			
			|  | 104 | +    (
 | 
	
		
			
			|  | 105 | +        if [ -f ${MATRIX_DATA_DIR}/vector.im.conf ] || [ -f ${MATRIX_DATA_DIR}/riot.im.conf ] ; then
 | 
	
		
			
			|  | 106 | +            echo "The riot web client is now handled via silvio/matrix-riot-docker"
 | 
	
		
			
			|  | 107 | +        fi
 | 
	
		
			
			|  | 108 | +    )
 | 
	
		
			
			|  | 109 | +
 | 
	
		
			
			|  | 110 | +    echo "-=> start matrix"
 | 
	
		
			
			|  | 111 | +    python -m synapse.app.homeserver --config-path ${MATRIX_DATA_DIR}/homeserver.yaml
 | 
	
		
			
			|  | 112 | +}
 | 
	
		
			
			|  | 113 | +
 | 
	
		
			
			|  | 114 | +function matrix_stop {
 | 
	
		
			
			|  | 115 | +    echo "-=> stop matrix"
 | 
	
		
			
			|  | 116 | +    echo "-=> via docker stop ..."
 | 
	
		
			
			|  | 117 | +}
 | 
	
		
			
			|  | 118 | +
 | 
	
		
			
			|  | 119 | +
 | 
	
		
			
			|  | 120 | +function matrix_diff {
 | 
	
		
			
			|  | 121 | +    echo "-=> Diff between local configfile and a fresh generated config file"
 | 
	
		
			
			|  | 122 | +    echo "-=>      some values are different in technical point of view, like"
 | 
	
		
			
			|  | 123 | +    echo "-=>      autogenerated secret keys etc..."
 | 
	
		
			
			|  | 124 | +
 | 
	
		
			
			|  | 125 | +    DIFFPARAMS="${DIFFPARAMS:-Naur}"
 | 
	
		
			
			|  | 126 | +    DEFAULT_DOMAIN_NAME="${DEFAULT_DOMAIN_NAME:-demo_server_name}"
 | 
	
		
			
			|  | 127 | +    REPORT_STATS="${REPORT_STATS:-no_or_yes}"
 | 
	
		
			
			|  | 128 | +    export DEFAULT_DOMAIN_NAME REPORT_STATS
 | 
	
		
			
			|  | 129 | +
 | 
	
		
			
			|  | 130 | +    matrix_generate_synapse_file /tmp/homeserver.synapse.yaml
 | 
	
		
			
			|  | 131 | +    diff -${DIFFPARAMS} /tmp/homeserver.synapse.yaml ${MATRIX_DATA_DIR}/homeserver.yaml
 | 
	
		
			
			|  | 132 | +    rm /tmp/homeserver.synapse.yaml
 | 
	
		
			
			|  | 133 | +}
 | 
	
		
			
			|  | 134 | +
 | 
	
		
			
			|  | 135 | +function matrix_generate {
 | 
	
		
			
			|  | 136 | +    breakup="0"
 | 
	
		
			
			|  | 137 | +    [[ -z "${DEFAULT_DOMAIN_NAME}" ]] && echo "STOP! environment variable DEFAULT_DOMAIN_NAME must be set" && breakup="1"
 | 
	
		
			
			|  | 138 | +    [[ -z "${REPORT_STATS}" ]] && echo "STOP! environment variable REPORT_STATS must be set to 'no' or 'yes'" && breakup="1"
 | 
	
		
			
			|  | 139 | +    [[ "${breakup}" == "1" ]] && exit 1
 | 
	
		
			
			|  | 140 | +
 | 
	
		
			
			|  | 141 | +    [[ "${REPORT_STATS}" != "yes" ]] && [[ "${REPORT_STATS}" != "no" ]] && \
 | 
	
		
			
			|  | 142 | +        echo "STOP! REPORT_STATS needs to be 'no' or 'yes'" && breakup="1"
 | 
	
		
			
			|  | 143 | +
 | 
	
		
			
			|  | 144 | +    echo "-=> generate turn config"
 | 
	
		
			
			|  | 145 | +    turnkey=$(pwgen -s 64 1)
 | 
	
		
			
			|  | 146 | +    matrix_generate_turn_key $turnkey ${MATRIX_DATA_DIR}/turnserver.conf
 | 
	
		
			
			|  | 147 | +
 | 
	
		
			
			|  | 148 | +    echo "-=> generate synapse config"
 | 
	
		
			
			|  | 149 | +    matrix_generate_synapse_file ${MATRIX_DATA_DIR}/homeserver.tmp
 | 
	
		
			
			|  | 150 | +
 | 
	
		
			
			|  | 151 | +    echo "-=> configure some settings in homeserver.yaml"
 | 
	
		
			
			|  | 152 | +    matrix_configure_homeserver_yaml $turnkey ${MATRIX_DATA_DIR}/homeserver.tmp
 | 
	
		
			
			|  | 153 | +
 | 
	
		
			
			|  | 154 | +    mv ${MATRIX_DATA_DIR}/homeserver.tmp ${MATRIX_DATA_DIR}/homeserver.yaml
 | 
	
		
			
			|  | 155 | +}
 | 
	
		
			
			|  | 156 | +
 | 
	
		
			
			|  | 157 | +function remove_user_matrix {
 | 
	
		
			
			|  | 158 | +    remove_username="$1"
 | 
	
		
			
			|  | 159 | +
 | 
	
		
			
			|  | 160 | +    ${PROJECT_NAME}-pass -u $remove_username --rmapp matrix
 | 
	
		
			
			|  | 161 | +
 | 
	
		
			
			|  | 162 | +    # TODO
 | 
	
		
			
			|  | 163 | +}
 | 
	
		
			
			|  | 164 | +
 | 
	
		
			
			|  | 165 | +function add_user_matrix {
 | 
	
		
			
			|  | 166 | +    new_username="$1"
 | 
	
		
			
			|  | 167 | +    new_user_password="$2"
 | 
	
		
			
			|  | 168 | +
 | 
	
		
			
			|  | 169 | +    ${PROJECT_NAME}-pass -u $new_username -a matrix -p "$new_user_password"
 | 
	
		
			
			|  | 170 | +
 | 
	
		
			
			|  | 171 | +    cd /etc/matrix
 | 
	
		
			
			|  | 172 | +    register_new_matrix_user -c ${MATRIX_DATA_DIR}/homeserver.yaml https://localhost:${MATRIX_PORT} -u "${new_username}" -p "${new_user_password}" -a
 | 
	
		
			
			|  | 173 | +    echo '0'
 | 
	
		
			
			|  | 174 | +}
 | 
	
		
			
			|  | 175 | +
 | 
	
		
			
			|  | 176 | +function install_interactive_matrix {
 | 
	
		
			
			|  | 177 | +    echo -n ''
 | 
	
		
			
			|  | 178 | +    APP_INSTALLED=1
 | 
	
		
			
			|  | 179 | +}
 | 
	
		
			
			|  | 180 | +
 | 
	
		
			
			|  | 181 | +function change_password_matrix {
 | 
	
		
			
			|  | 182 | +    curr_username="$1"
 | 
	
		
			
			|  | 183 | +    new_user_password="$2"
 | 
	
		
			
			|  | 184 | +
 | 
	
		
			
			|  | 185 | +    #${PROJECT_NAME}-pass -u "$curr_username" -a matrix -p "$new_user_password"
 | 
	
		
			
			|  | 186 | +}
 | 
	
		
			
			|  | 187 | +
 | 
	
		
			
			|  | 188 | +function reconfigure_matrix {
 | 
	
		
			
			|  | 189 | +    echo -n ''
 | 
	
		
			
			|  | 190 | +}
 | 
	
		
			
			|  | 191 | +
 | 
	
		
			
			|  | 192 | +function upgrade_matrix {
 | 
	
		
			
			|  | 193 | +    function_check set_repo_commit
 | 
	
		
			
			|  | 194 | +    set_repo_commit /etc/matrix "matrix commit" "$MATRIX_COMMIT" $MATRIX_REPO
 | 
	
		
			
			|  | 195 | +
 | 
	
		
			
			|  | 196 | +    pip install --upgrade --process-dependency-links .
 | 
	
		
			
			|  | 197 | +    chown -R matrix:matrix /etc/matrix
 | 
	
		
			
			|  | 198 | +    chown -R matrix:matrix /var/lib/matrix
 | 
	
		
			
			|  | 199 | +}
 | 
	
		
			
			|  | 200 | +
 | 
	
		
			
			|  | 201 | +function backup_local_matrix {
 | 
	
		
			
			|  | 202 | +    source_directory=/etc/matrix
 | 
	
		
			
			|  | 203 | +    if [ -d $source_directory ]; then
 | 
	
		
			
			|  | 204 | +        systemctl stop matrix
 | 
	
		
			
			|  | 205 | +        function_check backup_directory_to_usb
 | 
	
		
			
			|  | 206 | +        backup_directory_to_usb $source_directory matrix
 | 
	
		
			
			|  | 207 | +        source_directory=/var/lib/matrix
 | 
	
		
			
			|  | 208 | +        if [ -d $source_directory ]; then
 | 
	
		
			
			|  | 209 | +            backup_directory_to_usb $source_directory matrixdata
 | 
	
		
			
			|  | 210 | +        fi
 | 
	
		
			
			|  | 211 | +        systemctl start matrix
 | 
	
		
			
			|  | 212 | +    fi
 | 
	
		
			
			|  | 213 | +}
 | 
	
		
			
			|  | 214 | +
 | 
	
		
			
			|  | 215 | +function restore_local_matrix {
 | 
	
		
			
			|  | 216 | +    if [ -d /etc/matrix ]; then
 | 
	
		
			
			|  | 217 | +        systemctl stop matrix
 | 
	
		
			
			|  | 218 | +
 | 
	
		
			
			|  | 219 | +        temp_restore_dir=/root/tempmatrix
 | 
	
		
			
			|  | 220 | +        function_check restore_directory_from_usb
 | 
	
		
			
			|  | 221 | +        restore_directory_from_usb $temp_restore_dir matrix
 | 
	
		
			
			|  | 222 | +        cp -r $temp_restore_dir/etc/matrix/* /etc/matrix
 | 
	
		
			
			|  | 223 | +        if [ ! "$?" = "0" ]; then
 | 
	
		
			
			|  | 224 | +            function_check backup_unmount_drive
 | 
	
		
			
			|  | 225 | +            backup_unmount_drive
 | 
	
		
			
			|  | 226 | +            exit 3783
 | 
	
		
			
			|  | 227 | +        fi
 | 
	
		
			
			|  | 228 | +        rm -rf $temp_restore_dir
 | 
	
		
			
			|  | 229 | +        chown -R matrix:matrix /etc/matrix
 | 
	
		
			
			|  | 230 | +
 | 
	
		
			
			|  | 231 | +        temp_restore_dir=/root/tempmatrixdata
 | 
	
		
			
			|  | 232 | +        restore_directory_from_usb $temp_restore_dir matrixdata
 | 
	
		
			
			|  | 233 | +        cp -r $temp_restore_dir/var/lib/matrix/* /var/lib/matrix
 | 
	
		
			
			|  | 234 | +        if [ ! "$?" = "0" ]; then
 | 
	
		
			
			|  | 235 | +            function_check backup_unmount_drive
 | 
	
		
			
			|  | 236 | +            backup_unmount_drive
 | 
	
		
			
			|  | 237 | +            exit 78352
 | 
	
		
			
			|  | 238 | +        fi
 | 
	
		
			
			|  | 239 | +        rm -rf $temp_restore_dir
 | 
	
		
			
			|  | 240 | +        chown -R matrix:matrix /var/lib/matrix
 | 
	
		
			
			|  | 241 | +
 | 
	
		
			
			|  | 242 | +        systemctl start matrix
 | 
	
		
			
			|  | 243 | +    fi
 | 
	
		
			
			|  | 244 | +}
 | 
	
		
			
			|  | 245 | +
 | 
	
		
			
			|  | 246 | +function backup_remote_matrix {
 | 
	
		
			
			|  | 247 | +    source_directory=/etc/matrix
 | 
	
		
			
			|  | 248 | +    if [ -d $source_directory ]; then
 | 
	
		
			
			|  | 249 | +        systemctl stop matrix
 | 
	
		
			
			|  | 250 | +        function_check backup_directory_to_friend
 | 
	
		
			
			|  | 251 | +        backup_directory_to_friend $source_directory matrix
 | 
	
		
			
			|  | 252 | +        source_directory=/var/lib/matrix
 | 
	
		
			
			|  | 253 | +        if [ -d $source_directory ]; then
 | 
	
		
			
			|  | 254 | +            backup_directory_to_friend $source_directory matrixdata
 | 
	
		
			
			|  | 255 | +        fi
 | 
	
		
			
			|  | 256 | +        systemctl start matrix
 | 
	
		
			
			|  | 257 | +    fi
 | 
	
		
			
			|  | 258 | +}
 | 
	
		
			
			|  | 259 | +
 | 
	
		
			
			|  | 260 | +function restore_remote_synapse {
 | 
	
		
			
			|  | 261 | +    if [ -d /etc/matrix ]; then
 | 
	
		
			
			|  | 262 | +        systemctl stop matrix
 | 
	
		
			
			|  | 263 | +
 | 
	
		
			
			|  | 264 | +        temp_restore_dir=/root/tempmatrix
 | 
	
		
			
			|  | 265 | +        function_check restore_directory_from_friend
 | 
	
		
			
			|  | 266 | +        restore_directory_from_friend $temp_restore_dir matrix
 | 
	
		
			
			|  | 267 | +        cp -r $temp_restore_dir/etc/matrix/* /etc/matrix
 | 
	
		
			
			|  | 268 | +        if [ ! "$?" = "0" ]; then
 | 
	
		
			
			|  | 269 | +            exit 38935
 | 
	
		
			
			|  | 270 | +        fi
 | 
	
		
			
			|  | 271 | +        rm -rf $temp_restore_dir
 | 
	
		
			
			|  | 272 | +        chown -R matrix:matrix /etc/matrix
 | 
	
		
			
			|  | 273 | +
 | 
	
		
			
			|  | 274 | +        temp_restore_dir=/root/tempmatrixdata
 | 
	
		
			
			|  | 275 | +        restore_directory_from_friend $temp_restore_dir matrixdata
 | 
	
		
			
			|  | 276 | +        cp -r $temp_restore_dir/var/lib/matrix/* /var/lib/matrix
 | 
	
		
			
			|  | 277 | +        if [ ! "$?" = "0" ]; then
 | 
	
		
			
			|  | 278 | +            exit 60923
 | 
	
		
			
			|  | 279 | +        fi
 | 
	
		
			
			|  | 280 | +        rm -rf $temp_restore_dir
 | 
	
		
			
			|  | 281 | +        chown -R matrix:matrix /var/lib/matrix
 | 
	
		
			
			|  | 282 | +
 | 
	
		
			
			|  | 283 | +        systemctl start matrix
 | 
	
		
			
			|  | 284 | +    fi
 | 
	
		
			
			|  | 285 | +}
 | 
	
		
			
			|  | 286 | +
 | 
	
		
			
			|  | 287 | +function remove_matrix {
 | 
	
		
			
			|  | 288 | +    firewall_remove ${MATRIX_PORT}
 | 
	
		
			
			|  | 289 | +    firewall_remove ${MATRIX_TURN_PORT}
 | 
	
		
			
			|  | 290 | +    systemctl stop matrix
 | 
	
		
			
			|  | 291 | +    systemcrl disable matrix
 | 
	
		
			
			|  | 292 | +    if [ -f /etc/systemd/system/matrix.service ]; then
 | 
	
		
			
			|  | 293 | +        rm /etc/systemd/system/matrix.service
 | 
	
		
			
			|  | 294 | +    fi
 | 
	
		
			
			|  | 295 | +    apt-get -y remove --purge coturn
 | 
	
		
			
			|  | 296 | +    cd /etc/matrix
 | 
	
		
			
			|  | 297 | +    pip uninstall .
 | 
	
		
			
			|  | 298 | +    rm -rf $MATRIX_DATA_DIR
 | 
	
		
			
			|  | 299 | +    rm -rf /etc/matrix
 | 
	
		
			
			|  | 300 | +    deluser matrix
 | 
	
		
			
			|  | 301 | +    delgroup matrix
 | 
	
		
			
			|  | 302 | +    remove_onion_service matrix ${MATRIX_PORT}
 | 
	
		
			
			|  | 303 | +
 | 
	
		
			
			|  | 304 | +    remove_completion_param install_matrix
 | 
	
		
			
			|  | 305 | +    sed -i '/matrix/d' $COMPLETION_FILE
 | 
	
		
			
			|  | 306 | +}
 | 
	
		
			
			|  | 307 | +
 | 
	
		
			
			|  | 308 | +function install_matrix {
 | 
	
		
			
			|  | 309 | +    if [[ ${ONION_ONLY} == 'no' ]]; then
 | 
	
		
			
			|  | 310 | +        # obtain a cert for the default domain
 | 
	
		
			
			|  | 311 | +        if [[ "$(cert_exists ${DEFAULT_DOMAIN_NAME} pem)" == "0" ]]; then
 | 
	
		
			
			|  | 312 | +            echo $'Obtaining certificate for the main domain'
 | 
	
		
			
			|  | 313 | +            create_site_certificate ${DEFAULT_DOMAIN_NAME} 'yes'
 | 
	
		
			
			|  | 314 | +        fi
 | 
	
		
			
			|  | 315 | +    fi
 | 
	
		
			
			|  | 316 | +
 | 
	
		
			
			|  | 317 | +    REBUILD=1
 | 
	
		
			
			|  | 318 | +    export DEBIAN_FRONTEND=noninteractive
 | 
	
		
			
			|  | 319 | +    apt-get -yq install coreutils coturn \
 | 
	
		
			
			|  | 320 | +            curl file gcc git libevent-2.0-5 \
 | 
	
		
			
			|  | 321 | +            libevent-dev libffi-dev libffi6 \
 | 
	
		
			
			|  | 322 | +            libgnutls28-dev libjpeg62-turbo \
 | 
	
		
			
			|  | 323 | +            libjpeg62-turbo-dev libldap-2.4-2 \
 | 
	
		
			
			|  | 324 | +            libldap2-dev libsasl2-dev \
 | 
	
		
			
			|  | 325 | +            libsqlite3-dev libssl-dev \
 | 
	
		
			
			|  | 326 | +            libssl1.0.0 libtool libxml2 \
 | 
	
		
			
			|  | 327 | +            libxml2-dev libxslt1-dev libxslt1.1 \
 | 
	
		
			
			|  | 328 | +            make pwgen python python-dev \
 | 
	
		
			
			|  | 329 | +            python-pip python-psycopg2 \
 | 
	
		
			
			|  | 330 | +            python-virtualenv sqlite unzip \
 | 
	
		
			
			|  | 331 | +            zlib1g zlib1g-dev
 | 
	
		
			
			|  | 332 | +
 | 
	
		
			
			|  | 333 | +    pip install --upgrade pip
 | 
	
		
			
			|  | 334 | +    pip install --upgrade python-ldap
 | 
	
		
			
			|  | 335 | +    pip install --upgrade lxml
 | 
	
		
			
			|  | 336 | +
 | 
	
		
			
			|  | 337 | +    if [ ! -d /etc/matrix ]; then
 | 
	
		
			
			|  | 338 | +        function_check git_clone
 | 
	
		
			
			|  | 339 | +        git_clone $MATRIX_REPO /etc/matrix
 | 
	
		
			
			|  | 340 | +        if [ ! -d /etc/matrix ]; then
 | 
	
		
			
			|  | 341 | +            echo $'Unable to clone matrix repo'
 | 
	
		
			
			|  | 342 | +            exit 6724683
 | 
	
		
			
			|  | 343 | +        fi
 | 
	
		
			
			|  | 344 | +    fi
 | 
	
		
			
			|  | 345 | +
 | 
	
		
			
			|  | 346 | +    cd /etc/matrix
 | 
	
		
			
			|  | 347 | +    git checkout $MATRIX_COMMIT -b $MATRIX_COMMIT
 | 
	
		
			
			|  | 348 | +    set_completion_param "matrix commit" "$MATRIX_COMMIT"
 | 
	
		
			
			|  | 349 | +    pip install --upgrade --process-dependency-links .
 | 
	
		
			
			|  | 350 | +    if [ ! "$?" = "0" ]; then
 | 
	
		
			
			|  | 351 | +        exit 782542
 | 
	
		
			
			|  | 352 | +    fi
 | 
	
		
			
			|  | 353 | +
 | 
	
		
			
			|  | 354 | +    groupadd matrix
 | 
	
		
			
			|  | 355 | +    useradd -c "Matrix system account" -d /var/lib/matrix -m -r -g matrix matrix
 | 
	
		
			
			|  | 356 | +
 | 
	
		
			
			|  | 357 | +    chown -R matrix:matrix /etc/matrix
 | 
	
		
			
			|  | 358 | +    chown -R matrix:matrix /var/lib/matrix
 | 
	
		
			
			|  | 359 | +
 | 
	
		
			
			|  | 360 | +    echo '[Unit]' > /etc/systemd/system/matrix.service
 | 
	
		
			
			|  | 361 | +    echo 'Description=Matrix federated messaging' >> /etc/systemd/system/matrix.service
 | 
	
		
			
			|  | 362 | +    echo '' >> /etc/systemd/system/matrix.service
 | 
	
		
			
			|  | 363 | +    echo '[Service]' >> /etc/systemd/system/matrix.service
 | 
	
		
			
			|  | 364 | +    echo 'Type=simple' >> /etc/systemd/system/matrix.service
 | 
	
		
			
			|  | 365 | +    echo 'User=matrix' >> /etc/systemd/system/matrix.service
 | 
	
		
			
			|  | 366 | +    echo "WorkingDirectory=/etc/matrix" >> /etc/systemd/system/matrix.service
 | 
	
		
			
			|  | 367 | +    echo "ExecStart=/usr/bin/turnserver --daemon -c ${MATRIX_DATA_DIR}/turnserver.conf" >> /etc/systemd/system/matrix.service
 | 
	
		
			
			|  | 368 | +    echo "ExecStart=/usr/bin/python -m synapse.app.homeserver --config-path ${MATRIX_DATA_DIR}/homeserver.yaml" >> /etc/systemd/system/matrix.service
 | 
	
		
			
			|  | 369 | +    echo 'Restart=always' >> /etc/systemd/system/matrix.service
 | 
	
		
			
			|  | 370 | +    echo 'RestartSec=10' >> /etc/systemd/system/matrix.service
 | 
	
		
			
			|  | 371 | +    echo '' >> /etc/systemd/system/matrix.service
 | 
	
		
			
			|  | 372 | +    echo '[Install]' >> /etc/systemd/system/matrix.service
 | 
	
		
			
			|  | 373 | +    echo 'WantedBy=multi-user.target' >> /etc/systemd/system/matrix.service
 | 
	
		
			
			|  | 374 | +    systemctl enable matrix
 | 
	
		
			
			|  | 375 | +    systemctl daemon-reload
 | 
	
		
			
			|  | 376 | +    systemctl start matrix
 | 
	
		
			
			|  | 377 | +
 | 
	
		
			
			|  | 378 | +    update_default_domain
 | 
	
		
			
			|  | 379 | +
 | 
	
		
			
			|  | 380 | +    firewall_add matrix ${MATRIX_PORT}
 | 
	
		
			
			|  | 381 | +    firewall_add matrix-turn ${MATRIX_TURN_PORT}
 | 
	
		
			
			|  | 382 | +
 | 
	
		
			
			|  | 383 | +    MATRIX_ONION_HOSTNAME=$(add_onion_service matrix ${MATRIX_PORT} ${MATRIX_PORT})
 | 
	
		
			
			|  | 384 | +    if [ ! ${MATRIX_PASSWORD} ]; then
 | 
	
		
			
			|  | 385 | +        if [ -f ${IMAGE_PASSWORD_FILE} ]; then
 | 
	
		
			
			|  | 386 | +            MATRIX_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
 | 
	
		
			
			|  | 387 | +        else
 | 
	
		
			
			|  | 388 | +            MATRIX_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
 | 
	
		
			
			|  | 389 | +        fi
 | 
	
		
			
			|  | 390 | +    fi
 | 
	
		
			
			|  | 391 | +
 | 
	
		
			
			|  | 392 | +    add_user_matrix "${MY_USERNAME}" "${MATRIX_PASSWORD}"
 | 
	
		
			
			|  | 393 | +    APP_INSTALLED=1
 | 
	
		
			
			|  | 394 | +}
 |