summaryrefslogtreecommitdiff
path: root/src/local/rz-mux/rz-mux-tool.sh
blob: 7a9a4dc7acbdc2236f64ddf9ecd62b531005dd35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/bash

#
# Multiplexed Neo4J instance installer
#
# generated apache structure:
#
# /srv/www/rhizi/
# └── mux-root.d
#     └── dom-x
#         └── webapp
#             ├── etc -> /etc/rhizi/mux-conf.d/dom-x
#             ├── static
#             │   ├── css -> /usr/share/rhizi/webapp/static/css
#             │   ├── font -> /usr/share/rhizi/webapp/static/font
#             │   ├── img -> /usr/share/rhizi/webapp/static/img
#             │   ├── js -> /usr/lib/rhizi/webapp/static/js
#             │   └── lib -> /usr/share/rhizi/webapp/static/lib
#             └── fragment.d                                           // webapp fragments
#                 ├── js
#                 ├── img
#                 └── template.d                                       // HTML template fragments
#
# generated neo4j dir structure:
#
#    /etc/neo4j/mux-conf.d/
#    └── dom-x
#        ├── neo4j.properties
#        ├── neo4j-server.properties
#        └──  ...
#
#    /var/lib/neo4j/mux-root.d/
#    └── dom-x
#        ├── data/
#        ├── bin -> /var/lib/neo4j/bin
#        ├── conf -> /etc/neo4j/mux-conf.d/dom-y
#        ├── lib -> /usr/share/neo4j/lib
#        ├── plugins -> /usr/share/neo4j/plugins
#        └── system -> /usr/share/neo4j/system
#
# generated rz structure:
#
#    /etc/rhizi/
#    ├── domain-conf.d
#    └── mux-conf.d
#        └── dom-x
#            └── rhizi-server.conf.example
#

die() {
    echo "$1" 1>&2; exit 1
}

set_path_vars() {

    neo4j_module__confdir=/etc/neo4j/mux-conf.d/${RZI_NAME}
    neo4j_module__rootdir=/var/lib/neo4j/mux-root.d/${RZI_NAME}

    rz_module__confdir=/etc/rhizi/mux-conf.d/${RZI_NAME}
    rz_module__bkp=/var/lib/rhizi/mux-bkp.d/${RZI_NAME}

    apache_module__siteconf_filename=${RZI_NAME}.conf
    apache_module__rootdir=/srv/www/rhizi/mux-root.d/${RZI_NAME}

}

install_instance__neo4j() {

    [[ -e "${neo4j_module__rootdir}" ]] && die "error: neo4j module already installed: ${neo4j_module__rootdir}"

    install -v --owner=${NEO4J_USER} --group=adm --directory ${neo4j_module__confdir} \
                                                             ${neo4j_module__confdir}/ssl \
                                                             ${neo4j_module__rootdir} \
                                                             ${neo4j_module__rootdir}/data

    # copy non-generated config files
    cp /etc/neo4j/custom-logback.xml \
       /etc/neo4j/jmx.access \
       /etc/neo4j/jmx.password \
       /etc/neo4j/logging.properties \
       /etc/neo4j/neo4j-http-logging.xml \
       /etc/neo4j/neo4j-wrapper.conf \
       /etc/neo4j/mux-conf.d/${RZI_NAME}
    cp /etc/neo4j/custom-logback.xml \
       /etc/neo4j/ssl/snakeoil.cert \
       /etc/neo4j/ssl/snakeoil.key \
       /etc/neo4j/mux-conf.d/${RZI_NAME}/ssl

    ln -vfs -T /etc/neo4j/mux-conf.d/${RZI_NAME}   ${neo4j_module__rootdir}/conf
    ln -vfs -T /var/lib/neo4j/bin                  ${neo4j_module__rootdir}/bin
    ln -vfs -T /usr/share/neo4j/lib                ${neo4j_module__rootdir}/lib
    ln -vfs -T /usr/share/neo4j/plugins            ${neo4j_module__rootdir}/plugins
    ln -vfs -T /usr/share/neo4j/system             ${neo4j_module__rootdir}/system

}

install_instance__apache() {

    [[ -e "${apache_module__rootdir}" ]] && die "error: apache module already installed: ${apache_module__rootdir}"

    install -v --owner=${APACHE_USER} --group=${APACHE_USER} --directory ${apache_module__rootdir} \
                                                             --directory ${apache_module__rootdir}/auth \
                                                             --directory ${apache_module__rootdir}/webapp \
                                                             --directory ${apache_module__rootdir}/webapp/static \
                                                             --directory ${apache_module__rootdir}/webapp/fragment.d/img \
                                                             --directory ${apache_module__rootdir}/webapp/fragment.d/js \
                                                             --directory ${apache_module__rootdir}/webapp/fragment.d/template.d

    ln -vfs -T /etc/rhizi/mux-conf.d/${RZI_NAME}     ${apache_module__rootdir}/webapp/etc
    ln -vfs -T /usr/lib/rhizi/webapp/static/js       ${apache_module__rootdir}/webapp/static/js

    ln -vfs -T ${rhizi_custom__instance}/js/domain_types.js ${apache_module__rootdir}/webapp/fragment.d/js/domain_types.js
    ln -vfs -T ${rhizi_custom__instance}/css    ${apache_module__rootdir}/webapp/static/css

    ln -vfs -T /usr/share/rhizi/webapp/static/font   ${apache_module__rootdir}/webapp/static/font
    ln -vfs -T /usr/share/rhizi/webapp/static/img    ${apache_module__rootdir}/webapp/static/img
    ln -vfs -T /usr/share/rhizi/webapp/static/lib    ${apache_module__rootdir}/webapp/static/lib

    # enable site
    ln -vfs -T /etc/apache2/sites-available/${apache_module__siteconf_filename} /etc/apache2/sites-enabled/${apache_module__siteconf_filename}
}

install_instance__apache__custom() {
    if [ -e ${rhizi_custom__fragment} ]; then
        cp -vr ${rhizi_custom__fragment}/* ${apache_module__rootdir}/webapp/fragment.d/
    fi
}

install_instance__rhizi() { # depends on install_instance__apache()

    [[ -e "${rz_module__confdir}" ]] && die "error: rhizi module already installed: ${rz_module__confdir}"

    install -v --directory ${rz_module__confdir} \
               --directory ${rz_module__bkp}

    # install default fragments - JS
    cp -vr /usr/share/rhizi/webapp/domain-fragment.d/default/* ${apache_module__rootdir}/webapp/fragment.d/template.d/

}

uninstall_instance__apache() {

    rm -rvf ${apache_module__rootdir}
    rm -vf /etc/apache2/sites-enabled/${apache_module__siteconf_filename}
    rm -vf /etc/apache2/sites-available/${apache_module__siteconf_filename}
}

uninstall_instance__neo4j() {

     rm -vf ${RZI_NEO4J_INIT_SCRIPT_PATH}
     rm -rf ${neo4j_module__confdir}
     rm -rf ${neo4j_module__rootdir}
}

uninstall_instance__rhizi() {

    rm -vf ${RZI_RZ_INIT_SCRIPT_PATH}
    rm -rvf ${rz_module__confdir}
    echo "[!] leaving rhizi bkp dir removal to user: ${rz_module__bkp}"
}

#
# static variables
#
APACHE_USER=www-data
NEO4J_USER=neo4j

RZI_NAME=$2 # rhizi-instance name, preferable a FQDN
RZI_NEO4J_SVC_NAME=neo4j-service__${RZI_NAME}
RZI_NEO4J_INIT_SCRIPT_PATH=/etc/init.d/${RZI_NEO4J_SVC_NAME}
RZI_NEO4J_PID_FILE=/var/lib/neo4j/mux-root.d/${RZI_NAME}/data/neo4j-service.pid
RZI_RZ_PID_FILE=/var/run/rhizi/${RZI_NAME}.pid
RZI_RZ_INIT_SCRIPT_PATH=/etc/init.d/rhizi__${RZI_NAME}
RZI_RZ_CRON_SCRIPT_PATH=/etc/cron.daily/rhizi__${RZI_NAME}

rhizi_custom__rootdir=$3
rhizi_custom__instance=${rhizi_custom__rootdir}/domain-fragment.d/${RZI_NAME}
rhizi_custom__fragment=${rhizi_custom__rootdir}/fragment/by-domain/${RZI_NAME}

#
# sanity checks
#
[ 0 != $UID ] && die 'error: must be root to continue.'
[ -z ${RZI_NAME} ] && die 'error: RZI_NAME arg missing, aborting'
[ -e ${RZI_NEO4J_PID_FILE} ] && die "error: ${RZI_NEO4J_SVC_NAME} server seems to be running, please stop it before continuing"
[ -e ${RZI_RZ_PID_FILE} ] && die "error: ${RZI_RZ_PID_FILE} server seems to be running, please stop it before continuing"
[ ! -e ${rhizi_custom__rootdir}/domain-fragment.d ] && die 'error: 3rd argument must point to rhizi-custom git checkout'
[ ! -e ${rhizi_custom__instance} ] && die 'error: no such instance in rhizi-custom (${RZI_NAME} in ${rhizi_custom__rootdir}}'
[ -e /usr/lib/rhizi/webapp/static/js/domain_types.js ] && die 'unexpected domain_types.js in rhizi install'

set_path_vars

case $1 in
    install)

        set -e

        install_instance__apache
        install_instance__neo4j
        install_instance__rhizi
        install_instance__apache__custom

        # pass-on extra argument to python: install dom-x ...:
        #    - opt: --rz_config__disable_access_control
        if [ $# -ge 4 ] ; then shift 3; py_extra_args=$@ ; fi
        python /usr/lib/rhizi/tools/rz-mux/rz-mux-tool.py \
               --template-dir /usr/share/rhizi/rz-mux/ \
               --domain ${RZI_NAME} ${py_extra_args}

        # set modes, ownership
        chmod +x ${RZI_NEO4J_INIT_SCRIPT_PATH}
        chmod +x ${RZI_RZ_INIT_SCRIPT_PATH}
        chmod +x ${RZI_RZ_CRON_SCRIPT_PATH}
        chgrp ${APACHE_USER} /etc/rhizi/mux-conf.d/${RZI_NAME}/*
        chmod o-r /etc/rhizi/mux-conf.d/${RZI_NAME}/*

        tree -ug --noreport /etc/apache2/sites-available
        tree -ug --noreport /etc/apache2/sites-enabled
        tree -ug --noreport /srv/www/rhizi/
        tree -ug --noreport /var/lib/neo4j/mux-root.d/
        tree -ug --noreport /etc/neo4j/mux-conf.d/
        tree -ug --noreport /etc/rhizi/

        echo "# -----------------------------------------------------------------------------"
        echo "# Summery:"
        echo "#"
        echo "# rz instance installed: name: ${RZI_NAME}"
        echo "#"
        echo "# [!] please review / adjust the following:"
        echo "#     - /etc/rhizi/mux-conf.d/${RZI_NAME}/rhizi-server.conf"
        echo "#"
        echo "# [!] adjust instance-specific files here:"
        echo "#     - ${apache_module__rootdir}/webapp/static/fragment.d/js"
        echo "#     - ${apache_module__rootdir}/webapp/static/fragment.d/template.d"
        echo "#"
    ;;
    uninstall)
        echo "[!] This will purge all instance data including:"
        echo "       - '${neo4j_module__rootdir}"
        echo "       - '${apache_module__rootdir}"
        echo ""
        echo "Are you sure you want to proceed? [y/N]"
        read uninstall__proceed
        [ "$uninstall__proceed" != "y" ] && die 'Aborting'

        uninstall_instance__apache
        uninstall_instance__neo4j
        uninstall_instance__rhizi
    ;;
    *)
    echo "Usage: $0 <install|uninstall> <rhizi-instance-FQDN>"
    ;;
esac