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

Added ability to add url for write_files section.

This commit is contained in:
2021-10-01 19:09:38 +02:00
parent fe422576cd
commit f46432546e
3 changed files with 54 additions and 16 deletions

View File

@@ -19,6 +19,7 @@ Requirements
- Python 3.x - Python 3.x
- `pyyaml`_ - `pyyaml`_
- `requests`_
- Virtualbox (obviously) - Virtualbox (obviously)
- ``mkisofs`` or ``genisoimage`` command for generating iso image - ``mkisofs`` or ``genisoimage`` command for generating iso image
@@ -140,11 +141,20 @@ pass filenames to the custom config, instead of filling up
permissions: '0644' permissions: '0644'
filename: /path/to/local/file.txt filename: /path/to/local/file.txt
during processing this file, boxpy will look for ``filename`` key in the yaml or
file for the ``write_files`` sections, and it will remove that key, read the
file and put its contents under ``content`` key. What is more important, that .. code:: yaml
will be done after template processing, so there will be no interference for
possible ``$`` characters. write_files:
- path: /opt/somefile.txt
permissions: '0644'
url: https://some.url/content
during processing this file, boxpy will look for ``filename`` or ``url`` keys
in the yaml file for the ``write_files`` sections, and it will remove that key,
read the file and put its contents under ``content`` key. What is more
important, that will be done after template processing, so there will be no
interference for possible ``$`` characters.
What is more interesting is the fact, that you could use whatever cloud-init What is more interesting is the fact, that you could use whatever cloud-init
accepts, and a special section, for keeping configuration, so that you don't accepts, and a special section, for keeping configuration, so that you don't
@@ -202,3 +212,4 @@ This work is licensed under GPL-3.
.. _pyyaml: https://github.com/yaml/pyyaml .. _pyyaml: https://github.com/yaml/pyyaml
.. _cloud-init: https://cloudinit.readthedocs.io .. _cloud-init: https://cloudinit.readthedocs.io
.. _requests: https://docs.python-requests.org

48
box.py
View File

@@ -14,10 +14,11 @@ import time
import uuid import uuid
import xml.dom.minidom import xml.dom.minidom
import requests
import yaml import yaml
__version__ = "1.0" __version__ = "1.2"
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"
@@ -423,27 +424,52 @@ class Config:
if conf.get('write_files'): if conf.get('write_files'):
new_list = [] new_list = []
for file_data in conf['write_files']: for file_data in conf['write_files']:
content = None
fname = file_data.get('filename') fname = file_data.get('filename')
if not fname: url = file_data.get('url')
if not any((fname, url)):
new_list.append(file_data) new_list.append(file_data)
continue continue
fname = os.path.expanduser(os.path.expandvars(fname)) if fname:
if not os.path.exists(fname): key = 'filename'
LOG.warning("File '%s' doesn't exists", content = self._read_filename(fname)
file_data['filename']) if content is None:
continue LOG.warning("File '%s' doesn't exists", fname)
continue
with open(fname) as fobj: if url:
file_data['content'] = fobj.read() key = 'url'
del file_data['filename'] code, content = self._get_url(url)
new_list.append(file_data) if content is None:
LOG.warning("Getting url '%s' returns %s code",
url, code)
continue
if content:
file_data['content'] = content
del file_data[key]
new_list.append(file_data)
conf['write_files'] = new_list conf['write_files'] = new_list
# 3. finally dump it again. # 3. finally dump it again.
return "#cloud-config\n" + yaml.safe_dump(conf) return "#cloud-config\n" + yaml.safe_dump(conf)
def _get_url(self, url):
response = requests.get(url)
if response.status_code != 200:
return response.status_code, None
return response.status_code, response.text
def _read_filename(self, fname):
fullpath = os.path.expanduser(os.path.expandvars(fname))
if not os.path.exists(fullpath):
return
with open(fname) as fobj:
return fobj.read()
def _set_ssh_key_path(self): def _set_ssh_key_path(self):
self.ssh_key_path = self.key self.ssh_key_path = self.key

View File

@@ -1 +1,2 @@
pyyaml>=5.4.1 pyyaml>=5.4.1
requests>=2.26.0