Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a required version plugin #148

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ If you struggle with following the instructions for setting up the requirements
1. [VirtualBox](https://www.virtualbox.org/)
2. [Vagrant](http://www.vagrantup.com/) (Known to work with 2.0.1 - 2.1.2)
3. [git](https://git-scm.com/)
4. [ansible](https://www.ansible.com/community) 2.3+
4. [ansible](https://www.ansible.com/community) (restricted to versions 2.6.0 - 2.8.7)
5. [virtualbox-vbguest](https://github.com/dotless-de/vagrant-vbguest) plugin (If targeting CENTOS)

## Variables
Expand Down
5 changes: 5 additions & 0 deletions ansible-version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
# Minimum version of ansible this playbook works with.
islandora_ansible_version_min: 2.6.0
# Maximum version of ansible this playbook works with.
islandora_ansible_version_max: 2.8.7
52 changes: 52 additions & 0 deletions callback_plugins/ansible-version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- encoding:utf-8 -*-
# Copied from https://adamj.eu/tech/2016/08/19/restricting-the-ansible-version-in-use/
# and edited to our use-case.
from __future__ import absolute_import, division, print_function, unicode_literals

import sys
import os
import os.path
import yaml
from distutils.version import StrictVersion

import ansible

try:
# Version 2.0+
from ansible.plugins.callback import CallbackBase
except ImportError:
CallbackBase = object


class CallbackModule(CallbackBase):
def __init__(self):
self.minimum_version = StrictVersion('2.7.0')
self.maximum_version = StrictVersion('2.8.7')
self.load_from_main()
current_version = StrictVersion(ansible.__version__)
# Can't use `on_X` because this isn't forwards compatible with Ansible 2.0+
if current_version < self.minimum_version or current_version > self.maximum_version:
CallbackModule.print_red_bold(
"Islandora-Playbook restriction: only an Ansible version between {minversion} and {maxversion} is supported."
.format(minversion=self.minimum_version, maxversion=self.maximum_version)
)
sys.exit(1)

@staticmethod
def print_red_bold(text):
print('\x1b[31;1m' + text + '\x1b[0m')

def load_from_main(self):
current_dir = os.getcwd()
version_file = os.path.join(current_dir, 'ansible-version.yml')
if os.path.exists(version_file):
with open(version_file, 'r') as fp:
versions = yaml.load(fp, Loader=yaml.SafeLoader)
try:
self.minimum_version = StrictVersion(versions['islandora_ansible_version_min'])
except KeyError:
pass
try:
self.maximum_version = StrictVersion(versions['islandora_ansible_version_max'])
except KeyError:
pass