|
| 1 | +# (c) 2015, Robert Chady <rchady@sitepen.com> |
| 2 | +# Based on `runner/lookup_plugins/file.py` for Ansible |
| 3 | +# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com> |
| 4 | +# |
| 5 | +# This file is part of Debops. |
| 6 | +# This file is NOT part of Ansible yet. |
| 7 | +# |
| 8 | +# Debops is free software: you can redistribute it and/or modify |
| 9 | +# it under the terms of the GNU General Public License as published by |
| 10 | +# the Free Software Foundation, either version 3 of the License, or |
| 11 | +# (at your option) any later version. |
| 12 | +# |
| 13 | +# Ansible is distributed in the hope that it will be useful, |
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +# GNU General Public License for more details. |
| 17 | +# |
| 18 | +# You should have received a copy of the GNU General Public License |
| 19 | +# along with Debops. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + |
| 21 | +''' |
| 22 | +
|
| 23 | +This file implements the `template_src` lookup filter for Ansible. In difference |
| 24 | +to the `template` filter, this searches values based on the `template-paths` |
| 25 | +variable (colon separated) as configured in DebOps. |
| 26 | +
|
| 27 | +NOTE: This means this filter relies on DebOps. |
| 28 | +
|
| 29 | +''' |
| 30 | + |
| 31 | +from ansible import utils, errors |
| 32 | +import os |
| 33 | + |
| 34 | +from debops import * |
| 35 | + |
| 36 | +__author__ = "Robert Chady <rchady@sitepen.com>" |
| 37 | +__copyright__ = "Copyright 2015 by Robert Chady <rchady@sitepen.com>" |
| 38 | +__license__ = "GNU General Public LIcense version 3 (GPL v3) or later" |
| 39 | + |
| 40 | +conf_template = 'template-paths' |
| 41 | + |
| 42 | +class LookupModule(object): |
| 43 | + |
| 44 | + def __init__(self, basedir=None, **kwargs): |
| 45 | + self.basedir = basedir |
| 46 | + |
| 47 | + def run(self, terms, inject=None, **kwargs): |
| 48 | + |
| 49 | + terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject) |
| 50 | + ret = [] |
| 51 | + |
| 52 | + # this can happen if the variable contains a string, strictly not desired for lookup |
| 53 | + # plugins, but users may try it, so make it work. |
| 54 | + if not isinstance(terms, list): |
| 55 | + terms = [ terms ] |
| 56 | + |
| 57 | + project_root = find_debops_project(required=False) |
| 58 | + config = read_config(project_root) |
| 59 | + if conf_template_paths in config['paths']: |
| 60 | + places = config['paths'][conf_template_paths].split(':') |
| 61 | + else: |
| 62 | + places = [] |
| 63 | + |
| 64 | + |
| 65 | + for term in terms: |
| 66 | + if '_original_file' in inject: |
| 67 | + relative_path = utils.path_dwim_relative(inject['_original_file'], 'templates', '', self.basedir, check=False) |
| 68 | + places.append(relative_path) |
| 69 | + for path in places: |
| 70 | + template = os.path.join(path, term) |
| 71 | + if template and os.path.exists(template): |
| 72 | + ret.append(template) |
| 73 | + break |
| 74 | + else: |
| 75 | + raise errors.AnsibleError("could not locate file in lookup: %s" % term) |
| 76 | + |
| 77 | + return ret |
0 commit comments