Skip to content

Commit bdcfa44

Browse files
committed
lib: baseimage: factor out sudo and chroot calls into functions
Factoring out the calls on sudo and chroot into functions, which tidies up the code, eases debugging and clears the road for moving bootstrapping into container, in future. Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
1 parent c2939f3 commit bdcfa44

File tree

1 file changed

+43
-21
lines changed

1 file changed

+43
-21
lines changed

lib/baseimage.sh

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,35 @@ baseimage_info() {
77
info "[baseimage] $*"
88
}
99

10+
baseimage_builder_init() {
11+
BASEIMAGE_ROOTFS="$(cf_distro_debootstrap_chroot)"
12+
baseimage_info "builder type: sudo"
13+
baseimage_info "creating rootfs: ${BASEIMAGE_ROOTFS}"
14+
[ "${BASEIMAGE_ROOTFS}" ] || baseimage_die "no rootfs path"
15+
baseimage_builder_exec rm -Rf "${BASEIMAGE_ROOTFS}"
16+
baseimage_builder_exec mkdir -p "${BASEIMAGE_ROOTFS}" || baseimage_die "mkdir ${BASEIMAGE_ROOTFS}"
17+
}
18+
19+
baseimage_builder_exec() {
20+
baseimage_info "builder exec: $@"
21+
sudo "$@"
22+
return $?
23+
}
24+
25+
baseimage_builder_destroy() {
26+
[ "${BASEIMAGE_ROOTFS}" ] || baseimage_die "no rootfs path"
27+
baseimage_info "destroying rootfs: ${BASEIMAGE_ROOTFS}"
28+
baseimage_builder_exec rm -Rf "${BASEIMAGE_ROOTFS}"
29+
}
30+
31+
baseimage_rootfs_exec() {
32+
baseimage_info "exec in rootfs: $@"
33+
baseimage_builder_exec chroot "${BASEIMAGE_ROOTFS}" "$@"
34+
}
35+
1036
create_baseimage() {
1137
local baseimage_name=$(cf_distro_baseimage_name)
1238
local baseimage_id=$(docker_find_image "${baseimage_name}")
13-
local chroot_tmp=$(cf_distro_debootstrap_chroot)
1439
local tarball_tmp=$(cf_distro_debootstrap_tarball)
1540
local debootstra_args=""
1641

@@ -25,11 +50,6 @@ create_baseimage() {
2550
return $?
2651
fi
2752

28-
baseimage_info "no rootfs tarball: ${tarball_tmp} ... creating it"
29-
30-
sudo rm -Rf "${chroot_tmp}"
31-
sudo mkdir -p "${chroot_tmp}" || baseimage_die "mkdir ${chroot_tmp}"
32-
3353
local keyring=$(cf_distro_keyring)
3454

3555
[ "$DISTRO_ARCH" ] && debootstrap_args="$debootstrap_args --arch=$DISTRO_ARCH"
@@ -41,67 +61,69 @@ create_baseimage() {
4161

4262
[ "$DISTRO_SCRIPT" ] || DISTRO_SCRIPT="${DCK_BUILDPACKAGE_CFDIR}/bootstrap/$DISTRO_NAME"
4363

64+
baseimage_info "no rootfs tarball: ${tarball_tmp} ... creating it"
65+
66+
baseimage_builder_init
67+
4468
baseimage_info "running debootstrap: ${DISTRO_NAME} / ${DISTRO_ARCH}"
4569

46-
sudo debootstrap \
70+
baseimage_builder_exec debootstrap \
4771
$debootstrap_args \
4872
"${DISTRO_NAME}" \
49-
"${chroot_tmp}" \
73+
"${BASEIMAGE_ROOTFS}" \
5074
"${DISTRO_MIRROR}" \
5175
"${DISTRO_SCRIPT}" || baseimage_die "debootstrap"
5276

5377
baseimage_info "configure extra apt repos"
5478

5579
if [ "$DISTRO_APT_SOURCES" ]; then
56-
echo "$DISTRO_APT_SOURCES" | sudo chroot "${chroot_tmp}" bash -c 'cat >> /etc/apt/sources.list' || baseimage_die "failed adding apt sources"
80+
echo "$DISTRO_APT_SOURCES" | baseimage_rootfs_exec bash -c 'cat >> /etc/apt/sources.list' || baseimage_die "failed adding apt sources"
5781
fi
5882

5983
baseimage_info "installing extra packages"
6084

6185
if [ "$DISTRO_EXTRA_PACKAGES" ]; then
62-
sudo chroot "${chroot_tmp}" apt-get install -y $DISTRO_EXTRA_PACKAGES || baseimage_die "install extra packages: ${DISTRO_EXTRA_PACKAGES}"
86+
baseimage_rootfs_exec apt-get install -y $DISTRO_EXTRA_PACKAGES || baseimage_die "install extra packages: ${DISTRO_EXTRA_PACKAGES}"
6387
fi
6488

6589
baseimage_info "marking unneeded packages as auto"
6690

6791
for i in $DISTRO_MARK_AUTO ; do
68-
sudo chroot "${chroot_tmp}" apt-mark auto "$i"
92+
baseimage_rootfs_exec apt-mark auto "$i"
6993
done
7094

7195
baseimage_info "removing unneeded packages"
7296

73-
sudo chroot "${chroot_tmp}" apt-get autoremove -y
74-
sudo chroot "${chroot_tmp}" apt-get autoclean
75-
76-
sudo chroot "${chroot_tmp}" apt-get remove -y --purge $(sudo chroot "${chroot_tmp}" dpkg -l | grep "^rc" | awk '{print $2}' | tr '\n' ' ')
97+
baseimage_rootfs_exec apt-get autoremove -y
98+
baseimage_rootfs_exec apt-get autoclean
99+
baseimage_rootfs_exec apt-get remove -y --purge $(baseimage_rootfs_exec dpkg -l | grep "^rc" | awk '{print $2}' | tr '\n' ' ')
77100

78101
baseimage_info "extra package removal"
79102

80103
for i in $DISTRO_REMOVE_PACKAGES ; do
81104
baseimage_info " ... removing: $i"
82-
sudo chroot "${chroot_tmp}" dpkg --force-remove-essential --remove "$i"
83-
sudo chroot "${chroot_tmp}" dpkg --force-remove-essential --purge "$i"
105+
baseimage_rootfs_exec dpkg --force-remove-essential --remove "$i"
106+
baseimage_rootfs_exec dpkg --force-remove-essential --purge "$i"
84107
done
85108

86109
baseimage_info "removing unwanted files"
87110

88111
for i in $DISTRO_REMOVE_FILES ; do
89-
sudo chroot "${chroot_tmp}" rm -Rf "$i"
112+
baseimage_rootfs_exec rm -Rf "$i"
90113
done
91114

92115
baseimage_info "creating rootfs tarball"
93116

94117
mkdir -p `dirname "$tarball_tmp"` || baseimage_die "failed creating tarball dir"
95-
sudo tar -C $chroot_tmp -c . > "$tarball_tmp" || baseimage_die "failed taring chroot"
96-
sudo rm -Rf $chroot_tmp
118+
baseimage_builder_exec tar -C "${BASEIMAGE_ROOTFS}" -c . > "$tarball_tmp" || baseimage_die "failed taring chroot"
97119

98120
baseimage_info "importing docker image"
99121

100122
docker_import_tarball "${tarball_tmp}" "${baseimage_name}"
101123

102124
baseimage_info "cleanup"
103125

104-
sudo rm -Rf "$chroot_tmp"
126+
baseimage_builder_destroy
105127

106128
return $?
107129
}

0 commit comments

Comments
 (0)