Skip to content

Instantly share code, notes, and snippets.

@logan2211
Last active September 28, 2017 16:39
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 logan2211/f8ad9a03502e69971d2eee96049719d7 to your computer and use it in GitHub Desktop.
Save logan2211/f8ad9a03502e69971d2eee96049719d7 to your computer and use it in GitHub Desktop.
Ansible Role Requirements bumper
---
- name: apt_package_pinning
scm: git
src: https://git.openstack.org/openstack/openstack-ansible-apt_package_pinning
branch: master
version: dc560a65f9bbd9e867bcdf7bb9c52a95e0cfa8d5 # HEAD of "master" as of 2017/07/20
#!/usr/bin/env python
# Copyright 2017, Logan Vig <logan2211@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Based in part on
# https://github.com/openstack/openstack-ansible/blob/a1c1e6f529726da58e2abd6cf2a556b45acbe1fc/scripts/ansible-role-requirements-editor.py
from __future__ import print_function
import argparse
from datetime import datetime
import git
from ruamel.yaml import YAML
yaml = YAML()
def git_lsremote(url):
remote_refs = {}
g = git.cmd.Git()
for ref in g.ls_remote('--heads', '--tags', url).split('\n'):
sha, branch = ref.split('\t', 1)
if branch.startswith('refs/heads/'):
branch = branch[11:]
elif branch.startswith('refs/tags/'):
branch = branch[10:]
remote_refs[branch] = sha
return remote_refs
def main():
datestamp = datetime.now().strftime('%Y/%m/%d')
parser = argparse.ArgumentParser(
description='ansible-role-requirements SHA updater',
epilog='Licensed "Apache 2.0"')
parser.add_argument('-f', '--file', help='ansible-role-requirements file'
'location', required=True)
args = parser.parse_args()
# Read the ansible-role-requirements file into memory
with open(args.file, "r") as role_req_file:
reqs = yaml.load(role_req_file)
# Loop through the roles and bump the SHA to the head of the branch if one
# is defined.
for role_data in reqs:
if ('branch' in role_data and 'src' in role_data and
role_data['scm'] == 'git'):
refs = git_lsremote(role_data['src'])
if role_data['branch'] in refs:
if 'version' not in role_data or (
'version' in role_data and
role_data['version'] != refs[role_data['branch']]):
print ("Setting role %s to version %s based on branch %s"
% (role_data['name'], refs[role_data['branch']],
role_data['branch']))
role_data['version'] = refs[role_data['branch']]
role_data.yaml_add_eol_comment(
'HEAD of "{}" as of {}'.format(
role_data['branch'],
datestamp
),
'version'
)
with open(args.file, "w") as role_req_file:
yamlo = YAML(typ='rt')
yamlo.default_flow_style = False
yamlo.dump(reqs, role_req_file)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment