diff --git a/.travis.yml b/.travis.yml index a087d5f..ac98f6d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,6 @@ matrix: include: - python: 2.7 env: TOXENV=py27-ansible194 - allow_failures: - python: 2.7 env: TOXENV=py27-ansible2010 diff --git a/ansible_toolkit/__init__.py b/ansible_toolkit/__init__.py index e69de29..2928354 100644 --- a/ansible_toolkit/__init__.py +++ b/ansible_toolkit/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- + +from .dao import create_dao + +DaoImpl = create_dao() diff --git a/ansible_toolkit/dao.py b/ansible_toolkit/dao.py new file mode 100644 index 0000000..273eaef --- /dev/null +++ b/ansible_toolkit/dao.py @@ -0,0 +1,79 @@ +# -*- coding: utf-8 -*- + +import ansible + + +class AnsibleDao(object): + """ + Interface for Ansible Data Access Object implementation. + """ + + def __init__(self): + self.version = ansible.__version__ + + def get_vault_lib(self): + """ + Returns the Ansible VaultLib class. + """ + raise NotImplementedError + + def read_vault_file(vault_password_file): + """ + Read a vault password from a file or if executable, + execute the script and + retrieve password from STDOUT + """ + raise NotImplementedError + + +class Ansible2(AnsibleDao): + """ + Ansible 2.x implementation. + """ + + def __init__(self): + if not ansible.__version__.startswith('2'): + raise NotImplementedError( + 'Cannot use Ansible 2.x implementation with Ansible 1.x!') + + def get_vault_lib(self): + from ansible.parsing.vault import VaultLib + return VaultLib + + def read_vault_file(self, vault_password_file): + from ansible.cli import CLI + from ansible.parsing.dataloader import DataLoader + + return CLI.read_vault_password_file(vault_password_file, DataLoader()) + + +class Ansible1(AnsibleDao): + """ + Ansible 1.x implementation. + """ + + def __init__(self): + if not ansible.__version__.startswith('1'): + raise NotImplementedError( + 'Cannot use Ansible 1.x implementation with Ansible 2.x!') + + def get_vault_lib(self): + from ansible.utils.vault import VaultLib + return VaultLib + + def read_vault_file(self, vault_password_file): + from ansible.utils import read_vault_file + return read_vault_file(vault_password_file) + + +def create_dao(): + """ + Creates an Ansible data access object implementation that + implements the dao.AnsibleDao interface. + + :return: Ansible data access object. + """ + + if ansible.__version__.startswith('2'): + return Ansible2() + return Ansible1() diff --git a/ansible_toolkit/exceptions.py b/ansible_toolkit/exceptions.py index def127f..3d248f0 100644 --- a/ansible_toolkit/exceptions.py +++ b/ansible_toolkit/exceptions.py @@ -1,2 +1,5 @@ +# -*- coding: utf-8 -*- + + class MalformedGitDiff(Exception): pass diff --git a/ansible_toolkit/git_diff.py b/ansible_toolkit/git_diff.py index 008e28b..fc72384 100644 --- a/ansible_toolkit/git_diff.py +++ b/ansible_toolkit/git_diff.py @@ -1,13 +1,17 @@ +# -*- coding: utf-8 -*- + import difflib -from itertools import islice import re import subprocess -from ansible.utils.vault import VaultLib - from exceptions import MalformedGitDiff +from itertools import islice from utils import get_vault_password, green, red, cyan, intense +from . import DaoImpl + +VaultLib = DaoImpl.get_vault_lib() + def get_parts(git_diff_output): r = re.compile(r"^diff --git", re.MULTILINE) diff --git a/ansible_toolkit/show_template.py b/ansible_toolkit/show_template.py index 472755d..894b7ab 100644 --- a/ansible_toolkit/show_template.py +++ b/ansible_toolkit/show_template.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + from ansible.runner import Runner from ansible.utils.template import template_from_file diff --git a/ansible_toolkit/show_vars.py b/ansible_toolkit/show_vars.py index c239189..c8ae26d 100644 --- a/ansible_toolkit/show_vars.py +++ b/ansible_toolkit/show_vars.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + from ansible.runner import Runner from ansible.utils import combine_vars, template diff --git a/ansible_toolkit/utils.py b/ansible_toolkit/utils.py index d60a67c..e3abf02 100644 --- a/ansible_toolkit/utils.py +++ b/ansible_toolkit/utils.py @@ -1,10 +1,12 @@ +# -*- coding: utf-8 -*- + import ConfigParser import errno import os from ansible.inventory import Inventory -from ansible.utils import read_vault_file +from . import DaoImpl config = ConfigParser.ConfigParser() @@ -49,7 +51,7 @@ def get_vault_password(password_file=None): password_file = config.get('vault', 'password_file') except ConfigParser.NoSectionError: return None - return read_vault_file(password_file) + return DaoImpl.read_vault_file(password_file) # Inventory diff --git a/ansible_toolkit/utils_ansible.py b/ansible_toolkit/utils_ansible.py index 32ba70e..a26e9d4 100644 --- a/ansible_toolkit/utils_ansible.py +++ b/ansible_toolkit/utils_ansible.py @@ -1,9 +1,11 @@ +# -*- coding: utf-8 -*- + import tempfile import ansible.callbacks from ansible.playbook import PlayBook import ansible.constants as C -from utils import get_inventory, green, yellow +from utils import get_inventory, yellow SETUP_PLAYBOOK = """ diff --git a/ansible_toolkit/vault.py b/ansible_toolkit/vault.py index 679773a..5bd8e61 100644 --- a/ansible_toolkit/vault.py +++ b/ansible_toolkit/vault.py @@ -1,13 +1,16 @@ +# -*- coding: utf-8 -*- + import hashlib import os -from ansible.utils.vault import VaultLib - from utils import get_vault_password, mkdir_p, split_path, get_files +from . import DaoImpl ATK_VAULT = '.atk-vault' +VaultLib = DaoImpl.get_vault_lib() + def backup(path, password_file=None): """ diff --git a/ansible_toolkit/tests/git_diff_test.py b/tests/git_diff_test.py similarity index 99% rename from ansible_toolkit/tests/git_diff_test.py rename to tests/git_diff_test.py index a91fc69..46600cb 100644 --- a/ansible_toolkit/tests/git_diff_test.py +++ b/tests/git_diff_test.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + import tempfile import unittest