Last active
November 3, 2016 08:58
-
-
Save loa/399f6d962643e1efe441fcf2ec92bc46 to your computer and use it in GitHub Desktop.
Generate Vagrant Atlas Index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
## How to use | |
# - Create following structure with box/virtualbox/*.box | |
# - Follow naming convention box-name-SEMVER.box | |
# - Run script from root and index files will be generated for all unique box-names | |
# . | |
# ├── box | |
# │ └── virtualbox | |
# │ ├── centos6-0.1.0.box | |
# │ ├── centos6-0.2.0.box | |
# │ ├── centos6-0.3.0.box | |
# │ ├── centos7-0.1.0.box | |
# │ ├── centos7-0.2.0.box | |
# │ ├── centos7-0.3.0.box | |
# ├── centos6.json | |
# ├── centos7.json | |
# └── gen_vagrant_atlas_index.py | |
import glob | |
import json | |
import hashlib | |
import os | |
url_root = "https://example.com" | |
def gen_entry(box_path): | |
print(box_path) | |
with open(box_path, 'rb') as fd: | |
checksum = hashlib.sha256(fd.read()).hexdigest() | |
# get last column box-name-VERSION.box | |
version = box_path.split('-')[-1] | |
# remove file extension | |
version = version.rsplit('.', 1)[0] | |
return { | |
'version': version, | |
'providers': [{ | |
'name': 'virtualbox', | |
'url': url_root + box_path, | |
'checksum_type': 'sha256', | |
'checksum': checksum | |
}] | |
} | |
def gen_index(box_name): | |
print("Generating index for {0}".format(box_name)) | |
box_files = glob.glob("box/virtualbox/{0}*box".format(box_name)) | |
entries = [gen_entry(x) for x in box_files] | |
index = { | |
'name': '{0}'.format(box_name), | |
'description': '{0}'.format(box_name), | |
'versions': entries | |
} | |
with open('{0}.json'.format(box_name), 'w') as fd: | |
json.dump(index, fd, sort_keys=True, indent=2) | |
def main(): | |
box_files = glob.glob("box/virtualbox/*.box") | |
# get basename of files | |
boxes = [os.path.basename(s) for s in box_files] | |
# remove version to get box name | |
boxes = [s.rsplit('-', 1)[0] for s in boxes] | |
# remove duplicates | |
boxes = list(set(boxes)) | |
for box in boxes: | |
gen_index(box) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment