From 3cccd03d55a0910a34e02a5709de53938348f347 Mon Sep 17 00:00:00 2001 From: Neil Hanlon Date: Fri, 8 Jul 2022 17:29:32 -0400 Subject: [PATCH] Build azure images, too --- iso/empanadas/.gitignore | 1 + iso/empanadas/Containerfile.imagefactory | 8 +-- .../empanadas/scripts/build_image.py | 16 +++++- iso/empanadas/prep-azure.sh | 55 +++++++++++++++++++ 4 files changed, 74 insertions(+), 6 deletions(-) create mode 100755 iso/empanadas/prep-azure.sh diff --git a/iso/empanadas/.gitignore b/iso/empanadas/.gitignore index 961321b..6059633 100644 --- a/iso/empanadas/.gitignore +++ b/iso/empanadas/.gitignore @@ -2,3 +2,4 @@ __pycache__/ *.py[cod] *$py.class *.so +Containerfile*.devel diff --git a/iso/empanadas/Containerfile.imagefactory b/iso/empanadas/Containerfile.imagefactory index f4f91cb..aa34ac2 100644 --- a/iso/empanadas/Containerfile.imagefactory +++ b/iso/empanadas/Containerfile.imagefactory @@ -60,13 +60,11 @@ RUN pip install awscli ENV BRANCH r9 RUN git clone https://git.resf.org/sig_core/kickstarts.git --branch $BRANCH /kickstarts -# devel only -# COPY . /empanadas -# RUN pip install -e /empanadas - -# prod RUN pip install 'git+https://git.resf.org/sig_core/toolkit.git@devel#egg=empanadas&subdirectory=iso/empanadas' ENV LIBGUESTFS_BACKEND direct +COPY prep-azure.sh /prep-azure.sh +RUN chmod +x /prep-azure.sh + ENTRYPOINT ["/tini", "--"] diff --git a/iso/empanadas/empanadas/scripts/build_image.py b/iso/empanadas/empanadas/scripts/build_image.py index 817e5f2..f6ab2b4 100644 --- a/iso/empanadas/empanadas/scripts/build_image.py +++ b/iso/empanadas/empanadas/scripts/build_image.py @@ -106,6 +106,13 @@ class ImageBuild: self.stage_commands = [ ["qemu-img", "convert", "-f", "raw", "-O", "qcow2", lambda: f"{STORAGE_DIR}/{self.target_uuid}.body", f"{self.outdir}/{self.outname}.qcow2"] ] + if self.image_type in ["Azure"]: + self.stage_commands = [ + ["/empanadas/prep-azure.sh", lambda: f"{STORAGE_DIR}/{self.target_uuid}.body", f"{STORAGE_DIR}"], + ["cp", lambda: f"{STORAGE_DIR}/{self.target_uuid}.vhd", f"{self.outdir}/{self.outname}.vhd"] + ] + # ["qemu-img", "resize", "-f", "raw", lambda: f"{STORAGE_DIR}/{self.target_uuid}.body", lambda: f"{self.rounded_size()}"], + # ["qemu-img", "convert", "-f", "raw", "-o", "subformat=fixed,force_size" ,"-O", "vpc", lambda: f"{STORAGE_DIR}/{self.target_uuid}.body", f"{self.outdir}/{self.outname}.vhd"] if self.image_type in ["Vagrant"]: _map = { "VBox": "vmdk", @@ -136,6 +143,13 @@ class ImageBuild: finally: f.flush() + # def rounded_size(self) -> int: + # # Azure images need to be rounded to the nearest 1MB boundary. + # MB=1024*1024 + # + # raw_size = pathlib.Path(STORAGE_DIR},f"{self.target_uuid}.body").stat().st_size + # rounded_size = raw + def output_name(self) -> Tuple[pathlib.Path, str]: directory = f"Rocky-{self.architecture.major}-{self.type_variant}-{self.architecture.version}-{BUILDTIME.strftime('%Y%m%d')}.{self.release}" name = f"{directory}.{self.architecture.name}" @@ -245,7 +259,7 @@ class ImageBuild: def package(self) -> int: # Some build types don't need to be packaged by imagefactory # @TODO remove business logic if possible - if self.image_type in ["GenericCloud", "EC2"]: + if self.image_type in ["GenericCloud", "EC2", "Azure"]: self.target_uuid = self.base_uuid if hasattr(self, 'base_uuid') else "" if self.target_uuid: diff --git a/iso/empanadas/prep-azure.sh b/iso/empanadas/prep-azure.sh new file mode 100755 index 0000000..4f45658 --- /dev/null +++ b/iso/empanadas/prep-azure.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +usage() { + cat << EOF +$0: prep raw image for azure + +usage: $0 raw_image + +Description: Takes a raw image and calculates the closest whole-MegaByte, +resizing a copy of the raw image, and returning the path to the resize 'vpc' +image (a .vhd file to upload) + +Dumps VHD in \$PWD by default. Override with ``OUTDIR=/path/to/outdir`` + +Don't try to compress it. +EOF +} + +log() { + local level="$1"; shift + local msg="$@" + local out=$([ "$level" == "error" ] && echo 2 || echo 1) + printf "[%s] %s: %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "${level}" "${msg}" >&${out} + if [[ "${level}" == "error" ]]; then + exit + fi +} + +MB=$((1024*1024)) # for calculations - 1048576 bytes + +if ! command -v qemu-img 2>&1 >/dev/null; then + log error "Need qemu-img."; + usage + exit +fi + +rawdisk="$1" + +if [[ -z "$rawdisk" ]]; then + usage + log error "need path to a raw image to prep" +fi + +outdir="${2:-${PWD}}" + +size=$(qemu-img info -f raw --output json "${rawdisk}" | gawk 'match($0, /"virtual-size": ([0-9]+),/, val) {print val[1]}') + +rounded_size=$(((($size+$MB-1)/$MB)*$MB)) # size (in bytes) + 1MB, less one, and rounded. + +outfilename=$(basename ${rawdisk//body/vhd}) +outfile="${outdir}/${outfilename}" +qemu-img resize -f raw "${rawdisk}" "${rounded_size}" || log error "failed to resize" +qemu-img convert -f raw -o subformat=fixed,force_size -O vpc "${rawdisk}" "${outfile}" || log error "failed to convert to VHD format" + +echo "${outfile}"