1
0
mirror of https://github.com/gryf/boxpy.git synced 2025-12-19 05:30:18 +01:00

Added fedora cloud to supported distros.

This commit is contained in:
2021-06-04 18:39:10 +02:00
parent de61390d5e
commit 1a058a1e2a

72
box.py
View File

@@ -17,6 +17,7 @@ import yaml
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'}
META_DATA_TPL = string.Template('''\ META_DATA_TPL = string.Template('''\
instance-id: $instance_id instance-id: $instance_id
local-hostname: $vmhostname local-hostname: $vmhostname
@@ -42,7 +43,6 @@ boxpy_data:
key: ~/.ssh/id_rsa key: ~/.ssh/id_rsa
memory: 2048 memory: 2048
port: 2222 port: 2222
version: 20.04
''' '''
COMPLETIONS = {'bash': '''\ COMPLETIONS = {'bash': '''\
_boxpy() { _boxpy() {
@@ -670,39 +670,77 @@ class Ubuntu(Image):
print(f'Downloaded image {self._img_fname}') print(f'Downloaded image {self._img_fname}')
def _convert_to_raw(self): class Fedora(Image):
img_path = os.path.join(CACHE_DIR, self._img) URL = ("https://download.fedoraproject.org/pub/fedora/linux/releases/%s/"
raw_path = os.path.join(self._tmp, self._img + ".raw") "Cloud/%s/images/%s")
if subprocess.call(['qemu-img', 'convert', '-O', 'raw', IMG = "Fedora-Cloud-Base-%s-%s.%s.qcow2"
img_path, raw_path]) != 0: CHKS = "Fedora-Cloud-%s-%s-%s-CHECKSUM"
raise BoxConvertionError(f'Cannot convert image {self._img} to '
'RAW.') def __init__(self, vbox, version, arch, release):
super().__init__(vbox, version, arch, release)
self._img_fname = self.IMG % (version, release, arch)
self._img_url = self.URL % (version, arch, self._img_fname)
self._checksum_file = self.CHKS % (version, release, arch)
self._checksum_url = self.URL % (version, arch, self._checksum_file)
def _checksum(self):
"""
Get and check checkusm for downloaded image. Return True if the
checksum is correct, False otherwise.
"""
if not os.path.exists(os.path.join(CACHE_DIR, self._img_fname)):
return False
expected_sum = None
# TODO: make the verbosity switch be dependent from verbosity of the
# script.
fname = os.path.join(self._tmp, self._checksum_file)
subprocess.call(['wget', self._checksum_url, '-q', '-O', fname])
with open(fname) as fobj:
for line in fobj.readlines():
if line.startswith('#'):
continue
if self._img_fname in line:
expected_sum = line.split('=')[1].strip()
break
if not expected_sum:
raise BoxError('Cannot find provided cloud image')
if os.path.exists(os.path.join(CACHE_DIR, self._img_fname)):
cmd = 'sha256sum ' + os.path.join(CACHE_DIR, self._img_fname)
calulated_sum = subprocess.getoutput(cmd).split(' ')[0]
return calulated_sum == expected_sum
return False
def _download_image(self): def _download_image(self):
if self._checksum(): if self._checksum():
print(f'Image already downloaded: {self._img}') print(f'Image already downloaded: {self._img_fname}')
return return
url = "https://cloud-images.ubuntu.com/releases/" fname = os.path.join(CACHE_DIR, self._img_fname)
url += f"{self.version}/release/" subprocess.call(['wget', '-q', self._img_url, '-O', fname])
url += self._img
print(f'Downloading image {self._img}')
subprocess.call(['wget', '-q', url, '-O', os.path.join(CACHE_DIR,
self._img)])
if not self._checksum(): if not self._checksum():
# TODO: make some retry mechanism? # TODO: make some retry mechanism?
raise BoxSysCommandError('Checksum for downloaded image differ ' raise BoxSysCommandError('Checksum for downloaded image differ '
'from expected.') 'from expected.')
else: else:
print(f'Downloaded image {self._img}') print(f'Downloaded image {self._img_fname}')
DISTROS = {'ubuntu': {'username': 'ubuntu', DISTROS = {'ubuntu': {'username': 'ubuntu',
'realname': 'ubuntu', 'realname': 'ubuntu',
'img_class': Ubuntu, 'img_class': Ubuntu,
'amd64': 'amd64', 'amd64': 'amd64',
'default_version': '20.04'}} 'default_version': '20.04'},
'fedora': {'username': 'fedora',
'realname': 'fedora',
'img_class': Fedora,
'amd64': 'x86_64',
'default_version': '34'}}
def get_image(vbox, version, image='ubuntu', arch='amd64'): def get_image(vbox, version, image='ubuntu', arch='amd64'):