summarylogtreecommitdiffstats
path: root/zoneminder.install
blob: a1c83c9eaefa4f2e487214ebeca4467a330e1896 (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
post_install() {
    echo
    
    systemctl daemon-reload    
    
    
    ### Nginx & Apache ###
    
    # If Nginx is installed and either is active, or is inactive but Apache is not installed, we prefer Nginx.
    if [[ "$(pacman -Qs nginx)" = *"local/nginx"* ]] && { [ "$(systemctl is-active nginx)" = "active" ] || [[ "$(pacman -Qs apache)" != *"local/apache"* ]] ; }
    then
        # Ensure the main conf file is present
        if [ -f /etc/nginx/nginx.conf ] 
        then
            # Configure Nginx with the folder structure used in Debian-based distributions
            if ( ! grep -q 'sites-enabled' /etc/nginx/nginx.conf )
            then
                sed -i 's|http {|http {\'$'\n    include       sites-enabled/*.conf;|g' /etc/nginx/nginx.conf
            fi
            
            # Create the sites-enabled folder if it's not already created
            if [ ! -d /etc/nginx/sites-enabled ]
            then
                mkdir -p /etc/nginx/sites-enabled
            fi
            
            # Enable ZoneMinder's server block if it's not already enabled
            if [ ! -f /etc/nginx/sites-enabled/zoneminder.conf ]
            then
                ln -sf /etc/nginx/sites-{available,enabled}/zoneminder.conf
            fi
            
            # (Re)start Nginx
            systemctl is-active --quiet nginx && systemctl stop nginx
            systemctl start nginx
        else
            echo "ERROR: Nginx was detected but file '/etc/nginx/nginx.conf' was not found. Something seems broken."
            echo
        fi
    # Else, if Nginx either is not installed, or is installed and inactive but Apache is also installed, we prefer Apache.
    elif [[ "$(pacman -Qs apache)" = *"local/apache"* ]]
    then
        # Ensure the main conf file is present
        if [ -f /etc/httpd/conf/httpd.conf ]
        then
            # Enable mod_proxy
            sed -i 's|#LoadModule proxy_module modules/mod_proxy.so|LoadModule proxy_module modules/mod_proxy.so|g' /etc/httpd/conf/httpd.conf
            # Enable mod_proxy_fcgi
            sed -i 's|#LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so|LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so|g' /etc/httpd/conf/httpd.conf
            # Enable mod_rewrite
            sed -i 's|#LoadModule rewrite_module modules/mod_rewrite.so|LoadModule rewrite_module modules/mod_rewrite.so|g' /etc/httpd/conf/httpd.conf
            # Enable mod_cgid
            sed -i 's|#LoadModule cgid_module modules/mod_cgid.so|LoadModule cgid_module modules/mod_cgid.so|g' /etc/httpd/conf/httpd.conf
            
            # Enable ZoneMinder's VirtualHost if it's not already enabled
            if ( ! grep -q 'zoneminder.conf' /etc/httpd/conf/httpd.conf )
            then
                echo "Include conf/extra/zoneminder.conf" >> /etc/httpd/conf/httpd.conf
            fi
    
            # (Re)start Apache
            systemctl is-active --quiet httpd && systemctl stop httpd
            systemctl start httpd
        else
            echo "ERROR: Apache was detected but file '/etc/httpd/conf/httpd.conf' was not found. Something seems broken."
            echo
        fi
    # Else, if neither of them is installed, we inform the user that something is seriously messed up.
    else
        echo "ERROR: Neither Nginx nor Apache were detected in your system. I hope you know what you're doing."
        echo
    fi
    
    
    ### fcgiwrap ###
    
    if [[ "$(pacman -Qs fcgiwrap)" = *"local/fcgiwrap"* ]]
    then
        systemctl is-active --quiet fcgiwrap.socket && systemctl stop fcgiwrap.socket
        systemctl is-active --quiet fcgiwrap && systemctl stop fcgiwrap
        systemctl start fcgiwrap.socket
    fi
    
    
    ### PHP-FPM ###
    
    # Set local timezone in /etc/php/conf.d/zoneminder.ini
    sed -i 's|PLACEHOLDER|'`timedatectl | grep "Time zone" | tr -s ' ' | cut -f4 -d ' '`'|g' /etc/php/conf.d/zoneminder.ini
    
    if [[ "$(pacman -Qs php-fpm)" = *"local/php-fpm"* ]]
    then
        systemctl is-active --quiet php-fpm && systemctl stop php-fpm
        systemctl start php-fpm
    fi
    

    ### MariaDB ###
    
    # If MariaDB is installed
    if [[ "$(pacman -Qs mariadb)" = *"local/mariadb "* ]]
    then
        # Initialize MariaDB's default database if it's not already initialized
        if [ ! -d /var/lib/mysql/mysql ]
        then
            # Stop MariaDB if it's active
            systemctl is-active --quiet mariadb && systemctl stop mariadb
            # NOTE: This will need to be changed when MariaDB 10.2 hits the repos, as mysql_install_db has been deprecated in favor of mysqld
            mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql 2> /dev/null
        fi
        
        # Start MariaDB if it's not already active
        systemctl is-active --quiet mariadb || systemctl start mariadb
        
        # Create ZoneMinder's database & user if they do not exist
        if [ ! -d /var/lib/mysql/zm ]
        then
            # Check for database root password
            if [[ "$(mysql -uroot -e "select * from mysql.user;" 2>&1)" = *"Access denied"* ]]
            then
                # If a database root password is set
                echo \* Secure MariaDB installation found, please enter the database root password.
                echo
                mysql -uroot -p < /usr/share/zoneminder/db/zm_create.sql
                echo
                echo \* Enter the password one more time...
                echo
                mysql -uroot -p -e "grant select,insert,update,delete,create,alter,index,lock tables on zm.* to 'zmuser'@localhost identified by 'zmpass';"
                echo
            else
                # If a database root password is not set
                mysql -uroot < /usr/share/zoneminder/db/zm_create.sql
                mysql -uroot -e "grant select,insert,update,delete,create,alter,index,lock tables on zm.* to 'zmuser'@localhost identified by 'zmpass';"
            fi
        fi
    fi
    
    
    ### ZoneMinder ###
    
    systemctl is-active --quiet zoneminder && systemctl stop zoneminder
    systemd-tmpfiles --create
    systemctl start zoneminder
    
    
    echo ---------------
    echo
    
    if [[ "$(systemctl is-active nginx)" = active ]] || [[ "$(systemctl is-active httpd)" = active ]]
    then
        echo ZoneMinder is listening at http://localhost:8095
    else
        echo WARNING: No web server has been configured.
    fi
    
    echo
}

post_upgrade() {
    post_install
    
    echo ---------------
    echo
    
    echo The ZoneMinder updater will now run.
        
    # Make sure ZoneMinder is *not* running before we attempt to use the updater
    systemctl is-active --quiet zoneminder && systemctl stop zoneminder
    
    # Make sure MariaDB *is* running before we attempt to use the updater
    # NOTE: We probably don't want this in a Multi Server setup because MariaDB will not be running locally,
    # but since it will simply generate a harmless error and the updater will still run, for now I'm leaving it as is
    systemctl is-active --quiet mariadb || systemctl start mariadb
    
    # Run the updater
    /usr/bin/zmupdate.pl
    
    systemctl start zoneminder
    
    echo
    echo Update complete.
    echo
}

post_remove() {
    systemctl daemon-reload && systemctl is-active --quiet zoneminder && systemctl stop zoneminder
    
    # Disable ZoneMinder's Apache VirtualHost
    if [ -f /etc/httpd/conf/httpd.conf ]
    then
        sed -i '/zoneminder.conf/d' /etc/httpd/conf/httpd.conf
    fi
    
    # Disable ZoneMinder's Nginx server block
    if [ -h /etc/nginx/sites-enabled/zoneminder.conf ]
    then
        rm /etc/nginx/sites-enabled/zoneminder.conf
        
        # If the sites-enabled directory is empty, remove it as well and also remove reference in nginx.conf
        if [ -z "$(ls -A /etc/nginx/sites-enabled)" ]
        then
            rm -r /etc/nginx/sites-enabled
            sed -i '/sites-enabled/d' /etc/nginx/nginx.conf
        fi
    fi
    
    # Notify user about ZoneMinder's database and user not being removed
    if [ -d /var/lib/mysql/zm ]
    then
        echo
        echo ZoneMinder\'s database and user have been left intact. You can remove them by running the following:
        echo
        echo mysql -uroot -p -e \"drop database zm\;\"
        echo mysql -uroot -p -e \"drop user \'zmuser\'@localhost\;\"
        echo
        echo If you haven\'t yet configured a password for the database root user, omit the \'-p\' option.
        echo
    fi
}