Exemple #1
0
import os
import shutil
from nom import md2html, parsers
from jinja2 import FileSystemLoader, environment


dir = os.path.dirname(os.path.abspath(__file__))
templ_dir = os.path.join(dir, 'templates')
env = environment.Environment()
env.loader = FileSystemLoader(templ_dir)


def compile_note(note, outdir, stylesheet=None, templ='default', ignore_missing=False):
    title, _ = os.path.basename(note).rsplit('.', 1)
    content = open(note, 'r').read()
    templ = env.get_template('{}.html'.format(templ))

    # create output directory if necessary
    outdir = os.path.join(outdir, title)
    if not os.path.exists(outdir):
        os.makedirs(outdir)

    # create assets directory if necessary
    assetsdir = os.path.join(outdir, 'assets')
    if not os.path.exists(assetsdir):
        os.makedirs(assetsdir)

    # copy over any local images
    for img_path in parsers.md_images(content):
        # assume http indicates remote image
        if img_path.startswith('http'):
    def runner_on_ok(self, host, res):
        if (C.CACHE_PLUGIN_CONNECTION is None
                or os.getenv("ANSIBLE_PERSIST_VARIABLES_DISABLE") is not None):
            return

        if self.task:
            if self.task.register and self.task.register == "hlm_notify_set_persistent_facts_to_false":
                # Get hlm_notify variables from SETUP_CACHE and set changed to False
                update_dict = {}
                prefix_to_set_false = HLM_PREFIX
                if 'hlm_notify_set_false_prefix' in self.playbook.VARS_CACHE[
                        host].keys():
                    prefix = self.playbook.VARS_CACHE[host].pop(
                        'hlm_notify_set_false_prefix')
                    prefix_to_set_false = prefix if prefix.startswith(
                        HLM_PREFIX) else HLM_PREFIX
                for key in self.playbook.SETUP_CACHE[host].keys():
                    if key.startswith(prefix_to_set_false
                                      ) and not key.endswith(".deleted"):
                        new_state = deepcopy(
                            self.playbook.SETUP_CACHE[host][key])
                        if new_state.get("changed", True):
                            new_state["changed"] = False
                            update_dict[key] = new_state
                if update_dict:
                    res.update(update_dict)
                    self._update_hash(res, "ansible_facts", update_dict)

            elif self.task.register and self.task.register.startswith(
                    HLM_PREFIX):
                my_host = self._get_target_host(host)

                if res.get('changed'):
                    # save the facts immediately in case there is an exception elsewhere
                    self._update_hash(self.playbook.SETUP_CACHE, my_host,
                                      {self.task.register: res})

                    if my_host == host:
                        # return a copy of the result data for ansible to save as well when
                        # it writes down facts after the task is complete on all hosts
                        result = {
                            k: v
                            for k, v in res.items() if k != 'ansible_facts'
                        }
                        self._update_hash(res, 'ansible_facts',
                                          {self.task.register: result})

                else:
                    previous_state = self.playbook.SETUP_CACHE[my_host].get(
                        self.task.register, None)
                    if previous_state:
                        res.update(previous_state)
                        result = {
                            k: v
                            for k, v in res.items() if k != 'ansible_facts'
                        }
                        self._update_hash(res, 'ansible_facts',
                                          {self.task.register: result})

            if self.task.when:
                if isinstance(self.task.when, list):
                    when = None
                    for elem in self.task.when:
                        if HLM_PREFIX in elem:
                            when = elem
                else:
                    when = self.task.when if HLM_PREFIX in self.task.when else None
                if when:
                    # Mark saved variable for removal from persistent cache at end of run
                    # if check is "is defined"
                    my_host = self._get_target_host(host)
                    envir = environment.Environment()
                    when = when.replace('{{', '')
                    when = when.replace('}}', '')
                    parse = parser.Parser(envir, when, state='variable')
                    expr = parse.parse_expression()
                    deleted_keys = {}
                    for key in search_node(expr):
                        deleted_keys["%s.deleted" % key] = True

                    self._update_hash(res, 'ansible_facts', deleted_keys)

                    # If delegating task, mark persisted facts for deletion on node to which
                    # you're delegating
                    if my_host != host:
                        self._update_hash(self.playbook.SETUP_CACHE, my_host,
                                          deleted_keys)