Andreas Wacknitz
2024-04-05 88d415640d20174302d2d8eaa6b036dfaa19730f
commit | author | age
973b66 1 #!/bin/ksh
TW 2 # 2021-04-07 Olaf Bohlen <olbohlen@eenfach.de>
3 # 2021-11-30 Till Wegmueller <toasterson@gmail.com>
4 # instead of putting all this into the Jenkinsfile I decided to put the actual code in this script
5
6 set -ex
7
8 # global config
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() {
14     echo "jenkinshelper: preparing..."
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
24
25     cat >/etc/apache2/2.4/conf.d/00-proxy.conf <<EOF
26 LoadModule proxy_module libexec/mod_proxy.so
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
38
39     chown root:builders "${HTTPCONF}"
40     chmod 664 "${HTTPCONF}"
41     svcadm enable svc:/network/http:apache24
42 }
43
44 # setup oi-userland
45 stage_setup() {
46     if [ ! -f /etc/apache2/2.4/conf.d/00-proxy.conf ]; then
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
52 }
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)
57 stage_build_changed() {
184afb 58     echo "Last successful commit $1"
03bb12 59     worst=0
d49c44 60     for f in $(cat changed_files.txt | grep Makefile; exit 0); do
973b66 61         echo "jenkinshelper: building for ${f%/*}..."
TW 62         curpwd=$(pwd)
63         cd "${f%/*}" && COMPONENT_BUILD_ARGS=-j$(psrinfo -t -c) gmake publish
64         rc=$?
65         cd "${curpwd}"
66         echo "jenkinshelper: done with ${f%/*} return code ${rc}"
03bb12 67         if [ rc -ne 0 ] ; then
JK 68             worst=$rc
69         fi
973b66 70     done
03bb12 71     return $worst
973b66 72 }
TW 73
74 # prepare the pkg.depotd server instance, if an instance already
75 # exists for this branch, we will use that - otherwise create a
76 # fresh one
77 stage_prepare_pkgdepotd() {
78     # we need the platform for the path to the repo
79     platform=$(uname -p)
80
81     # check if we already have this branch set up as a pkg.depotd:
82     if ! svcs "pkg/server:${BRANCH_NAME}" >/dev/null 2>&1; then
83
84         # get highest port from ${HTTPCONF} to figure out the next free one
85         nextport=$(($(nawk -F '[/:]' '{ print $7 }' <"${HTTPCONF}" | sort -n | tail -1) + 1))
86
87         # set-up a new pkg/server instance
88         svccfg -s pkg/server <<EOF
89 add ${BRANCH_NAME}
90 select ${BRANCH_NAME}
91 addpg pkg application
92 setprop pkg/port=${nextport}
93 setprop pkg/readonly=true
94 setprop pkg/pkg_root=/
95 setprop pkg/inst_root=${WORKSPACE}/${platform}/repo
96 setprop pkg/proxy_base=${JENKINS_URL%:[0-9]*/}/${BRANCH_NAME}/
97 end
98 EOF
99
100         # enable the new pkg/server instance
101         svcadm enable pkg/server:${BRANCH_NAME}
102
103         # add the new proxy rule to our apache config
104         echo "ProxyPassMatch /${BRANCH_NAME}/(.*)\$ http://127.0.0.1:${nextport}/\$1 nocanon max=200" >>"${HTTPCONF}"
105
106     fi
107
108     # we need to refresh the repo:
109     pkgrepo refresh -s ${WORKSPACE}/${platform}/repo
110
111     # also restarting pkg.depotd does not hurt
112     svcadm restart pkg/server:${BRANCH_NAME}
113
114     # graceful restart apache to reload config
115     svcadm refresh svc:/network/http:apache24
116 }
117
118 # cleanup the pkg.depotd server instance
119 stage_cleanup_pr() {
120     # we need the platform for the path to the repo
121     platform=$(uname -p)
122
123     if ! "svcs pkg/server:${BRANCH_NAME}" >/dev/null 2>&1; then
124         # disable the instance
125         svcadm disable pkg/server:${BRANCH_NAME}
126
127         # svcadm is a async operation thus sleep here
128         sleep 1
129
130         # remove instance from SMF
131         svccfg delete pkg/server:${BRANCH_NAME}
132
133         # Remove apache port
134         sed "/^ProxyPassMatch /${BRANCH_NAME}/d"
135
136         # graceful restart apache to reload config
137         svcadm refresh svc:/network/http:apache24
138     fi
139 }
140
141 usage() {
142     cat <<EOF
143 Usage:
144 jenkinshelper.ksh [ -h | -p | -s <stage> ]
145           -h    show this help
146           -p    run only ONCE to initialize your environment
147           -s    followed by the stage to run, currently:
148                 setup, build_changed, prepare_pkgdepotd
149 EOF
150     exit 0
151 }
152
153 ## MAIN ##
154 # call this script with the stage as an argument to -s
155
156 while getopts s:hp argv; do
157     case ${argv} in
158     s) stage="stage_${OPTARG}" ;;
159     p) stage="prepare" ;;
160     h) usage ;;
161     esac
162 done
163 shift $(expr ${OPTIND} - 1)
164
165 # run requested stage
166 rt=0
167 ${stage} || rt=$?
168
169 # we are done
170 exit ${rt}