2

I'm trying to write a bash script to automate the install of nginx with pagespeed module.

Part of this requires me to add this: --add-module=$(MODULESDIR)/ngx_pagespeed \ to a section of the /usr/src/nginx/nginx-X.X.6/debian/rules file.

Each section is similar to:

light_configure_flags := \
                    $(common_configure_flags) \
                    --with-http_gzip_static_module \
                    --without-http_browser_module \
                    --without-http_geo_module \
                    --without-http_limit_req_module \
                    --without-http_limit_conn_module \
                    --without-http_memcached_module \
                    --without-http_referer_module \
                    --without-http_scgi_module \
                    --without-http_split_clients_module \
                    --without-http_ssi_module \
                    --without-http_userid_module \
                    --without-http_uwsgi_module \
                    --add-module=$(MODULESDIR)/nginx-echo

Full file is here: https://jsfiddle.net/72hL5pya/1/ (sorry, didn't know where else to put it)

And each section is *_configure_flags

there are a number of these sections for each "version" of nginx to compile (light, full, extras, etc...)

So that last --add-module= line is different in each section.

How can I append the --add-module=$(MODULESDIR)/ngx_pagespeed \ to each?

NOTE: Looks like as of whatever date, the structure of this rules file has changed.

#!/usr/bin/make -f

#export DH_VERBOSE=1
CFLAGS ?= $(shell dpkg-buildflags --get CFLAGS)
LDFLAGS ?= $(shell dpkg-buildflags --get LDFLAGS)
WITH_HTTP2 := $(shell printf \
    "Source: nginx\nBuild-Depends: libssl-dev (>= 1.0.1)\n" | \
    dpkg-checkbuilddeps - >/dev/null 2>&1 && \
    echo "--with-http_v2_module")
PKGS = nginx nginx-dbg \
    nginx-module-xslt nginx-module-geoip nginx-module-image-filter \
    nginx-module-perl nginx-module-njs
COMMON_CONFIGURE_ARGS := \
    --prefix=/etc/nginx \
    --sbin-path=/usr/sbin/nginx \
    --modules-path=/usr/lib/nginx/modules \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/run/nginx.lock \
    --http-client-body-temp-path=/var/cache/nginx/client_temp \
    --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
    --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
    --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
    --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
    --user=nginx \
    --group=nginx \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_stub_status_module \
    --with-http_auth_request_module \
    --with-http_xslt_module=dynamic \
    --with-http_image_filter_module=dynamic \
    --with-http_geoip_module=dynamic \
    --with-http_perl_module=dynamic \
    --add-dynamic-module=debian/extra/njs-1c50334fbea6/nginx \
    --with-threads \
    --with-stream \
    --with-stream_ssl_module \
    --with-http_slice_module \
    --with-mail \
    --with-mail_ssl_module \
    --with-file-aio \
    --with-ipv6 \
    $(WITH_HTTP2) \
    --with-cc-opt="$(CFLAGS)" \
    --with-ld-opt="$(LDFLAGS)"

%:
    dh $@ 

override_dh_auto_configure: configure_debug

override_dh_strip:
    dh_strip --dbg-package=nginx-dbg

override_dh_auto_build:
    dh_auto_build
    mv objs/nginx objs/nginx-debug
    mv objs/ngx_http_xslt_filter_module.so objs/ngx_http_xslt_filter_module-debug.so
    mv objs/ngx_http_image_filter_module.so objs/ngx_http_image_filter_module-debug.so
    mv objs/ngx_http_geoip_module.so objs/ngx_http_geoip_module-debug.so
    mv objs/ngx_http_perl_module.so objs/ngx_http_perl_module-debug.so
    mv objs/src/http/modules/perl/blib/arch/auto/nginx/nginx.so objs/src/http/modules/perl/blib/arch/auto/nginx/nginx-debug.so
    mv objs/ngx_http_js_module.so objs/ngx_http_js_module-debug.so
    CFLAGS="" ./configure $(COMMON_CONFIGURE_ARGS)
    dh_auto_build

configure_debug:
    CFLAGS="" ./configure $(COMMON_CONFIGURE_ARGS) \
        --with-debug

override_dh_auto_install:
    sed -e 's/%%PROVIDES%%/nginx/g' \
        -e 's/%%DEFAULTSTART%%/2 3 4 5/g' \
        -e 's/%%DEFAULTSTOP%%/0 1 6/g' \
        < debian/init.d.in > debian/init.d
    dh_auto_install
    mkdir -p debian/nginx/etc/init.d debian/nginx/etc/default \
        debian/nginx/usr/lib/nginx/modules
    sed -e 's/%%PROVIDES%%/nginx-debug/g' \
        -e 's/%%DEFAULTSTART%%//g' \
        -e 's/%%DEFAULTSTOP%%/0 1 2 3 4 5 6/g' \
        < debian/init.d.in > debian/debug.init.d
    /usr/bin/install -m 755 debian/debug.init.d \
        debian/nginx/etc/init.d/nginx-debug
    /usr/bin/install -m 644 debian/nginx-debug.default \
        debian/nginx/etc/default/nginx-debug
    /usr/bin/install -m 644 debian/nginx.conf debian/nginx/etc/nginx/
    /usr/bin/install -m 644 conf/win-utf debian/nginx/etc/nginx/
    /usr/bin/install -m 644 conf/koi-utf debian/nginx/etc/nginx/
    /usr/bin/install -m 644 conf/koi-win debian/nginx/etc/nginx/
    /usr/bin/install -m 644 conf/mime.types debian/nginx/etc/nginx/
    /usr/bin/install -m 644 conf/scgi_params debian/nginx/etc/nginx/
    /usr/bin/install -m 644 conf/fastcgi_params debian/nginx/etc/nginx/
    /usr/bin/install -m 644 conf/uwsgi_params debian/nginx/etc/nginx/
    /usr/bin/install -m 644 html/index.html \
        debian/nginx/usr/share/nginx/html/
    /usr/bin/install -m 644 html/50x.html \
        debian/nginx/usr/share/nginx/html/
    /usr/bin/install -m 644 debian/nginx.vh.default.conf \
        debian/nginx/etc/nginx/conf.d/default.conf
    /usr/bin/install -m 755 objs/nginx  debian/nginx/usr/sbin/
    /usr/bin/install -m 755 objs/nginx-debug  debian/nginx/usr/sbin/
    cd debian/nginx/etc/nginx && /bin/ln -s \
        ../../usr/lib/nginx/modules modules && cd -

override_dh_gencontrol:
    for p in $(PKGS); do \
        if [ -e debian/$$p.version ]; then \
            dpkg-gencontrol -p$$p -ldebian/changelog -Tdebian/$$p.substvars -Pdebian/$$p -v`cat debian/$$p.version`~`lsb_release -cs`; \
        else \
            dpkg-gencontrol -p$$p -ldebian/changelog -Tdebian/$$p.substvars -Pdebian/$$p ; \
        fi ; \
    done

override_dh_clean:
    dh_clean
    rm -f debian/*init.d
3
  • how do you define a "section"? Is it something following a line ending := \ ? Dec 17, 2015 at 21:11
  • not really sure. I'll throw the whole "rules" file up on something and link here to it. few minutes
    – Kevin
    Dec 17, 2015 at 21:21
  • added it to jsfiddle (only because it was the only thing I could think of :) )
    – Kevin
    Dec 17, 2015 at 21:33

3 Answers 3

3

If it doesn't matter where in the list of options the new one goes, you could

sed '/_configure_flags *:=/ a\
                    --add-module=$(MODULESDIR)/ngx_pagespeed \\
' file
light_configure_flags := \
                    --add-module=$(MODULESDIR)/ngx_pagespeed \
                    $(common_configure_flags) \
                    --with-http_gzip_static_module \
...

To add after the "common_configure_flags" line, you could:

sed -r '
    # when the line ends with a backslash
    # add the new line with a backslash
    /\$\(common_configure_flags\)[[:blank:]]*\\$/ a\
                    --add-module=$(MODULESDIR)/ngx_pagespeed \\

    # when the line does not end with a backslash,
    # add a backslash, then
    # add the new line without a backslash
    /\$\(common_configure_flags\)[[:blank:]]*$/ {
            s/$/ \\/
            a\
                    --add-module=$(MODULESDIR)/ngx_pagespeed
    }
' file
4
  • yeah, I don't think it would matter. I have to build-out a new server to test this and @thrig's solution may be a few days
    – Kevin
    Dec 18, 2015 at 18:17
  • How would I do this to add it directly after the `$(common_configure_flags) ` line?
    – Kevin
    Dec 22, 2015 at 14:01
  • Changed to: sed '/common_configure_flags *:=/ a\ --add-module=$(MODULESDIR)/ngx_pagespeed \\ ' file worked like a champ. thanks a ton
    – Kevin
    Dec 22, 2015 at 14:24
  • Sorry to bring up an older topic, but this is no longer working. I'll post the new 'rules' in the quesiton
    – Kevin
    Oct 17, 2016 at 12:58
1

I needed to add multiple lines and could not get sed to work so used awk instead:

awk '/full_configure_flags :=/{print $0 RS \
"                        --add-dynamic-module=/opt/ngx_http_split_clients/ngx_http_split_clients_module.c" RS \
"                        --with-http_addition_module" RS \
"                        --with-http_sub_module" RS \
"                        --with-http_flv_module" RS \
"                        --with-http_mp4_module" RS \
"                        --with-http_gunzip_module" RS \
"                        --with-http_random_index_module" RS \
"                        --with-http_secure_link_module" RS \
"                        --with-http_stub_status_module" RS \
"                        --with-mail" RS \
"                        --with-mail_ssl_module" RS \
"                        --with-file-aio" RS \
"                        --with-ipv6" RS \
"                        --with-http_spdy_module" RS \
"                        --with-cc-opt";next}1' /opt/rebuildnginx/nginx-1.10.2/debian/rules

This SO post was useful when figuring out syntax.

1
  • Welcome to Unix.stackexchange! Jan 30, 2017 at 3:04
1

Some digging around points to the rules file being at present available here.

Since the package-specific configures appear to inherit via the $(common_configure_flags) statement, modifying only that block may suffice:

$ perl -00 -ple 's{$}{ \\\n\t\t\t--add-module=\$(MODULESDIR)/ngx_pagespeed} if m/common_configure_flags :=/' rules > nu
$ diff -u rules nu
--- rules       2015-12-17 14:49:12.000000000 -0800
+++ nu  2015-12-17 14:52:52.171942309 -0800
@@ -47,7 +47,8 @@
                        --with-http_ssl_module \
                        --with-http_stub_status_module \
                        --with-http_realip_module \
-                       --with-http_auth_request_module
+                       --with-http_auth_request_module \
+                       --add-module=$(MODULESDIR)/ngx_pagespeed

This solution treats the file contents in turns of paragraphs (-00 -p), and adds the necessary data at the end of each (and appropriate backwhacking, newline, and tabbery), but only for the paragraph that matches the common_configure_flags := bit. To target each section (instead of just the common one), one would need to fiddle around with the if regular expression as appropriate.

3
  • I apologize, but I cannot count on perl being installed, otherwise this is extremely viable, and probably more efficient than sed
    – Kevin
    Dec 22, 2015 at 14:02
  • Sorry I couldn't give you "points", if there was a way to split an acceptance, you would've gotten it too.
    – Kevin
    Dec 22, 2015 at 14:25
  • Sorry to bring up an older topic, but this is no longer working. I'll post the new 'rules' in the quesiton
    – Kevin
    Oct 17, 2016 at 12:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .