Skip to content

Instantly share code, notes, and snippets.

@michaelgugino
Last active July 28, 2016 15:47
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 michaelgugino/fc3fe3d635396f0c15df401fb087c0c4 to your computer and use it in GitHub Desktop.
Save michaelgugino/fc3fe3d635396f0c15df401fb087c0c4 to your computer and use it in GitHub Desktop.
Generate a requirements.yml from a role's meta for OSA
import io
import subprocess
import os
import yaml
filename = 'openstack-ansible/ansible-role-requirements.yml'
DEVNULL = open(os.devnull, 'w')
# load the yaml file
with io.open(filename, 'rb') as f:
roles = yaml.load(f)
role_names = []
role_dict = {}
formatted_dict = {}
# convert the list of dicts to a more useful pattern.
for role in roles:
role_name = role['name']
role_names.append(role_name)
role_scm = role.get('scm')
if role_scm == 'git' and 'openstack-ansible' in role.get('src'):
role_dict[role_name] = role['src']
role_dict[role_name + "__version"] = role['version']
else:
print("role %s will not be cloned" % role['name'])
formatted_string = '''- name: %s\n''' % role_name
fields = ['scm', 'src', 'version']
for field in fields:
if role.get(field):
formatted_string += ''' %s: %s\n''' % (field, role[field])
formatted_dict[role_name] = formatted_string
for role in role_names:
if role_dict.get(role):
# clone or update the roles, and checkout the version
# specified by the master role requirements
if not os.path.exists(role):
subprocess.check_call(["git", "clone", role_dict[
role], role], stdout=DEVNULL, stderr=subprocess.STDOUT)
os.chdir(role)
else:
os.chdir(role)
subprocess.check_call(
["git", "checkout", "master"], stdout=DEVNULL, stderr=subprocess.STDOUT)
subprocess.check_call(
["git", "pull"], stdout=DEVNULL, stderr=subprocess.STDOUT)
subprocess.check_call(["git", "checkout", role_dict[
role + "__version"]], stdout=DEVNULL, stderr=subprocess.STDOUT)
os.chdir('..')
requirements_list = []
output_yaml = '---\n'
# Try to read the dependencies from the role's meta/main.yml
try:
with io.open(os.path.join(role, "meta", "main.yml")) as f:
y = yaml.load(f)
for dep in y['dependencies']:
try:
dep = dep['role']
except:
pass
if dep in role_names:
requirements_list.append(dep)
else:
print("Unknown dependency found!: %s" % dep)
except:
print("Error getting role dependencies for: %s" % role)
requirements_list.sort()
for r in requirements_list:
output_yaml += formatted_dict[r]
try:
with io.open(os.path.join(role, 'requirements.yml'), 'wb') as f:
f.write(output_yaml)
f.truncate()
print("Successfully wrote: %s" %
os.path.join(role, 'requirements.yml'))
except:
print("Error writing requirements.yml for %s" % role)
DEVNULL.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment