Build azure images, too

This commit is contained in:
Neil Hanlon 2022-07-08 17:29:32 -04:00
parent 767362aceb
commit 3cccd03d55
Signed by untrusted user: neil
GPG Key ID: 705BC21EC3C70F34
4 changed files with 74 additions and 6 deletions

View File

@ -2,3 +2,4 @@ __pycache__/
*.py[cod]
*$py.class
*.so
Containerfile*.devel

View File

@ -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", "--"]

View File

@ -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:

55
iso/empanadas/prep-azure.sh Executable file
View File

@ -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}"