1
0
mirror of https://github.com/gryf/boxpy.git synced 2025-12-18 21:10:17 +01:00

Add experimental Debian support.

Note, there is something weird with those images, as the will segfault
on the first run (I've checked that with latest Debian 11 images, maybe
it will change in the future), so there is forced reboot after certain
amount of time as a workaround.
This commit is contained in:
2023-04-18 21:48:16 +02:00
parent 15a6ecb540
commit b2457d497e
2 changed files with 55 additions and 9 deletions

View File

@@ -2,8 +2,8 @@
box.py box.py
====== ======
Box.py is a simple automation tool meant to run Ubuntu, Fedora or Centos Stream Box.py is a simple automation tool meant to run Ubuntu, Fedora, Centos Stream
cloud images on top of VirtualBox. or Debian cloud images on top of VirtualBox.
What it does is simply download official cloud image, set up VM, tweak it up What it does is simply download official cloud image, set up VM, tweak it up
and do the initial pre-configuration using generated config drive. and do the initial pre-configuration using generated config drive.
@@ -16,7 +16,7 @@ weird named options for ``vboxmanage`` ;P)
Requirements Requirements
------------ ------------
- Python >=3.7 - Python >=3.8
- `pyyaml`_ - `pyyaml`_
- `requests`_ - `requests`_
@@ -24,7 +24,7 @@ Requirements
- Virtualbox (obviously) - Virtualbox (obviously)
- ``mkisofs`` or ``genisoimage`` command for generating ISO image - ``mkisofs`` or ``genisoimage`` command for generating ISO image
- ``wget`` command for fetching images - ``wget`` command for fetching images
- ``sha256sum`` command for checksum check - ``sha256sum`` and ``sha512sum`` commands for checksum check
- ``qemu-img`` from *qemu-utils* package command for converting between images - ``qemu-img`` from *qemu-utils* package command for converting between images
formats formats

56
box.py
View File

@@ -23,9 +23,10 @@ __version__ = "1.3"
CACHE_DIR = os.environ.get('XDG_CACHE_HOME', os.path.expanduser('~/.cache')) CACHE_DIR = os.environ.get('XDG_CACHE_HOME', os.path.expanduser('~/.cache'))
CLOUD_IMAGE = "ci.iso" CLOUD_IMAGE = "ci.iso"
FEDORA_RELEASE_MAP = {'32': '1.6', '33': '1.2', '34': '1.2'} FEDORA_RELEASE_MAP = {'32': '1.6', '33': '1.2', '34': '1.2'}
DEBIAN_CODENAME_MAP = {'12': 'bookworm', '11': 'bullseye', '10': 'buster'}
TYPE_MAP = {'HardDisk': 'disk', 'DVD': 'dvd', 'Floppy': 'floppy'} TYPE_MAP = {'HardDisk': 'disk', 'DVD': 'dvd', 'Floppy': 'floppy'}
DISTRO_MAP = {'ubuntu': 'Ubuntu', 'fedora': 'Fedora', DISTRO_MAP = {'ubuntu': 'Ubuntu', 'fedora': 'Fedora',
'centos': 'Centos Stream'} 'centos': 'Centos Stream', 'debian': 'Debian'}
META_DATA_TPL = string.Template('''\ META_DATA_TPL = string.Template('''\
instance-id: $instance_id instance-id: $instance_id
local-hostname: $vmhostname local-hostname: $vmhostname
@@ -160,8 +161,8 @@ _boxpy() {
_ssh_identityfile _ssh_identityfile
;; ;;
--distro) --distro)
COMPREPLY=( $(compgen -W "ubuntu fedora centos" \ COMPREPLY=( $(compgen -W "ubuntu fedora centos
-- ${cur}) ) debian" -- ${cur}) )
;; ;;
--type) --type)
COMPREPLY=( $(compgen -W "gui headless sdl separate" \ COMPREPLY=( $(compgen -W "gui headless sdl separate" \
@@ -592,6 +593,9 @@ class OsTypes:
def fedora(conf): def fedora(conf):
return "Fedora_64" return "Fedora_64"
def debian(conf):
return "Debian%s_64" % conf.version
class VBoxManage: class VBoxManage:
""" """
@@ -930,6 +934,7 @@ class VBoxManage:
class Image: class Image:
URL = "" URL = ""
IMG = "" IMG = ""
CHECKSUMTOOL = 'sha256sum'
def __init__(self, vbox, version, arch, release, fname=None): def __init__(self, vbox, version, arch, release, fname=None):
self.vbox = vbox self.vbox = vbox
@@ -980,7 +985,7 @@ class Image:
return False return False
if os.path.exists(os.path.join(CACHE_DIR, self._img_fname)): if os.path.exists(os.path.join(CACHE_DIR, self._img_fname)):
cmd = ['sha256sum', os.path.join(CACHE_DIR, self._img_fname)] cmd = [self.CHECKSUMTOOL, os.path.join(CACHE_DIR, self._img_fname)]
calulated_sum = Run(cmd).stdout.split(' ')[0] calulated_sum = Run(cmd).stdout.split(' ')[0]
LOG.details('Checksum for image: %s, expected: %s', calulated_sum, LOG.details('Checksum for image: %s, expected: %s', calulated_sum,
expected_sum) expected_sum)
@@ -1033,6 +1038,30 @@ class Ubuntu(Image):
return expected_sum return expected_sum
class Debian(Image):
URL = "https://cloud.debian.org/images/cloud/%s/daily/latest/%s"
IMG = "debian-%s-generic-%s-daily.qcow2"
CHECKSUMTOOL = 'sha512sum'
def __init__(self, vbox, version, arch, release, fname=None):
super().__init__(vbox, version, arch, release)
self._img_fname = self.IMG % (version, arch)
self._img_url = self.URL % (release, self._img_fname)
self._checksum_file = 'SHA512SUMS'
self._checksum_url = self.URL % (release, self._checksum_file)
def _get_checksum(self, fname):
expected_sum = None
Run(['wget', self._checksum_url, '-q', '-O', fname])
with open(fname) as fobj:
for line in fobj.readlines():
if self._img_fname in line:
expected_sum = line.split(' ')[0]
break
return expected_sum
class Fedora(Image): class Fedora(Image):
URL = ("https://download.fedoraproject.org/pub/fedora/linux/releases/%s/" URL = ("https://download.fedoraproject.org/pub/fedora/linux/releases/%s/"
"Cloud/%s/images/%s") "Cloud/%s/images/%s")
@@ -1131,13 +1160,20 @@ DISTROS = {'ubuntu': {'username': 'ubuntu',
'realname': 'centos', 'realname': 'centos',
'img_class': CentosStream, 'img_class': CentosStream,
'amd64': 'x86_64', 'amd64': 'x86_64',
'default_version': '8'}} 'default_version': '8'},
'debian': {'username': 'debian',
'realname': 'debian',
'img_class': Debian,
'amd64': 'amd64',
'default_version': '11'}}
def get_image_object(vbox, version, image='ubuntu', arch='amd64'): def get_image_object(vbox, version, image='ubuntu', arch='amd64'):
release = None release = None
if image == 'fedora': if image == 'fedora':
release = FEDORA_RELEASE_MAP[version] release = FEDORA_RELEASE_MAP[version]
if image == 'debian':
release = DEBIAN_CODENAME_MAP[version]
return DISTROS[image]['img_class'](vbox, version, DISTROS[image]['amd64'], return DISTROS[image]['img_class'](vbox, version, DISTROS[image]['amd64'],
release, DISTROS[image].get('image')) release, DISTROS[image].get('image'))
@@ -1263,6 +1299,7 @@ def vmcreate(args, conf=None):
f'ssh://{username}@localhost:{vbox.vm_info["port"]}', f'ssh://{username}@localhost:{vbox.vm_info["port"]}',
'sudo cloud-init status'] 'sudo cloud-init status']
try: try:
counter = 0
while True: while True:
out = Run(cmd) out = Run(cmd)
LOG.debug('Out: %s', out.stdout) LOG.debug('Out: %s', out.stdout)
@@ -1282,6 +1319,15 @@ def vmcreate(args, conf=None):
f'VM with ssh for user {username}. ' f'VM with ssh for user {username}. '
f'Check output in debug mode.') f'Check output in debug mode.')
time.sleep(3) time.sleep(3)
counter += 1
if counter == 8 and conf.distro == 'debian':
counter += 1
# there is something odd with debian cloud images, as they
# segfault on first run. after ~20 seconds there should
# already be panic, reset machine should help
vbox.poweroff()
time.sleep(3)
vbox.poweron(args.type)
continue continue
LOG.info(' done') LOG.info(' done')