Till Wegmueller
2021-11-29 2f1c14436212d63ae00db8878a2b7d26f1c90ab0
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() {
TW 58     for f in $(git diff --name-only HEAD..origin/oi/hipster | grep Makefile); do
59         echo "jenkinshelper: building for ${f%/*}..."
60         curpwd=$(pwd)
61         cd "${f%/*}" && gmake publish
62         rc=$?
63         cd "${curpwd}"
64         echo "jenkinshelper: done with ${f%/*} return code ${rc}"
65     done
f90860 66 }
TW 67
68 # prepare the pkg.depotd server instance, if an instance already
69 # exists for this branch, we will use that - otherwise create a
70 # fresh one
2adcba 71 stage_prepare_pkgdepotd() {
TW 72     # we need the platform for the path to the repo
73     platform=$(uname -p)
f90860 74
2adcba 75     # check if we already have this branch set up as a pkg.depotd:
2d9c60 76     if ! svcs "pkg/server:${BRANCH_NAME}" >/dev/null 2>&1; then
f90860 77
2adcba 78         # get highest port from ${HTTPCONF} to figure out the next free one
TW 79         nextport=$(($(nawk -F '[/:]' '{ print $7 }' <"${HTTPCONF}" | sort -n | tail -1) + 1))
80
81         # set-up a new pkg/server instance
82         svccfg -s pkg/server <<EOF
f90860 83 add ${BRANCH_NAME}
TW 84 select ${BRANCH_NAME}
85 addpg pkg application
86 setprop pkg/port=${nextport}
87 setprop pkg/readonly=true
88 setprop pkg/pkg_root=/
89 setprop pkg/inst_root=${WORKSPACE}/${platform}/repo
90 setprop pkg/proxy_base=${JENKINS_URL%:[0-9]*/}/${BRANCH_NAME}/
91 end
92 EOF
93
2adcba 94         # enable the new pkg/server instance
TW 95         svcadm enable pkg/server:${BRANCH_NAME}
96
97         # add the new proxy rule to our apache config
98         echo "ProxyPassMatch /${BRANCH_NAME}/(.*)\$ http://127.0.0.1:${nextport}/\$1 nocanon max=200" >>"${HTTPCONF}"
f90860 99
TW 100     fi
101
2adcba 102     # we need to refresh the repo:
TW 103     pkgrepo refresh -s ${WORKSPACE}/${platform}/repo
f90860 104
2adcba 105     # also restarting pkg.depotd does not hurt
TW 106     svcadm restart pkg/server:${BRANCH_NAME}
107
108     # graceful restart apache to reload config
109     svcadm refresh svc:/network/http:apache24
110 }
111
112 # cleanup the pkg.depotd server instance
113 stage_cleanup_pr() {
114     # we need the platform for the path to the repo
115     platform=$(uname -p)
116
117     if ! "svcs pkg/server:${BRANCH_NAME}" >/dev/null 2>&1; then
118         # disable the instance
119         svcadm disable pkg/server:${BRANCH_NAME}
120
121         # svcadm is a async operation thus sleep here
122         sleep 1
123
124         # remove instance from SMF
125         svccfg delete pkg/server:${BRANCH_NAME}
126
127         # Remove apache port
128         sed "/^ProxyPassMatch /${BRANCH_NAME}/d"
129
130         # graceful restart apache to reload config
131         svcadm refresh svc:/network/http:apache24
132     fi
f90860 133 }
TW 134
135 usage() {
2adcba 136     cat <<EOF
f90860 137 Usage:
TW 138 jenkinshelper.ksh [ -h | -p | -s <stage> ]
139           -h    show this help
140           -p    run only ONCE to initialize your environment
141           -s    followed by the stage to run, currently:
142                 setup, build_changed, prepare_pkgdepotd
143 EOF
2adcba 144     exit 0
f90860 145 }
TW 146
147 ## MAIN ##
148 # call this script with the stage as an argument to -s
149
2adcba 150 while getopts s:hp argv; do
TW 151     case ${argv} in
152     s) stage="stage_${OPTARG}" ;;
153     p) stage="prepare" ;;
154     h) usage ;;
155     esac
f90860 156 done
2adcba 157 shift $(expr ${OPTIND} - 1)
f90860 158
TW 159 # run requested stage
160 rt=0
161 ${stage} || rt=$?
162
163 # we are done
164 exit ${rt}