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