16
     /ΚΞwΏΜ [    /σ
oBϊ]c           @   sc   d  Z  d d l m Z d d l m Z i i d d 6d 6Z d   Z d	   Z e   Z e e  d
 S(   s   
cloud-init reporting framework

The reporting framework is intended to allow all parts of cloud-init to
report events in a structured manner.
i   (   t   DictRegistryi   (   t   available_handlerst   logt   typet   loggingc         C   s   x |  j    D]v \ } } | s8 t j | d t q n  | j   } t j | j d  } t j |  | |   } t j | |  q Wd S(   sρ   Update the instantiated_handler_registry.

    :param config:
        The dictionary containing changes to apply.  If a key is given
        with a False-ish value, the registered handler matching that name
        will be unregistered.
    t   forceR   N(	   t   itemst   instantiated_handler_registryt   unregister_itemt   Truet   copyR   t   registered_itemst   popt   register_item(   t   configt   handler_namet   handler_configt   clst   instance(    (    s@   /usr/lib/python2.7/site-packages/cloudinit/reporting/__init__.pyt   update_configuration   s    c          C   s@   x9 t  j j   D]( \ }  } t | d  r | j   q q Wd  S(   Nt   flush(   R   R   R   t   hasattrR   (   t   _t   handler(    (    s@   /usr/lib/python2.7/site-packages/cloudinit/reporting/__init__.pyt   flush_events(   s    N(	   t   __doc__t   registryR    t   handlersR   t   DEFAULT_CONFIGR   R   R   (    (    (    s@   /usr/lib/python2.7/site-packages/cloudinit/reporting/__init__.pyt   <module>
   s   			     1ΟΞe[ΏΜ     1# Copyright (C) 2017 Canonical Ltd.
#
# This file is part of cloud-init. See LICENSE file for license information.

"""Define 'clean' utility and handler as part of cloud-init commandline."""

import argparse
import glob
import os
import sys

from cloudinit.stages import Init
from cloudinit.util import (
    ProcessExecutionError, del_dir, del_file, get_config_logfiles,
    is_link, subp)


def error(msg):
    sys.stderr.write("ERROR: " + msg + "\n")


def get_parser(parser=None):
    """Build or extend an arg parser for clean utility.

    @param parser: Optional existing ArgumentParser instance representing the
        clean subcommand which will be extended to support the args of
        this utility.

    @returns: ArgumentParser with proper argument configuration.
    """
    if not parser:
        parser = argparse.ArgumentParser(
            prog='clean',
            description=('Remove logs and artifacts so cloud-init re-runs on '
                         'a clean system'))
    parser.add_argument(
        '-l', '--logs', action='store_true', default=False, dest='remove_logs',
        help='Remove cloud-init logs.')
    parser.add_argument(
        '-r', '--reboot', action='store_true', default=False,
        help='Reboot system after logs are cleaned so cloud-init re-runs.')
    parser.add_argument(
        '-s', '--seed', action='store_true', default=False, dest='remove_seed',
        help='Remove cloud-init seed directory /var/lib/cloud/seed.')
    return parser


def remove_artifacts(remove_logs, remove_seed=False):
    """Helper which removes artifacts dir and optionally log files.

    @param: remove_logs: Boolean. Set True to delete the cloud_dir path. False
        preserves them.
    @param: remove_seed: Boolean. Set True to also delete seed subdir in
        paths.cloud_dir.
    @returns: 0 on success, 1 otherwise.
    """
    init = Init(ds_deps=[])
    init.read_cfg()
    if remove_logs:
        for log_file in get_config_logfiles(init.cfg):
            del_file(log_file)

    if not os.path.isdir(init.paths.cloud_dir):
        return 0  # Artifacts dir already cleaned
    seed_path = os.path.join(init.paths.cloud_dir, 'seed')
    for path in glob.glob('%s/*' % init.paths.cloud_dir):
        if path == seed_path and not remove_seed:
            continue
        try:
            if os.path.isdir(path) and not is_link(path):
                del_di