-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathrun-koji-container.sh
executable file
·167 lines (132 loc) · 5.35 KB
/
run-koji-container.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
set -eu
SHARE_DIR=/tmp/osbuild-composer-koji-test
koji_stop () {
echo "Shutting down containers, please wait..."
${CONTAINER_RUNTIME} stop org.osbuild.koji.koji || true
${CONTAINER_RUNTIME} rm org.osbuild.koji.koji || true
${CONTAINER_RUNTIME} stop org.osbuild.koji.kdc || true
${CONTAINER_RUNTIME} rm org.osbuild.koji.kdc || true
${CONTAINER_RUNTIME} stop org.osbuild.koji.postgres || true
${CONTAINER_RUNTIME} rm org.osbuild.koji.postgres || true
${CONTAINER_RUNTIME} network rm -f org.osbuild.koji || true
rm -rf "${SHARE_DIR}" || true
}
koji_clean_up_bad_start () {
# remember the exit code, so we can report it later
EXIT_CODE=$?
echo "Start failed, removing containers."
koji_stop
exit $EXIT_CODE
}
# helper to simplify sql queries to the postgres instance
psql_cmd () {
${CONTAINER_RUNTIME} exec org.osbuild.koji.postgres psql -U koji -d koji "$@"
}
# helper to simplify running commands in the kdc container
kdc_exec() {
${CONTAINER_RUNTIME} exec org.osbuild.koji.kdc "$@"
}
koji_start() {
local cert_dir
cert_dir="$1"
trap koji_clean_up_bad_start EXIT
# create a share directory which is used to share files between the host and containers
mkdir "${SHARE_DIR}"
cp "${cert_dir}/kojihub-key.pem" "${SHARE_DIR}/key.pem"
cp "${cert_dir}/kojihub-crt.pem" "${SHARE_DIR}/crt.pem"
cp "${cert_dir}/ca-crt.pem" "${SHARE_DIR}/ca-crt.pem"
${CONTAINER_RUNTIME} network create org.osbuild.koji
${CONTAINER_RUNTIME} run -d --name org.osbuild.koji.postgres --network org.osbuild.koji \
-e POSTGRES_USER=koji \
-e POSTGRES_PASSWORD=kojipass \
-e POSTGRES_DB=koji \
quay.io/osbuild/postgres:13-alpine
${CONTAINER_RUNTIME} run -d --name org.osbuild.koji.kdc \
--network org.osbuild.koji \
-v "${SHARE_DIR}:/share:z" \
-p 88:88/udp \
quay.io/osbuild/kdc:latest
# initialize krb pricipals and create keytabs for them
# HTTP/localhost@LOCAL for kojihub
kdc_exec kadmin.local -r LOCAL add_principal -randkey HTTP/localhost@LOCAL
kdc_exec kadmin.local -r LOCAL ktadd -k /share/koji.keytab HTTP/localhost@LOCAL
kdc_exec chmod 644 /share/koji.keytab
# osbuild-krb@LOCAL for koji clients
kdc_exec kadmin.local -r LOCAL add_principal -randkey osbuild-krb@LOCAL
kdc_exec kadmin.local -r LOCAL ktadd -k /share/client.keytab osbuild-krb@LOCAL
kdc_exec chmod 644 /share/client.keytab
${CONTAINER_RUNTIME} run -d --name org.osbuild.koji.koji --network org.osbuild.koji \
-v "${SHARE_DIR}:/share:z" \
-p 8080:80 \
-p 4343:443 \
-e POSTGRES_USER=koji \
-e POSTGRES_PASSWORD=kojipass \
-e POSTGRES_DB=koji \
-e POSTGRES_HOST=org.osbuild.koji.postgres \
quay.io/osbuild/koji:latest
# TODO: we need to wait for the database to be initialized here. A better method should be used.
sleep 10
${CONTAINER_RUNTIME} logs org.osbuild.koji.postgres
${CONTAINER_RUNTIME} logs org.osbuild.koji.koji
# create koji users
# kojiadmin/kojipass - admin
# osbuild/osbuildpass - regular user
# osbuild-krb: - regular user authenticated with Kerberos principal osbuild-krb@LOCAL
psql_cmd -c "insert into users (name, password, status, usertype) values ('kojiadmin', 'kojipass', 0, 0)" >/dev/null
psql_cmd -c "insert into user_perms (user_id, perm_id, creator_id) values (1, 1, 1)" >/dev/null
psql_cmd -c "insert into users (name, password, status, usertype) values ('osbuild', 'osbuildpass', 0, 0)" >/dev/null
psql_cmd -c "insert into users (name, status, usertype) values ('osbuild-krb', 0, 0)" >/dev/null
psql_cmd -c "insert into user_krb_principals (user_id, krb_principal) values (3, 'osbuild-krb@LOCAL')" >/dev/null
# create content generator osbuild, give osbuild and osbuild-krb users access to it
psql_cmd -c "insert into content_generator (name) values ('osbuild')" >/dev/null
psql_cmd -c "insert into cg_users (cg_id, user_id, creator_id, active) values (1, 2, 1, true), (1, 3, 1, true)" >/dev/null
# When the test upload a vhd.xz image to koji, it returns `koji.GenericError:
# multiple matches for file extension: vhd.xz`. It seems like the default
# schema is not valid for vhd.xz images because it contains two archive types
# for them which koji cannot handle. I reported this issue as
#
# https://pagure.io/koji/issue/3605
#
# This line works around that by removing one of the archive types, so koji
# isn't confused by two same records.
psql_cmd -c "delete from archivetypes where name='vhdx-compressed'" >/dev/null
echo "Containers are running, to stop them use:"
echo "$0 stop"
trap - EXIT
}
# check arguments
if [[ $# -lt 1 || ( "$1" != "start" && "$1" != "stop" ) ]]; then
cat <<DOC
usage: $0 command
Commands:
start - starts the koji containers,
optionally takes a directory with kojihub certificates as an argument
stop - stops and removes the koji containers
DOC
exit 3
fi
# this script must be run as root
if [ $UID != 0 ]; then
echo This script must be run as root.
exit 1
fi
# decide whether podman or docker should be used
if which podman 2>/dev/null >&2; then
CONTAINER_RUNTIME=podman
elif which docker 2>/dev/null >&2; then
CONTAINER_RUNTIME=docker
else
echo No container runtime found, install podman or docker.
exit 2
fi
if [ "$1" == "start" ]; then
cert_dir="/etc/osbuild-composer"
if [[ $# -eq 2 ]]; then
cert_dir="$2"
fi
koji_start "$cert_dir"
fi
if [ "$1" == "stop" ]; then
koji_stop
fi