Skip to content

Commit 2b9c9af

Browse files
authored
Update stemcell-repack-debugger (#452)
* Always remove all created temp dirs * Add variable to write patched stemcell to a different file * Add the invisible but obvious option to choose a different tmp dir * Make script compatible with both Jammy and Noble * Fix typo in script file name * Add manual version setting and autobump based on parameter or output tgz * Replace yq with bosh int * Replace yq with bosh int
1 parent 27c1ccd commit 2b9c9af

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed
Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ show_help() {
99
echo " --help, -h Show this help message and exit"
1010
echo " --debug Enable debug mode"
1111
echo " --debug_pub_key FILE Set a multiline public key file"
12-
echo " --bump-version Bump the version in the stemcell with 0.0.<timestamp>"
12+
echo " --bump-version[=version] Bump the version in the stemcell to a dev version that will be increased if you use the same stemcell tgz, e.g. <version>+dev.<int>"
13+
echo " The optional version can be used instead of the stemcell version and increased if it is already a dev version"
14+
echo " If als OUT is specified and the file is present and a tgz containing stemcell.MF, the version information is automatically used"
1315
echo
1416
echo "Environment Variables:"
1517
echo " stemcell_tgz Path to the stemcell tarball *REQUIRED*"
1618
echo " AGENT_BINARY Path to the agent binary to be copied into the stemcell *OPTIONAL*"
1719
echo " AGENT_JSON Path to the agent JSON configuration file *OPTIONAL*"
1820
echo " BOSH_DEBUG_PUB_KEY Public key for the BOSH debug user in a single line *OPTIONAL*"
21+
echo " OUT Path to the patched stemcell tarball *OPTIONAL*"
22+
echo " TMPDIR Path with enough disk space to hold all temporary files for repacking *OPTIONAL*"
1923
echo
2024
echo "Example:"
2125
echo " export stemcell_tgz=\"/home/username/workspace/bosh/bosh-linux-stemcell-builder/tmp/bosh-stemcell-0.0.8-google-kvm-ubuntu-noble-go_agent.tgz\""
@@ -48,8 +52,13 @@ for arg in "$@"; do
4852
convert_cert_file="$2"
4953
shift 2
5054
;;
51-
--bump-version)
55+
--bump-version*)
5256
bump_version=true
57+
if [ -n "$OUT" -a -f "$OUT" ]; then
58+
version=$(bosh int <(tar xf "$OUT" stemcell.MF --occurrence=1 -O) --path /version)
59+
else
60+
version=$(echo "$arg" | awk -F'=' '{print $2}')
61+
fi
5362
shift
5463
;;
5564
*)
@@ -73,23 +82,39 @@ temp_dir=$(mktemp -d)
7382
stemcell_dir=${temp_dir}/stemcell
7483
image_dir=${temp_dir}/image
7584
mkdir -p $stemcell_dir $image_dir
76-
trap 'rm -rf "${temp_dir}"' EXIT
85+
traps='rm -rf "${temp_dir}";'
86+
trap "$traps" EXIT
7787

7888
# Repack stemcell
7989
cd $stemcell_dir
8090
tar xvf $stemcell_tgz
81-
new_ver=`date +%s`
8291

8392
# Update stemcell with new agent
8493
cd $image_dir
8594
tar xvf $stemcell_dir/image
95+
DISK_NAME=disk.raw
96+
[ -f root.img ] && DISK_NAME=root.img
8697
mnt_dir=$(mktemp -d)
87-
trap 'rm -rf "${mnt_dir}"' EXIT
88-
device=$(sudo kpartx -sav disk.raw | grep '^add' | tail -n1 | cut -d' ' -f3)
98+
traps+='rm -rf "${mnt_dir}";'
99+
trap "${traps}" EXIT
100+
device=$(sudo kpartx -sav $DISK_NAME | grep '^add' | tail -n1 | cut -d' ' -f3)
89101
sudo mount -o loop,rw /dev/mapper/$device $mnt_dir
90102

91103
if [ "$bump_version" = true ]; then
92-
echo -n "0.0.${new_ver}" | sudo tee "$mnt_dir/var/vcap/bosh/etc/stemcell_version"
104+
[ -z "$version" ] && version=$(bosh int $stemcell_dir/stemcell.MF --path /version)
105+
base_version=$(echo -n "$version" | awk -F'+' '{print $1}')
106+
dev_version=$(echo -n "$version" | awk -F'+' '{print $2}')
107+
if [ -n "$dev_version" ]; then
108+
dev_version=$(echo -n "$dev_version" | sed -e 's/dev[.]//')
109+
dev_version=$((dev_version+1))
110+
else
111+
dev_version=1
112+
fi
113+
new_version="${base_version}+dev.${dev_version}"
114+
sudo cat "$mnt_dir/var/vcap/bosh/etc/stemcell_version"
115+
echo -n "${new_version}" | sudo tee "$mnt_dir/var/vcap/bosh/etc/stemcell_version"
116+
sudo cat "$mnt_dir/var/vcap/bosh/etc/stemcell_version"
117+
sed -r -i "s/^([[:space:]]*)version: .*/\1version: ${new_version}/" $stemcell_dir/stemcell.MF
93118
fi
94119

95120
if [ -n "$AGENT_BINARY" ]; then
@@ -112,14 +137,12 @@ EOF
112137
fi
113138

114139
sudo umount $mnt_dir
115-
sudo kpartx -dv disk.raw
140+
sudo kpartx -dv "$DISK_NAME"
116141

117142
tar czvf $stemcell_dir/image *
118143

119144
cd $stemcell_dir
120-
if [ "$bump_version" = true ]; then
121-
sed -i.bak "s/version: .*/version: 0.0.${new_ver}/" stemcell.MF
122-
fi
123-
tar czvf $stemcell_tgz *
145+
# Change the order of how files are compressed to speed up version detection
146+
tar czvf ${OUT:-$stemcell_tgz} $(du -b * | sort -n | awk '{print $2}')
124147

125-
echo "ALL DONE!!!"
148+
echo "ALL DONE!!!"

0 commit comments

Comments
 (0)