Skip to content

Instantly share code, notes, and snippets.

@mbarnes
Last active August 4, 2021 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbarnes/a7804786c35d10621a31b95000ec7e4a to your computer and use it in GitHub Desktop.
Save mbarnes/a7804786c35d10621a31b95000ec7e4a to your computer and use it in GitHub Desktop.
Unpack files in a CoreOS ignition config file
#!/usr/bin/python3
#
# Unpack files in a CoreOS ignition config file.
#
# This is a quick hack just to help examine file contents.
# Ignores file ownership and permission attributes in the ignition config.
#
# e.g. $ ./unpack-ignitionconfig-files.py bootstrap.ign
# creates ./bootstrap.ign.d directory tree with files decoded.
import json
import os
import sys
from urllib.request import urlopen
def unpack_filespec(basedir, filespec):
path = basedir + filespec['path']
os.makedirs(os.path.dirname(path), exist_ok=True)
if 'contents' in filespec:
data_uris = [filespec['contents']['source']]
elif 'append' in filespec:
data_uris = [item['source'] for item in filespec['append']]
with open(path, 'wb') as outfile:
for data_uri in data_uris:
with urlopen(data_uri) as response:
outfile.write(response.read())
def main(ign_files):
for filename in ign_files:
with open(filename) as ign_stream:
rel_basedir = os.path.basename(filename) + '.d'
abs_basedir = os.path.abspath(rel_basedir)
ign_config = json.load(ign_stream)
list_of_files = ign_config.get('storage', {}).get('files', [])
for filespec in list_of_files:
unpack_filespec(abs_basedir, filespec)
if __name__ == '__main__':
if len(sys.argv) == 1:
print('Usage: {} IGNFILES'.format(sys.argv[0]))
sys.exit(1)
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment