Till Wegmueller
2021-11-29 2adcba05392a63f9a586ad7916deffb8f2086aae
commit | author | age
f90860 1 #!/bin/ksh
TW 2 # 2021-04-07 Olaf Bohlen <olbohlen@eenfach.de>
3 # instead of putting all this into the Jenkinsfile I decided to put the actual code in this script
4
5 # global config
6 HTTPCONF="/etc/apache2/2.4/conf.d/pkgdepotd.conf"
7
8 # just run prepare once to initially set up the environment
9 # this must be run as root
10 prepare() {
2adcba 11     echo "jenkinshelper: preparing..."
TW 12     pkg install web/server/apache-24
13     mkdir -p /etc/apache2/2.4/conf.d && chown root:sys /etc/apache2/2.4/conf.d
14     grep "#ProxyPassMatch /default/(.*)\$ http://127.0.0.1:10000/\$1 nocanon max=200" "${HTTPCONF}" >/dev/null 2>&1
15     if [ $? -gt 0 ]; then
16         echo "jenkinshelper: Initializing a new apache config at ${HTTPCONF}"
17         echo "#ProxyPassMatch /default/(.*)\$ http://127.0.0.1:10000/\$1 nocanon max=200" >"${HTTPCONF}"
18     else
19         echo "jenkinshelper: Preserving an existing ${HTTPCONF}"
20     fi
f90860 21
2adcba 22     cat >/etc/apache2/2.4/conf.d/00-proxy.conf <<EOF
f90860 23 LoadModule proxy_module libexec/mod_proxy.so
TW 24 LoadModule proxy_connect_module libexec/mod_proxy_connect.so
25 LoadModule proxy_ftp_module libexec/mod_proxy_ftp.so
26 LoadModule proxy_http_module libexec/mod_proxy_http.so
27 LoadModule proxy_ajp_module libexec/mod_proxy_ajp.so
28
29 ProxyRequests Off
30 ProxyVia Off
31 ProxyPreserveHost on
32 RequestHeader unset Origin
33 AllowEncodedSlashes NoDecode
34 EOF
2adcba 35
TW 36     chown root:builders "${HTTPCONF}"
37     chmod 664 "${HTTPCONF}"
38     svcadm enable svc:/network/http:apache24
f90860 39 }
TW 40
41 # setup oi-userland
42 stage_setup() {
2adcba 43     if [ ! -f /etc/apache2/2.4/conf.d/00-proxy.conf ]; then
TW 44         echo "jenkinshelper: aborting, please run \"jenkinshelper -p\" initially as root on this jenkins instance once"
45         exit 1
46     fi
47     echo "jenkinshelper: running gmake setup"
48     gmake setup
f90860 49 }
TW 50
51 # scan the git log for changed components
52 # we try to be smart and assume that all updates will always touch
53 # the components Makefile also (to update COMPONENT_REVISION, etc)
2adcba 54 stage_build_changed() {
TW 55     for f in $(git diff --name-only HEAD..origin/oi/hipster | grep Makefile); do
56         echo "jenkinshelper: building for ${f%/*}..."
57         curpwd=$(pwd)
58         cd "${f%/*}" && gmake publish
59         rc=$?
60         cd "${curpwd}"
61         echo "jenkinshelper: done with ${f%/*} return code ${rc}"
62     done
f90860 63 }
TW 64
65 # prepare the pkg.depotd server instance, if an instance already
66 # exists for this branch, we will use that - otherwise create a
67 # fresh one
2adcba 68 stage_prepare_pkgdepotd() {
TW 69     # we need the platform for the path to the repo
70     platform=$(uname -p)
f90860 71
2adcba 72     # check if we already have this branch set up as a pkg.depotd:
TW 73     if ! "svcs pkg/server:${BRANCH_NAME}" >/dev/null 2>&1; then
f90860 74
2adcba 75         # get highest port from ${HTTPCONF} to figure out the next free one
TW 76         nextport=$(($(nawk -F '[/:]' '{ print $7 }' <"${HTTPCONF}" | sort -n | tail -1) + 1))
77
78         # set-up a new pkg/server instance
79         svccfg -s pkg/server <<EOF
f90860 80 add ${BRANCH_NAME}
TW 81 select ${BRANCH_NAME}
82 addpg pkg application
83 setprop pkg/port=${nextport}
84 setprop pkg/readonly=true
85 setprop pkg/pkg_root=/
86 setprop pkg/inst_root=${WORKSPACE}/${platform}/repo
87 setprop pkg/proxy_base=${JENKINS_URL%:[0-9]*/}/${BRANCH_NAME}/
88 end
89 EOF
90
2adcba 91         # enable the new pkg/server instance
TW 92         svcadm enable pkg/server:${BRANCH_NAME}
93
94         # add the new proxy rule to our apache config
95         echo "ProxyPassMatch /${BRANCH_NAME}/(.*)\$ http://127.0.0.1:${nextport}/\$1 nocanon max=200" >>"${HTTPCONF}"
f90860 96
TW 97     fi
98
2adcba 99     # we need to refresh the repo:
TW 100     pkgrepo refresh -s ${WORKSPACE}/${platform}/repo
f90860 101
2adcba 102     # also restarting pkg.depotd does not hurt
TW 103     svcadm restart pkg/server:${BRANCH_NAME}
104
105     # graceful restart apache to reload config
106     svcadm refresh svc:/network/http:apache24
107 }
108
109 # cleanup the pkg.depotd server instance
110 stage_cleanup_pr() {
111     # we need the platform for the path to the repo
112     platform=$(uname -p)
113
114     if ! "svcs pkg/server:${BRANCH_NAME}" >/dev/null 2>&1; then
115         # disable the instance
116         svcadm disable pkg/server:${BRANCH_NAME}
117
118         # svcadm is a async operation thus sleep here
119         sleep 1
120
121         # remove instance from SMF
122         svccfg delete pkg/server:${BRANCH_NAME}
123
124         # Remove apache port
125         sed "/^ProxyPassMatch /${BRANCH_NAME}/d"
126
127         # graceful restart apache to reload config
128         svcadm refresh svc:/network/http:apache24
129     fi
f90860 130 }
TW 131
132 usage() {
2adcba 133     cat <<EOF
f90860 134 Usage:
TW 135 jenkinshelper.ksh [ -h | -p | -s <stage> ]
136           -h    show this help
137           -p    run only ONCE to initialize your environment
138           -s    followed by the stage to run, currently:
139                 setup, build_changed, prepare_pkgdepotd
140 EOF
2adcba 141     exit 0
f90860 142 }
TW 143
144 ## MAIN ##
145 # call this script with the stage as an argument to -s
146
2adcba 147 while getopts s:hp argv; do
TW 148     case ${argv} in
149     s) stage="stage_${OPTARG}" ;;
150     p) stage="prepare" ;;
151     h) usage ;;
152     esac
f90860 153 done
2adcba 154 shift $(expr ${OPTIND} - 1)
f90860 155
TW 156 # run requested stage
157 rt=0
158 ${stage} || rt=$?
159
160 # we are done
161 exit ${rt}